From 2036c3be622dd677f0e50f0aabc280e12c50e34e Mon Sep 17 00:00:00 2001 From: OfficialDakari Date: Tue, 30 Apr 2024 11:28:45 +0500 Subject: [PATCH] some fixes --- server/index.js | 124 ++++++++++++++++++++---------------------------- 1 file changed, 52 insertions(+), 72 deletions(-) diff --git a/server/index.js b/server/index.js index 9bab5ee..6a8a1fa 100644 --- a/server/index.js +++ b/server/index.js @@ -32,7 +32,7 @@ tun.on('data', (buff) => { const user = Object.values(config.users).find(x => x.addr == p.destinationIp); //(user); if (!user) return; - const targetIp = Object.entries(conns).find(x => x[1] == user.username)[0]; + const targetIp = ips[user.username]; const targetPort = ports[user.username]; //(targetIp, targetPort); if (!targetIp || !targetPort) return; @@ -45,86 +45,66 @@ sock.on('listening', () => { }); sock.on('message', (msg, info) => { - if (!conns[info.address]) { - for (const uname in config.users) { - const u = config.users[uname]; - const dec = decrypt(msg, u.key, config.iv, config.algorithm); - if (dec.length == 2 && dec[0] == 0x13 && dec[1] == 0x37) { - conns[info.address] = uname; - ips[uname] = info.address; - const spl = u.addr.split('.').map(s => parseInt(s)); - const buff = Buffer.from([ - 0x13, - 0xb4, - 0x37, - ...spl - ]); - const enc = encrypt(buff, u.key, config.iv, config.algorithm); - sock.send(enc, info.port, info.address); - return; - } + var dec; + var uname; + var acc; + for (const un in config.users) { + const u = config.users[un]; + dec = decrypt(msg, u.key, config.iv, config.algorithm); + if (dec.length != 0) { + uname = un; + acc = u; + break; } + } + ips[uname] = info.address; + ports[uname] = info.port; + //const dec = decrypt(msg, acc.key, config.iv, config.algorithm); + //(dec); + if (dec.length == 0) { + console.error(`Empty or malformed packet from ${uname}`); + return; + } + if (dec.length == 2 && dec[0] == 0x13 && dec[1] == 0x37) { + const spl = acc.addr.split('.').map(s => parseInt(s)); + const buff = Buffer.from([ + 0x13, + 0xb4, + 0x37, + ...spl + ]); + const enc = encrypt(buff, acc.key, config.iv, config.algorithm); + sock.send(enc, info.port, info.address); + return; + } + if (dec.length == 4 && + dec[0] == 0x55 && dec[1] == 0x44 && dec[2] == 0xe9 && dec[3] == 0x37) { const buff = Buffer.from([ 0xe7, 0x5a, 0x3d, - 0xed + 0xea ]); - const enc = encrypt(buff, config.globalKey, config.iv, config.algorithm); + const enc = encrypt(buff, acc.key, config.iv, config.algorithm); sock.send(enc, info.port, info.address); + delete conns[info.address]; return; - } else { - const uname = conns[info.address]; - ports[uname] = info.port; - ips[uname] = info.address; - const acc = config.users[uname]; - const dec = decrypt(msg, acc.key, config.iv, config.algorithm); - //(dec); - if (dec.length == 0) { - console.error(`Empty or malformed packet from ${uname}`); - return; - } - if (dec.length == 2 && dec[0] == 0x13 && dec[1] == 0x37) { - const spl = acc.addr.split('.').map(s => parseInt(s)); - const buff = Buffer.from([ - 0x13, - 0xb4, - 0x37, - ...spl - ]); - const enc = encrypt(buff, acc.key, config.iv, config.algorithm); - sock.send(enc, info.port, info.address); - return; - } - if (dec.length == 4 && - dec[0] == 0x55 && dec[1] == 0x44 && dec[2] == 0xe9 && dec[3] == 0x37) { - const buff = Buffer.from([ - 0xe7, - 0x5a, - 0x3d, - 0xea - ]); - const enc = encrypt(buff, acc.key, config.iv, config.algorithm); - sock.send(enc, info.port, info.address); - delete conns[info.address]; - return; - } - - if ((dec[0] >> 4) !== 4) return; - const packet = IP.decode(dec); - //(packet); - packet.sourceIp = acc.addr; - if ((config.allow_outbound && !l.check(packet.destinationIp)) || packet.destinationIp == config.addr) { - tun.write(dec); - return; - } - const user = Object.values(config.users).find(x => x.addr == packet.destinationIp); - if (!user) return; - const targetIp = conns[user.username]; - const targetPort = ports[user.username]; - if (!targetIp || !targetPort) return; - sock.send(encrypt(dec, user.key, config.iv, config.algorithm), targetPort, targetIp); } + + if ((dec[0] >> 4) !== 4) return; + const packet = IP.decode(dec); + //(packet); + packet.sourceIp = acc.addr; + if ((config.allow_outbound && !l.check(packet.destinationIp)) || packet.destinationIp == config.addr) { + tun.write(dec); + return; + } + const user = Object.values(config.users).find(x => x.addr == packet.destinationIp); + if (!user) return; + const targetIp = ips[user.username]; + const targetPort = ports[user.username]; + if (!targetIp || !targetPort) return; + sock.send(encrypt(dec, user.key, config.iv, config.algorithm), targetPort, targetIp); }); sock.bind(config.port); \ No newline at end of file