WebSocket的Frame协议解析

版权声明:欢迎访问个人博客网站 www.dubby.cn,和个人微信公众号ITBusTech https://blog.csdn.net/u011499747/article/details/83055845

原文链接:https://www.dubby.cn/detail.html?id=9105

先给出WebSocket Frame的协议

image

复制抓包抓到的数据:

81 85 30 6c e2 9a 54 19 80 f8 49

字段分析:

81 85 30 6c e2 9a 54 19 80 f8 49
10000001 10000101 00110000 01101100 11100010 10011010 01010100 00011001 10000000 11111000 01001001
FIN:1,RSV1-3=0,OpCode=1 mask=1,payloadLen=101 Masking-key Masking-key Masking-key Masking-key Payload Data Payload Data Payload Data Payload Data Payload Data
  • FIN=1,说明这是这个消息的最后一个片段,我们这次的消息很短,第一个也就是最后一个
  • RSV1, RSV2, RSV3:分别是1bit,且一般时候都是0
  • OpCode=1(表示是一个text frame)
    • %x0 连续的frame
    • %x1 文本frame
    • %x2 二进制frame
    • %x3-7 预留
    • %x8 连接关闭
    • %x9 ping
    • %xA pong
    • %xB-F 预留
  • 如果mask=1,那么Masking-key存在,且都是4个字节(32位)
  • payloadLen=101(5),说明字节长度是4

最后我们看到数据部分是:

01010100 00011001 10000000 11111000 01001001
54 19 80 f8 49

掩码解码逻辑:

//掩码
byte[] maskingKeyBytes = {(byte) 0x30, (byte) 0x6c, (byte) 0xe2, (byte) 0x9a};
//掩码编码过得payload
byte[] maskedBytes = {(byte) 0x54, (byte) 0x19, (byte) 0x80, (byte) 0xf8, (byte) 0x49};
int length = maskedBytes.length;
//解码的结果
byte[] unmaskedByte = new byte[length];

for (int i = 0; i < length; ++i) {
    byte masking = maskingKeyBytes[i % 4];
    unmaskedByte[i] = (byte) (maskedBytes[i] ^ masking);
}

for (byte b : unmaskedByte) {
    System.out.print(b + " ");
}

输出:

100 117 98 98 121

转成16进制,正好是

64 75 62 62 79

dubby的utf-8编码后是:

01100100 01110101 01100010 01100010 01111001
64 75 62 62 79

没错,我发的消息就是dubby。

猜你喜欢

转载自blog.csdn.net/u011499747/article/details/83055845