From 5af80ad18f3190978b2ebaf011bf355058f5aca2 Mon Sep 17 00:00:00 2001 From: memetrollsXD Date: Fri, 29 Jul 2022 00:54:24 +0200 Subject: [PATCH 1/5] Update README.md I really need to work on a better README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d1d6645..9421490 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # CrepeSR -SR + +[Discord](https://discord.gg/sCAC282C) From c422bf2fe99129e5fe972bf1dd3daed4e52b0fce Mon Sep 17 00:00:00 2001 From: TheLostTree <65834918+TheLostTree@users.noreply.github.com> Date: Thu, 28 Jul 2022 18:40:59 -0700 Subject: [PATCH 2/5] better typed packetIds --- src/data/packetIds.json | 8 ++++---- src/server/kcp/Packet.ts | 15 ++++++++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/data/packetIds.json b/src/data/packetIds.json index 4ae5e47..1bca59c 100644 --- a/src/data/packetIds.json +++ b/src/data/packetIds.json @@ -1,6 +1,6 @@ { - "101": "DebugNotify", - "5": "PlayerGetTokenCsReq", - "48": "PlayerGetTokenScRsp", - "22": "PlayerKeepAliveNotify" + "DebugNotify": "101", + "PlayerGetTokenCsReq": "5", + "PlayerGetTokenScRsp": "48", + "PlayerKeepAliveNotify": "22" } \ No newline at end of file diff --git a/src/server/kcp/Packet.ts b/src/server/kcp/Packet.ts index 503fe37..8534d36 100644 --- a/src/server/kcp/Packet.ts +++ b/src/server/kcp/Packet.ts @@ -2,16 +2,21 @@ import Logger, { VerboseLevel } from "../../util/Logger"; import protobuf from 'protobufjs'; import { resolve } from 'path'; import _packetIds from '../../data/packetIds.json'; -const packetIds = _packetIds as { [key: string]: string }; -const switchedPacketIds: { [key: string]: number } = (function () { - const obj: { [key: string]: number } = {}; - Object.keys(packetIds).forEach((key) => { - obj[packetIds[key]] = Number(key); + +export type PacketTypes = keyof typeof _packetIds; +const switchedPacketIds = _packetIds as { [key in PacketTypes]: string }; +const packetIds: { [key: string]: PacketTypes } = (function () { + const obj: { [key: string]: PacketTypes } = {}; + + Object.keys(switchedPacketIds).forEach((key) => { + obj[switchedPacketIds[key as PacketTypes]] = key as PacketTypes; }); return obj; })(); + + const c = new Logger("Packet") export default class Packet { From 400bb016b83d67b5d34dd2b7da2f606e8c7712ac Mon Sep 17 00:00:00 2001 From: TheLostTree <65834918+TheLostTree@users.noreply.github.com> Date: Thu, 28 Jul 2022 18:41:51 -0700 Subject: [PATCH 3/5] slight change to encode func --- src/server/kcp/Packet.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/server/kcp/Packet.ts b/src/server/kcp/Packet.ts index 8534d36..81f0fd1 100644 --- a/src/server/kcp/Packet.ts +++ b/src/server/kcp/Packet.ts @@ -4,13 +4,13 @@ import { resolve } from 'path'; import _packetIds from '../../data/packetIds.json'; -export type PacketTypes = keyof typeof _packetIds; -const switchedPacketIds = _packetIds as { [key in PacketTypes]: string }; -const packetIds: { [key: string]: PacketTypes } = (function () { - const obj: { [key: string]: PacketTypes } = {}; +export type PacketType = keyof typeof _packetIds; +const switchedPacketIds = _packetIds as { [key in PacketType]: string }; +const packetIds: { [key: string]: PacketType } = (function () { + const obj: { [key: string]: PacketType } = {}; Object.keys(switchedPacketIds).forEach((key) => { - obj[switchedPacketIds[key as PacketTypes]] = key as PacketTypes; + obj[switchedPacketIds[key as PacketType]] = key as PacketType; }); return obj; @@ -54,9 +54,9 @@ export default class Packet { return str.startsWith("01234567") && str.endsWith("89abcdef"); } - public static encode(name: string, body: {}, customCmdId?: number): Packet | null { + public static encode(name: PacketType, body: {}, customCmdId?: number): Packet | null { try { - const cmdid = switchedPacketIds[name]; + const cmdid = Number(switchedPacketIds[name]); const root = protobuf.loadSync(resolve(__dirname, `../../data/proto/${name}.proto`)); const Message = root.lookupTypeOrEnum(name); From d0881c9f91032655439e5f59afd9bd23e8dee8e5 Mon Sep 17 00:00:00 2001 From: TheLostTree <65834918+TheLostTree@users.noreply.github.com> Date: Thu, 28 Jul 2022 18:44:34 -0700 Subject: [PATCH 4/5] i missed a spot... --- src/server/kcp/Packet.ts | 3 +++ src/server/kcp/Session.ts | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/server/kcp/Packet.ts b/src/server/kcp/Packet.ts index 81f0fd1..0db8cfd 100644 --- a/src/server/kcp/Packet.ts +++ b/src/server/kcp/Packet.ts @@ -17,6 +17,8 @@ const packetIds: { [key: string]: PacketType } = (function () { })(); + +console.log(packetIds.PlayerGetTokenReq) const c = new Logger("Packet") export default class Packet { @@ -30,6 +32,7 @@ export default class Packet { this.data = rawData.subarray(12 + metadataLength, 12 + metadataLength + rawData.readUInt32BE(8)); this.cmdid = this.rawData.readUInt16BE(4); + this.protoName = this.protoName || packetIds[this.cmdid.toString()]; if (this.protoName) { try { diff --git a/src/server/kcp/Session.ts b/src/server/kcp/Session.ts index da90c1a..55e0686 100644 --- a/src/server/kcp/Session.ts +++ b/src/server/kcp/Session.ts @@ -3,7 +3,7 @@ import { RemoteInfo } from 'dgram'; import { resolve } from 'path'; import fs from 'fs'; import KCP from 'node-kcp-token'; -import Packet from './Packet'; +import Packet, { PacketType } from './Packet'; import Logger, { VerboseLevel } from '../../util/Logger'; import defaultHandler from '../packets/PacketHandler'; @@ -71,7 +71,7 @@ export default class Session { }); } - public send(name: string, body: {}) { + public send(name: PacketType, body: {}) { const packet = Packet.encode(name, body); if (!packet) return; if (Logger.VERBOSE_LEVEL >= VerboseLevel.WARNS) this.c.log(packet.protoName); From 0d4d16f9a7d3486733480060a8e02e392ec8327b Mon Sep 17 00:00:00 2001 From: memetrollsXD Date: Fri, 29 Jul 2022 12:17:32 +0200 Subject: [PATCH 5/5] Remove unnecessary debug logging from @TheLostTree --- src/server/kcp/Packet.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/server/kcp/Packet.ts b/src/server/kcp/Packet.ts index 0db8cfd..c2a8926 100644 --- a/src/server/kcp/Packet.ts +++ b/src/server/kcp/Packet.ts @@ -16,9 +16,6 @@ const packetIds: { [key: string]: PacketType } = (function () { return obj; })(); - - -console.log(packetIds.PlayerGetTokenReq) const c = new Logger("Packet") export default class Packet { @@ -78,4 +75,4 @@ export default class Packet { return null; } } -} \ No newline at end of file +}