如何使用netcat/socat/telnet手动连接到websocket?

use*_*678 19 bash terminal socat websocket

我试图"手动" 连接到参考websocket echo服务器,以了解协议如何工作(我正在使用socat).但是,服务器总是在不提供答案的情况下关闭连接.知道为什么吗?

这是我做的:

socat - TCP:echo.websocket.org:80
Run Code Online (Sandbox Code Playgroud)

然后,我在终端中粘贴以下文本:

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
Run Code Online (Sandbox Code Playgroud)

我在同一台机器上使用firefox中的开发人员工具嗅探了连接的参数,这样可以完美地运行:因此,我认为它们是正确的.但是,之后,服务器立即关闭连接,而不提供答案.为什么?如何"手动"实施协议?

我想在我的终端中键入test并让服务器回复我输入的内容(它在Web浏览器中工作).

Mic*_*tch 28

我想你想修改套接字流来将\n(换行)转换为CRLF(回车和换行).执行info socat生成包含此修饰符的详细信息:

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.
Run Code Online (Sandbox Code Playgroud)

所以我认为你应该能够做到这一点:

socat - TCP:echo.websocket.org:80,crnl
Run Code Online (Sandbox Code Playgroud)


Vi.*_*Vi. 5

我想补充一点,我的 WebSocket 工具websocat可以帮助调试 WebSocket 协议,尤其是与 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                                            ..
--
Run Code Online (Sandbox Code Playgroud)

如果手动驱动失败socat -v -x - TCP:echo.websocket.org:80,crnl(在另一个答案中提到),您可以将它与 WebSocat 驱动的 socat 进行比较,就像上面描述的会话一样。

带有 socat 调试转储的反向(服务器)示例:

socat -v -x tcp-l:1234,fork,reuseaddr exec:'websocat -t ws-u\:stdio\: mirror\:'
Run Code Online (Sandbox Code Playgroud)