Commit 0b6da0fdd1a0fd2379024c3a9ee177c923faa5d9

Authored by Adhidarma Hadiwinoto
1 parent 21d8b2ad9f
Exists in master

Add clientName on connected client

Showing 2 changed files with 12 additions and 3 deletions Side-by-side Diff

lib/is-valid-apikey.js
... ... @@ -3,9 +3,11 @@ module.exports = (apikey, apikeys) => {
3 3 if (typeof apikey !== 'string') return false;
4 4 if (!apikeys || !Array.isArray(apikeys)) return false;
5 5  
6   - return !!apikeys.find((item) => !item.disabled
  6 + const matchedApikey = !!apikeys.find((item) => !item.disabled
7 7 && (
8 8 (typeof item === 'string' && item === apikey)
9 9 || (item.value === apikey)
10 10 ));
  11 +
  12 + return matchedApikey || false;
11 13 };
... ... @@ -46,11 +46,12 @@ if (!wsListenPort) {
46 46 // eslint-disable-next-line no-param-reassign
47 47 ws.isAlive = true;
48 48  
49   - const { remoteAddress, apikey } = client;
  49 + const { remoteAddress, apikey, name: clientName } = client;
50 50  
51 51 logger.info(`${MODULE_NAME} F7755A03: Client connected`, {
52 52 xid: connectionXid,
53 53 remoteAddress,
  54 + clientName,
54 55 apikey,
55 56 });
56 57  
... ... @@ -164,7 +165,12 @@ if (!wsListenPort) {
164 165 server.on('upgrade', (req, socket, head) => {
165 166 const apikey = req.headers && (req.headers.apikey || req.headers.token);
166 167  
167   - if (!isValidApikey(apikey, config.push_trx_server && config.push_trx_server.apikey)) {
  168 + const matchedApikey = isValidApikey(
  169 + apikey,
  170 + config.push_trx_server && config.push_trx_server.apikey,
  171 + );
  172 +
  173 + if (!matchedApikey) {
168 174 rejectConnection(req, socket);
169 175 return;
170 176 }
... ... @@ -177,6 +183,7 @@ if (!wsListenPort) {
177 183 const client = {
178 184 remoteAddress: req.socket.remoteAddress,
179 185 apikey,
  186 + name: matchedApikey.name,
180 187 };
181 188  
182 189 wss.emit('connection', ws, req, client);