Files
Ydisks-Xianyu-Helper/internal/xianyu/protocol/msgpack.go
T
ChristandClaude Opus 4.8 980de41b53 chore: 重构配套改动 + 包文档 + 构建产物清理
automation 重构期间的配套修改:
- browser:订单抓取/密码登录/二维码刷新/stealth 等适配新流程,删 search.go
- db:cookies/orders_ext/extras/sessions/users/password/repository 等调整
- xianyu:mtop client、protocol(sign/decrypt/msgpack/cookies)、qrlogin、ws、ua
- server:admin/analytics/auth/cookie/helpers/item/item_publish_batch handlers
- frontend:App/Dashboard/ItemList/Rules/Sidebar/api/types,删 Keywords/TestPage
- auth、notify、webui、go.mod 等

本次会话补充:
- cmd/server、cmd/init-admin 改为 // Package main 开头的标准包文档
- .gitignore 加 internal/webui/static/assets/(hash 化构建产物不入库)
- internal/webui/static/index.html 重新构建

全量 go build/vet/test + race + 前端 test/build 通过。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 21:41:03 +08:00

221 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package protocol
import (
"encoding/binary"
"fmt"
"math"
)
// msgpackDecoder 解析闲鱼消息使用的 MessagePack 数据。
// 解码结果类型:int64/uint64/float64/string/bool/nil/[]byte/[]any/map[any]any。
// bin 解码为 []byte,map 键保留原类型(整数键为 int64)。
type msgpackDecoder struct {
data []byte
pos int
}
func (d *msgpackDecoder) readByte() (byte, error) {
if d.pos >= len(d.data) {
return 0, fmt.Errorf("msgpack: unexpected end of data")
}
b := d.data[d.pos]
d.pos++
return b, nil
}
func (d *msgpackDecoder) readBytes(n int) ([]byte, error) {
if d.pos+n > len(d.data) {
return nil, fmt.Errorf("msgpack: unexpected end of data")
}
r := d.data[d.pos : d.pos+n]
d.pos += n
return r, nil
}
func (d *msgpackDecoder) decodeValue() (any, error) {
fb, err := d.readByte()
if err != nil {
return nil, err
}
switch {
// positive fixint
case fb <= 0x7f:
return int64(fb), nil
// fixmap
case fb >= 0x80 && fb <= 0x8f:
return d.decodeMap(int(fb & 0x0f))
// fixarray
case fb >= 0x90 && fb <= 0x9f:
return d.decodeArray(int(fb & 0x0f))
// fixstr
case fb >= 0xa0 && fb <= 0xbf:
return d.readString(int(fb & 0x1f))
}
switch fb {
case 0xc0:
return nil, nil
case 0xc2:
return false, nil
case 0xc3:
return true, nil
case 0xc4: // bin 8
n, err := d.readByte()
if err != nil {
return nil, err
}
return d.readBytes(int(n))
case 0xc5: // bin 16
b, err := d.readBytes(2)
if err != nil {
return nil, err
}
return d.readBytes(int(binary.BigEndian.Uint16(b)))
case 0xc6: // bin 32
b, err := d.readBytes(4)
if err != nil {
return nil, err
}
return d.readBytes(int(binary.BigEndian.Uint32(b)))
case 0xca: // float 32
b, err := d.readBytes(4)
if err != nil {
return nil, err
}
bits := binary.BigEndian.Uint32(b)
return float64(math.Float32frombits(bits)), nil
case 0xcb: // float 64
b, err := d.readBytes(8)
if err != nil {
return nil, err
}
return math.Float64frombits(binary.BigEndian.Uint64(b)), nil
case 0xcc: // uint 8
n, err := d.readByte()
return uint64(n), err
case 0xcd: // uint 16
b, err := d.readBytes(2)
if err != nil {
return nil, err
}
return uint64(binary.BigEndian.Uint16(b)), nil
case 0xce: // uint 32
b, err := d.readBytes(4)
if err != nil {
return nil, err
}
return uint64(binary.BigEndian.Uint32(b)), nil
case 0xcf: // uint 64
b, err := d.readBytes(8)
if err != nil {
return nil, err
}
return binary.BigEndian.Uint64(b), nil
case 0xd0: // int 8
n, err := d.readByte()
// #nosec G115 -- MessagePack 有符号整数使用二进制补码编码,此转换用于符号扩展。
return int64(int8(n)), err
case 0xd1: // int 16
b, err := d.readBytes(2)
if err != nil {
return nil, err
}
return int64(int16(binary.BigEndian.Uint16(b))), nil // #nosec G115 -- 协议要求的符号扩展
case 0xd2: // int 32
b, err := d.readBytes(4)
if err != nil {
return nil, err
}
return int64(int32(binary.BigEndian.Uint32(b))), nil // #nosec G115 -- 协议要求的符号扩展
case 0xd3: // int 64
b, err := d.readBytes(8)
if err != nil {
return nil, err
}
return int64(binary.BigEndian.Uint64(b)), nil // #nosec G115 -- 协议要求的符号扩展
case 0xd9: // str 8
n, err := d.readByte()
if err != nil {
return nil, err
}
return d.readString(int(n))
case 0xda: // str 16
b, err := d.readBytes(2)
if err != nil {
return nil, err
}
return d.readString(int(binary.BigEndian.Uint16(b)))
case 0xdb: // str 32
b, err := d.readBytes(4)
if err != nil {
return nil, err
}
return d.readString(int(binary.BigEndian.Uint32(b)))
case 0xdc: // array 16
b, err := d.readBytes(2)
if err != nil {
return nil, err
}
return d.decodeArray(int(binary.BigEndian.Uint16(b)))
case 0xdd: // array 32
b, err := d.readBytes(4)
if err != nil {
return nil, err
}
return d.decodeArray(int(binary.BigEndian.Uint32(b)))
case 0xde: // map 16
b, err := d.readBytes(2)
if err != nil {
return nil, err
}
return d.decodeMap(int(binary.BigEndian.Uint16(b)))
case 0xdf: // map 32
b, err := d.readBytes(4)
if err != nil {
return nil, err
}
return d.decodeMap(int(binary.BigEndian.Uint32(b)))
}
// negative fixint
if fb >= 0xe0 {
// #nosec G115 -- negative fixint 使用二进制补码编码。
return int64(int8(fb)), nil
}
return nil, fmt.Errorf("msgpack: unknown format byte 0x%02x", fb)
}
func (d *msgpackDecoder) readString(n int) (string, error) {
b, err := d.readBytes(n)
if err != nil {
return "", err
}
return string(b), nil // UTF-8
}
func (d *msgpackDecoder) decodeArray(n int) (any, error) {
arr := make([]any, n)
for i := 0; i < n; i++ {
v, err := d.decodeValue()
if err != nil {
return nil, err
}
arr[i] = v
}
return arr, nil
}
func (d *msgpackDecoder) decodeMap(n int) (any, error) {
m := make(map[any]any, n)
for i := 0; i < n; i++ {
k, err := d.decodeValue()
if err != nil {
return nil, err
}
v, err := d.decodeValue()
if err != nil {
return nil, err
}
m[k] = v
}
return m, nil
}