2015-08-05 04:35:59 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-08-27 02:53:27 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const logger = require('./logger.js');
|
2017-06-23 04:19:34 +00:00
|
|
|
const Events = require('./events.js');
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2016-08-27 02:53:27 +00:00
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
2018-06-04 01:58:31 +00:00
|
|
|
const hashids = require('hashids');
|
2016-01-30 22:18:55 +00:00
|
|
|
|
2015-10-18 02:03:51 +00:00
|
|
|
exports.getActiveConnections = getActiveConnections;
|
2016-01-30 22:18:55 +00:00
|
|
|
exports.getActiveNodeList = getActiveNodeList;
|
2015-08-05 04:35:59 +00:00
|
|
|
exports.addNewClient = addNewClient;
|
|
|
|
exports.removeClient = removeClient;
|
2016-10-25 03:49:45 +00:00
|
|
|
exports.getConnectionByUserId = getConnectionByUserId;
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2016-08-27 02:53:27 +00:00
|
|
|
const clientConnections = [];
|
2015-08-05 04:35:59 +00:00
|
|
|
exports.clientConnections = clientConnections;
|
|
|
|
|
2016-08-27 03:28:17 +00:00
|
|
|
function getActiveConnections() { return clientConnections; }
|
2015-10-18 02:03:51 +00:00
|
|
|
|
2016-08-27 02:53:27 +00:00
|
|
|
function getActiveNodeList(authUsersOnly) {
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!_.isBoolean(authUsersOnly)) {
|
|
|
|
authUsersOnly = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const now = moment();
|
|
|
|
|
|
|
|
const activeConnections = getActiveConnections().filter(ac => {
|
|
|
|
return ((authUsersOnly && ac.user.isAuthenticated()) || !authUsersOnly);
|
|
|
|
});
|
|
|
|
|
|
|
|
return _.map(activeConnections, ac => {
|
|
|
|
const entry = {
|
|
|
|
node : ac.node,
|
|
|
|
authenticated : ac.user.isAuthenticated(),
|
|
|
|
userId : ac.user.userId,
|
|
|
|
action : _.has(ac, 'currentMenuModule.menuConfig.desc') ? ac.currentMenuModule.menuConfig.desc : 'Unknown',
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// There may be a connection, but not a logged in user as of yet
|
|
|
|
//
|
|
|
|
if(ac.user.isAuthenticated()) {
|
|
|
|
entry.userName = ac.user.username;
|
|
|
|
entry.realName = ac.user.properties.real_name;
|
|
|
|
entry.location = ac.user.properties.location;
|
|
|
|
entry.affils = ac.user.properties.affiliation;
|
|
|
|
|
|
|
|
const diff = now.diff(moment(ac.user.properties.last_login_timestamp), 'minutes');
|
|
|
|
entry.timeOn = moment.duration(diff, 'minutes');
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
});
|
2016-01-30 22:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 21:39:33 +00:00
|
|
|
function addNewClient(client, clientSock) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const id = client.session.id = clientConnections.push(client) - 1;
|
|
|
|
const remoteAddress = client.remoteAddress = clientSock.remoteAddress;
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
// create a uniqe identifier one-time ID for this session
|
|
|
|
client.session.uniqueId = new hashids('ENiGMA½ClientSession').encode([ id, moment().valueOf() ]);
|
2018-06-04 01:58:31 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
// 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 } );
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const connInfo = {
|
|
|
|
remoteAddress : remoteAddress,
|
|
|
|
serverName : client.session.serverName,
|
|
|
|
isSecure : client.session.isSecure,
|
|
|
|
};
|
2015-08-05 04:35:59 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(client.log.debug()) {
|
|
|
|
connInfo.port = clientSock.localPort;
|
|
|
|
connInfo.family = clientSock.localFamily;
|
|
|
|
}
|
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(
|
|
|
|
{
|
|
|
|
connectionCount : clientConnections.length,
|
|
|
|
clientId : client.session.id
|
|
|
|
},
|
|
|
|
'Client disconnected'
|
|
|
|
);
|
|
|
|
|
|
|
|
if(client.user && client.user.isValid()) {
|
|
|
|
Events.emit(Events.getSystemEvents().UserLogoff, { user : client.user } );
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|