This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdht.js
executable file
·138 lines (117 loc) · 3.92 KB
/
dht.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node
const createDht = require('@hyperswarm/dht')
const minimist = require('minimist')
const os = require('os')
const path = require('path')
const fs = require('fs')
const dhtrpc = require('dht-rpc')
const argv = minimist(process.argv, {
boolean: [
'adaptive',
'verbose',
'help',
'json'
],
string: [
'address',
'id-file',
'id'
],
default: {
adaptive: true,
'id-file': path.join(os.tmpdir(), 'dht-rpc-id'),
verbose: false,
port: 49737,
address: '0.0.0.0'
},
alias: {
bootstrap: 'b',
verbose: 'V',
address: 'a',
port: 'p',
'id-file': 'f',
id: 'i',
help: 'h',
json: 'j'
}
})
if (argv.help) {
console.error(`Usage: ${process.argv[1]} [options]
--id, -i [key] ID for this dht node
--id-file, -f [path] Path to store a random id for this node (ignored if id is given)
--port, -p [port] Specify port to listen to (optional, as dht-nodes don't require listening)
--address, -a [addr] Specify address to listen to (optional, only used if port is given)
--json Output all messages as json.
--verbose, -V Print all lookups,announces,unannounces
--bootstrap, -b Specify bootstrap peers (optional)
--no-adaptive Disable adaptive ephemerality
`)
process.exit(1)
}
const idFile = argv['id-file']
const id = resolveId(argv.id, idFile)
const adaptive = argv.adaptive
const port = argv.port
const address = argv.address
const bootstrap = argv.boostrap ? [].concat(argv.bootstrap || []) : undefined
const version = require('@hyperswarm/dht/package.json').version
const verbose = argv.verbose
const dht = createDht({ adaptive: adaptive, ephemeral: adaptive, id, bootstrap })
const msg = `DHT version ${version}
id=${id.toString('hex')}
id-file=${idFile}
port=${port}
address=${address}
adaptive=${adaptive}
bootstrap=${bootstrap || '(default)'}
verbose=${verbose}
`
log(msg, { version, id: id.toString('hex'), idFile, port, address, adaptive, bootstrap, verbose })
if (adaptive) {
if (!argv.json) log('Running in adaptive mode. Will go persistent once running for ~30 min and holepunchable')
}
dht.on('listening', function () {
const { address, port, family } = this.socket.address()
log(`Listening on ${address}:${port} (udp,${family})`, { state: 'listening', address, port, family })
})
dht.on('ready', function () {
log('DHT node fully bootstrapped', { state: 'bootstrapped' })
})
dht.on('initial-nodes', function () {
const holepunchable = dht.holepunchable()
const remoteAddress = dht.remoteAddress()
log(holepunchable ? `Network appears holepunchable (remote address is ${remoteAddress.host}:${remoteAddress.port})` : 'Warning: Network does not appear holepunchable', { holepunchable, remoteAddress })
})
dht.on('persistent', function () {
log('DHT appears holepunchable and stable. Now persistent', { state: 'persistent' })
})
dht.on('warning', function (warning) {
log(`Warning: ${warning.message}`, { warning: { message: warning.message, stack: warning.stack } })
})
dht.on('error', function (error) {
log(`Error: ${error.stack}`, { error: { message: error.message, stack: error.stack } })
process.exit(1)
})
dht.listen(port, address)
if (argv.verbose) {
for (const event of ['announce', 'unannounce', 'lookup']) {
dht.on(event, function (target, peer) {
log(`Received ${event}: ${peer.host}:${peer.port} @ ${target.toString('hex')}`, { event, target: target.toString('hex'), peer })
})
}
}
function log (s, obj) {
if (argv.json) {
console.log(JSON.stringify(obj))
} else {
console.log(s)
}
}
function resolveId (id, idFile) {
if (id !== undefined) {
// Skip writing the id to disc as passing in the id is supposed to increase the startup speed.
return Buffer.from(id, 'hex')
}
if (!fs.existsSync(idFile)) fs.writeFileSync(idFile, dhtrpc.id().slice(0, 32))
return fs.readFileSync(idFile).slice(0, 32)
}