/
index.js
executable file
·31 lines (28 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#! /usr/bin/env node
import { createServer, connect } from 'net'
import jsonfile from 'jsonfile'
import { Command } from 'commander'
import { dirname } from 'dirname-filename-esm'
import { join } from 'path'
const { name, version } = jsonfile.readFileSync(join(dirname(import.meta), './package.json'))
new Command(name)
.version(version)
.requiredOption('-p, --port <port>', 'Port to start TCP server on')
.requiredOption('-s, --source <source>', 'Source address to forward')
.action(async ({ port, source }) => {
const listenPort = parseInt(port)
const [sourceHost, sourcePortString] = source.split(':')
const sourcePort = parseInt(sourcePortString)
console.log('Source host:', sourceHost, 'Source port:', sourcePort)
createServer(serverSocket => {
const clientSocket = connect(sourcePort, sourceHost)
serverSocket.pipe(clientSocket)
clientSocket.pipe(serverSocket)
clientSocket.on('error', e => {
console.error(e)
})
}).listen(listenPort, () => {
console.log('TCP server is listening on port', listenPort)
})
})
.parse()