WebSocket的Frame协议解析

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

先给出WebSocket Frame的协议

image

复制抓包抓到的数据:

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

字段分析:

8185306ce29a541980f849
1000000110000101001100000110110011100010100110100101010000011001100000001111100001001001
FIN:1,RSV1-3=0,OpCode=1mask=1,payloadLen=101Masking-keyMasking-keyMasking-keyMasking-keyPayload DataPayload DataPayload DataPayload DataPayload 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

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

0101010000011001100000001111100001001001
541980f849

掩码解码逻辑:

//掩码
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编码后是:

0110010001110101011000100110001001111001
6475626279

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值