From 130c0c7f7e4812bc523dff25878a2fead07abe93 Mon Sep 17 00:00:00 2001 From: DavidVentura Date: Fri, 26 Jan 2024 17:36:06 +0100 Subject: [PATCH] refactor a bit more --- func_replacements.js | 31 ++----------------------------- handlers.ts | 7 +------ impl.ts | 3 ++- server.ts | 4 ++-- 4 files changed, 7 insertions(+), 38 deletions(-) diff --git a/func_replacements.js b/func_replacements.js index 3f5b502..6902d7c 100644 --- a/func_replacements.js +++ b/func_replacements.js @@ -1,33 +1,5 @@ import { swap_endianness_u32, swap_endianness_u16, u16_swap } from "./utils.js"; -export const Commands = { - Close: 0xf1f0, - LanSearchExt: 0xf132, - LanSearch: 0xf130, - P2PAlive: 0xf1e0, - P2PAliveAck: 0xf1e1, - Hello: 0xf100, - P2pRdy: 0xf142, - P2pReq: 0xf120, - LstReq: 0xf167, - DrwAck: 0xf1d1, - Drw: 0xf1d0, - - // From CSession_CtrlPkt_Proc, incomplete - PunchTo: 0xf140, - PunchPkt: 0xf141, - HelloAck: 0xf101, - RlyTo: 0xf102, - DevLgnAck: 0xf111, - P2PReqAck: 0xf121, - ListenReqAck: 0xf169, - RlyHelloAck: 0xf170, // always - RlyHelloAck2: 0xf171, // if len >1?? -}; - -export const CommandsByValue = Object.keys(Commands).reduce((acc, cur) => { - acc[Commands[cur]] = cur; - return acc; -}, {}); +import { Commands, CommandsByValue } from "./datatypes.js"; const writeCommand2 = (command, buf) => { buf.writeByteArray([(command & 0xff00) >> 8, command & 0xff]); @@ -37,6 +9,7 @@ const writeCommand4 = (command, buf) => { }; export const XqBytesDec = (inoutbuf, buflen, rotate) => { + // only rotation is different let new_buf = new Uint8Array(buflen); new_buf.fill(0x1); for (let i = 0; i < buflen; i++) { diff --git a/handlers.ts b/handlers.ts index a628cb8..3821919 100644 --- a/handlers.ts +++ b/handlers.ts @@ -1,5 +1,5 @@ import { sock } from "./server.js"; -import { Commands, CommandsByValue } from "./func_replacements.js"; +import { ControlCommands, Commands, CommandsByValue } from "./datatypes.js"; import { createWriteStream } from "node:fs"; import { SendUsrAck, SendUsrChk, create_P2pRdy } from "./impl.js"; @@ -97,11 +97,6 @@ const deal_with_data = (dv: DataView) => { } }; -const ControlCommands = { - ConnectUser: 0x2010, - ConnectUserAck: 0x2011, -}; - const makeDrwAck = (dv: DataView): DataView => { const pkt_id = dv.add(6).readU16(); const m_stream = dv.add(5).readU8(); // data = 1, control = 0 diff --git a/impl.ts b/impl.ts index 51530bd..ca913de 100644 --- a/impl.ts +++ b/impl.ts @@ -1,6 +1,7 @@ import "./shim.ts"; -import { Commands, XqBytesEnc } from "./func_replacements.js"; +import { XqBytesEnc } from "./func_replacements.js"; +import { Commands } from "./datatypes.js"; import { u16_swap } from "./utils.js"; const str2byte = (s: string): number[] => { diff --git a/server.ts b/server.ts index ab71c84..c1ef28b 100644 --- a/server.ts +++ b/server.ts @@ -1,5 +1,6 @@ import dgram from "node:dgram"; -import { create_LanSearch, Commands, CommandsByValue } from "./func_replacements.js"; +import { create_LanSearch } from "./func_replacements.js"; +import { Commands, CommandsByValue } from "./datatypes.js"; import { handle_P2PAlive, handle_PunchPkt, handle_P2PRdy, handle_Drw, notImpl, noop } from "./handlers.js"; import { hexdump } from "./hexdump.js"; @@ -54,7 +55,6 @@ const MakeSock = (cb: msgCb, connCb: connCb, options?: opt): sock => { }; const Handlers: Record void> = { - // FIXME: keys are 'any' bc import? PunchPkt: handle_PunchPkt, Close: notImpl,