forked from ryanblenis/MeshCentral-RoutePlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouteplus.js
446 lines (423 loc) · 18.8 KB
/
routeplus.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* @description MeshCentral RoutePlus Plugin
* @author Ryan Blenis
* @copyright
* @license Apache-2.0
*/
"use strict";
module.exports.routeplus = function (parent) {
var obj = {};
obj.parent = parent;
obj.meshServer = parent.parent;
obj.debug = obj.meshServer.debug;
obj.onlineNodes = [];
obj.VIEWS = __dirname + '/views/';
obj.exports = [
'onWebUIStartupEnd',
'openSettings',
'goPageStart',
'mapUpdate',
'resizeContent',
'onDeviceRefreshEnd',
'setUserRdpLinks',
'updateUserRdpLinks',
'dlRDPfile',
'updateRdpDeviceLinks',
'cantMap'
];
obj.server_startup = function() {
obj.meshServer.pluginHandler.routeplus_db = require (__dirname + '/db.js').CreateDB(obj.meshServer);
obj.db = obj.meshServer.pluginHandler.routeplus_db;
};
obj.onWebUIStartupEnd = function() {
var ld = document.querySelectorAll('#p2AccountActions > p.mL')[0];
var as = Q('plugin_routePlusSettings');
if (as) as.parentNode.removeChild(as);
var x = '<span id="plugin_routePlusSettings" style="display: block;"><a onclick="pluginHandler.routeplus.openSettings();">RoutePlus</a></span>';
ld.innerHTML += x;
pluginHandler.routeplus.updateUserRdpLinks();
};
obj.updateUserRdpLinks = function() {
if (userinfo == null) { setTimeout(pluginHandler.routeplus.updateUserRdpLinks, 100); return; }
meshserver.send({
'action': 'plugin',
'plugin': 'routeplus',
'pluginaction': 'getRdpLinks',
'user': userinfo._id
});
};
obj.updateRdpDeviceLinks = function() {
var links = document.querySelectorAll('.routePlusRdpLink');
if (links.length) {
links.forEach(function(l) {
l.parentNode.removeChild(l);
});
}
if (pluginHandler.routeplus.myRDPLinks != null) {
if (currentNode != null && pluginHandler.routeplus.myRDPLinks[currentNode._id] != null) {
let l = pluginHandler.routeplus.myRDPLinks[currentNode._id];
var holderC = Q('p10html3'), holder = holderC.querySelectorAll('.p10html3left')[0];
let tpl = `<a href="#" class="routePlusRdpLink" onclick="pluginHandler.routeplus.dlRDPfile('${l}', '${currentNode.name}');">RoutePlus RDP</a>`;
holder.insertAdjacentHTML( 'beforeend', tpl );
}
}
};
obj.onDeviceRefreshEnd = function() {
pluginHandler.routeplus.updateRdpDeviceLinks();
};
obj.dlRDPfile = function(port, name) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + 'full address:s:127.0.0.1:' + port);
element.setAttribute('download', encodeURIComponent(name) + '.rdp');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
};
obj.hook_userLoggedIn = function(user) {
var myComp = null;
const rcookie = parent.parent.encodeCookie({ userid: user._id, domainid: user.domain }, obj.meshServer.loginCookieEncryptionKey);
obj.debug('PLUGIN', 'RoutePlus', 'User logged in... Processing');
obj.onlineNodes = Object.keys(obj.meshServer.webserver.wsagents);
//console.log('s1', obj.meshServer.webserver.wssessions);
//console.log('s2', Object.keys(obj.meshServer.webserver.wssessions2));
obj.db.getMyComputer(user._id)
.then(mys => {
if (mys.length) {
myComp = mys[0].node;
}
return obj.db.getUserMaps(user._id);
})
.then((maps) => {
if (myComp == null) return;
obj.debug('PLUGIN', 'RoutePlus', 'Number of user maps found: ' + maps.length);
if (maps.length == 0) return;
maps.forEach(map => {
//if (obj.onlineNodes.indexOf(fromNode) === -1) return; // skip offline nodes
obj.startRoute(myComp, map, rcookie);
});
})
.catch(e => console.log('PLUGIN: RoutePlus: Error starting routes for user '+ user._id + '. Error was: ', e));
};
obj.hook_agentCoreIsStable = function(myparent, gp) { // check for remaps when an agent logs in
obj.db.getMyComputerByNode(myparent.dbNodeKey)
.then((mys) => {
if (mys.length) {
var my = mys[0];
obj.db.getUserMaps(my.user)
.then(maps => {
var onlineUsers = Object.keys(obj.meshServer.webserver.wssessions);
if (maps.length && onlineUsers.indexOf(my.user) !== -1) { // if we have a mapping and our user is online, map it
var uinfo = my.user.split('/');
var rcookie = parent.parent.encodeCookie({ userid: my.user, domainid: uinfo[1] }, obj.meshServer.loginCookieEncryptionKey);
maps.forEach(function(map) {
obj.startRoute(my.node, map, rcookie);
});
}
})
.catch(e => console.log('PLUGIN: RoutePlus: Error adding routes to agent on checkin 1: ', e));
}
})
.catch(e => console.log('PLUGIN: RoutePlus: Error adding routes to agent on checkin 2: ', e));
};
obj.startRoute = function(comp, map, rcookie) {
const command = {
action: 'plugin',
plugin: 'routeplus',
pluginaction: 'startRoute',
mid: map._id,
rauth: rcookie,
nodeid: map.toNode,
remotetarget: map.toIP,
remoteport: map.port,
localport: map.localport,
forceSrcPort: map.forceSrcPort ? map.forceSrcPort : false
};
//obj.debug('PLUGIN', 'RoutePlus', 'Mapping route to ', map.toNode);
try {
obj.debug('PLUGIN', 'RoutePlus', 'Starting route ' + map._id + ' to ' + comp);
obj.meshServer.webserver.wsagents[comp].send(JSON.stringify(command));
} catch (e) {
obj.debug('PLUGIN', 'RoutePlus', 'Could not send map to ' + comp);
}
};
obj.mapUpdate = function() {
// placeholder. If settings is never opened, updates sent to user throw console error.
};
obj.cantMap = function(e, msg) {
if (pluginHandler.routeplus.cantMapStore == null) pluginHandler.routeplus.cantMapStore = [];
pluginHandler.routeplus.cantMapStore.push(msg.mapId);
try {
pluginHandler.routeplus.cantMapIn();
} catch (e) { }
};
obj.setUserRdpLinks = function(state, msg) {
var links = msg.data;
pluginHandler.routeplus.myRDPLinks = {};
if (links.length) {
links.forEach(function(l) {
pluginHandler.routeplus.myRDPLinks[l.toNode] = l.localport;
});
}
pluginHandler.routeplus.updateRdpDeviceLinks();
};
obj.openSettings = function() {
let spage = `<div id="routePlusSettings" style="height:100%;">
<div><div class="backButton" tabindex=0 onclick="go(2);" title="Back" onkeypress="if (event.key == 'Enter') go(2);"><div class="backButtonEx"></div></div></div>
<h1>My Server Plugins - <span>RoutePlus</span></h1>
<iframe id="routePlusiframe" src="/pluginadmin.ashx?pin=routeplus" frameBorder=0 style="width:100%;height:calc(100vh - 245px);max-height:calc(100vh - 245px)"></iframe>
</div>`;
QV('p2', 0);
xxcurrentView = null;
document.getElementById('column_l').insertAdjacentHTML( 'beforeend', spage );
};
obj.goPageStart = function(pageNum, event) {
let r = Q('routePlusSettings');
if (r) r.parentNode.removeChild(r);
};
obj.resizeContent = function() {
var iFrame = document.getElementById('routePlusiframe');
var newHeight = 800;
var sHeight = iFrame.contentWindow.document.body.scrollHeight;
if (sHeight > newHeight) newHeight = sHeight;
iFrame.style.height = newHeight + 'px';
};
obj.handleAdminReq = function(req, res, user) {
if ((user.siteadmin & 0xFFFFFFFF) == 1 && req.query.admin == 1)
{
// admin wants admin, grant
var vars = {};
res.render(obj.VIEWS + 'admin', vars);
return;
} else {
var vars = {};
obj.db.getUserMaps(user._id)
.then(maps => {
if (maps.length) vars.mappings = JSON.stringify(maps);
else vars.mappings = 'null';
return obj.db.getMyComputer(user._id);
})
.then(mys => {
if (mys.length) {
vars.myComputer = JSON.stringify(mys[0]);
} else {
vars.myComputer = 'null';
}
return Promise.resolve();
})
.then(() => {
res.render(obj.VIEWS + 'user', vars);
})
.catch(e => console.log('PLUGIN: RoutePlus: Error parsing user options. ', e));
return;
}
res.sendStatus(401);
return;
};
obj.removeMapFromComp = function(id) {
var usr = null;
obj.endRoute(id)
.then(() => {
return obj.db.get(id);
})
.then((maps) => {
if (maps.length) {
usr = maps[0].user;
}
return obj.db.delete(id);
})
.then(() => obj.db.getUserMaps(usr))
.then(maps => {
var x = { action: "plugin", plugin: "routeplus", method: "mapUpdate", data: maps };
obj.sendUpdateToUser(usr, x);
return Promise.resolve();
})
.catch(e => console.log('PLUGIN: RoutePlus: Error removing map: ', e));
};
obj.endRoute = function (mapId) {
var mapRef = null;
return obj.db.get(mapId)
.then((maps) => {
mapRef = maps[0];
return obj.db.getMyComputer(maps[0].user);
})
.then((mcs) => {
var mc = mcs[0];
// destroy the user map
const cmd = {
action: 'plugin',
plugin: 'routeplus',
pluginaction: 'endRoute',
mid: mapId
};
try {
obj.debug('PLUGIN', 'RoutePlus', 'Ending route for ID ' + mapId + ' (User: ' + mapRef.user + ')');
obj.meshServer.webserver.wsagents[mc.node].send(JSON.stringify(cmd));
} catch (e) {
obj.debug('PLUGIN', 'RoutePlus', 'Could not end map route for ' + mc.node + ' (agent offline)');
}
});
};
obj.sendUpdateToUser = function(user, msg) {
if (obj.meshServer.webserver.wssessions[user] != null) {
obj.meshServer.webserver.wssessions[user].forEach(function(sess) {
obj.meshServer.webserver.wssessions2[sess.sessionId].send(JSON.stringify(msg));
});
}
};
obj.serveraction = function(command, myparent, grandparent) {
switch (command.pluginaction) {
case 'addMap':
var newMapId = null, myComp = null;
obj.db.addMap(command.user, command.toNode, command.port, command.srcport, command.forceSrcPort, command.toIP)
.then((newMapInfo) => {
newMapId = newMapInfo.insertedId;
return obj.db.getUserMaps(command.user);
})
.then(maps => {
var x = { action: "plugin", plugin: "routeplus", method: "mapUpdate", data: maps};
myparent.ws.send(JSON.stringify(x));
return obj.db.getMyComputer(command.user);
})
.then((mcs) => {
myComp = mcs[0].node;
return obj.db.get(newMapId);
})
.then((maps) => {
var uinfo = command.user.split('/');
var rcookie = parent.parent.encodeCookie({ userid: command.user, domainid: uinfo[1] }, obj.meshServer.loginCookieEncryptionKey);
obj.startRoute(myComp, maps[0], rcookie);
})
.catch(e => console.log('PLUGIN: RoutePlus: Error adding a map: ', e));
break;
case 'removeMap':
obj.removeMapFromComp(command.id);
break;
case 'userLoggedIn':
// obj.userLoggedIn();
break;
case 'setMyComputer':
// remove mappings from current computer first
obj.db.getMyComputer(command.user)
.then(mys => {
if (mys.length == 0) return Promise.resolve();
var my = mys[0];
return obj.db.getUserMaps(mys[0].user)
.then(maps => {
obj.debug('PLUGIN', 'RoutePlus', 'setMyComputer maps length: ' + maps.length);
if (maps.length == 0) return Promise.resolve();
maps.forEach(function(map) {
obj.endRoute(map._id);
});
return Promise.resolve();
})
.catch(e => console.log('PLUGIN: RoutePlus: setMyComputer user map changing failed with: ', e));
})
.then(() => {
return obj.db.setMyComputer({
user: command.user,
node: command.node
});
})
.then(() => { // update new MyComputer with mappings
return obj.db.getUserMaps(command.user)
.then(maps => {
if (maps.length == 0) return Promise.resolve();
var uinfo = command.user.split('/');
var rcookie = parent.parent.encodeCookie({ userid: command.user, domainid: uinfo[1] }, obj.meshServer.loginCookieEncryptionKey);
maps.forEach(function(map) {
obj.startRoute(command.node, map, rcookie);
});
return Promise.resolve();
})
.catch(e => console.log('PLUGIN: RoutePlus: setMyComputer user map adding failed with: ', e));
})
.then(() => { // update front end
obj.db.getMyComputer(command.user)
.then(rows => {
if (rows.length) {
var x = { action: "plugin", plugin: "routeplus", method: "myComputerUpdate", data: rows[0]};
myparent.ws.send(JSON.stringify(x));
}
})
});
break;
case 'updateMapPort':
obj.debug('PLUGIN', 'RoutePlus', 'Updating mapped port for ' + command.mid + ' to ' + command.port);
var upUser = null;
obj.db.update(command.mid, { localport: command.port })
.then(() => {
return obj.db.get(command.mid);
})
.then((mObj) => {
mObj = mObj[0];
upUser = mObj.user;
return obj.db.getUserMaps(mObj.user);
})
.then(maps => {
var x = { action: "plugin", plugin: "routeplus", method: "mapUpdate", data: maps};
//myparent.ws.send(JSON.stringify(x));
//obj.debug('PLUGIN', 'RoutePlus', 'Checking for session for '+ maps[0].user);
// send the port to the RoutePlus settings screen
obj.sendUpdateToUser(maps[0].user, x);
//console.log('s1', Object.keys(obj.meshServer.webserver.wssessions));
//var targets = ['*', 'server-users'];
//obj.meshServer.DispatchEvent(targets, obj, { action: 'plugin', plugin: 'routeplus', pluginaction: 'mapUpdate', data: maps });
})
.then(() => {
return obj.db.getRdpLinksForUser(upUser)
})
.then(links => {
var x = { action: "plugin", plugin: "routeplus", method: "setUserRdpLinks", data: links};
obj.sendUpdateToUser(upUser, x);
})
.catch(e => console.log('PLUGIN: RoutePlus: Error updating mapped port: ', e));
break;
case 'cantMapPort':
obj.debug('PLUGIN', 'RoutePlus', 'Client cannot map forced port for id: ' + command.mid);
obj.db.get(command.mid)
.then(maps => {
var x = { action: "plugin", plugin: "routeplus", method: "cantMap", mapId: maps[0]._id };
obj.sendUpdateToUser(maps[0].user, x);
})
.catch(e => console.log('PLUGIN: RoutePlus: CantMap Reporting Error: ', e));
break;
case 'rdpLinkUpdate':
var upUser = null;
obj.db.update(command.mid, { rdplink: command.rdplink })
.then(() => {
return obj.db.get(command.mid);
})
.then((mObj) => {
mObj = mObj[0];
upUser = mObj.user;
return obj.db.getUserMaps(mObj.user);
})
.then(maps => {
var x = { action: "plugin", plugin: "routeplus", method: "mapUpdate", data: maps};
obj.sendUpdateToUser(maps[0].user, x);
})
.then(() => {
return obj.db.getRdpLinksForUser(upUser)
})
.then(links => {
var x = { action: "plugin", plugin: "routeplus", method: "setUserRdpLinks", data: links};
obj.sendUpdateToUser(upUser, x);
})
.catch(e => console.log('PLUGIN: RoutePlus: Error updating RDP link setting: ', e));
break;
case 'getRdpLinks':
obj.db.getRdpLinksForUser(command.user)
.then(links => {
var x = { action: "plugin", plugin: "routeplus", method: "setUserRdpLinks", data: links};
obj.sendUpdateToUser(command.user, x);
})
.catch(e => console.log('PLUGIN: RoutePlus: Error updating RDP link maps: ', e));
break;
default:
console.log('PLUGIN: RoutePlus: unknown action');
break;
}
};
return obj;
}