some fixes

This commit is contained in:
OfficialDakari 2024-04-30 11:28:45 +05:00
parent 056f7eef54
commit 2036c3be62
1 changed files with 52 additions and 72 deletions

View File

@ -32,7 +32,7 @@ tun.on('data', (buff) => {
const user = Object.values(config.users).find(x => x.addr == p.destinationIp); const user = Object.values(config.users).find(x => x.addr == p.destinationIp);
//(user); //(user);
if (!user) return; 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]; const targetPort = ports[user.username];
//(targetIp, targetPort); //(targetIp, targetPort);
if (!targetIp || !targetPort) return; if (!targetIp || !targetPort) return;
@ -45,86 +45,66 @@ sock.on('listening', () => {
}); });
sock.on('message', (msg, info) => { sock.on('message', (msg, info) => {
if (!conns[info.address]) { var dec;
for (const uname in config.users) { var uname;
const u = config.users[uname]; var acc;
const dec = decrypt(msg, u.key, config.iv, config.algorithm); for (const un in config.users) {
if (dec.length == 2 && dec[0] == 0x13 && dec[1] == 0x37) { const u = config.users[un];
conns[info.address] = uname; dec = decrypt(msg, u.key, config.iv, config.algorithm);
ips[uname] = info.address; if (dec.length != 0) {
const spl = u.addr.split('.').map(s => parseInt(s)); uname = un;
const buff = Buffer.from([ acc = u;
0x13, break;
0xb4,
0x37,
...spl
]);
const enc = encrypt(buff, u.key, config.iv, config.algorithm);
sock.send(enc, info.port, info.address);
return;
}
} }
}
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([ const buff = Buffer.from([
0xe7, 0xe7,
0x5a, 0x5a,
0x3d, 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); sock.send(enc, info.port, info.address);
delete conns[info.address];
return; 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); sock.bind(config.port);