Skip to content

Commit 397ed95

Browse files
committed
Adjust server.js for redis 5.x
1 parent 4981300 commit 397ed95

1 file changed

Lines changed: 112 additions & 99 deletions

File tree

  • files_wcf/acp/be.bastelstu.wcf.nodePush

files_wcf/acp/be.bastelstu.wcf.nodePush/server.js

Lines changed: 112 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -166,122 +166,137 @@ ${sourceFiles.map((item) => `* /source/${item}`).join('\n')}`)
166166
})
167167
}
168168

169-
server.listen(config.outbound.port, config.outbound.host, null, function () {
170-
const rsub = redis.createClient(config.redis)
171-
const r = redis.createClient(config.redis)
172-
173-
io = new socket_io.Server(server, {
174-
cors: { }
175-
})
169+
const rsub = redis.createClient({
170+
url: config.redis,
171+
})
172+
const r = redis.createClient({
173+
url: config.redis,
174+
})
176175

177-
io.on('connection', function (socket) {
178-
const id = ++stats.outbound.total
179-
stats.outbound.current++
180-
181-
debug(`Client ${id} connected`)
182-
183-
let channels
184-
let rekeyTimer = undefined
185-
let rekey = function () {
186-
debug(`Client ${id} (${JSON.stringify(channels)}) receiving new keys`)
187-
crypto.randomBytes(32, function (err, buf) {
188-
if (err) {
189-
socket.disconnect()
190-
return
191-
}
192-
r.set(`${config.uuid}:nodePush:token:${buf.toString('hex')}`, JSON.stringify(channels), 'EX', REKEY_INTERVAL * 3)
193-
socket.emit('rekey', buf.toString('hex'))
194-
})
195-
}
196-
let connected = function () {
197-
rekey()
198-
rekeyTimer = setInterval(rekey, REKEY_INTERVAL * 1e3)
199-
socket.emit('authenticated')
200-
}
201-
202-
socket.on('disconnect', function () {
203-
debug(`Client ${id} disconnected`)
204-
205-
stats.outbound.current--
206-
clearInterval(rekeyTimer)
176+
Promise.all([
177+
r.connect(),
178+
rsub.connect(),
179+
]).then(function () {
180+
server.listen(config.outbound.port, config.outbound.host, null, function () {
181+
io = new socket_io.Server(server, {
182+
cors: { }
207183
})
208184

209-
socket.on('connectData', function (connectData) {
210-
debug(`Client ${id} sent connectData ${connectData}`)
211-
212-
let payload
185+
io.on('connection', function (socket) {
186+
const id = ++stats.outbound.total
187+
stats.outbound.current++
213188

214-
if (!(payload = checkSignature(connectData, config.signerKey))) {
215-
debug(`Client ${id} sent incorrectly signed connectData, disonnecting`)
216-
socket.disconnect()
217-
return
218-
}
219-
debug(`Client ${id} connectData: ${payload.toString('utf8')}`)
220-
payload = JSON.parse(payload.toString('utf8'))
221-
if (!payload.timestamp || (payload.timestamp * 1000) < (Date.now() - 15e3)) {
222-
debug(`Client ${id} sent outdated connectData, disonnecting`)
223-
socket.disconnect()
224-
return
189+
debug(`Client ${id} connected`)
190+
191+
let channels
192+
let rekeyTimer = undefined
193+
let rekey = function () {
194+
debug(`Client ${id} (${JSON.stringify(channels)}) receiving new keys`)
195+
crypto.randomBytes(32, function (err, buf) {
196+
if (err) {
197+
socket.disconnect()
198+
return
199+
}
200+
r.set(`${config.uuid}:nodePush:token:${buf.toString('hex')}`, JSON.stringify(channels), { EX: REKEY_INTERVAL * 3 })
201+
.then(function () {
202+
socket.emit('rekey', buf.toString('hex'))
203+
})
204+
.catch(function (err) {
205+
socket.disconnect()
206+
})
207+
})
225208
}
226-
payload.userID = parseInt(payload.userID, 10)
227-
if (!(payload.groups instanceof Array)) {
228-
debug(`Client ${id} sent malformed groups in connectData, disonnecting`)
229-
socket.disconnect()
230-
return
231-
}
232-
if (!(payload.channels instanceof Array)) {
233-
debug(`Client ${id} sent malformed channels in connectData, disonnecting`)
234-
socket.disconnect()
235-
return
209+
let connected = function () {
210+
rekey()
211+
rekeyTimer = setInterval(rekey, REKEY_INTERVAL * 1e3)
212+
socket.emit('authenticated')
236213
}
237214

238-
channels = [ 'authenticated' ]
239-
channels.push(`user-${payload.userID}`)
240-
if (payload.userID === 0) {
241-
channels.push('guest')
242-
}
243-
else {
244-
channels.push('registered')
245-
}
246-
payload.groups.forEach(groupID => channels.push(`group-${groupID}`))
247-
payload.channels.forEach(channel => channels.push(`channel-${channel}`))
248-
249-
channels.forEach(channel => socket.join(channel))
250-
connected()
251-
})
252-
253-
socket.on('token', function (token) {
254-
debug(`Client ${id} sent reconnect token ${token}`)
255-
r.get(`${config.uuid}:nodePush:token:${token}`, function (err, reply) {
256-
r.del(`${config.uuid}:nodePush:token:${token}`)
257-
if (err) {
258-
debug(`Client ${id} failed to look up reconnect token, disconnecting`)
215+
socket.on('disconnect', function () {
216+
debug(`Client ${id} disconnected`)
217+
218+
stats.outbound.current--
219+
clearInterval(rekeyTimer)
220+
})
221+
222+
socket.on('connectData', function (connectData) {
223+
debug(`Client ${id} sent connectData ${connectData}`)
224+
225+
let payload
226+
227+
if (!(payload = checkSignature(connectData, config.signerKey))) {
228+
debug(`Client ${id} sent incorrectly signed connectData, disonnecting`)
259229
socket.disconnect()
260230
return
261231
}
262-
if (reply === null) {
263-
debug(`Client ${id} reconnect token does not exist, disconnecting`)
232+
debug(`Client ${id} connectData: ${payload.toString('utf8')}`)
233+
payload = JSON.parse(payload.toString('utf8'))
234+
if (!payload.timestamp || (payload.timestamp * 1000) < (Date.now() - 15e3)) {
235+
debug(`Client ${id} sent outdated connectData, disonnecting`)
264236
socket.disconnect()
265237
return
266238
}
267-
268-
try {
269-
channels = JSON.parse(reply)
270-
channels.forEach(channel => socket.join(channel))
271-
272-
connected()
239+
payload.userID = parseInt(payload.userID, 10)
240+
if (!(payload.groups instanceof Array)) {
241+
debug(`Client ${id} sent malformed groups in connectData, disonnecting`)
242+
socket.disconnect()
243+
return
273244
}
274-
catch (e) {
245+
if (!(payload.channels instanceof Array)) {
246+
debug(`Client ${id} sent malformed channels in connectData, disonnecting`)
275247
socket.disconnect()
276248
return
277249
}
250+
251+
channels = [ 'authenticated' ]
252+
channels.push(`user-${payload.userID}`)
253+
if (payload.userID === 0) {
254+
channels.push('guest')
255+
}
256+
else {
257+
channels.push('registered')
258+
}
259+
payload.groups.forEach(groupID => channels.push(`group-${groupID}`))
260+
payload.channels.forEach(channel => channels.push(`channel-${channel}`))
261+
262+
channels.forEach(channel => socket.join(channel))
263+
connected()
264+
})
265+
266+
socket.on('token', function (token) {
267+
debug(`Client ${id} sent reconnect token ${token}`)
268+
r.get(`${config.uuid}:nodePush:token:${token}`)
269+
.then(function (reply) {
270+
return r.del(`${config.uuid}:nodePush:token:${token}`)
271+
.then(() => {
272+
if (reply === null) {
273+
debug(`Client ${id} reconnect token does not exist, disconnecting`)
274+
socket.disconnect()
275+
return
276+
}
277+
278+
try {
279+
channels = JSON.parse(reply)
280+
channels.forEach(channel => socket.join(channel))
281+
282+
connected()
283+
}
284+
catch (e) {
285+
socket.disconnect()
286+
return
287+
}
288+
})
289+
})
290+
.catch(function (err) {
291+
debug(`Client ${id} failed to look up reconnect token, disconnecting: ${err}`)
292+
socket.disconnect()
293+
})
278294
})
279295
})
280-
})
281296

282-
rsub.on('message', function (channel, _message) {
283-
stats.inbound++
284-
if (channel === `${config.uuid}:nodePush`) {
297+
rsub.subscribe(`${config.uuid}:nodePush`, (_message) => {
298+
stats.inbound++
299+
285300
debug(`Push: ${_message}`)
286301
try {
287302
_message = JSON.parse(_message)
@@ -298,8 +313,6 @@ server.listen(config.outbound.port, config.outbound.host, null, function () {
298313
const payload = _message.payload
299314

300315
sendMessage(message, target, payload)
301-
}
316+
})
302317
})
303-
304-
rsub.subscribe(`${config.uuid}:nodePush`)
305318
})

0 commit comments

Comments
 (0)