-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbeam.js
88 lines (69 loc) · 2.59 KB
/
beam.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Hyperbeam from 'hyperbeam'
import randomWords from 'random-words'
import chalk from 'chalk'
import clipboardy from 'clipboardy'
const FULL_USAGE = `
The beam command is a general-purpose tool for sending data over the network
according to a secret passphrase. You choose a phrase (try to make it hard-ish
to guess!) and then share the phrase with your recipient. The phrase is only
good for 30-60 minutes.
On the sending device:
cat hello.txt | hyp beam "for bob roberts"
On the receiving device:
hyp beam "for bob roberts" > ./hello.txt
This can be really useful for sharing hyper keys between devices. For instance:
> hyp sync ./my-folder
Creating new hyperdrive...
Source: my-folder/
Target: hyper://f7145e1bbc0d17705861e996b47422e0ca50a80db9441249bd721ff426b79f2a/
Begin sync? [y/N] y
Syncing...
Synced
> echo "hyper://f7145e1bbc0d17705861e996b47422e0ca50a80db9441249bd721ff426b79f2a/" \\
| hyp beam "nobody can guess"
You can copy the generated passphrase with the --copy option
`
export default {
name: 'beam',
description: 'Send a stream of data over the network.',
usage: {
simple: '[passphrase]',
full: FULL_USAGE
},
command: async function (args) {
var phrase = args._[0] ? args._.join(' ') : randomWords(3).join(' ')
const beam = new Hyperbeam(phrase)
if (!args._[0]) {
console.error('[hyperbeam] Generated passphrase:\n')
console.error(' ', chalk.bold(phrase))
if (process.argv.indexOf("--copy") != -1){
console.error("Copying passphrase to clipboard")
clipboardy.writeSync(phrase)
}
console.error('')
}
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)
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)
})
}
}
}