diff --git a/CHANGELOG.md b/CHANGELOG.md index a8a923c5..2585ec6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ > 本仓 CHANGELOG 自重建起记(POC 明细见归档仓 /data/poc-archive/repos/go-sms.git 的 CHANGELOG.md,已冻结)。 +## 2026-11-16(晚四)· Issue#3 联调装备:CMPP 抓包分析能力 + 连接日志补盲 + +- **背景**:客户重测在即,-1 类问题需「数据说话」——Connect 成功零日志+无抓包能力导致此前排查吃力(且上轮公网归因教训:通道表第一条即上游 5.16:7890,内网双 7890 并存,客户地址指向待确认)。 +- **抓包**:`apt install tcpdump 4.99.5`(新机原未装);`scripts/cmpp-capture.sh`(start/stop/status/analyze;`-i any -s 0 -C 50 -W 6` 环形 300MB 防打爆;**坑留痕:tcpdump 4.x 写 pcap 默认降权 tcpdump 用户致 Permission denied,须 `-Z root`**;PID 用 pgrep 管理+启动失败即报 stderr)。 +- **分析器**:`scripts/cmpp-analyze.py`(**纯 python 标准库零依赖**):pcap 魔数/纳秒自适应→链路层自适应(SLL/SLL2/以太+VLAN/RAW)→TCP 按 seq 重组(容重传/重叠)→CMPP PDU 按 TotalLength 切分;输出按连接分组时间线(时:分:秒.毫秒),CONNECT(sp/ver/ts)/CONNECT_RESP(**布局自动判定 21B=2.0/18B=3.0**+status 语义)/SUBMIT(srcid/码数/result)/DELIVER/心跳;**status≠0/result≠0 自动 ⚠️**;`analyze all` 合并环形切片。 +- **日志补盲**:server.go `cmpp_conn_accepted{client_ip}`(TCP 到达即留痕)+`cmpp_conn_ok{source_addr,client_ip,ver}`(Connect 成功留痕)——与抓包时间戳毫秒级互证,「客户连没连上」从此可追溯。 +- **自测实证**:三场景探针(本机正确密码 status=0/错误密码 status=2 ⚠️/上游 5.16 status=3 ⚠️)抓包时间线与 gateway 日志**毫秒级对齐**;顺带抓到 4 条上游通道连接心跳(运维流量可见)。 +- **联调 SOP**:`cmpp-capture.sh start` → 客户连 → `stop`+`analyze` → 30 秒出结论(到达与否/卡在哪步/谁拒绝/布局版本是否匹配);与 `cmpp_conn_*` 日志双通道互证。 + ## 2026-11-16(晚三)· Issue#3 追报:客户连公网 7890 非 本平台 + CMPP 版本协商 + 弹窗防截断 - **「登录仍报 -1」根因(网络层,非协议)**:探针对照实证——**公网 121.46.230.53:7890 是另一个 CMPP 网关**(回 Status=3 拒绝,本平台码值从不返回 3),s2demo 在彼处不存在;**本平台 CMPP 服务在内网 192.168.5.18:7890**(同请求 Status=0 鉴权通过)且 gateway 日志零客户连接记录(请求未达)。**结论:客户交付的连接地址指向公网机上的第三方/旧平台网关;本平台无公网 CMPP 入口。**解法=公网机 nginx 加 TCP stream 反代(7890 已被占用须用新端口如 17890→192.168.5.18:7890,需运维执行,配置片段已交付);过渡期客户可内网直连 192.168.5.18:7890。 diff --git a/internal/protocol/cmpp/server.go b/internal/protocol/cmpp/server.go index 82c1d6fe..b3827629 100644 --- a/internal/protocol/cmpp/server.go +++ b/internal/protocol/cmpp/server.go @@ -279,6 +279,9 @@ func (s *Server) handleConn(ctx context.Context, conn net.Conn) { ss := &session{conn: conn} defer s.dropConn(conn, ss) ip := remoteIP(conn) + // Issue#3 联调补盲:TCP 到达即留痕(此前 Connect 成功零日志,客户「连没连上」不可追溯; + // 配合 scripts/cmpp-capture.sh 抓包分析双通道定位)。 + logger.L().Info("cmpp_conn_accepted", zap.String("client_ip", ip)) // ---- Connect 握手 ---- _ = conn.SetReadDeadline(time.Now().Add(s.cfg.LoginTimeout)) @@ -327,6 +330,10 @@ func (s *Server) handleConn(ctx context.Context, conn net.Conn) { if rl, ok := s.h.(SubmitRateLimiter); ok { ss.rate.limit = rl.SubmitRateLimit(sourceAddr) } + // Issue#3 联调补盲:Connect 成功留痕(账号/IP/客户端声明版本——与抓包时间线互证)。 + logger.L().Info("cmpp_conn_ok", + zap.String("source_addr", sourceAddr), zap.String("client_ip", ip), + zap.Uint8("ver", ver)) // ---- 会话循环 ---- for { diff --git a/memory-bank/activeContext.md b/memory-bank/activeContext.md index ffe79530..501f2e8e 100644 --- a/memory-bank/activeContext.md +++ b/memory-bank/activeContext.md @@ -2,7 +2,7 @@ ## 当前状态快照(2026-11-16,**Issue#3 三连修复+方案B 系统配置落地,S4 泳道收官,一期 18/27 计划日**) -- **Issue#3 追报闭环(2026-11-16 晚三)**:①「客户登录仍 -1」根因=**网络层**——探针对照实证公网 121.46.230.53:7890 是**另一个 CMPP 网关**(Status=3 拒绝),本平台 CMPP 在内网 192.168.5.18:7890(Status=0 正常)且日志零客户连接;**解法=公网机 nginx TCP stream 反代(7890 被占须用新端口 17890,运维项,配置片段已交付)/过渡期内网直连**;②CMPP **版本协商**加固(Connect.Version≥0x30 回 3.0 布局 18B/1B,否则 2.0 21B/4B)+**ISMG 串规范修正**(server 恒空 secret→ISMGSecretSource 接口掺账号 ApiSecret;严格客户端重算 ISMG 双版本均匹配);③系统配置弹窗防截断(align-center+append-to-body+92vw+62vh 内滚+select 两行选项+keyHint)。 +- **Issue#3 联调装备(2026-11-16 晚四)**:tcpdump 4.99.5 已装(坑:-Z root 防降权拒写);`scripts/cmpp-capture.sh`(start/stop/status/analyze,环形 300MB)+`scripts/cmpp-analyze.py`(纯标准库 pcap→CMPP 时间线,布局自动判定+异常⚠️);server.go 补 `cmpp_conn_accepted/cmpp_conn_ok` 日志(与抓包毫秒互证);三场景自测全对。**内网事实(数据说话)**:192.168.5.16:7890=上游网关(通道表 9102 对接点,对 s2demo 回 status=3 拒);192.168.5.18:7890=本平台(status=0 正常);平台零客户到达记录——**客户配置地址指向待其确认(疑 5.16/公网)**;重测 SOP=capture start→客户连→analyze,30 秒定位。 - **Issue#3 三连修复(2026-11-16 晚)**:①CMPP 连接报 -1 根因=`ConnectResp` 误按 **3.0 的 1B Status** 编码(标准 2.0 客户端按 4B 大端解析恒非 0)→ 修复=Marshal 4B(21B 包体)+AuthenticatorISMG 4B 参与 MD5+`ParseConnectResp` 自适应双布局(≥20B/2.0,17~19B/3.0);标准 2.0 探针真机实证 18B/垃圾值→21B/Status=0,**教训:2.0 与 3.0 在 CONNECT_RESP.Status/SUBMIT_RESP.Result 上互为镜像(2.0=4B/1B),自家 client+server 同源对称自测掩盖协议错位——同 Day39 坑**;②充值无响应=前端发 `{count}` 后端要 `{amount}`+submit 无 catch 静默吞错 → API/mock/组件三处修复;③SrcID 核验**用户裁定方案 C(前缀+动态扩展,行业惯例)**——s2demo 已配 ext_code=10690331(通道码即前缀),客户 SrcID 须以 10690331 开头+可追加扩展(≤21 位);真机四场景实证(纯码→10 过/码+扩展→10 过/不匹配→9/空→9);**运营口径:ext_code 变更后 30s 缓存生效(Day63 accCache)**。顺手修 TestClient_LoginOK 时序竞争(轮询吸收);门禁 -race 41 包/vitest 165/build 全绿;gateway 已串行部署(md5 比对 ✓)。 - **仓库**:main 已推 Gitea;每日 bundle+push 双轨。 diff --git a/scripts/cmpp-analyze.py b/scripts/cmpp-analyze.py new file mode 100755 index 00000000..72d1de49 --- /dev/null +++ b/scripts/cmpp-analyze.py @@ -0,0 +1,257 @@ +#!/usr/bin/env python3 +# cmpp-analyze.py — CMPP 联调 pcap 分析器(Issue#3 排查装备;纯标准库零依赖) +# +# 输入:tcpdump -i any 抓的 pcap(含环形切片);输出:按 TCP 连接分组的 CMPP 时间线 +# + 异常自动标记(status≠0/result≠0/RST/握手失败/未知命令)。 +# 用法:python3 cmpp-analyze.py [--pkts] +import struct +import sys +import time + + +def fmt_ts(ts): + t = time.localtime(ts) + return "%02d:%02d:%02d.%03d" % (t.tm_hour, t.tm_min, t.tm_sec, int((ts % 1) * 1000)) + +# ---- CMPP 2.0 命令字 ---- +CMDS = { + 0x00000001: "CONNECT", 0x80000001: "CONNECT_RESP", + 0x00000002: "TERMINATE", 0x80000002: "TERMINATE_RESP", + 0x00000003: "SUBMIT", 0x80000003: "SUBMIT_RESP", + 0x00000004: "DELIVER", 0x80000004: "DELIVER_RESP", + 0x00000008: "ACTIVE_TEST", 0x80000008: "ACTIVE_TEST_RESP", +} +CONN_STATUS = {0: "成功", 1: "结构非法", 2: "账号密码错", 3: "版本/企业代码错", + 4: "IP白名单拒", 5: "账号停用", 8: "连接数超限"} +SUBMIT_RESULT = {0: "成功", 1: "结构错", 8: "流量控制", 9: "通道号前缀不匹配", 10: "业务拒绝(余额/审核)"} + + +def trim_zero(b): + return b.rstrip(b"\x00").decode("utf-8", "replace") + + +# ---- pcap 文件层(大小端/纳秒魔数自适应) ---- +def read_pcap(path): + with open(path, "rb") as f: + gh = f.read(24) + if len(gh) < 24: + raise SystemExit("pcap 头不足") + magic = gh[:4] + if magic == b"\xd4\xc3\xb2\xa1": + end, nano = "<", False + elif magic == b"\xa1\xb2\xc3\xd4": + end, nano = ">", False + elif magic == b"\x4d\x3c\xb2\xa1": + end, nano = "<", True + elif magic == b"\xa1\xb2\x3c\x4d": + end, nano = ">", True + else: + raise SystemExit("非 pcap 文件(magic=%s)" % magic.hex()) + link = struct.unpack(end + "I", gh[20:24])[0] + while True: + rh = f.read(16) + if len(rh) < 16: + return + ts_s, ts_f, incl, _orig = struct.unpack(end + "IIII", rh) + data = f.read(incl) + if len(data) < incl: + return + yield (ts_s + (ts_f / 1e9 if nano else ts_f / 1e6), link, data) + + +# ---- 链路层 → IP(自适应 -i any 的 SLL/SLL2/以太+VLAN/RAW) ---- +def to_ip(link, data): + if link == 1: # Ethernet(含 VLAN 堆叠) + if len(data) < 14: + return None + eth, off = data[12:14], 14 + while eth == b"\x81\x00" and len(data) >= off + 4: + eth, off = data[off + 2:off + 4], off + 4 + return data[off:] if eth == b"\x08\x00" else None + if link == 101: # RAW IP + return data + if link == 113: # LINUX_SLL + if len(data) < 16 or struct.unpack(">H", data[14:16])[0] != 0x0800: + return None + return data[16:] + if link == 276: # LINUX_SLL2 + if len(data) < 20 or struct.unpack(">H", data[0:2])[0] != 0x0800: + return None + return data[20:] + if link == 0: # NULL/LOOP + return data[4:] if len(data) > 4 else None + return None + + +def parse_ip(data): + if len(data) < 20 or (data[0] >> 4) != 4: + return None + ihl = (data[0] & 0xF) * 4 + if len(data) < ihl or data[9] != 6: # 仅 TCP + return None + src = ".".join(str(b) for b in data[12:16]) + dst = ".".join(str(b) for b in data[16:20]) + return src, dst, data[ihl:] + + +def parse_tcp(data): + if len(data) < 20: + return None + sport, dport, seq = struct.unpack(">HHI", data[0:8]) + off = (data[12] >> 4) * 4 + return sport, dport, seq, data[13], data[off:] + + +# ---- 流重组:双向归并;载荷按 seq 排序去重拼接(容重传/重叠) ---- +def stream_key(src, sport, dst, dport): + a, b = (src, sport), (dst, dport) + return (a, b) if a <= b else (b, a) + + +def reassemble(events): + streams = {} + for ts, key, d, seq, flags, payload in events: + st = streams.setdefault(key, {"first": ts, "last": ts, + "segs": [], "data": {0: [], 1: []}}) + st["last"] = ts + st["segs"].append((ts, d, flags)) + if payload: + st["data"][d].append((seq & 0xFFFFFFFF, payload)) + for st in streams.values(): + for d in (0, 1): + buf, expect = bytearray(), None + for seq, payload in sorted(st["data"][d], key=lambda x: x[0]): + if expect is None or seq == expect: + buf += payload + elif seq < expect: + skip = expect - seq + if skip < len(payload): + buf += payload[skip:] + else: + continue + else: + buf += payload # 缺块乱序:尽力拼接 + expect = (seq + len(payload)) & 0xFFFFFFFF + st["data"][d] = bytes(buf) + return streams + + +# ---- CMPP PDU 切分与解码 ---- +def split_pdus(buf): + out, off = [], 0 + while off + 12 <= len(buf): + total = struct.unpack(">I", buf[off:off + 4])[0] + if total < 13 or total > 65535: + break + if off + total > len(buf): + break # 尾部截断(环形切片) + out.append((struct.unpack(">I", buf[off + 4:off + 8])[0], + struct.unpack(">I", buf[off + 8:off + 12])[0], + buf[off + 12:off + total])) + off += total + return out, buf[off:] + + +def dec_connect(body): + return "sp=%s ver=0x%02x ts=%d" % (trim_zero(body[0:6]), body[22], + struct.unpack(">I", body[23:27])[0]) + + +def dec_connect_resp(body): + if len(body) >= 20: + st, layout = struct.unpack(">I", body[0:4])[0], "2.0(4B)" + ver = body[20] if len(body) > 20 else 0 + else: + st, layout = (body[0] if body else -1), "3.0(1B)" + ver = body[17] if len(body) > 17 else 0 + return "布局=%s(%dB) status=%d(%s) ver=0x%02x" % ( + layout, len(body), st, CONN_STATUS.get(st, "?"), ver), st + + +def dec_pdu(cmd, body): + name = CMDS.get(cmd, "UNKNOWN_0x%08x" % cmd) + detail, bad = "", False + if cmd == 0x00000001 and len(body) >= 27: + detail = dec_connect(body) + elif cmd == 0x80000001: + detail, st = dec_connect_resp(body) + bad = st != 0 + elif cmd == 0x00000003 and len(body) >= 140: + n = body[116] + ml = body[117 + 21 * n] if len(body) > 117 + 21 * n else -1 + detail = "srcid=%s %d码 %dB udhi=%d" % (trim_zero(body[95:116]), n, ml, body[45]) + elif cmd == 0x80000003 and len(body) >= 9: + r = body[8] + detail = "msgid=%d result=%d(%s)" % (struct.unpack(">Q", body[0:8])[0], + r, SUBMIT_RESULT.get(r, "?")) + bad = r != 0 + elif cmd == 0x00000004 and len(body) >= 65: + detail = "src=%s report=%s" % (trim_zero(body[42:63]), "是" if body[63] == 1 else "否") + return name, detail, bad + + +def flags_str(f): + s = "" + if f & 0x02: s += "SYN " + if f & 0x01: s += "FIN " + if f & 0x04: s += "RST " + return s.strip() + + +def main(): + if len(sys.argv) < 2: + raise SystemExit("用法: cmpp-analyze.py [--pkts]") + show_pkts, path = "--pkts" in sys.argv, sys.argv[1] + events, skipped = [], 0 + for ts, link, data in read_pcap(path): + ip = to_ip(link, data) + if not ip: + continue + pr = parse_ip(ip) + if not pr: + continue + src, dst, tcp = pr + tp = parse_tcp(tcp) + if not tp: + continue + sport, dport, seq, flags, payload = tp + if sport != 7890 and dport != 7890: + skipped += 1 + continue + key = stream_key(src, sport, dst, dport) + events.append((ts, key, 0 if (src, sport) == key[0] else 1, seq, flags, payload)) + if not events: + print("无 7890 端口流量(过滤非 7890 包 %d 个)——确认客户端目标地址/端口。" % skipped) + return + streams = reassemble(events) + print("=" * 96) + print("pcap=%s | 连接数=%d | %.3f ~ %.3f" % (path, len(streams), + events[0][0], max(e[0] for e in events))) + print("=" * 96) + for i, (key, st) in enumerate(sorted(streams.items(), key=lambda x: x[1]["first"]), 1): + (s1, p1), (s2, p2) = key + print("\n[连接%d] %s:%d ⇄ %s:%d" % (i, s1, p1, s2, p2)) + for d in (0, 1): + pdus, tail = split_pdus(st["data"][d]) + a = (s1, p1) if d == 0 else (s2, p2) + b = (s2, p2) if d == 0 else (s1, p1) + t0 = next((t for t, dd, _ in st["segs"] if dd == d), st["first"]) + for cmd, sq, body in pdus: + name, detail, bad = dec_pdu(cmd, body) + print(" %s %s:%d→%s:%d %-14s seq=%-6d %s%s" % ( + fmt_ts(t0), a[0], a[1], b[0], b[1], name, sq, detail, " ⚠️" if bad else "")) + if len(tail) >= 4: + print(" %s %s:%d→%s:%d (尾段 %dB 未成完整 PDU)" % (fmt_ts(t0), a[0], a[1], b[0], b[1], len(tail))) + if show_pkts: + for ts, d, fl in st["segs"]: + fs = flags_str(fl) + if fs: + side = key[0] if d == 0 else key[1] + print(" %s TCP %-10s %s:%d" % (fmt_ts(ts), fs, side[0], side[1])) + print("\n" + "=" * 96) + print("⚠️=拒绝/异常码;CONNECT_RESP 布局自动判定(2.0=21B/3.0=18B)。") + print("客户端报-1:先看 CONNECT_RESP.status 及布局是否匹配其版本;无本机流量=地址/端口未指向本平台。") + + +if __name__ == "__main__": + main() diff --git a/scripts/cmpp-capture.sh b/scripts/cmpp-capture.sh new file mode 100755 index 00000000..577bdc90 --- /dev/null +++ b/scripts/cmpp-capture.sh @@ -0,0 +1,97 @@ +#!/usr/bin/env bash +# cmpp-capture.sh — CMPP 联调抓包控制(Issue#3:客户连接报 -1 排查装备) +# +# 用法: +# scripts/cmpp-capture.sh start [端口] # 后台抓包(默认 7890;可 'host x.x.x.x' 过滤) +# scripts/cmpp-capture.sh stop # 停止 +# scripts/cmpp-capture.sh status # 运行状态/文件/当前 7890 连接 +# scripts/cmpp-capture.sh analyze [pcap] # 解析最新(或指定)pcap → CMPP 时间线 +# +# 抓包口径:-i any(LINUX_SLL 链路层,分析器自适应)/ -s 0 全包(长短信 Submit ~3KB) +# / -C 50 -W 6 环形 50MB×6 防打爆 / -tttt.n 微秒时间戳 +# 权限:sudo(sudoers NOPASSWD 已配);无 root 时提示降级方案(python 探针)。 +set -euo pipefail + +CAP_DIR="/data/go-sms-v2/logs/capture" +PID_FILE="$CAP_DIR/capture.pid" +LOG_FILE="$CAP_DIR/tcpdump-stderr.log" + +ensure_dir() { mkdir -p "$CAP_DIR"; } + +running() { pgrep -x tcpdump >/dev/null 2>&1; } + +cmd_start() { + if running; then echo "已在运行(PID $(cat "$PID_FILE")),文件:"; ls -lh "$CAP_DIR"/*.pcap 2>/dev/null | tail -2; exit 0; fi + ensure_dir + local filter="${1:-7890}" + local f="port ${filter}" + # 传 host x.x.x.x 形式时原样作过滤器 + [[ "$filter" == *" "* ]] && f="$filter" + local ts file + ts=$(date +%Y%m%d-%H%M%S) + file="$CAP_DIR/cmpp-$ts.pcap" + if ! sudo -n true 2>/dev/null; then + echo "ERROR: sudo 免密不可用,tcpdump 需 root(抓不了包;可用 python3 探针替代连通性探测)" >&2; exit 1 + fi + # -Z root:tcpdump 4.x 写 pcap 默认降权 tcpdump 用户(目录无权→Permission denied) + sudo -n nohup tcpdump -Z root -i any -s 0 -C 50 -W 6 -tttt -n -U -w "$file" "$f" > "$LOG_FILE" 2>&1 & + sleep 1 + local pid + pid=$(pgrep -x tcpdump | head -1 || true) + if [[ -z "$pid" ]]; then + echo "ERROR: tcpdump 启动失败:" >&2; cat "$LOG_FILE" >&2; exit 1 + fi + echo "$pid" > "$PID_FILE" + echo "抓包已启动: PID=$pid 过滤器='$f'" + echo "文件: $file(环形 50MB×6)" + echo "停止: scripts/cmpp-capture.sh stop ;分析: scripts/cmpp-capture.sh analyze" +} + +cmd_stop() { + if ! running; then echo "未在运行"; rm -f "$PID_FILE"; exit 0; fi + sudo -n pkill -x tcpdump 2>/dev/null || pkill -x tcpdump 2>/dev/null || true + sleep 1 + rm -f "$PID_FILE" + echo "已停止。最近文件:" + ls -lht "$CAP_DIR"/*.pcap* 2>/dev/null | head -3 + echo "分析: scripts/cmpp-capture.sh analyze" +} + +cmd_status() { + if running; then + echo "运行中 PID=$(cat "$PID_FILE")" + else + echo "未在运行" + fi + ls -lht "$CAP_DIR"/*.pcap* 2>/dev/null | head -3 || echo "(无 pcap)" + echo "--- 当前 7890 连接(含出向)---" + ss -tn 2>/dev/null | grep ':7890' || echo "(无)" + tail -3 "$LOG_FILE" 2>/dev/null || true +} + +cmd_analyze() { + local mode="${1:-}" + ensure_dir + # 环形切片 .pcapN:默认分析最新单片;all=cat 全组(按编号序)到临时文件合并分析 + local f newest group + f=$(ls -t "$CAP_DIR"/*.pcap* 2>/dev/null | head -1 || true) + if [[ -z "$f" ]]; then echo "ERROR: 无 pcap 文件(先 start)" >&2; exit 1; fi + if [[ "$mode" == "all" ]]; then + group="${f%.pcap*}.pcap" + local merged="/tmp/cmpp-merged-$$.pcap" + cat "$group" "$group"[0-9]* 2>/dev/null > "$merged" || cat "$group"* > "$merged" + echo "(合并 $(ls "$group"* 2>/dev/null | wc -l) 个切片 → $merged)" + python3 /data/go-sms-v2/scripts/cmpp-analyze.py "$merged" + rm -f "$merged" + else + python3 /data/go-sms-v2/scripts/cmpp-analyze.py "$f" + fi +} + +case "${1:-status}" in + start) shift || true; cmd_start "$@" ;; + stop) cmd_stop ;; + status) cmd_status ;; + analyze) shift || true; cmd_analyze "$@" ;; + *) echo "用法: $0 {start [端口|过滤式]|stop|status|analyze [pcap]}"; exit 1 ;; +esac