|
| 1 | +import { randomBytes, randomUUID } from 'node:crypto'; |
| 2 | +import { Field, Root, Type } from 'protobufjs'; |
| 3 | + |
| 4 | +export interface ConnHead { |
| 5 | + cmdType: number; |
| 6 | + cmd: string; |
| 7 | + seqNo: number; |
| 8 | + msgId: string; |
| 9 | + module: string; |
| 10 | + needAck?: boolean; |
| 11 | + status?: number; |
| 12 | +} |
| 13 | + |
| 14 | +export interface ConnMessage { |
| 15 | + head: ConnHead; |
| 16 | + data: Buffer; |
| 17 | +} |
| 18 | + |
| 19 | +export interface YuanbaoMessageElement { |
| 20 | + msgType: string; |
| 21 | + msgContent: { |
| 22 | + text?: string; |
| 23 | + uuid?: string; |
| 24 | + imageFormat?: number; |
| 25 | + data?: string; |
| 26 | + desc?: string; |
| 27 | + imageInfoArray?: Array<{ type: number; size: number; width: number; height: number; url: string }>; |
| 28 | + url?: string; |
| 29 | + fileSize?: number; |
| 30 | + fileName?: string; |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +const root = new Root(); |
| 35 | + |
| 36 | +function type(name: string, fields: Array<[string, number, string, 'repeated'?]>): Type { |
| 37 | + const value = new Type(name); |
| 38 | + for (const [fieldName, id, fieldType, rule] of fields) { |
| 39 | + value.add(new Field(fieldName, id, fieldType, rule)); |
| 40 | + } |
| 41 | + return value; |
| 42 | +} |
| 43 | + |
| 44 | +const conn = root.define('trpc.yuanbao.conn_common'); |
| 45 | +conn.add(type('Head', [ |
| 46 | + ['cmdType', 1, 'uint32'], ['cmd', 2, 'string'], ['seqNo', 3, 'uint32'], |
| 47 | + ['msgId', 4, 'string'], ['module', 5, 'string'], ['needAck', 6, 'bool'], |
| 48 | + ['status', 10, 'int32'], |
| 49 | +])); |
| 50 | +conn.add(type('ConnMsg', [['head', 1, 'Head'], ['data', 2, 'bytes']])); |
| 51 | +conn.add(type('AuthInfo', [['uid', 1, 'string'], ['source', 2, 'string'], ['token', 3, 'string']])); |
| 52 | +conn.add(type('DeviceInfo', [['instanceId', 10, 'string']])); |
| 53 | +conn.add(type('AuthBindReq', [ |
| 54 | + ['bizId', 1, 'string'], ['authInfo', 2, 'AuthInfo'], ['deviceInfo', 3, 'DeviceInfo'], |
| 55 | + ['envName', 5, 'string'], ['bindMode', 6, 'uint32'], ['forceToken', 7, 'string'], |
| 56 | +])); |
| 57 | +conn.add(type('AuthBindRsp', [ |
| 58 | + ['code', 1, 'int32'], ['message', 2, 'string'], ['connectId', 3, 'string'], |
| 59 | + ['timestamp', 4, 'uint64'], ['clientIp', 5, 'string'], |
| 60 | +])); |
| 61 | +conn.add(type('PingReq', [])); |
| 62 | +conn.add(type('PingRsp', [['heartInterval', 1, 'uint32'], ['timestamp', 2, 'uint64']])); |
| 63 | +conn.add(type('KickoutMsg', [['status', 1, 'int32'], ['reason', 2, 'string'], ['otherDeviceName', 3, 'string']])); |
| 64 | + |
| 65 | +const biz = root.define('trpc.yuanbao.yuanbao_conn.yuanbao_openclaw_proxy'); |
| 66 | +biz.add(type('ImImageInfoArray', [ |
| 67 | + ['type', 1, 'uint32'], ['size', 2, 'uint32'], ['width', 3, 'uint32'], |
| 68 | + ['height', 4, 'uint32'], ['url', 5, 'string'], |
| 69 | +])); |
| 70 | +biz.add(type('MsgContent', [ |
| 71 | + ['text', 1, 'string'], ['uuid', 2, 'string'], ['imageFormat', 3, 'uint32'], |
| 72 | + ['data', 4, 'string'], ['desc', 5, 'string'], ['imageInfoArray', 8, 'ImImageInfoArray', 'repeated'], |
| 73 | + ['url', 10, 'string'], ['fileSize', 11, 'uint32'], ['fileName', 12, 'string'], |
| 74 | +])); |
| 75 | +biz.add(type('MsgBodyElement', [['msgType', 1, 'string'], ['msgContent', 2, 'MsgContent']])); |
| 76 | +biz.add(type('SendC2CMessageReq', [ |
| 77 | + ['msgId', 1, 'string'], ['toAccount', 2, 'string'], ['fromAccount', 3, 'string'], |
| 78 | + ['msgRandom', 4, 'uint32'], ['msgBody', 5, 'MsgBodyElement', 'repeated'], |
| 79 | + ['groupCode', 6, 'string'], ['msgSeq', 7, 'uint64'], |
| 80 | +])); |
| 81 | +biz.add(type('SendGroupMessageReq', [ |
| 82 | + ['msgId', 1, 'string'], ['groupCode', 2, 'string'], ['fromAccount', 3, 'string'], |
| 83 | + ['toAccount', 4, 'string'], ['random', 5, 'string'], |
| 84 | + ['msgBody', 6, 'MsgBodyElement', 'repeated'], ['refMsgId', 7, 'string'], ['msgSeq', 8, 'uint64'], |
| 85 | +])); |
| 86 | +biz.add(type('SendPrivateHeartbeatReq', [ |
| 87 | + ['fromAccount', 1, 'string'], ['toAccount', 2, 'string'], ['heartbeat', 3, 'int32'], |
| 88 | +])); |
| 89 | +biz.add(type('SendGroupHeartbeatReq', [ |
| 90 | + ['fromAccount', 1, 'string'], ['toAccount', 2, 'string'], ['groupCode', 3, 'string'], |
| 91 | + ['sendTime', 4, 'int64'], ['heartbeat', 5, 'int32'], |
| 92 | +])); |
| 93 | +root.resolveAll(); |
| 94 | + |
| 95 | +const CONN_MSG = 'trpc.yuanbao.conn_common.ConnMsg'; |
| 96 | +const AUTH_BIND_REQ = 'trpc.yuanbao.conn_common.AuthBindReq'; |
| 97 | +const AUTH_BIND_RSP = 'trpc.yuanbao.conn_common.AuthBindRsp'; |
| 98 | +const PING_REQ = 'trpc.yuanbao.conn_common.PingReq'; |
| 99 | +const PING_RSP = 'trpc.yuanbao.conn_common.PingRsp'; |
| 100 | +const KICKOUT_MSG = 'trpc.yuanbao.conn_common.KickoutMsg'; |
| 101 | +const BIZ = 'trpc.yuanbao.yuanbao_conn.yuanbao_openclaw_proxy'; |
| 102 | + |
| 103 | +export const CMD_TYPE_REQUEST = 0; |
| 104 | +export const CMD_TYPE_RESPONSE = 1; |
| 105 | +export const CMD_TYPE_PUSH = 2; |
| 106 | +export const CMD_TYPE_PUSH_ACK = 3; |
| 107 | +export const CMD_AUTH_BIND = 'auth-bind'; |
| 108 | +export const CMD_PING = 'ping'; |
| 109 | +export const CMD_KICKOUT = 'kickout'; |
| 110 | + |
| 111 | +let seq = 0; |
| 112 | + |
| 113 | +function nextSeq(): number { |
| 114 | + seq = (seq + 1) % 0x80000000; |
| 115 | + return seq; |
| 116 | +} |
| 117 | + |
| 118 | +function messageId(): string { |
| 119 | + return randomUUID().replace(/-/g, ''); |
| 120 | +} |
| 121 | + |
| 122 | +function randomUint32(): number { |
| 123 | + return randomBytes(4).readUInt32BE(0); |
| 124 | +} |
| 125 | + |
| 126 | +function encode(typeName: string, value: Record<string, unknown>): Buffer { |
| 127 | + const messageType = root.lookupType(typeName); |
| 128 | + const error = messageType.verify(value); |
| 129 | + if (error) throw new Error(`${typeName}: ${error}`); |
| 130 | + return Buffer.from(messageType.encode(messageType.create(value)).finish()); |
| 131 | +} |
| 132 | + |
| 133 | +function decode(typeName: string, data: Uint8Array): any { |
| 134 | + const messageType = root.lookupType(typeName); |
| 135 | + return messageType.toObject(messageType.decode(data), { |
| 136 | + longs: String, |
| 137 | + bytes: Buffer, |
| 138 | + defaults: false, |
| 139 | + }); |
| 140 | +} |
| 141 | + |
| 142 | +function encodeConn(head: ConnHead, data?: Buffer): Buffer { |
| 143 | + return encode(CONN_MSG, data?.length ? { head, data } : { head }); |
| 144 | +} |
| 145 | + |
| 146 | +export function decodeConn(raw: Uint8Array): ConnMessage { |
| 147 | + const decoded = decode(CONN_MSG, raw); |
| 148 | + return { |
| 149 | + head: decoded.head as ConnHead, |
| 150 | + data: decoded.data ? Buffer.from(decoded.data) : Buffer.alloc(0), |
| 151 | + }; |
| 152 | +} |
| 153 | + |
| 154 | +export function buildAuthBindMessage(uid: string, source: string, token: string): Buffer { |
| 155 | + const data = encode(AUTH_BIND_REQ, { |
| 156 | + bizId: 'ybBot', |
| 157 | + authInfo: { uid, source, token }, |
| 158 | + deviceInfo: { instanceId: '16' }, |
| 159 | + }); |
| 160 | + return encodeConn({ |
| 161 | + cmdType: CMD_TYPE_REQUEST, cmd: CMD_AUTH_BIND, seqNo: nextSeq(), |
| 162 | + msgId: messageId(), module: 'conn_access', |
| 163 | + }, data); |
| 164 | +} |
| 165 | + |
| 166 | +export function buildPingMessage(): Buffer { |
| 167 | + return encodeConn({ |
| 168 | + cmdType: CMD_TYPE_REQUEST, cmd: CMD_PING, seqNo: nextSeq(), |
| 169 | + msgId: messageId(), module: 'conn_access', |
| 170 | + }, encode(PING_REQ, {})); |
| 171 | +} |
| 172 | + |
| 173 | +export function buildPushAck(head: ConnHead): Buffer { |
| 174 | + return encodeConn({ |
| 175 | + cmdType: CMD_TYPE_PUSH_ACK, |
| 176 | + cmd: head.cmd, |
| 177 | + seqNo: nextSeq(), |
| 178 | + msgId: head.msgId, |
| 179 | + module: head.module, |
| 180 | + }); |
| 181 | +} |
| 182 | + |
| 183 | +function buildBusinessMessage(cmd: string, data: Buffer): Buffer { |
| 184 | + return encodeConn({ |
| 185 | + cmdType: CMD_TYPE_REQUEST, cmd, seqNo: nextSeq(), |
| 186 | + msgId: messageId(), module: 'yuanbao_openclaw_proxy', |
| 187 | + }, data); |
| 188 | +} |
| 189 | + |
| 190 | +export function buildSendMessage( |
| 191 | + chatType: 'c2c' | 'group', |
| 192 | + targetId: string, |
| 193 | + fromAccount: string, |
| 194 | + elements: YuanbaoMessageElement[], |
| 195 | +): Buffer { |
| 196 | + if (chatType === 'group') { |
| 197 | + return buildBusinessMessage('send_group_message', encode(`${BIZ}.SendGroupMessageReq`, { |
| 198 | + groupCode: targetId, |
| 199 | + fromAccount, |
| 200 | + random: String(randomUint32()), |
| 201 | + msgBody: elements, |
| 202 | + })); |
| 203 | + } |
| 204 | + return buildBusinessMessage('send_c2c_message', encode(`${BIZ}.SendC2CMessageReq`, { |
| 205 | + toAccount: targetId, |
| 206 | + fromAccount, |
| 207 | + msgRandom: randomUint32(), |
| 208 | + msgBody: elements, |
| 209 | + })); |
| 210 | +} |
| 211 | + |
| 212 | +export function buildTypingMessage( |
| 213 | + chatType: 'c2c' | 'group', |
| 214 | + targetId: string, |
| 215 | + fromAccount: string, |
| 216 | + senderId: string, |
| 217 | + heartbeat: 1 | 2, |
| 218 | +): Buffer { |
| 219 | + if (chatType === 'group') { |
| 220 | + return buildBusinessMessage('send_group_heartbeat', encode(`${BIZ}.SendGroupHeartbeatReq`, { |
| 221 | + fromAccount, toAccount: senderId, groupCode: targetId, sendTime: 0, heartbeat, |
| 222 | + })); |
| 223 | + } |
| 224 | + return buildBusinessMessage('send_private_heartbeat', encode(`${BIZ}.SendPrivateHeartbeatReq`, { |
| 225 | + fromAccount, toAccount: targetId, heartbeat, |
| 226 | + })); |
| 227 | +} |
| 228 | + |
| 229 | +export const decodeAuthBindResponse = (data: Uint8Array): any => decode(AUTH_BIND_RSP, data); |
| 230 | +export const decodePingResponse = (data: Uint8Array): any => decode(PING_RSP, data); |
| 231 | +export const decodeKickoutMessage = (data: Uint8Array): any => decode(KICKOUT_MSG, data); |
0 commit comments