Files
Ydisks-Xianyu-Helper/internal/xianyu/protocol/decrypt.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

103 lines
2.3 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/base64"
"encoding/json"
"fmt"
"strconv"
"strings"
)
// Decrypt 解密闲鱼 WS 同步包载荷:base64 解码 → MessagePack 解码 → 归一化为 JSON 字符串。
// - map 的键统一转为字符串(整数键 → 数字字符串)
// - []byte(msgpack bin)→ UTF-8 字符串(忽略无效字节)
// - 其余类型原样保留
func Decrypt(data string) (string, error) {
// 清理非 ASCII。
data = stripNonASCII(data)
// base64 解码,必要时补 padding。
decoded, err := base64.StdEncoding.DecodeString(data)
if err != nil {
if pad := len(data) % 4; pad != 0 {
decoded, err = base64.StdEncoding.DecodeString(data + strings.Repeat("=", 4-pad))
}
if err != nil {
return "", fmt.Errorf("解密失败: base64 解码: %w", err)
}
}
d := &msgpackDecoder{data: decoded}
val, err := d.decodeValue()
if err != nil {
return "", fmt.Errorf("解密失败: %w", err)
}
normalized := normalizeForJSON(val)
b, err := json.Marshal(normalized)
if err != nil {
return "", fmt.Errorf("解密失败: JSON 序列化: %w", err)
}
return string(b), nil
}
// stripNonASCII 删除非 ASCII 字符。
func stripNonASCII(s string) string {
var b strings.Builder
b.Grow(len(s))
for _, r := range s {
if r < 0x80 {
b.WriteRune(r)
}
}
return b.String()
}
// normalizeForJSON 把 msgpack 解码出的任意结构转为 json.Marshal 友好的结构。
func normalizeForJSON(v any) any {
switch x := v.(type) {
case map[any]any:
m := make(map[string]any, len(x))
for k, val := range x {
m[keyToString(k)] = normalizeForJSON(val)
}
return m
case []any:
out := make([]any, len(x))
for i, e := range x {
out[i] = normalizeForJSON(e)
}
return out
case []byte:
// 忽略无效 UTF-8 字节。
return strings.ToValidUTF8(string(x), "")
default:
return v
}
}
// keyToString 将非字符串 map 键转换为 JSON 对象键。
func keyToString(k any) string {
switch x := k.(type) {
case string:
return x
case int64:
return strconv.FormatInt(x, 10)
case uint64:
return strconv.FormatUint(x, 10)
case bool:
if x {
return "true"
}
return "false"
case nil:
return "null"
case float64:
return strconv.FormatFloat(x, 'g', -1, 64)
case []byte:
return strings.ToValidUTF8(string(x), "")
default:
return fmt.Sprintf("%v", k)
}
}