Skip to content

Instantly share code, notes, and snippets.

@fand
Last active July 4, 2019 03:16
Show Gist options
  • Save fand/3953146c535a5861ec2f182daa831004 to your computer and use it in GitHub Desktop.
Save fand/3953146c535a5861ec2f182daa831004 to your computer and use it in GitHub Desktop.
Forward TCP to UDP
#!/usr/bin/env node
const net = require('net');
const dgram = require('dgram');
if (process.argv.length !== 4) {
console.error(`
Usage:
node tcp2udp.js <TCP_SRC_PORT> <UDP_DST_PORT>
`);
process.exit(-1);
}
// Get ports
const pIn = process.argv[2] | 0;
const pOut = process.argv[3] | 0;
// Create UDP Client
const client = dgram.createSocket('udp4');
// Create TCP Server
const tcpServer = net.createServer(socket => {
socket.on('data', (d) => {
client.send(d, pOut);
});
});
// Bind
tcpServer.listen(pIn);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment