vi / websocat

Command-line client for WebSockets, like netcat (or curl) for ws:// with advanced socat-like functions
MIT License
7.18k stars 278 forks source link

Reply with header #120

Closed latalkdesk closed 3 years ago

latalkdesk commented 3 years ago

Hi! Trying to create a server to reply with the sent Sec-WebSocket-Protocol.

So far i have this, my thought were to open the socket, pass header to variable and print the variable: websocat -v -E -t ws-l:0.0.0.0:2700 --header-to-env=Sec-WebSocket-Protocol exec:perl --exec-args -e 'print $ENV{"H_Sec-WebSocket-Protocol"}'

Using a client like this: websocat -k -v "ws://0.0.0:2700" -H='Sec-WebSocket-Protocol:token'

Shouldn't the server send the header back to the client?

Thanks for the help!

vi commented 3 years ago

Shouldn't the server send the header back to the client?

Yes, it should and it does in your example.

If you increase verbosity on server further (-v -v), you should see:

[DEBUG websocat::ws_server_peer] Incoming { version: Http11, subject: (Get, AbsolutePath("/")), headers: Headers { Sec-WebSocket-Protocol: token
, Host: 0.0.0.0:2700
, Connection: Upgrade
, Upgrade: websocket
, Sec-WebSocket-Version: 13
, Sec-WebSocket-Key: er2j3UAM6sme/KGYhcs/Ow==
, } }
[DEBUG websocat::ws_server_peer] Headers { Sec-WebSocket-Protocol: token
, }
[DEBUG websocat::ws_server_peer] Headers { Sec-WebSocket-Protocol: token
, Sec-WebSocket-Accept: qaTGdsJpvSivIbYwlL61Ly9chrU=
, Connection: Upgrade
, Upgrade: websocket
, }

straceing the client also shows reveals the incoming header:

[pid 11484] sendto(6, "GET / HTTP/1.1\r\nSec-WebSocket-Protocol: token\r\nHost: 0.0.0.0:2700\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: er2j3UAM6sme/KGYhcs/Ow==\r\n\r\n", 182, MSG_NOSIGNAL, NULL, 0) = 182
[pid 11484] recvfrom(6, "HTTP/1.1 101 Switching Protocols\r\nSec-WebSocket-Protocol: token\r\nSec-WebSocket-Accept: qaTGdsJpvSivIbYwlL61Ly9chrU=\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n", 8192, 0, NULL, NULL) = 160
[INFO  websocat::ws_client_peer] Connected to ws
vi commented 3 years ago

--header-to-env

Don't you see the warning in stderr?

websocat: --header-to-env is meaningless without -e (--set-environment)

Fixing issues in the command line leads to working example. Server:

$ websocat -eE -t ws-l:0.0.0.0:2700 --header-to-env=Sec-WebSocket-Protocol exec:perl --exec-args -E 'say $ENV{"H_Sec-WebSocket-Protocol"}'

Client:

$ /opt/websocat "ws://127.0.0.1:2700" -H='Sec-WebSocket-Protocol:qwerty'
qwerty
^C
$ /opt/websocat "ws://127.0.0.1:2700" -H='Sec-WebSocket-Protocol:token'
token
^C
latalkdesk commented 3 years ago

websocat: --header-to-env is meaningless without -e (--set-environment)

totally missed that!

Thank you so much for this incredible tool!