-
Notifications
You must be signed in to change notification settings - Fork 36
/
bin.js
executable file
·65 lines (53 loc) · 1.86 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
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
#!/usr/bin/env node
const Hyperbeam = require('./')
if (process.argv.includes('-h') || process.argv.includes('--help')) {
console.error('Usage: hyperbeam [passphrase]')
console.error('')
console.error(' Creates a 1-1 end-to-end encrypted network pipe.')
console.error(' If a passphrase is not supplied, will create a new phrase and begin listening.')
process.exit(1)
}
let beam
try {
beam = new Hyperbeam(process.argv[2], process.argv.includes('-r'))
} catch (e) {
if (e.constructor.name === 'PassphraseError') {
console.error(e.message)
console.error('(If you are attempting to create a new pipe, do not provide a phrase and hyperbeam will generate one for you.)')
process.exit(1)
} else {
throw e
}
}
if (beam.announce) {
console.error('[hyperbeam] Run hyperbeam ' + beam.key + ' to connect')
console.error('[hyperbeam] To restart this side of the pipe with the same key add -r to the above')
} else {
console.error('[hyperbeam] Connecting pipe...')
}
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)
})
beam.on('connected', function () {
console.error('[hyperbeam] Success! Encrypted tunnel established to remote peer')
})
beam.on('error', function (e) {
console.error('[hyperbeam] Error:', e.message)
closeASAP()
})
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)
})
}