Skip to content

Instantly share code, notes, and snippets.

@Warr1024
Created Oct 17, 2019
Embed
What would you like to do?
'use strict';
const process = require('process');
const dgram = require('dgram');
let [listenport, fwdport, fwdaddr, mindelay, maxdelay] = process.argv.slice(2);
listenport = listenport || 30001;
fwdport = fwdport || 30000;
fwdaddr = fwdaddr || '127.0.0.1';
mindelay = mindelay || 400;
maxdelay = maxdelay || (mindelay * 1.25);
function delaysend(sock, msg, toport, toaddr, fromport, fromaddr) {
const delay = Math.floor(Math.random() * (maxdelay - mindelay) + mindelay);
console.log(`${fromaddr}:${fromport} -> len ${msg.length} delay ${delay} -> ${toaddr}:${toport}`);
setTimeout(() => sock.send(msg, toport, toaddr), delay);
}
const server = dgram.createSocket('udp4');
server.on('error', e => { throw e; });
server.on('listening', () => `listening on ${listenport}`);
server.bind(listenport);
const fwds = {};
server.on('message', (msg, info) => {
const { address, port } = info;
const key = `${address}:${port}`;
let sock = fwds[key];
if(!sock) {
fwds[key] = sock = dgram.createSocket('udp4');
sock.on('error', e => { throw e; });
server.on('listening', () => console.log(`new connection from ${address}:${port
} relaying via port ${sock.address().port}`));
sock.on('message', m => delaysend(server, m, port, address, fwdport, fwdaddr));
}
delaysend(sock, msg, fwdport, fwdaddr, port, address);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment