-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
123 lines (104 loc) · 2.91 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
const tcp = require('tcp-forward')
const network = require('@hyperswarm/network')
const pump = require('pump')
exports.Local = tcp.Local
exports.Remote = class Remote extends tcp.Remote {
constructor () {
super()
const announcing = new Map()
this.network = network({
ephemeral: false,
socket (socket) {
// destroy all incoming sockets
socket.on('error', () => socket.destroy())
socket.destroy()
}
})
this.on('forward-listening', (port, topic) => {
this.network.bind(() => {
const t = this.network.discovery.announce(topic, { port })
const key = topic.toString('hex') + ':' + port
announcing.set(key, t)
})
})
this.on('forward-close', (port, topic) => {
this.network.bind(() => {
const key = topic.toString('hex') + ':' + port
const t = announcing.get(key)
if (t) {
t.destroy()
announcing.delete(key)
}
})
})
this.on('forward-connect', (socket, topic) => {
let socketClosed = false
socket.on('error', () => socket.destroy())
socket.on('close', function () {
socketClosed = true
})
this.network.bind(() => {
const t = this.network.lookup(topic)
const seen = new Set()
const queued = []
let destroyOnEmpty = false
const self = this
t.on('peer', function (peer) {
const id = peer.host + ':' + peer.port
if (seen.has(id)) return
seen.add(id)
queued.push(peer)
if (queued.length === 1) kick()
})
t.on('update', function () {
destroyOnEmpty = true
if (!queued.length) destroy()
})
function destroy () {
t.destroy()
socket.destroy()
}
function kick () {
if (self.network.destroyed) return
if (socketClosed) return destroy()
if (!queued.length) {
if (destroyOnEmpty) destroy()
return
}
const next = queued[queued.length - 1]
self.network.connect(next, function (err, connection) {
queued.pop()
if (err || self.network.destroyed) return kick()
t.destroy()
if (socketClosed) return connection.destroy()
pump(connection, socket, connection)
})
}
})
})
}
announce (topic, cb) {
if (!cb) cb = noop
let port = 0
try {
port = this.address().port
} catch (_) {
throw new Error('Server must be listening first')
}
this.network.bind((err) => {
if (err) return cb(err)
this.network.discovery.announce(topic, { port })
})
}
destroy () {
this.network.close((err) => {
if (!err) this.emit('network-close')
})
super.destroy()
}
listen (...args) {
this.network.bind()
super.listen(...args)
}
}
function noop () {}