-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswarm.js
More file actions
executable file
·63 lines (52 loc) · 1.26 KB
/
swarm.js
File metadata and controls
executable file
·63 lines (52 loc) · 1.26 KB
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
#!/usr/bin/env node
const dwebswarm = require('dwebswarm')
const sodium = require('sodium-native')
const minimist = require('minimist')
const pump = require('pump')
const argv = minimist(process.argv, {
boolean: [
'hash'
],
alias: {
hash: 'h',
announce: 'a',
lookup: 'l',
key: 'k'
}
})
if (!argv.lookup && !argv.announce) {
console.error(`Usage: ${process.argv[1]} [options]
--announce, -a
--lookup, -l
--hash, -h
--key, -k
`)
process.exit(1)
}
const swarm = dwebswarm()
let id = 0
swarm.on('connection', function (connection, info) {
const i = id++
console.error('[connection start id=' + i + ' type=' + info.type + ' client=' + info.client + ']')
pump(process.stdin, connection, process.stdout, function (err) {
console.error('[connection end id=' + i + ' err=' + (err || null) + ']')
})
})
swarm.join(key(), {
lookup: argv.lookup,
announce: argv.announce
})
process.once('SIGINT', function () {
console.error('[swarm destroying ...]')
swarm.destroy()
})
function key () {
const k = argv.key || argv.announce || argv.lookup
if (argv.hash) return hash(Buffer.from(k))
return Buffer.from(k, 'hex')
}
function hash (data) {
const out = Buffer.allocUnsafe(32)
sodium.crypto_generichash(out, data)
return out
}