websocat icon indicating copy to clipboard operation
websocat copied to clipboard

Newbie question: why do we need this tool, since we already have socat?

Open oucfei opened this issue 5 years ago • 4 comments

Sorry for the newbie question, I have almost zero experience on networks, hope anyone can teach me: if I want to forward traffic from/to WebSocket, why can't I just use socat? My understanding is socat can be used to forward TCP traffic, and WebSocket is just a protocol on top of TCP, so isn't the underlying traffic still TCP? I'm asking because I did try to use socat to forward a ws traffic and there was some issues. Even with websocat I'm having trouble making it work, but I want to know which is the right direction... Thanks in advance!

oucfei avatar Jan 24 '20 04:01 oucfei

By the way, I know there's a docker container for this and I've started one like this: docker run -p 9229:9229 --name websocat solsson/websocat --binary -v -E ws-listen:127.0.0.1:9229 ws://172.17.0.2:4848 The container starts successfully and prints some message. I've verified the connectivity to ws://172.17.0.2:4848 inside the container is fine. However when I try to connect from host echo "some data" | websocat ws://127.0.0.1:9229 I got: websocat: WebSocketError: WebSocket protocol error websocat: error running

and the container seems didn't receive anything because it's not printing anything. Shouldn't it listening on port 9229 for ws traffic now? Why it doesn't work?

oucfei avatar Jan 24 '20 04:01 oucfei

if I want to forward traffic from/to WebSocket, why can't I just use socat?

For plain ws-l:127.0.0.1:9229 -> ws://172.17.0.2:4848 case socat (or websocat tcp-l:127.0.0.1:9229 tcp:172.17.0.2:4848) may indeed be more appropriate.

Main usage of websocat is when you want to extract WebSocket messages and do something with them. Another case is when you want websocat's autoreconnect: or reuse: features.


I may answer later about debugging WebSocket protocol error.

vi avatar Jan 24 '20 07:01 vi

WebSocket protocol error

This looks like as if client Websocat is connected to a TCP socket, but something other than a proper WebSocket reply (including immediate connection drop) is received.

You can get traffic dump of how Websocat tries to connect to the socket by combining it with socat:

websocat -t - ws-c:sh-c:'socat -v -x - tcp:127.0.0.1:9229'

The port is not properly forwaded to websocat running inside Docker. Either you should listen 0.0.0.0 instead of 127.0.0.1 (docker run -p 9229:9229 --name websocat solsson/websocat --binary -v -E ws-listen:0.0.0.0:9229 ws://172.17.0.2:4848) or use --network=host (docker run --network=host -p 9229:9229 --name websocat solsson/websocat --binary -v -E ws-listen:127.0.0.1:9229 ws://172.17.0.2:4848)

vi avatar Jan 24 '20 22:01 vi

I encounted one case of WebSocket protocol error. It turned out that the server request a Sec-WebSocket-Protocol header, but I didn't provide in connecting. Fixed by websocat ws://[ip]:[port] --protocol [sec-websocket-protocol]

Maybe irrelevant to this issue, just for reference to others.

birdinforest avatar Jan 14 '21 03:01 birdinforest