Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul the old TCP server type to use PodeListener not TCPListener #902

Closed
Badgerati opened this issue Jan 10, 2022 · 3 comments · Fixed by #963
Closed

Overhaul the old TCP server type to use PodeListener not TCPListener #902

Badgerati opened this issue Jan 10, 2022 · 3 comments · Fixed by #963

Comments

@Badgerati
Copy link
Owner

There's an old, undocumented server type in Pode: TCP.

It needs an overhaul to move away from TCPListener, and use the PodeListener. This will make multiple endpoint support possible, better socket support and support certificate/ssl streams as well.

The current implementation is not great - hence never being documented. This should make it far better, and allow people to build anything on TCP sockets using Pode - not just web servers :)

@phatmandrake
Copy link
Contributor

phatmandrake commented Feb 3, 2022

So I am in the middle of testing Arbitrary TCP with Cloudflare Tunnels. I was hoping to use PODE for this, but I haven't been able to get the example implementation working (not using the tunnel). I don't seem to get any output when I send a TCP Message.

Using the example here to send a TCP Message with Powershell: https://riptutorial.com/powershell/example/18118/tcp-sender

Using the TCP-Server example, doesn't seem to work. I am uncertain of where to troubleshoot this at. Is the example TCP-Server example validated to be fully functional? One thing I notice is that if an Endpoint is specified, when pode start the only verbose console output I see is tcp://:8999 there's no domain or IP address.

I'm fairly new to this so I'm not certain where to troubleshoot first

@Badgerati
Copy link
Owner Author

Hi @phatmandrake,

With the way TCP works at the moment, in the example, delete the Write-PodeTcpClient -Message 'gief data' line and you'll get the message you send outputted on the console.

The example is expecting a client to connect, read the message written by the Pode server, and then the client sends its own message for Pode to read. If you're just sending a message for Pode to read, then you only need the Read-PodeTcpClient line 😃

As you've probably noticed from tcp://:8999, it likely has some bugs! This issue to fix/update it and get it documented officially!

@Badgerati
Copy link
Owner Author

I've given the TCP server type it's first overhaul. This completely breaks the old implementation, but seeing as it was never documented, unstable, and never stated to exist, this should be all right.

It no longer uses Add-PodeHandler, and instead now uses a new "Verb" system. They're like Routes, but for TCP commands, and they even support :parameter syntax:

Start-PodeServer {
    Add-PodeEndpoint -Address * -Port 8999 -Protocol Tcp

    # hello verb
    Add-PodeVerb -Verb 'HELLO :forename :surname' -ScriptBlock {
        Write-PodeTcpClient -Message "HI, $($TcpEvent.Parameters.forename) $($TcpEvent.Parameters.surname)"
    }

    # catch-all verb
    Add-PodeVerb -Verb '*' -ScriptBlock {
        Write-PodeTcpClient -Message "Unrecognised verb sent"
    }

    # quit verb
    Add-PodeVerb -Verb 'Quit' -Close
}

If you were to connect to localhost:8999 and send HELLO Joe Bloggs, the server would respond back with HI, Joe Bloggs.

The * verb is like * on route paths, and is a catch-all for anything. The -Close on the Quit verb above will automatically close the client connection.

TCP endpoints also support SSL, as both implicit and explicit TLS (implicit by default). If you wanted to use explicit and have an "upgrade" verb, you could do:

Add-PodeVerb -Verb 'StartTLS' -UpgradeToSsl

In a verb's scriptblock, you can use Write-PodeTcpClient to send data back to the client. Or you can use Read-PodeTcpClient to get adhoc data from the client.

Note: If you test this via telnet, you'll need to pass -CRLFMessageEnd on the Add-PodeEndpoint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants