feat(issue#3 联调装备): tcpdump+CMPP pcap 时间线分析器(纯标准库)+cmpp_conn_accepted/ok 日志补盲——三场景自测与日志毫秒互证

This commit is contained in:
2026-11-18 10:58:18 +08:00
parent cc99aac16f
commit 5591a485ad
5 changed files with 371 additions and 1 deletions
+257
View File
@@ -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 <file.pcap> [--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 <file.pcap> [--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()
+97
View File
@@ -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