-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.js
256 lines (196 loc) · 5.87 KB
/
server.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const { EventEmitter } = require('events')
const os = require('os')
const hyperswarm = require('@hyperswarm/network')
const HyperswarmProxyStream = require('./')
module.exports = class HyperswarmProxyServer extends EventEmitter {
constructor (opts = {}) {
super()
const {
bootstrap,
ephemeral,
network = hyperswarm({
bootstrap,
ephemeral,
socket: (socket) => this.handleIncoming(socket),
bind: () => this.emit('ready'),
close: () => this.emit('close')
}),
handleIncoming
} = opts
this.shouldAnnounce = false
if (handleIncoming) {
this.handleIncoming = handleIncoming
this.shouldAnnounce = true
}
this.network = network
this.clients = new Set()
}
handleIncoming (socket) {
// By default we just kill incoming connections
socket.end()
}
handleStream (stream, cb = noop) {
this.network.bind((err) => {
if (err) return cb(err)
const client = new Client(stream, this.network, this.shouldAnnounce)
this.clients.add(client)
stream.once('close', () => {
this.clients.delete(client)
})
client.once('error', (e) => {
// TODO: Output errors to DEBUG
this.emit('error', e)
})
cb(null, client)
})
}
connectClientsTo (topic, port, host) {
const topicString = topic.toString('hex')
const peerData = {
port,
host,
local: false,
referrer: null,
topic
}
for (const client of this.clients) {
if (client.lookups.has(topicString)) {
client.lookups.get(topicString).emit('peer', peerData)
}
}
}
destroy (cb) {
for (const client of this.clients) {
client.destroy()
}
this.network.close(cb)
}
}
class Client extends HyperswarmProxyStream {
constructor (stream, network, shouldAnnounce) {
super(stream)
this.network = network
this.lookups = new Map()
this.peerMap = new Map()
this.connections = new Set()
this.streamCounter = 0
this.shouldAnnounce = shouldAnnounce
this.handleJoin = this.handleJoin.bind(this)
this.handleLeave = this.handleLeave.bind(this)
this.handleConnect = this.handleConnect.bind(this)
this.once('ready', () => {
this.init()
})
}
init () {
this.on('join', this.handleJoin)
this.on('leave', this.handleLeave)
this.on('connect', this.handleConnect)
this.ready()
}
nextStreamId () {
return this.streamCounter++
}
handleJoin ({ topic }) {
const lookupString = topic.toString('hex')
// No need to join if already joined
if (this.lookups.has(lookupString)) {
// Force restart of lookups
this.lookups.get(lookupString).update()
return
}
// Build up the list of what we think might be "us"
// This should be done very time to account for IP/network interface changes
// TODO: Move it a layer up and debounce
this.network.discovery.ping((err, results) => {
if (!this.network) return
if (err) return this.emit('error', err)
const pingAddresses = results.map(({ pong }) => {
const { host, port } = pong
return `${host}:${port}`
})
const boundPort = this.network.address().port
const localAddresses = getIPv4Addresses()
.map(({ address }) => `${address}:${boundPort}`)
const selfPeerData = new Set(pingAddresses.concat(localAddresses))
const handlePeer = (peer) => {
const { host, port } = peer
const peerString = `${host}:${port}`
// Ignore connections to ourselves
if (selfPeerData.has(peerString)) return
const id = `${peerString}:${topic.toString('hex')}`
this.peerMap.set(id, peer)
this.onPeer(topic, id)
}
const handleUpdate = () => {
this.onUpdated(topic)
}
const lookup = this.network.lookup(topic)
this.lookups.set(lookupString, lookup)
let announce = null
if (this.shouldAnnounce) {
announce = this.network.announce(topic)
}
lookup.on('peer', handlePeer)
lookup.on('update', handleUpdate)
lookup.on('close', () => {
this.lookups.delete(lookupString)
if (announce) announce.destroy()
})
})
}
handleLeave ({ topic }) {
const lookupString = topic.toString('hex')
// No need to try to leave topics we're not a part of
if (!this.lookups.has(lookupString)) return
const lookup = this.lookups.get(lookupString)
lookup.destroy()
}
connectTo (peerData) {
const { topic, host, port } = peerData
const peerId = `${host}:${port}:${topic.toString('hex')}`
const id = this.nextStreamId()
const proxy = this.openStream(topic, peerId, id)
this.network.connect(peerData, (err, socket) => {
// Tell the other side about the error
if (err) {
this.onStreamError(id, err.message)
proxy.end()
return
}
this.connections.add(socket)
socket.pipe(proxy).pipe(socket)
socket.once('error', (err) => {
this.onStreamError(id, err.message)
})
socket.once('close', () => {
this.connections.delete(socket)
})
})
}
handleConnect ({ peer }) {
// Don't try connecting to a peer that doesn't exist
if (!this.peerMap.has(peer)) return
const peerData = this.peerMap.get(peer)
this.connectTo(peerData)
}
destroy () {
this.removeListener('join', this.handleJoin)
this.removeListener('leave', this.handleLeave)
this.removeListener('connect', this.handleConnect)
for (const socket of this.connections) {
socket.end()
}
this.network = null
this.end()
}
}
function noop () {}
function getIPv4Addresses () {
const interfaces = os.networkInterfaces()
return Object
.keys(interfaces)
.map((name) => interfaces[name])
.reduce((a, b) => a.concat(b))
.filter(({ family }) => family === 'IPv4')
}