This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathlistener.js
138 lines (105 loc) · 3.55 KB
/
listener.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
'use strict'
const EventEmitter = require('events')
const debug = require('debug')
const log = debug('libp2p:webrtc-star:listener')
log.error = debug('libp2p:webrtc-star:listener:error')
const multiaddr = require('multiaddr')
const io = require('socket.io-client')
const SimplePeer = require('simple-peer')
const pDefer = require('p-defer')
const toConnection = require('./socket-to-conn')
const { cleanUrlSIO } = require('./utils')
const { CODE_P2P } = require('./constants')
const sioOptions = {
transports: ['websocket'],
'force new connection': true
}
module.exports = ({ handler, upgrader }, WebRTCStar, options = {}) => {
const listener = new EventEmitter()
let listeningAddr
listener.__connections = []
listener.listen = (ma) => {
const defer = pDefer()
listeningAddr = ma
if (!ma.protoCodes().includes(CODE_P2P) && upgrader.localPeer) {
WebRTCStar._signallingAddr = ma.encapsulate(`/p2p/${upgrader.localPeer.toB58String()}`)
} else {
WebRTCStar._signallingAddr = ma
}
const sioUrl = cleanUrlSIO(ma)
log('Dialing to Signalling Server on: ' + sioUrl)
listener.io = io.connect(sioUrl, sioOptions)
const incommingDial = (offer) => {
if (offer.answer || offer.err) {
return
}
const spOptions = {
trickle: false,
...options
}
// Use custom WebRTC implementation
if (WebRTCStar.wrtc) { spOptions.wrtc = WebRTCStar.wrtc }
const channel = new SimplePeer(spOptions)
channel.once('signal', (signal) => {
offer.signal = signal
offer.answer = true
listener.io.emit('ss-handshake', offer)
})
channel.signal(offer.signal)
channel.once('connect', async () => {
const maConn = toConnection(channel)
log('new inbound connection %s', maConn.remoteAddr)
let conn
try {
conn = await upgrader.upgradeInbound(maConn)
} catch (err) {
log.error('inbound connection failed to upgrade', err)
return maConn.close()
}
if (!conn.remoteAddr) {
try {
conn.remoteAddr = ma.decapsulateCode(CODE_P2P).encapsulate(`/p2p/${conn.remotePeer.toB58String()}`)
} catch (err) {
log.error('could not determine remote address', err)
}
}
log('inbound connection %s upgraded', maConn.remoteAddr)
trackConn(listener, maConn)
listener.emit('connection', conn)
handler(conn)
})
}
listener.io.once('connect_error', (err) => defer.reject(err))
listener.io.once('error', (err) => {
listener.emit('error', err)
listener.emit('close')
})
listener.io.on('ws-handshake', incommingDial)
listener.io.on('ws-peer', WebRTCStar._peerDiscovered)
listener.io.on('connect', () => {
listener.io.emit('ss-join', WebRTCStar._signallingAddr.toString())
})
listener.io.once('connect', () => {
listener.emit('listening')
defer.resolve()
})
return defer.promise
}
listener.close = async () => {
listener.io && listener.io.emit('ss-leave')
await Promise.all(listener.__connections.map(maConn => maConn.close()))
listener.emit('close')
}
listener.getAddrs = () => {
return [listeningAddr]
}
WebRTCStar.listenersRefs[multiaddr.toString()] = listener
return listener
}
function trackConn (listener, maConn) {
listener.__connections.push(maConn)
const untrackConn = () => {
listener.__connections = listener.__connections.filter(c => c !== maConn)
}
maConn.conn.once('close', untrackConn)
}