diff --git a/core/client.js b/core/client.js index a6cfae52..40872fa2 100644 --- a/core/client.js +++ b/core/client.js @@ -96,7 +96,7 @@ function Client(/*input, output*/) { Object.defineProperty(this, 'node', { get : function() { - return self.session.id + 1; + return self.session.id; } }); diff --git a/core/client_connections.js b/core/client_connections.js index e2c8d577..d1a6be6e 100644 --- a/core/client_connections.js +++ b/core/client_connections.js @@ -61,27 +61,27 @@ function getActiveConnectionList(authUsersOnly) { function addNewClient(client, clientSock) { // - // Assign ID/client ID to next lowest & available # + // Find a node ID "slot" // - let id = 0; - for(let i = 0; i < clientConnections.length; ++i) { - if(clientConnections[i].id > id) { - break; + let nodeId; + for (nodeId = 1; nodeId < Number.MAX_SAFE_INTEGER; ++nodeId) { + const existing = clientConnections.find(client => nodeId === client.node); + if (!existing) { + break; // available slot } - id++; } - client.session.id = id; - const remoteAddress = client.remoteAddress = clientSock.remoteAddress; + client.session.id = nodeId; + const remoteAddress = client.remoteAddress = clientSock.remoteAddress; // create a unique identifier one-time ID for this session - client.session.uniqueId = new hashids('ENiGMA½ClientSession').encode([ id, moment().valueOf() ]); + client.session.uniqueId = new hashids('ENiGMA½ClientSession').encode([ nodeId, moment().valueOf() ]); clientConnections.push(client); clientConnections.sort( (c1, c2) => c1.session.id - c2.session.id); // Create a client specific logger // Note that this will be updated @ login with additional information - client.log = logger.log.child( { clientId : id, sessionId : client.session.uniqueId } ); + client.log = logger.log.child( { nodeId, sessionId : client.session.uniqueId } ); const connInfo = { remoteAddress : remoteAddress, @@ -101,7 +101,7 @@ function addNewClient(client, clientSock) { { client : client, connectionCount : clientConnections.length } ); - return id; + return nodeId; } function removeClient(client) { @@ -114,7 +114,7 @@ function removeClient(client) { logger.log.info( { connectionCount : clientConnections.length, - clientId : client.session.id + nodeId : client.node, }, 'Client disconnected' ); diff --git a/core/download_queue.js b/core/download_queue.js index d80ddf34..4380ded1 100644 --- a/core/download_queue.js +++ b/core/download_queue.js @@ -86,9 +86,9 @@ module.exports = class DownloadQueue { this.add(entry, true); // true=systemFile // clean up after ourselves when the session ends - const thisClientId = this.client.session.id; + const thisUniqueId = this.client.session.uniqueId; Events.once(Events.getSystemEvents().ClientDisconnected, evt => { - if(thisClientId === _.get(evt, 'client.session.id')) { + if(thisUniqueId === _.get(evt, 'client.session.uniqueId')) { FileEntry.removeEntry(entry, { removePhysFile : true }, err => { const Log = require('./logger').log; if(err) { diff --git a/core/login_server_module.js b/core/login_server_module.js index a08abfe9..da3e06de 100644 --- a/core/login_server_module.js +++ b/core/login_server_module.js @@ -72,12 +72,12 @@ module.exports = class LoginServerModule extends ServerModule { }); client.on('error', err => { - logger.log.info({ clientId : client.session.id, error : err.message }, 'Connection error'); + logger.log.info({ nodeId : client.node, error : err.message }, 'Connection error'); }); client.on('close', err => { const logFunc = err ? logger.log.info : logger.log.debug; - logFunc( { clientId : client.session.id }, 'Connection closed'); + logFunc( { nodeId : client.node }, 'Connection closed'); clientConns.removeClient(client); }); diff --git a/core/user_login.js b/core/user_login.js index 99faa1a2..3db6b5cc 100644 --- a/core/user_login.js +++ b/core/user_login.js @@ -81,9 +81,9 @@ function userLogin(client, username, password, options, cb) { if(existingClientConnection) { client.log.info( { - existingClientId : existingClientConnection.session.id, - username : user.username, - userId : user.userId + existingNodeId : existingClientConnection.node, + username : user.username, + userId : user.userId }, 'Already logged in' ); @@ -97,11 +97,12 @@ function userLogin(client, username, password, options, cb) { // update client logger with addition of username client.log = logger.log.child( { - clientId : client.log.fields.clientId, + nodeId : client.log.fields.nodeId, sessionId : client.log.fields.sessionId, username : user.username, } ); + client.log.info('Successful login'); // User's unique session identifier is the same as the connection itself