Skip to content

Instantly share code, notes, and snippets.

@donghaoren
Last active June 19, 2024 13:49
Show Gist options
  • Save donghaoren/c7bbddd2ea865b0f25c3ed778db6d7db to your computer and use it in GitHub Desktop.
Save donghaoren/c7bbddd2ea865b0f25c3ed778db6d7db to your computer and use it in GitHub Desktop.
UDP Forwarder
/*!
Copyright (c) Donghao Ren
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
let dgram = require('dgram');
function getTimestamp() {
return new Date().getTime();
}
class ClientConnection {
constructor(server, remote_address, remote_port, forward_address, forward_port) {
this.socket = dgram.createSocket("udp4");
this.timeCreated = getTimestamp();
this.timeLastActivity = this.timeCreated;
this.totalIn = 0;
this.totalOut = 0;
this.forward_address = forward_address;
this.forward_port = forward_port;
this.remote_address = remote_address;
this.remote_port = remote_port;
this.socket.on("message", (message) => {
server.send(message, 0, message.length, remote_port, remote_address);
this.timeLastActivity = getTimestamp();
this.totalIn += message.length;
});
}
send(message) {
this.socket.send(message, 0, message.length, this.forward_port, this.forward_address);
this.timeLastActivity = getTimestamp();
this.totalOut += message.length;
}
digest() {
return [
`Remote: ${this.remote_address}:${this.remote_port}`,
` Created: ${this.timeCreated}, Last activity: ${this.timeLastActivity}`,
` Total In: ${this.totalIn} bytes`,
` Total Out: ${this.totalOut} bytes`
].join("\n");
}
shouldDelete() {
return getTimestamp() - this.timeLastActivity > 300 * 1000;
}
close() {
this.socket.close();
}
}
class UDPForwarder {
constructor(remoteAddress, remotePort, localBind, localPort) {
this.remoteAddress = remoteAddress;
this.remotePort = remotePort;
this.localBind = localBind;
this.localPort = localPort;
this.server = dgram.createSocket('udp4');
this.remoteMap = new Map();
this.timer = null;
this.server.on('listening', () => {
let address = this.server.address();
this.log('Listening at ' + address.address + ":" + address.port);
});
this.server.on("message", (message, remote) => {
let remoteID = remote.address + ":" + remote.port;
if (this.remoteMap.has(remoteID)) {
let r = this.remoteMap.get(remoteID);
r.send(message);
} else {
let r = new ClientConnection(this.server, remote.address, remote.port, remoteAddress, remotePort);
this.remoteMap.set(remoteID, r);
this.log('New client at ' + remote.address + ":" + remote.port);
r.send(message);
}
});
}
start() {
this.server.bind(this.localPort, this.localBind);
this.timer = setInterval(() => {
let keysToDelete = [];
this.remoteMap.forEach((item, key) => {
this.log(item.digest());
if (item.shouldDelete()) {
item.close()
keysToDelete.push(key);
}
});
for (let k of keysToDelete) {
this.remoteMap.delete(k);
}
}, 5000);
}
log(str) {
console.log("UDPForwarder: " + str);
}
}
let config = {
remoteAddress: "localhost",
remotePort: 1000,
bindAddress: "0.0.0.0",
bindPort: 10000
}
for(let argv of process.argv.slice(2)) {
name = argv.split("=")[0].toLowerCase();
value = argv.split("=")[1];
if(name == "remote-address") {
config.remoteAddress = value;
} else if(name == "bind-address") {
config.bindAddress = value;
} else if(name == "remote-port") {
config.remotePort = parseInt(value);
} else if(name == "bind-port") {
config.bindPort = parseInt(value);
} else {
console.log("UDPForwarder: unrecognized arg: " + argv);
}
}
forwarder = new UDPForwarder(config.remoteAddress, config.remotePort, config.bindAddress, config.bindPort);
forwarder.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment