2015-08-05 04:35:59 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +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
|
2022-06-05 20:04:25 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
|
|
|
const hashids = require('hashids/cjs');
|
2016-01-30 22:18:55 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.getActiveConnections = getActiveConnections;
|
2018-11-14 03:03:54 +00:00
|
|
|
exports.getActiveConnectionList = getActiveConnectionList;
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.addNewClient = addNewClient;
|
|
|
|
exports.removeClient = removeClient;
|
|
|
|
exports.getConnectionByUserId = getConnectionByUserId;
|
|
|
|
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 => {
|
2022-06-05 20:04:25 +00:00
|
|
|
return (authUsersOnly && conn.user.isAuthenticated()) || !authUsersOnly;
|
2018-11-13 05:03:28 +00:00
|
|
|
});
|
|
|
|
}
|
2015-10-18 02:03:51 +00:00
|
|
|
|
2018-11-14 03:03:54 +00:00
|
|
|
function getActiveConnectionList(authUsersOnly) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isBoolean(authUsersOnly)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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 = {
|
2022-06-05 20:04:25 +00:00
|
|
|
node: ac.node,
|
|
|
|
authenticated: ac.user.isAuthenticated(),
|
|
|
|
userId: ac.user.userId,
|
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (ac.user.isAuthenticated()) {
|
|
|
|
entry.userName = ac.user.username;
|
|
|
|
entry.realName = ac.user.properties[UserProps.RealName];
|
|
|
|
entry.location = ac.user.properties[UserProps.Location];
|
|
|
|
entry.affils = entry.affiliation = ac.user.properties[UserProps.Affiliations];
|
|
|
|
|
|
|
|
const diff = now.diff(
|
|
|
|
moment(ac.user.properties[UserProps.LastLoginTs]),
|
|
|
|
'minutes'
|
|
|
|
);
|
|
|
|
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
|
|
|
//
|
2020-05-14 01:30:57 +00:00
|
|
|
// Find a node ID "slot"
|
2019-01-04 04:02:21 +00:00
|
|
|
//
|
2020-05-14 01:30:57 +00:00
|
|
|
let nodeId;
|
|
|
|
for (nodeId = 1; nodeId < Number.MAX_SAFE_INTEGER; ++nodeId) {
|
|
|
|
const existing = clientConnections.find(client => nodeId === client.node);
|
|
|
|
if (!existing) {
|
2022-06-05 20:04:25 +00:00
|
|
|
break; // available slot
|
2019-01-04 04:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2020-05-14 01:30:57 +00:00
|
|
|
client.session.id = nodeId;
|
2022-06-05 20:04:25 +00:00
|
|
|
const remoteAddress = (client.remoteAddress = clientSock.remoteAddress);
|
2018-11-24 00:41:16 +00:00
|
|
|
// create a unique identifier one-time ID for this session
|
2022-06-05 20:04:25 +00:00
|
|
|
client.session.uniqueId = new hashids('ENiGMA½ClientSession').encode([
|
|
|
|
nodeId,
|
|
|
|
moment().valueOf(),
|
|
|
|
]);
|
2018-06-04 01:58:31 +00:00
|
|
|
|
2019-01-04 04:02:21 +00:00
|
|
|
clientConnections.push(client);
|
2022-06-05 20:04:25 +00:00
|
|
|
clientConnections.sort((c1, c2) => c1.session.id - c2.session.id);
|
2019-01-04 04:02:21 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// Create a client specific logger
|
|
|
|
// Note that this will be updated @ login with additional information
|
2022-06-05 20:04:25 +00:00
|
|
|
client.log = logger.log.child({ nodeId, sessionId: client.session.uniqueId });
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const connInfo = {
|
2022-06-05 20:04:25 +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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (client.log.debug()) {
|
|
|
|
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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Events.emit(Events.getSystemEvents().ClientConnected, {
|
|
|
|
client: client,
|
|
|
|
connectionCount: clientConnections.length,
|
|
|
|
});
|
2017-06-11 01:46:28 +00:00
|
|
|
|
2020-05-14 01:30:57 +00:00
|
|
|
return nodeId;
|
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);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (i > -1) {
|
2018-06-22 05:15:04 +00:00
|
|
|
clientConnections.splice(i, 1);
|
|
|
|
|
|
|
|
logger.log.info(
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
connectionCount: clientConnections.length,
|
|
|
|
nodeId: client.node,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
'Client disconnected'
|
|
|
|
);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (client.user && client.user.isValid()) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Events.emit(Events.getSystemEvents().ClientDisconnected, {
|
|
|
|
client: client,
|
|
|
|
connectionCount: clientConnections.length,
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-08-05 04:35:59 +00:00
|
|
|
}
|
2016-10-25 03:49:45 +00:00
|
|
|
|
|
|
|
function getConnectionByUserId(userId) {
|
2022-06-05 20:04:25 +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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return getActiveConnections().find(ac => nodeId == ac.node);
|
2018-11-13 05:03:28 +00:00
|
|
|
}
|