fix recursion
This commit is contained in:
parent
c9c30952ff
commit
e8e9bfd6ec
1 changed files with 18 additions and 17 deletions
35
p2p.js
35
p2p.js
|
@ -1,6 +1,8 @@
|
|||
// by zestylemonade
|
||||
const net = require("net");
|
||||
const genId = () => Math.floor(Math.random() * (2 << 16));
|
||||
const ipWrite = (addr, buf) => addr.split(".").forEach((i, x) => buf[x] = parseInt(i, 10));
|
||||
const ipRead = (buf) => buf.reduce((a, i) => `${a}.${i.toString(10)}`, "").slice(1);
|
||||
|
||||
class Messenger extends net.Server {
|
||||
constructor() {
|
||||
|
@ -10,7 +12,7 @@ class Messenger extends net.Server {
|
|||
this.on("connection", conn => {
|
||||
conn.on("data", data => this.handle(data, conn));
|
||||
conn.on("disconnect", () => this.conns.delete(conn));
|
||||
conn.write(this.find());
|
||||
conn.write(Buffer.from([0x00]));
|
||||
this.conns.add(conn);
|
||||
});
|
||||
}
|
||||
|
@ -20,17 +22,14 @@ class Messenger extends net.Server {
|
|||
const addrs = [...this.conns.values()]
|
||||
.filter(i => i.address().family === "IPv4")
|
||||
.slice(-3)
|
||||
.map(i => {
|
||||
const addr = i.address();
|
||||
const buf = Buffer.alloc(6);
|
||||
addr.address.split(".").forEach((i, x) => (buf[x] = i));
|
||||
buf.writeUInt16BE(addr.port, 4);
|
||||
return buf;
|
||||
});
|
||||
.map(i => i.address());
|
||||
const buf = Buffer.alloc(addrs.length * 6 + 2);
|
||||
buf[0] = 0x01;
|
||||
buf[0] = 0x02;
|
||||
buf[1] = addrs.length;
|
||||
addrs.forEach((i, x) => i.copy(buf, 2 + 6 * x));
|
||||
addrs.forEach((i, x) => {
|
||||
ipWrite(i.address, buf.slice(x * 6, x * 6 + 4));
|
||||
buf.writeUInt16BE(i.port, x * 6 + 4);
|
||||
});
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
@ -38,15 +37,15 @@ class Messenger extends net.Server {
|
|||
handle(data, conn) {
|
||||
switch(data[0]) {
|
||||
case 0x00: return this.emit("ping", conn); // pings
|
||||
case 0x01: { // redundant conns
|
||||
const int = (off, i) => data[2 + off + i * 6].toString();
|
||||
case 0x01: conn.write(this.find()); // redundant ips request
|
||||
case 0x02: { // redundant conns
|
||||
for(let i = 0; i < data[1]; i++) {
|
||||
const ip = `${int(0, i)}.${int(1, i)}.${int(2, i)}.${int(3, i)}`;
|
||||
this.connect(`${ip}:${data.readUInt16BE(6 + i * 6)}`)
|
||||
const ip = ipRead(data.slice(i * 6 + 2, i * 6 + 4));
|
||||
this.connect(`${ip}:${data.readUInt16BE(i * 6) + 4}`, false);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0x02: { // messages
|
||||
case 0x03: { // messages
|
||||
const id = data.readUInt32BE(1);
|
||||
const channel = data.readUInt32BE(5);
|
||||
if(this.recieved.has(id)) return;
|
||||
|
@ -60,10 +59,12 @@ class Messenger extends net.Server {
|
|||
}
|
||||
|
||||
// create a new connection
|
||||
connect(where) {
|
||||
connect(where, requestRedundant = true) {
|
||||
const conn = net.connect(where);
|
||||
conn.on("data", data => this.handle(data));
|
||||
conn.on("disconnect", () => this.conns.delete(conn));
|
||||
conn.write(Buffer.from([0x00]));
|
||||
if(requestRedundant) conn.write(Buffer.from([0x01]));
|
||||
this.conns.add(conn);
|
||||
}
|
||||
|
||||
|
@ -77,7 +78,7 @@ class Messenger extends net.Server {
|
|||
msg = Buffer.from(msg);
|
||||
const id = genId();
|
||||
const buf = Buffer.alloc(msg.length + 9);
|
||||
buf[0] = 0x02;
|
||||
buf[0] = 0x03;
|
||||
buf.writeUInt32BE(id, 1);
|
||||
buf.writeUInt32BE(channel, 5);
|
||||
msg.copy(buf, 9);
|
||||
|
|
Loading…
Reference in a new issue