-
Notifications
You must be signed in to change notification settings - Fork 104
/
client_connections.js
208 lines (176 loc) · 6.3 KB
/
client_connections.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* jslint node: true */
'use strict';
// ENiGMA½
const logger = require('./logger.js');
const Events = require('./events.js');
const UserProps = require('./user_property.js');
// deps
const _ = require('lodash');
const moment = require('moment');
const hashids = require('hashids/cjs');
exports.getActiveConnections = getActiveConnections;
exports.getActiveConnectionList = getActiveConnectionList;
exports.addNewClient = addNewClient;
exports.removeClient = removeClient;
exports.getConnectionByUserId = getConnectionByUserId;
exports.getConnectionByNodeId = getConnectionByNodeId;
const clientConnections = [];
exports.clientConnections = clientConnections;
const AllConnections = { authUsersOnly: false, visibleOnly: false, availOnly: false };
exports.AllConnections = AllConnections;
const UserVisibleConnections = {
authUsersOnly: false,
visibleOnly: true,
availOnly: false,
};
exports.UserVisibleConnections = UserVisibleConnections;
const UserMessageableConnections = {
authUsersOnly: true,
visibleOnly: true,
availOnly: true,
};
exports.UserMessageableConnections = UserMessageableConnections;
function getActiveConnections(
options = { authUsersOnly: true, visibleOnly: true, availOnly: false }
) {
return clientConnections.filter(conn => {
if (options.authUsersOnly && !conn.user.isAuthenticated()) {
return false;
}
if (options.visibleOnly && !conn.user.isVisible()) {
return false;
}
if (options.availOnly && !conn.user.isAvailable()) {
return false;
}
return true;
});
}
function getActiveConnectionList(
options = { authUsersOnly: true, visibleOnly: true, availOnly: false }
) {
const now = moment();
return _.map(getActiveConnections(options), ac => {
let action;
try {
// attempting to fetch a bad menu stack item can blow up/assert
action = _.get(ac, 'currentMenuModule.menuConfig.desc', 'Unknown');
} catch (e) {
action = 'Unknown';
}
const entry = {
node: ac.node,
authenticated: ac.user.isAuthenticated(),
userId: ac.user.userId,
action: action,
serverName: ac.session.serverName,
isSecure: ac.session.isSecure,
isVisible: ac.user.isVisible(),
isAvailable: ac.user.isAvailable(),
remoteAddress: ac.friendlyRemoteAddress(),
};
//
// There may be a connection, but not a logged in user as of yet
//
entry.text = ac.user?.username || 'N/A';
entry.userName = ac.user?.username || 'N/A';
entry.realName = ac.user?.getProperty(UserProps.RealName) || 'N/A';
entry.location = ac.user?.getProperty(UserProps.Location) || 'N/A';
entry.affils = entry.affiliation =
ac.user?.getProperty(UserProps.Affiliations) || 'N/A';
if (ac.user.isAuthenticated()) {
// :TODO: track pre-auth time so we can properly track this
const diff = now.diff(
moment(ac.user.properties[UserProps.LastLoginTs]),
'minutes'
);
entry.timeOn = moment.duration(diff, 'minutes');
}
return entry;
});
}
function addNewClient(client, clientSock) {
//
// Find a node ID "slot"
//
let nodeId;
for (nodeId = 1; nodeId < Number.MAX_SAFE_INTEGER; ++nodeId) {
const existing = clientConnections.find(client => nodeId === client.node);
if (!existing) {
break; // available slot
}
}
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([
nodeId,
moment().valueOf(),
]);
// kludge to refresh process update stats at first client
if (clientConnections.length < 1) {
setTimeout(() => {
const StatLog = require('./stat_log');
const SysProps = require('./system_property');
StatLog.getSystemStat(SysProps.ProcessTrafficStats);
}, 3000); // slight pause to wait for updates
}
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({ nodeId, sessionId: client.session.uniqueId });
const connInfo = {
remoteAddress: remoteAddress,
friendlyRemoteAddress: client.friendlyRemoteAddress(),
serverName: client.session.serverName,
isSecure: client.session.isSecure,
};
if (client.log.debug()) {
connInfo.port = clientSock.localPort;
connInfo.family = clientSock.localFamily;
}
client.log.info(
connInfo,
`Client connected on node ${nodeId} (${connInfo.serverName}/${connInfo.port})`
);
Events.emit(Events.getSystemEvents().ClientConnected, {
client: client,
connectionCount: clientConnections.length,
});
return nodeId;
}
function removeClient(client) {
client.end();
const i = clientConnections.indexOf(client);
if (i > -1) {
clientConnections.splice(i, 1);
logger.log.info(
{
connectionCount: clientConnections.length,
nodeId: client.node,
},
`Client disconnected from node ${client.node}`
);
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,
});
}
Events.emit(Events.getSystemEvents().ClientDisconnected, {
client: client,
connectionCount: clientConnections.length,
});
}
}
function getConnectionByUserId(userId) {
return getActiveConnections(AllConnections).find(ac => userId === ac.user.userId);
}
function getConnectionByNodeId(nodeId) {
return getActiveConnections(AllConnections).find(ac => nodeId == ac.node);
}