-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
192 lines (163 loc) · 4.26 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const { Duplex } = require('streamx')
const sodium = require('sodium-universal')
const b4a = require('b4a')
const queueTick = require('queue-tick')
const b32 = require('hi-base32')
const DHT = require('hyperdht')
module.exports = class Hyperbeam extends Duplex {
constructor (key, options) {
super()
if (typeof key !== 'string') {
options = key
key = null
}
if (typeof options === 'boolean') {
options = { announce: options }
} else if (typeof options !== 'object') {
options = {}
}
let announce = !!options.announce
if (!key) {
key = toBase32(randomBytes(32))
announce = true
}
this.key = key
this.announce = announce
this._node = options.dht || null
this._server = null
this._out = null
this._inc = null
this._now = Date.now()
this._ondrain = null
this._onopen = null
this._onread = null
}
get connected () {
return !!this._out
}
_ondrainDone (err) {
if (this._ondrain) {
const cb = this._ondrain
this._ondrain = null
cb(err)
}
}
_onreadDone (err) {
if (this._onread) {
const cb = this._onread
this._onread = null
cb(err)
}
}
_onopenDone (err) {
if (this._onopen) {
const cb = this._onopen
this._onopen = null
cb(err)
}
}
async _open (cb) {
const keyPair = DHT.keyPair(fromBase32(this.key))
this._onopen = cb
if (!this._node) this._node = new DHT({ ephemeral: true })
const onConnection = s => {
s.on('data', (data) => {
if (!this._inc) {
this._inc = s
this._inc.on('error', (err) => this.destroy(err))
this._inc.on('end', () => this._push(null))
}
if (s !== this._inc) return
if (this._push(data) === false) s.pause()
})
s.on('end', () => {
if (this._inc) return
this._push(null)
})
if (!this._out) {
this._out = s
this._out.on('error', (err) => this.destroy(err))
this._out.on('drain', () => this._ondrain(null))
this.emit('connected')
this._onopenDone(null)
}
}
if (this.announce) {
this._server = this._node.createServer({
firewall (remotePublicKey) {
return !b4a.equals(remotePublicKey, keyPair.publicKey)
}
})
this._server.on('connection', onConnection)
try {
await this._server.listen(keyPair)
} catch (err) {
this._onopenDone(err)
return
}
this.emit('remote-address', { host: this._node.host, port: this._node.port })
return
}
const connection = this._node.connect(keyPair.publicKey, { keyPair })
try {
await new Promise((resolve, reject) => {
connection.once('open', resolve)
connection.once('close', reject)
connection.once('error', reject)
})
} catch (err) {
this._onopenDone(err)
return
}
this.emit('remote-address', { host: this._node.host, port: this._node.port })
onConnection(connection)
}
_read (cb) {
this._onread = cb
if (this._inc) this._inc.resume()
}
_push (data) {
const res = this.push(data)
queueTick(() => this._onreadDone(null))
return res
}
_write (data, cb) {
if (this._out.write(data) !== false) return cb(null)
this._ondrain = cb
}
_final (cb) {
const done = () => {
this._out.removeListener('finish', done)
this._out.removeListener('error', done)
cb(null)
}
this._out.end()
this._out.on('finish', done)
this._out.on('error', done)
}
_predestroy () {
if (this._inc) this._inc.destroy()
if (this._out) this._out.destroy()
const err = new Error('Destroyed')
this._onopenDone(err)
this._onreadDone(err)
this._ondrainDone(err)
}
async _destroy (cb) {
if (!this._node) return cb(null)
if (this._server) await this._server.close().catch(e => undefined)
await this._node.destroy().catch(e => undefined)
cb(null)
}
}
function toBase32 (buf) {
return b32.encode(buf).replace(/=/g, '').toLowerCase()
}
function fromBase32 (str) {
return b4a.from(b32.decode.asBytes(str.toUpperCase()))
}
function randomBytes (length) {
const buffer = b4a.alloc(length)
sodium.randombytes_buf(buffer)
return buffer
}