-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
226 lines (184 loc) · 5.2 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
const { EventEmitter } = require('events')
const WebSocketServer = require('simple-websocket/server')
const { Socket } = require('./socket')
const crypto = require('hypercore-crypto')
const https = require('https')
const pump = require('pump')
const url = require('url')
class Server extends EventEmitter {
constructor(opts) {
if (opts === null || typeof opts !== 'object') {
opts = {}
}
super()
this.setMaxListeners(0)
this.connections = new WeakMap()
this.destroyed = false
this.opts = Object.assign({}, opts)
this.onconnection = this.onconnection.bind(this)
this.onlistening = this.onlistening.bind(this)
this.onrequest = this.onrequest.bind(this)
this.onclose = this.onclose.bind(this)
this.onerror = this.onerror.bind(this)
}
onrequest(conn, request) {
const connection = this.connections.get(conn)
const keyPair = crypto.keyPair()
const socket = new Socket({
userData: keyPair.publicKey,
socket: connection,
})
if (request && request.headers) {
socket.headers = request.headers
}
if (request && request.method) {
socket.method = request.method
}
if (request && request.url) {
socket.url = request.url
}
socket.on('close', () => this.connections.delete(conn))
socket.on('error', this.onerror)
this.emit('connection', socket, request)
socket.once('connect', () => {
const req = socket
const res = new Socket({
userData: keyPair.publicKey,
socket: {},
stream: req.stream,
key: keyPair.publicKey
})
socket.stream.once('feed', (discoveryKey) => {
req.writable = false
req.discoveryKey = discoveryKey
res.secretKey = keyPair.secretKey
res.discoveryKey = discoveryKey
this.emit('request', req, res, discoveryKey)
})
})
}
onconnection(connection) {
const conn = connection._ws
this.connections.set(conn, connection)
}
onlistening() {
this.emit('listening')
}
onclose() {
this.close()
}
onerror(err) {
this.emit('error', err)
}
address() {
if (this.server) {
const { protocol } = this
const addrinfo = this.server.address()
if (addrinfo) {
return Object.assign({ protocol }, addrinfo)
}
}
return null
}
listen(port, host, callback) {
let opts = {}
let protocol = 'ws:'
if ('function' === typeof port) {
callback = port
port = 0
}
if ('string' === typeof port) {
callback = host
host = port
port = 0
}
if ('function' === typeof host) {
callback = host
host = undefined
}
if (port && 'object' === typeof port) {
opts = port
port = undefined
} else if (undefined !== port && host && 'object' === typeof host) {
opts = host
host = undefined
}
if (this.server) {
const err = new Error('Already listening')
if ('function' === typeof callback) {
callback(err)
return this
} else {
this.emit('error',err)
throw this
}
}
Object.assign(opts, this.opts)
if (undefined !== host) {
opts.host = host
}
if (undefined !== port) {
opts.port = port
}
if (!opts.port && undefined !== opts.host) {
if (!/^wss?:/.test(opts.host)) {
opts.host = `ws://${opts.host}`
}
const uri = url.parse(opts.host)
opts.host = uri.hostname
opts.port = parseInt(uri.port) || port
protocol = uri.protocol
}
if ('wss:' === protocol && !opts.server) {
const httpsServer = https.createServer(this.opts)
opts.server = httpsServer
}
if (opts.server) {
opts.server.listen(opts.port, opts.host)
// delete 'opts.host' and 'opts.port' so `WebSocketServer
// does not start listening when these properties are present
// and the http server is supplied
delete opts.host
delete opts.port
}
//opts.clientTracking = true
this.protocol = protocol
this.server = new WebSocketServer(opts)
this.server.setMaxListeners(0)
if ('function' === typeof callback) {
this.server.once('error', callback)
this.server.once('listening', () => {
this.server.removeListener('error', callback)
callback(null)
})
}
this.server._server.on('connection', this.onrequest)
this.server.on('connection', this.onconnection)
this.server.on('listening', this.onlistening)
this.server.on('close', this.onclose)
this.server.on('error', this.onerror)
}
close(callback) {
if (this.destroyed) {
if ('function' === typeof callback) {
callback(new Error('Server is closed.'))
}
return this
}
this.destroyed = true
if (this.server) {
this.server._server.removeListener('connection', this.onrequest)
this.server.removeListener('connection', this.onconnection)
this.server.removeListener('listening', this.onlistening)
this.server.removeListener('close', this.onclose)
this.server.removeListener('error', this.onerror)
this.server.close(callback)
this.server = null
this.emit('close')
}
return this
}
}
module.exports = {
Server
}