// body.go CMPP 2.0 包体(Day 31:Connect 鉴权握手 + ActiveTest 心跳; // Submit/Deliver 包体 Day 32 实现)。 // // 鉴权算法(CMPP 2.0 官方规范,经真实网关验证): // // timestamp = 月×1e8 + 日×1e6 + 时×1e4 + 分×100 + 秒(uint32 十进制压缩) // authenticatorSource = MD5(sourceAddr + 9×0x00 + shared + timestamp[4B大端]) // authenticatorISMG = MD5(status + authenticatorSource + shared) // Connect 包体 27B = source_addr(6) + authenticator(16) + version(1) + timestamp(4B大端) // (Day 31 二次修正:此前误用 10 字节 ASCII 时间戳/33 字节包体——网关按 27B 解析错位, // 鉴权恒不匹配 status=3;参照 /data/sms 同网关成功实现定位。) package cmpp import ( "crypto/md5" "encoding/binary" "errors" "fmt" "time" ) // Connect 包体(source_addr 6B + authenticator 16B + version 1B + timestamp 4B)。 type Connect struct { SourceAddr string // 企业代码(≤6 字节,左对齐) Secret string // 共享密钥(不直接编码,参与 MD5) Version byte // 协议版本(0x20 = 2.0) } // Connect 包体长度。 const connectBodyLen = 6 + 16 + 1 + 4 // timestamp 鉴权时间戳:MMDDHHMMSS 十进制压缩为 uint32(如 9月5日23:03:01 → 905230301)。 func authTimestamp(t time.Time) uint32 { return uint32(t.Month())*1e8 + uint32(t.Day())*1e6 + uint32(t.Hour())*1e4 + uint32(t.Minute())*100 + uint32(t.Second()) } // Authenticator 计算客户端鉴权串。 // 真实网关验证(Day 31,authprobe v8 命中):MD5(SourceAddr + 9×0x00 + Secret + ts十位ASCII)。 // 注:包体 Timestamp 字段为 4B 大端数值,但鉴权串中时间戳以 10 位十进制 ASCII // ("0905230301")参与拼接——同一规范的两种实现,以网关实际行为为准。 func (c *Connect) Authenticator(ts uint32) []byte { buf := make([]byte, 0, len(c.SourceAddr)+9+len(c.Secret)+10) buf = append(buf, c.SourceAddr...) buf = append(buf, make([]byte, 9)...) // 9 个零字节 buf = append(buf, c.Secret...) buf = fmt.Appendf(buf, "%010d", ts) sum := md5.Sum(buf) return sum[:] } // Marshal 编码 Connect 包体(27B;ts 为十进制压缩时间戳)。 func (c *Connect) Marshal(ts uint32) []byte { b := make([]byte, connectBodyLen) copy(b[0:6], c.SourceAddr) // 左对齐右补零 copy(b[6:22], c.Authenticator(ts)) b[22] = c.Version binary.BigEndian.PutUint32(b[23:27], ts) return b } // ParseConnect 解析并校验 Connect 包体(返回企业码、鉴权串与时间戳)。 func ParseConnect(body []byte) (sourceAddr string, authenticator []byte, ts uint32, err error) { if len(body) < connectBodyLen { return "", nil, 0, fmt.Errorf("cmpp: connect 包体 %d 字节不足 %d", len(body), connectBodyLen) } return trimRightZero(body[0:6]), body[6:22], binary.BigEndian.Uint32(body[23:27]), nil } // trimRightZero 去除右补零。 func trimRightZero(b []byte) string { for i := len(b) - 1; i >= 0; i-- { if b[i] != 0 { return string(b[:i+1]) } } return "" } // VerifyAuth 校验客户端鉴权串(模拟网关/服务端用)。 func VerifyAuth(sourceAddr, secret string, ts uint32, authenticator []byte) bool { c := &Connect{SourceAddr: sourceAddr, Secret: secret} want := c.Authenticator(ts) if len(authenticator) != len(want) { return false } for i := range want { if want[i] != authenticator[i] { return false } } return true } // ConnectResp 包体(CMPP 2.0 规范:Status 4B 大端 + authenticator_ISMG 16B + version 1B = 21B)。 type ConnectResp struct { Status byte Secret string // 计算服务端鉴权串(shared_secret;server 侧应填账号密钥) AuthenticatorClient []byte // 客户端鉴权串(参与计算) // ClientVersion 客户端 Connect.Version 声明(Issue#3 加固):≥0x30 按 3.0 // 布局应答(1B Status,18B 包体,ISMG 串 Status 以 1B 参与)——防严格 3.0 // 客户端按 1B 校验 ISMG 串错位报 -1;0/0x20 按 2.0(4B Status,21B 包体)。 ClientVersion byte } // AuthenticatorISMG 计算服务端鉴权串:MD5(status + authenticatorSource + shared)。 // Issue#3 修正:CMPP 2.0 规范中 Status 以 4 字节大端参与拼接(3.0 为 1 字节)。 func (r *ConnectResp) AuthenticatorISMG() []byte { buf := make([]byte, 0, 4+len(r.AuthenticatorClient)+len(r.Secret)) if r.ClientVersion >= 0x30 { // 3.0:Status 1B 参与 buf = append(buf, r.Status) } else { // 2.0:Status 4B 大端参与 var st [4]byte binary.BigEndian.PutUint32(st[:], uint32(r.Status)) buf = append(buf, st[:]...) } buf = append(buf, r.AuthenticatorClient...) buf = append(buf, r.Secret...) sum := md5.Sum(buf) return sum[:] } // Marshal 编码 ConnectResp 包体。 // Issue#3 根因修复:CMPP 2.0 规范 CMPP_CONNECT_RESP 的 Status 为 4 字节整数 // (与 3.0 的 1 字节不同——而 2.0 的 SUBMIT_RESP Result 恰为 1 字节,两版本在该 // 两字段上互为镜像,极易混淆)。此前按 1B 编码(18B 包体),标准 2.0 客户端按 // 4B 大端解析 Status 时读到 [status,MD5[0..2]] 恒非 0,客户连接被误判失败 // (客户端工具报 -1);自家客户端同按 1B 解析,自测格式对称掩盖了缺陷。 // Issue#3 加固:按客户端 Connect.Version 协商——0x30 回 3.0 布局(18B)。 func (r *ConnectResp) Marshal() []byte { if r.ClientVersion >= 0x30 { b := make([]byte, 1+16+1) b[0] = r.Status copy(b[1:17], r.AuthenticatorISMG()) b[17] = 0x30 return b } b := make([]byte, 4+16+1) binary.BigEndian.PutUint32(b[0:4], uint32(r.Status)) copy(b[4:20], r.AuthenticatorISMG()) b[20] = 0x20 return b } // ParseConnectResp 解析 ConnectResp(客户端校验服务端身份用)。 // 自适应两种规范布局(Issue#3): // - CMPP 2.0(≥20B):Status 4B 大端 + AuthenticatorISMG 16B(+Version 1B=21B) // - CMPP 3.0/兼容(17~19B):Status 1B + AuthenticatorISMG 16B(+Version 1B) // - 其余短包:取首字节(历史行为;主流网关均回完整包)。 func ParseConnectResp(body []byte) (status byte, authenticatorISMG []byte, err error) { if len(body) < 1 { return 0, nil, errors.New("cmpp: connect_resp 包体为空") } if len(body) >= 20 { // CMPP 2.0 布局(4B Status) status = byte(binary.BigEndian.Uint32(body[0:4])) authenticatorISMG = body[4:20] return status, authenticatorISMG, nil } if len(body) >= 17 { // 3.0/兼容布局(1B Status) return body[0], body[1:17], nil } return body[0], nil, nil } // ActiveTest 心跳包体(空)。 func ActiveTest() []byte { return nil } // ActiveTestResp 心跳应答包体(status 1B:0 成功)。 func ActiveTestResp(status byte) []byte { return []byte{status} }