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');
|
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) {
|
|
|
|
|
|
|
|
if(!_.isBoolean(authUsersOnly)) {
|
2017-06-11 01:46:28 +00:00
|
|
|
authUsersOnly = true;
|
2016-08-27 02:53:27 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 22:18:55 +00:00
|
|
|
const now = moment();
|
2016-08-27 02:53:27 +00:00
|
|
|
|
|
|
|
const activeConnections = getActiveConnections().filter(ac => {
|
|
|
|
return ((authUsersOnly && ac.user.isAuthenticated()) || !authUsersOnly);
|
|
|
|
});
|
2017-06-11 01:46:28 +00:00
|
|
|
|
2016-08-27 02:53:27 +00:00
|
|
|
return _.map(activeConnections, ac => {
|
|
|
|
const entry = {
|
2016-01-30 22:18:55 +00:00
|
|
|
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
|
2017-06-11 01:46:28 +00:00
|
|
|
//
|
2016-01-30 22:18:55 +00:00
|
|
|
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');
|
2017-06-11 01:46:28 +00:00
|
|
|
entry.timeOn = moment.duration(diff, 'minutes');
|
2016-01-30 22:18:55 +00:00
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-20 21:39:33 +00:00
|
|
|
function addNewClient(client, clientSock) {
|
2017-02-18 16:37:22 +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
|
|
|
|
2017-06-11 01:46:28 +00:00
|
|
|
// Create a client specific logger
|
2016-01-02 04:17:13 +00:00
|
|
|
// Note that this will be updated @ login with additional information
|
2015-08-05 04:35:59 +00:00
|
|
|
client.log = logger.log.child( { clientId : id } );
|
|
|
|
|
2016-08-27 02:53:27 +00:00
|
|
|
const connInfo = {
|
2017-02-18 16:37:22 +00:00
|
|
|
remoteAddress : remoteAddress,
|
|
|
|
serverName : client.session.serverName,
|
|
|
|
isSecure : client.session.isSecure,
|
2015-10-22 04:51:35 +00:00
|
|
|
};
|
2015-08-05 04:35:59 +00:00
|
|
|
|
|
|
|
if(client.log.debug()) {
|
2015-10-20 21:39:33 +00:00
|
|
|
connInfo.port = clientSock.localPort;
|
|
|
|
connInfo.family = clientSock.localFamily;
|
2015-08-05 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client.log.info(connInfo, 'Client connected');
|
|
|
|
|
2017-06-23 04:19:34 +00:00
|
|
|
Events.emit('codes.l33t.enigma.system.connected', { client : client, connectionCount : clientConnections.length } );
|
2017-06-11 01:46:28 +00:00
|
|
|
|
2015-08-05 04:35:59 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeClient(client) {
|
|
|
|
client.end();
|
|
|
|
|
2016-08-27 02:53:27 +00:00
|
|
|
const i = clientConnections.indexOf(client);
|
2015-08-05 04:35:59 +00:00
|
|
|
if(i > -1) {
|
|
|
|
clientConnections.splice(i, 1);
|
2017-06-11 01:46:28 +00:00
|
|
|
|
2015-08-05 04:35:59 +00:00
|
|
|
logger.log.info(
|
2017-06-11 01:46:28 +00:00
|
|
|
{
|
2015-08-05 04:35:59 +00:00
|
|
|
connectionCount : clientConnections.length,
|
2017-06-11 01:46:28 +00:00
|
|
|
clientId : client.session.id
|
|
|
|
},
|
2015-08-05 04:35:59 +00:00
|
|
|
'Client disconnected'
|
|
|
|
);
|
2017-06-11 01:46:28 +00:00
|
|
|
|
2017-06-23 04:19:34 +00:00
|
|
|
Events.emit('codes.l33t.enigma.system.disconnected', { client : client, connectionCount : clientConnections.length } );
|
2015-08-05 04:35:59 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-25 03:49:45 +00:00
|
|
|
|
|
|
|
function getConnectionByUserId(userId) {
|
|
|
|
return getActiveConnections().find( ac => userId === ac.user.userId );
|
2017-06-11 01:46:28 +00:00
|
|
|
}
|