-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
227 lines (193 loc) · 5.35 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
const DiscoveryChannel = require('discovery-channel')
const { EventEmitter } = require('events')
const RTMPSession = require('node-media-server/node_rtmp_session')
const extend = require('extend')
const Batch = require('batch')
const debug = require('debug')('rtmp-swarm')
const pump = require('pump')
const cuid = require('cuid')
const net = require('net')
//require('node-media-server/logger').setLogType(0)
const PEER_SEEN = 1
const PEER_BANNED = 2
const MAX_RETRIES = 16
function createRTMPDiscoverySwarm(opts) {
return new DiscoverySwarm(opts)
}
class DiscoverySwarmSession extends RTMPSession {
constructor(socket, opts) {
opts = extend(true, opts || {}, {
rtmp: {
chunk_size: 60000, gop_cache: true,
ping_timeout: 30, ping: 60
}
})
super(opts, socket)
}
}
class DiscoverySwarm extends EventEmitter {
constructor(opts) {
super()
opts = extend(true, opts || {}, {
hash: false,
dht: {interval: 30*1000},
dns: {tty: 0, multicast: true, loopback: false, interval: 30*1000},
}, opts)
this.onpeer = this.onpeer.bind(this)
this.onclose = this.onclose.bind(this)
this.onerror = this.onerror.bind(this)
this.onwhoami = this.onwhoami.bind(this)
this.onlistening = this.onlistening.bind(this)
this.onconnection = this.onconnection.bind(this)
this.id = Buffer.from(opts.id || cuid())
this.net = opts.net || net
this.stream = opts.stream || null
this.server = this.net.createServer(this.onconnection)
this.channel = new DiscoveryChannel(opts)
this.announce = !!opts.announce
this.closed = false
this.listening = false
this.destroyed = false
this.seen = {}
this.peers = {}
this.pending = []
this.sessions = []
this.connected = {}
this.maxConnections = opts.maxConnections || 0
this.totalConnections = 0
this.server.on('close', this.onclose)
this.server.on('listening', this.onlistening)
this.channel.on('peer', this.onpeer)
this.channel.on('close', this.onclose)
this.channel.on('whoami', this.onwhoami)
this.setMaxListeners(0)
}
onconnection(socket) {
debug("onconnection")
const { maxConnections, connected, onerror, stream, peers, seen, net } = this
const swarm = this
this.totalConnections++
//this.on('peer', pool)
this.emit('connection', socket)
this.session(socket)
socket.once('close', () => { this.totalConnections-- })
}
onlistening() {
debug("onlistening")
this.listening = true
this.emit('listening')
}
onwhoami(me) {
debug("onwhoami:", me)
const key = `${me.host}:${port}`
this.seen[key] = PEER_BANNED
this.emit('whoami', me)
}
onerror(err) {
debug("onerror:", err)
this.emit('error', err)
}
onclose() {
debug("onclose")
this.emit('close')
}
onpeer(channel, peer, type) {
debug("onpeer:", channel, peer, type)
this.emit('discovery', channel, peer, type)
const key = `${peer.host}:${peer.port}`
if (false == key in this.seen) {
peer = peerify(peer, channel, 0)
this.peers[key] = peer
this.seen[key] = PEER_SEEN
this.pending.push(peer)
this.emit('peer', peer)
}
}
session(socket, opts) {
const { sessions } = this
const session = new DiscoverySwarmSession(socketify(socket), opts)
session.run()
sessions.push(session)
socket.on('destroy', onclose)
socket.on('close', onclose)
socket.on('end', onclose)
return session
function onclose() {
sessions.splice(sessions.indexOf(session), 1)
}
}
address() {
return this.server.address()
}
listen(port, address, cb) {
debug("listen:", port)
this.server.listen(port, address, cb)
return this
}
join(key, cb) {
debug("join:", key)
if (false == this.listening) {
return this.once('listening', () => this.join(key, cb))
}
if (this.announce) {
const { port } = this.address()
this.channel.join(key, port, cb)
} else {
this.channel.join(key, cb)
}
return this
}
close(cb) {
debug("close")
return this.destroy(cb)
}
destroy(cb) {
debug("destroy")
const batch = new Batch()
for (const session of this.sessions) {
try {
session.socket.destroy()
session.stop()
} catch (err) {
debug("destroy: error:", err)
}
}
batch.push((done) => this.server.close(done))
batch.push((done) => this.channel.destroy(done))
batch.end((err) => {
if (err) {
this.onerror(err)
} else {
this.listening = false
this.destroyed = true
this.closed = true
}
if ('function' == typeof cb) {
cb(er)
}
})
return this
}
}
// ported from: https://github.com/mafintosh/discovery-swarm/blob/master/index.js
function peerify(peer, channel, retries) {
if ('number' == typeof peer ) {
peer = {port: peer}
}
if (!peer.host) {
peer.host = '127.0.0.1'
}
retries = retries || 0
channel = Buffer.isBuffer(channel) ? channel.toString('hex') : channel
id = `${peer.host}:${peer.port}@${channel}`
return Object.assign(peer, { id, channel, retries })
}
function socketify(socket) {
if ('function' != typeof socket.setTimeout) {
socket.setTimeout = net.Socket.prototype.setTimeout.bind(socket)
}
return socket
}
module.exports = Object.assign(createRTMPDiscoverySwarm, {
DiscoverySwarm
})