From 0b6da0fdd1a0fd2379024c3a9ee177c923faa5d9 Mon Sep 17 00:00:00 2001 From: Adhidarma Hadiwinoto <adhisimon@gmail.com> Date: Mon, 7 Feb 2022 15:27:26 +0700 Subject: [PATCH] Add clientName on connected client --- lib/is-valid-apikey.js | 4 +++- server.js | 11 +++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/is-valid-apikey.js b/lib/is-valid-apikey.js index 280cba4..b3e23d8 100644 --- a/lib/is-valid-apikey.js +++ b/lib/is-valid-apikey.js @@ -3,9 +3,11 @@ module.exports = (apikey, apikeys) => { if (typeof apikey !== 'string') return false; if (!apikeys || !Array.isArray(apikeys)) return false; - return !!apikeys.find((item) => !item.disabled + const matchedApikey = !!apikeys.find((item) => !item.disabled && ( (typeof item === 'string' && item === apikey) || (item.value === apikey) )); + + return matchedApikey || false; }; diff --git a/server.js b/server.js index 9b33eab..85535fa 100644 --- a/server.js +++ b/server.js @@ -46,11 +46,12 @@ if (!wsListenPort) { // eslint-disable-next-line no-param-reassign ws.isAlive = true; - const { remoteAddress, apikey } = client; + const { remoteAddress, apikey, name: clientName } = client; logger.info(`${MODULE_NAME} F7755A03: Client connected`, { xid: connectionXid, remoteAddress, + clientName, apikey, }); @@ -164,7 +165,12 @@ if (!wsListenPort) { server.on('upgrade', (req, socket, head) => { const apikey = req.headers && (req.headers.apikey || req.headers.token); - if (!isValidApikey(apikey, config.push_trx_server && config.push_trx_server.apikey)) { + const matchedApikey = isValidApikey( + apikey, + config.push_trx_server && config.push_trx_server.apikey, + ); + + if (!matchedApikey) { rejectConnection(req, socket); return; } @@ -177,6 +183,7 @@ if (!wsListenPort) { const client = { remoteAddress: req.socket.remoteAddress, apikey, + name: matchedApikey.name, }; wss.emit('connection', ws, req, client); -- 1.9.0