-
Notifications
You must be signed in to change notification settings - Fork 3
/
fio.js
executable file
·69 lines (62 loc) · 1.81 KB
/
fio.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env node
const net = require('net')
const fs = require('fs')
main()
function main () {
const [from, to] = process.argv.slice(2, 4)
if (!to) return info()
if (fs.existsSync(from)) return relay({path: from}, to)
let [host, port] = from.split(':')
if (!port) {
port = host
host = 'localhost'
}
relay({host, port}, to)
}
function relay (from, to) {
const relayServer = net.createServer(listenEvents.bind(this, from))
relayServer.on('error', handleErrors)
relayServer.listen(to, () => console.log('relaying', from, 'to', to))
}
function listenEvents (from, client) {
const connection = net.createConnection(from)
client.on('data', (data) => connection.write(data))
connection.on('data', (data) => client.write(data))
connection.on('end', () => {
if (!client.destroyed) client.end()
})
client.on('end', () => {
if (!connection.destroyed) connection.end()
})
}
function handleErrors (e) {
if (e.code === 'EACCES') {
return console.error('ERROR!', 'Permission denied', `(${e.message})`)
}
if (e.code === 'EADDRINUSE') {
return console.error('ERROR!', 'Address already in use', `(${e.message})`)
}
return console.error('ERROR!', e.message)
}
function info () {
const {version} = require('./package.json')
const examples = [
[
' FROM unix socket TO the 8080 tcp port (localhost)',
'fio /my/socket/path.sock 8080'
],
[
' FROM tcp port 22 TO tcp port 4242 (localhost)',
'fio localhost:22 4242'
],
[
' FROM tcp port 80 at google.com TO unix socket',
'fio google.com:80 ./test.sock'
]
]
console.log('fio - simple socket relay tool')
console.log(`Version: ${version}`)
console.log('Usage: fio [FROM] [TO]')
console.log('Examples:')
console.log(examples.map((example) => example.join('\n\t')).join('\n\n'))
}