ITPub博客

首页 > IT职业 > IT职场 > WebSocket的Frame协议解析

WebSocket的Frame协议解析

IT职场 作者:yzx19 时间:2021-09-09 10:01:56 0 删除 编辑

先给出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};//掩码编码过得payloadbyte[] 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



作者:我是杨正
链接:https://www.jianshu.com/p/9cd229384726


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3269/viewspace-2815398/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 函数的作用域
请登录后发表评论 登录
全部评论

注册时间:2019-08-13

  • 博文量
    87
  • 访问量
    973