2015-08-05 04:35:59 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2018-11-24 00:41:16 +00:00
|
|
|
const logger = require('./logger.js');
|
|
|
|
const Events = require('./events.js');
|
|
|
|
const UserProps = require('./user_property.js');
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2018-11-24 00:41:16 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
2019-10-30 03:17:58 +00:00
|
|
|
const hashids = require('hashids/cjs');
|
2016-01-30 22:18:55 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.getActiveConnections = getActiveConnections;
|
2018-11-14 03:03:54 +00:00
|
|
|
exports.getActiveConnectionList = getActiveConnectionList;
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.addNewClient = addNewClient;
|
|
|
|
exports.removeClient = removeClient;
|
|
|
|
exports.getConnectionByUserId = getConnectionByUserId;
|
2018-11-13 05:03:28 +00:00
|
|
|
exports.getConnectionByNodeId = getConnectionByNodeId;
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2016-08-27 02:53:27 +00:00
|
|
|
const clientConnections = [];
|
2018-11-13 05:03:28 +00:00
|
|
|
exports.clientConnections = clientConnections;
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2018-11-13 05:03:28 +00:00
|
|
|
function getActiveConnections(authUsersOnly = false) {
|
|
|
|
return clientConnections.filter(conn => {
|
|
|
|
return ((authUsersOnly && conn.user.isAuthenticated()) || !authUsersOnly);
|
|
|
|
});
|
|
|
|
}
|
2015-10-18 02:03:51 +00:00
|
|
|
|
2018-11-14 03:03:54 +00:00
|
|
|
function getActiveConnectionList(authUsersOnly) {
|
2016-08-27 02:53:27 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!_.isBoolean(authUsersOnly)) {
|
|
|
|
authUsersOnly = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const now = moment();
|
|
|
|
|
2018-11-13 05:03:28 +00:00
|
|
|
return _.map(getActiveConnections(authUsersOnly), ac => {
|
2018-06-22 05:15:04 +00:00
|
|
|
const entry = {
|
2018-06-23 03:26:46 +00:00
|
|
|
node : ac.node,
|
|
|
|
authenticated : ac.user.isAuthenticated(),
|
|
|
|
userId : ac.user.userId,
|
2018-07-22 16:55:39 +00:00
|
|
|
action : _.get(ac, 'currentMenuModule.menuConfig.desc', 'Unknown'),
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// There may be a connection, but not a logged in user as of yet
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(ac.user.isAuthenticated()) {
|
2018-06-23 03:26:46 +00:00
|
|
|
entry.userName = ac.user.username;
|
2018-11-24 00:41:16 +00:00
|
|
|
entry.realName = ac.user.properties[UserProps.RealName];
|
|
|
|
entry.location = ac.user.properties[UserProps.Location];
|
|
|
|
entry.affils = entry.affiliation = ac.user.properties[UserProps.Affiliations];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-11-24 00:41:16 +00:00
|
|
|
const diff = now.diff(moment(ac.user.properties[UserProps.LastLoginTs]), 'minutes');
|
2018-06-23 03:26:46 +00:00
|
|
|
entry.timeOn = moment.duration(diff, 'minutes');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
});
|
2016-01-30 22:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 21:39:33 +00:00
|
|
|
function addNewClient(client, clientSock) {
|
2019-01-04 04:02:21 +00:00
|
|
|
//
|
|
|
|
// Assign ID/client ID to next lowest & available #
|
|
|
|
//
|
|
|
|
let id = 0;
|
|
|
|
for(let i = 0; i < clientConnections.length; ++i) {
|
|
|
|
if(clientConnections[i].id > id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
id++;
|
|
|
|
}
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2019-01-04 04:02:21 +00:00
|
|
|
client.session.id = id;
|
|
|
|
const remoteAddress = client.remoteAddress = clientSock.remoteAddress;
|
2018-11-24 00:41:16 +00:00
|
|
|
// create a unique identifier one-time ID for this session
|
2018-06-22 05:15:04 +00:00
|
|
|
client.session.uniqueId = new hashids('ENiGMA½ClientSession').encode([ id, moment().valueOf() ]);
|
2018-06-04 01:58:31 +00:00
|
|
|
|
2019-01-04 04:02:21 +00:00
|
|
|
clientConnections.push(client);
|
|
|
|
clientConnections.sort( (c1, c2) => c1.session.id - c2.session.id);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// Create a client specific logger
|
|
|
|
// Note that this will be updated @ login with additional information
|
2018-06-22 05:15:04 +00:00
|
|
|
client.log = logger.log.child( { clientId : id, sessionId : client.session.uniqueId } );
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const connInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
remoteAddress : remoteAddress,
|
|
|
|
serverName : client.session.serverName,
|
|
|
|
isSecure : client.session.isSecure,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(client.log.debug()) {
|
2018-06-23 03:26:46 +00:00
|
|
|
connInfo.port = clientSock.localPort;
|
|
|
|
connInfo.family = clientSock.localFamily;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
client.log.info(connInfo, 'Client connected');
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
Events.emit(
|
|
|
|
Events.getSystemEvents().ClientConnected,
|
|
|
|
{ client : client, connectionCount : clientConnections.length }
|
|
|
|
);
|
2017-06-11 01:46:28 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return id;
|
2015-08-05 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeClient(client) {
|
2018-06-22 05:15:04 +00:00
|
|
|
client.end();
|
|
|
|
|
|
|
|
const i = clientConnections.indexOf(client);
|
|
|
|
if(i > -1) {
|
|
|
|
clientConnections.splice(i, 1);
|
|
|
|
|
|
|
|
logger.log.info(
|
|
|
|
{
|
2018-06-23 03:26:46 +00:00
|
|
|
connectionCount : clientConnections.length,
|
|
|
|
clientId : client.session.id
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
'Client disconnected'
|
|
|
|
);
|
|
|
|
|
|
|
|
if(client.user && client.user.isValid()) {
|
2019-01-18 03:13:49 +00:00
|
|
|
const minutesOnline = moment().diff(moment(client.user.properties[UserProps.LastLoginTs]), 'minutes');
|
|
|
|
Events.emit(Events.getSystemEvents().UserLogoff, { user : client.user, minutesOnline } );
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Events.emit(
|
|
|
|
Events.getSystemEvents().ClientDisconnected,
|
|
|
|
{ client : client, connectionCount : clientConnections.length }
|
|
|
|
);
|
|
|
|
}
|
2015-08-05 04:35:59 +00:00
|
|
|
}
|
2016-10-25 03:49:45 +00:00
|
|
|
|
|
|
|
function getConnectionByUserId(userId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return getActiveConnections().find( ac => userId === ac.user.userId );
|
2017-06-11 01:46:28 +00:00
|
|
|
}
|
2018-11-13 05:03:28 +00:00
|
|
|
|
|
|
|
function getConnectionByNodeId(nodeId) {
|
|
|
|
return getActiveConnections().find( ac => nodeId == ac.node );
|
|
|
|
}
|