-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
executable file
·57 lines (46 loc) · 1.5 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
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
#!/usr/bin/env node
const Hyperbeam = require('hyperbeam')
const { spawn } = require('child_process')
if (process.argv.length < 3) {
console.error('Usage: hyperphone <topic>')
process.exit(1)
}
let record
let play
const beam = new Hyperbeam('hyperphone ' + 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')
record = spawn('sox', ['-q', '--buffer', '512', '-d', '-r', '44100', '-c', '1', '-e', 'signed-integer', '-b', '16', '-t', 'wav', '-'], {
stdio: ['inherit', 'pipe', 'inherit' ]
})
play = spawn('play', ['-q', '--buffer', '512', '-'], {
stdio: ['pipe', 'inherit', 'inherit' ]
})
record.stdout.pipe(beam).pipe(play.stdin)
})
beam.on('end', () => {
beam.end()
if (record) record.kill()
if (play) play.kill()
})
beam.resume()
beam.pause()
process.once('SIGINT', () => {
if (record) record.kill()
if (play) play.kill()
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)
})
}