forked from holepunchto/hyperbeam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.js
executable file
·40 lines (30 loc) · 1.06 KB
/
bin.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
#!/usr/bin/env node
const Hyperbeam = require('./')
if (process.argv.length < 3) {
console.error('Usage: hyperbeam <topic>')
process.exit(1)
}
const beam = new Hyperbeam(process.argv.slice(2).join(' '))
beam.on('remote-address', function ({ host, port }) {
if (!host) console.error('[hyperbeam] Could not detect remote address')
else console.error('[hyperbeam] Joined the DHT - remote address is ' + host + ':' + port)
if (port) console.error('[hyperbeam] Network is holepunchable \\o/')
})
beam.on('connected', function () {
console.error('[hyperbeam] Success! Encrypted tunnel established to remote peer')
})
beam.on('end', () => beam.end())
process.stdin.pipe(beam).pipe(process.stdout)
if (typeof process.stdin.unref === 'function') process.stdin.unref()
process.once('SIGINT', () => {
if (!beam.connected) closeASAP()
else beam.end()
})
function closeASAP () {
console.error('[hyperbeam] Shutting down beam...')
const timeout = setTimeout(() => process.exit(1), 2000)
beam.destroy()
beam.on('close', function () {
clearTimeout(timeout)
})
}