-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
237 lines (195 loc) · 4.83 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
const hyperswarm = require('hyperswarm')
const sodium = require('sodium-native')
const noise = require('noise-peer')
const streamx = require('streamx')
const parallel = require('run-parallel')
const lpstream = require('length-prefixed-stream')
const Nanoresource = require('nanoresource/emitter')
const pump = require('pump')
class Peer extends Nanoresource {
constructor (socket, behaviour) {
super()
this.socket = socket
this.behaviour = behaviour
this.remotePublicKey = Buffer.alloc(0)
}
_open (cb) {
const done = (err) => {
this.socket.off('connected', done)
this.socket.off('error', done)
if (err == null && this.behaviour.open) this.behaviour.open(this)
return cb(err)
}
this.socket.once('handshake', ({ remoteStaticKey }) => {
if (remoteStaticKey) this.remotePublicKey = Buffer.from(remoteStaticKey)
})
this.socket.once('connected', done)
this.socket.once('error', done)
this.socket.once('end', this.close.bind(this))
this.messenger = new streamx.Duplex({
write: (data, cb) => {
if (this.behaviour.message) this.behaviour.message(this, data)
cb()
}
})
pump(
this.socket,
lpstream.decode(),
this.messenger,
lpstream.encode(),
this.socket,
(err) => {
if (this.behaviour.error && err) this.behaviour.error(this, err)
}
)
}
send (msg) {
return this.messenger.push(msg)
}
_close (cb) {
if (this.behaviour.close) this.behaviour.close(this)
this.socket.end(null, cb)
}
}
class Server extends Nanoresource {
constructor ({
topic,
keyPair,
clientKeys,
open,
message,
error,
close
}) {
super()
this.topic = topic
this.keyPair = keyPair
this.clientKeys = clientKeys
this.swarm = hyperswarm({
ephemeral: false,
maxPeers: 32,
maxServerSockets: Infinity,
maxClientSockets: -1,
queue: {
multiplex: false
}
})
this.behaviour = { open, message, error, close }
this.active = new Set()
this.swarm.on('connection', this._onconnection.bind(this))
}
_open (cb) {
this.swarm.join(this.topic, {
announce: true,
lookup: false
}, cb)
}
_onconnection (socket, info) {
var p = new Peer(noise(socket, false, {
pattern: 'XK',
staticKeyPair: this.keyPair,
onstatickey: (remoteKey, cb) => {
const valid = this.clientKeys.some(k => sodium.sodium_memcmp(remoteKey, k))
if (valid === false) return cb(new Error('Invalid key', remoteKey.toString('hex')))
return cb()
}
}), this.behaviour)
p.open((err) => {
if (err) return this.emit('connection-error', err)
this.active.add(p)
})
socket.on('close', () => {
this.active.delete(p)
})
}
publish (message, cb) {
parallel(Array.from(this.active, c => cb => c.send(message, cb)), function (err) {
if (cb) return cb(err)
})
}
listen (cb) {
this.open(cb)
}
_close (cb) {
this.swarm.leave(this.topic, () => {
parallel(Array.from(this.active, c => c.close.bind(c)), (err) => {
return cb(err)
})
})
}
}
class Client extends Nanoresource {
constructor ({
topic,
serverKey,
keyPair,
open,
message,
error,
close
}) {
super()
this.topic = topic
this.keyPair = keyPair
this.serverKey = serverKey
this.swarm = hyperswarm({
ephemeral: true,
maxPeers: 3,
maxServerSockets: -1,
maxClientSockets: 3,
queue: {
multiplex: false
}
})
this.behaviour = { open, message, error, close }
this.active = new Set()
this.swarm.on('connection', this._onconnection.bind(this))
}
_open (cb) {
this._reconnect(cb)
}
_reconnect (cb) {
if (this.active.size > 0) {
this.emit('reconnect')
return cb()
}
this.swarm.join(this.topic, {
announce: false,
lookup: true
}, () => {
setTimeout(() => this._reconnect(cb), 5000)
})
}
_onconnection (socket, info) {
var p = new Peer(noise(socket, true, {
pattern: 'XK',
remoteStaticKey: this.serverKey,
staticKeyPair: this.keyPair
}), this.behaviour)
p.remoteStaticKey = this.serverKey
p.open((err) => {
if (err) return this.emit('connection-error', err)
this.active.add(p)
})
socket.on('close', () => {
this.active.delete(p)
this._reconnect(() => {})
})
}
publish (message, cb) {
parallel(Array.from(this.active, c => cb => c.send(message, cb)), function (err) {
return cb(err)
})
}
connect (cb) {
this.open(cb)
}
_close (cb) {
this.swarm.leave(this.topic, () => {
parallel(Array.from(this.active, c => c.close.bind(c)), (err) => {
return cb(err)
})
})
}
}
module.exports = { Server, Client }