133 lines
4.5 KiB
Go
133 lines
4.5 KiB
Go
// 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 包体(status 1B + authenticator_ISMG 16B + version 1B)。
|
||
type ConnectResp struct {
|
||
Status byte
|
||
Secret string // 计算服务端鉴权串
|
||
AuthenticatorClient []byte // 客户端鉴权串(参与计算)
|
||
}
|
||
|
||
// AuthenticatorISMG 计算服务端鉴权串:MD5(status + authenticatorSource + shared)。
|
||
func (r *ConnectResp) AuthenticatorISMG() []byte {
|
||
sum := md5.Sum(append(append([]byte{r.Status}, r.AuthenticatorClient...), []byte(r.Secret)...))
|
||
return sum[:]
|
||
}
|
||
|
||
// Marshal 编码 ConnectResp 包体。
|
||
func (r *ConnectResp) Marshal() []byte {
|
||
b := make([]byte, 1+16+1)
|
||
b[0] = r.Status
|
||
copy(b[1:17], r.AuthenticatorISMG())
|
||
b[17] = 0x20
|
||
return b
|
||
}
|
||
|
||
// ParseConnectResp 解析 ConnectResp(客户端校验服务端身份用)。
|
||
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) >= 17 {
|
||
authenticatorISMG = body[1:17]
|
||
}
|
||
return body[0], authenticatorISMG, nil
|
||
}
|
||
|
||
// ActiveTest 心跳包体(空)。
|
||
func ActiveTest() []byte { return nil }
|
||
|
||
// ActiveTestResp 心跳应答包体(status 1B:0 成功)。
|
||
func ActiveTestResp(status byte) []byte { return []byte{status} }
|