diff --git a/src/data/packetIds.json b/src/data/packetIds.json index 5529bed..4ae5e47 100644 --- a/src/data/packetIds.json +++ b/src/data/packetIds.json @@ -1,6 +1,6 @@ { "101": "DebugNotify", "5": "PlayerGetTokenCsReq", - "49": "PlayerGetTokenScRsp", + "48": "PlayerGetTokenScRsp", "22": "PlayerKeepAliveNotify" } \ No newline at end of file diff --git a/src/server/kcp/Packet.ts b/src/server/kcp/Packet.ts index 05075ec..503fe37 100644 --- a/src/server/kcp/Packet.ts +++ b/src/server/kcp/Packet.ts @@ -19,13 +19,13 @@ export default class Packet { public readonly data: Buffer; public body: {} = {}; - public constructor(public readonly rawData: Buffer, public readonly protoName = "") { + public constructor(public readonly rawData: Buffer, public readonly protoName: string = "") { // Remove the header and metadata const metadataLength = rawData.readUInt16BE(6); this.data = rawData.subarray(12 + metadataLength, 12 + metadataLength + rawData.readUInt32BE(8)); this.cmdid = this.rawData.readUInt16BE(4); - this.protoName = packetIds[this.cmdid.toString()]; + this.protoName = this.protoName || packetIds[this.cmdid.toString()]; if (this.protoName) { try { const root = protobuf.loadSync(resolve(__dirname, `../../data/proto/${this.protoName}.proto`)); @@ -49,7 +49,7 @@ export default class Packet { return str.startsWith("01234567") && str.endsWith("89abcdef"); } - public static encode(name: string, body: {}): Packet | null { + public static encode(name: string, body: {}, customCmdId?: number): Packet | null { try { const cmdid = switchedPacketIds[name]; const root = protobuf.loadSync(resolve(__dirname, `../../data/proto/${name}.proto`)); @@ -58,13 +58,13 @@ export default class Packet { const data = Buffer.from(Message.encode(body).finish()); const packet = Buffer.allocUnsafe(16 + data.length); packet.writeUInt32BE(0x1234567); - packet.writeUint16BE(cmdid, 4); + packet.writeUint16BE(customCmdId || cmdid, 4); packet.writeUint16BE(0, 6); packet.writeUint32BE(data.length, 8); data.copy(packet, 12); packet.writeUint32BE(0x89abcdef, 12 + data.length); - return new Packet(packet); + return new Packet(packet, name); } catch (e) { c.error(e as Error); return null; diff --git a/src/server/kcp/SRServer.ts b/src/server/kcp/SRServer.ts index 4e22590..f461b31 100644 --- a/src/server/kcp/SRServer.ts +++ b/src/server/kcp/SRServer.ts @@ -33,10 +33,10 @@ export default class SRServer { private async onMessage(data: Buffer, rinfo: RemoteInfo) { const client = `${rinfo.address}:${rinfo.port}`; + c.debug(data.toString("hex")); if (data.byteLength == 20) { // Hamdshanke const handshake = new Handshake(data); - c.debug(data.toString("hex")); switch (handshake.handshakeType) { case HandshakeType.CONNECT: diff --git a/src/server/kcp/Session.ts b/src/server/kcp/Session.ts index 3069a53..da90c1a 100644 --- a/src/server/kcp/Session.ts +++ b/src/server/kcp/Session.ts @@ -78,4 +78,8 @@ export default class Session { this.c.debug(`send ${packet.rawData.toString('hex')}`); this.kcpobj.send(packet.rawData); } + + public sendRaw(data: Buffer) { + this.kcpobj.send(data); + } } \ No newline at end of file diff --git a/src/server/packets/PlayerGetTokenCsReq.ts b/src/server/packets/PlayerGetTokenCsReq.ts index 2cea4b9..0d40983 100644 --- a/src/server/packets/PlayerGetTokenCsReq.ts +++ b/src/server/packets/PlayerGetTokenCsReq.ts @@ -17,14 +17,14 @@ export default async function handle(session: Session, packet: Packet) { c.error(`Account not found: ${body.accountUid}`); return; } - + const isTokenValid = account.token === body.accountToken; if (!isTokenValid) { c.error(`Token invalid (${session.ctx.address}:${session.ctx.port})`); return; } - session.send('PlayerGetTokenScRsp', { + session.send("PlayerGetTokenScRsp", { uid: account.uid, token: body.accountToken, secretKey: BigInt(0).toString(),