How do I connect to a websocket manually, with netcat/socat/telnet?

和自甴很熟 提交于 2020-12-24 06:42:51

问题


I am trying to connect to the reference websocket echo server "manually", in order to learn how the protocol works (I am using socat for that). However, the server invariably closes the connection without providing an answer. Any idea why?

Here is what I do:

socat - TCP:echo.websocket.org:80

Then, I paste the following text in the terminal:

GET /?encoding=text HTTP/1.1
Origin: http://www.websocket.org
Connection: Upgrade
Host: echo.websocket.org
Sec-WebSocket-Key: P7Kp2hTLNRPFMGLxPV47eQ==
Upgrade: websocket
Sec-WebSocket-Version: 13

I sniffed the parameters of the connection with the developer tools, in firefox, on the same machine, where this works flawlessly: therefore, I would assume they are correct. However after that, the server closes the connection immediately, without providing an answer. Why? How can I implement the protocol "manually"?

I would like type test in my terminal and get the server to reply with what I typed (It works in a web browser).


回答1:


I think you want to modify the socket stream to translate \n (line feed) to CRLF (Carriage return & line feed). Doing info socat produces detailed information which includes this modifier:

crnl   Converts the default line termination character NL ('\n',  0x0a)
       to/from CRNL ("\r\n", 0x0d0a) when writing/reading on this chan-
       nel (example).  Note: socat simply strips all CR characters.

So I think you should be able to do this:

socat - TCP:echo.websocket.org:80,crnl



回答2:


I'd like to add that my WebSocket tool websocat can help in debugging the WebSocket protocol, especially when combined with socat:

$ websocat - ws-c:sh-c:"socat -v -x - tcp:echo.websocket.org:80" --ws-c-uri ws://echo.websocket.org
> 2018/07/03 16:30:06.021658  length=157 from=0 to=156
 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a  GET / HTTP/1.1..
 48 6f 73 74 3a 20 65 63 68 6f 2e 77 65 62 73 6f  Host: echo.webso
 63 6b 65 74 2e 6f 72 67 0d 0a                    cket.org..
 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72  Connection: Upgr
 61 64 65 0d 0a                                   ade..
 55 70 67 72 61 64 65 3a 20 77 65 62 73 6f 63 6b  Upgrade: websock
 65 74 0d 0a                                      et..
 53 65 63 2d 57 65 62 53 6f 63 6b 65 74 2d 56 65  Sec-WebSocket-Ve
 72 73 69 6f 6e 3a 20 31 33 0d 0a                 rsion: 13..
 53 65 63 2d 57 65 62 53 6f 63 6b 65 74 2d 4b 65  Sec-WebSocket-Ke
 79 3a 20 59 76 36 32 44 31 57 6d 7a 79 79 31 65  y: Yv62D1Wmzyy1e
 69 6d 62 47 6d 68 69 61 67 3d 3d 0d 0a           imbGmhiag==..
 0d 0a                                            ..
--
< 2018/07/03 16:30:06.164057  length=201 from=0 to=200
 48 54 54 50 2f 31 2e 31 20 31 30 31 20 57 65 62  HTTP/1.1 101 Web
 20 53 6f 63 6b 65 74 20 50 72 6f 74 6f 63 6f 6c   Socket Protocol
 20 48 61 6e 64 73 68 61 6b 65 0d 0a               Handshake..
 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 55 70 67 72  Connection: Upgr
 61 64 65 0d 0a                                   ade..
 44 61 74 65 3a 20 54 75 65 2c 20 30 33 20 4a 75  Date: Tue, 03 Ju
 6c 20 32 30 31 38 20 31 33 3a 31 35 3a 30 30 20  l 2018 13:15:00 
 47 4d 54 0d 0a                                   GMT..
 53 65 63 2d 57 65 62 53 6f 63 6b 65 74 2d 41 63  Sec-WebSocket-Ac
 63 65 70 74 3a 20 55 56 6a 32 74 35 50 43 7a 62  cept: UVj2t5PCzb
 58 49 32 52 4e 51 75 70 2f 71 48 31 63 5a 44 6e  XI2RNQup/qH1cZDn
 38 3d 0d 0a                                      8=..
 53 65 72 76 65 72 3a 20 4b 61 61 7a 69 6e 67 20  Server: Kaazing 
 47 61 74 65 77 61 79 0d 0a                       Gateway..
 55 70 67 72 61 64 65 3a 20 77 65 62 73 6f 63 6b  Upgrade: websock
 65 74 0d 0a                                      et..
 0d 0a                                            ..
--
ABCDEF
> 2018/07/03 16:30:12.707919  length=13 from=157 to=169
 82 87 40 57 f5 88 01 15 b6 cc 05 11 ff           ..@W.........
--
< 2018/07/03 16:30:12.848398  length=9 from=201 to=209
 82 07 41 42 43 44 45 46 0a                       ..ABCDEF.
--
ABCDEF
> 2018/07/03 16:30:14.528333  length=6 from=170 to=175
 88 80 18 ec 05 a8                                ......
--
< 2018/07/03 16:30:14.671629  length=2 from=210 to=211
 88 00                                            ..
--

In case of failures with manually driven socat -v -x - TCP:echo.websocket.org:80,crnl (mentioned in the other answer), you can compare it with WebSocat-driven socat like in session depicted above.

Reverse (server) example with socat debug dump:

socat -v -x tcp-l:1234,fork,reuseaddr exec:'websocat -t ws-u\:stdio\: mirror\:'



回答3:


Alternatively, here is a way to connect and read the stream from a wss secure websocket stream from the command line using solely core php.

php -r '$sock=stream_socket_client("tls://echo.websocket.org:443",$e,$n,30,STREAM_CLIENT_CONNECT,stream_context_create(null));if(!$sock){echo"[$n]$e".PHP_EOL;}else{fwrite($sock,"GET / HTTP/1.1\r\nHost: echo.websocket.org\r\nAccept: */*\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: ".rand(0,999)."\r\n\r\n");while(!feof($sock)){var_dump(fgets($sock,2048));}}'

Other similar example, pulling from another wss server: (Do not get rekt)

php -r '$sock=stream_socket_client("tls://stream.binance.com:9443",$e,$n,30,STREAM_CLIENT_CONNECT,stream_context_create(null));if(!$sock){echo"[$n]$e".PHP_EOL;}else{fwrite($sock,"GET /stream?streams=btcusdt@kline_1m HTTP/1.1\r\nHost: stream.binance.com:9443\r\nAccept: */*\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: ".rand(0,999)."\r\n\r\n");while(!feof($sock)){var_dump(explode(",",fgets($sock,512)));}}'


来源:https://stackoverflow.com/questions/26507959/how-do-i-connect-to-a-websocket-manually-with-netcat-socat-telnet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!