Fixed agent desktop/terminal pipes + added IP location info store in the server

Ylian Saint-Hilaire committed Sep 6, 2017 at 10:08 UTC c3005f47703b9bb56dc74998ea0d9f3bc5e1575f
4 files changed +46 -30
agents/meshcore.js
+4 -6
@@ -373,10 +373,8 @@ function createMeshCore(agent) {
373 return;
374 }
375 // Setup remote desktop & terminal without using native pipes
376 - if (useNativePipes == false) {
377 - if (this.httprequest.desktop) { this.httprequest.desktop.kvm.write(data); return; }
378 - if (this.httprequest.terminal) { this.httprequest.terminal.write(data); return; }
379 - }
376 + if ((this.httprequest.desktop) && (obj.useNativePipes == false)) { this.httprequest.desktop.kvm.write(data); return; }
377 + if ((this.httprequest.terminal) && (obj.useNativePipes == false)) { this.httprequest.terminal.write(data); return; }
378
379 if (this.httprequest.state == 0) {
380 // Check if this is a relay connection
@@ -388,7 +386,7 @@ function createMeshCore(agent) {
386 this.httprequest.protocol = parseInt(data);
387 if (typeof this.httprequest.protocol != 'number') { this.httprequest.protocol = 0; }
388 if (this.httprequest.protocol == 1) {
391 - if (useNativePipes == false) {
389 + if (obj.useNativePipes == false) {
390 // Remote Terminal without using native pipes
391 if (process.platform == "win32") {
392 this.httprequest.terminal = processManager.CreateProcess("%windir%\\system32\\cmd.exe");
@@ -412,7 +410,7 @@ function createMeshCore(agent) {
410 }
411 }
412 if (this.httprequest.protocol == 2) {
415 - if (useNativePipes == false) {
413 + if (obj.useNativePipes == false) {
414 // Remote Desktop without using native pipes
415 this.httprequest.desktop = { state: 0, kvm: mesh.getRemoteDesktopStream(), tunnel: this };
416 this.httprequest.desktop.kvm.tunnel = this;
meshagent.js
+39 -22
@@ -294,31 +294,42 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
294 obj.agentInfo = obj.parent.parent.meshAgentBinaries[obj.agentInfo.agentId];
295 if ((obj.agentInfo != undefined) && (obj.agentInfo.update == true)) { obj.send(obj.common.ShortToStr(12) + obj.common.ShortToStr(0)); } // Ask the agent for it's executable binary hash
296
297 - // Check if we need to ask for the IP location
298 - var doIpLocation = 0;
299 - if (device.iploc == null) {
300 - doIpLocation = 1;
301 - } else {
302 - var loc = device.iploc.split(',');
303 - if (loc.length < 3) {
304 - doIpLocation = 2;
297 + // Check if we already have IP location information for this node
298 + obj.db.Get('iploc_' + obj.remoteaddr, function (err, iplocs) {
299 + if (iplocs.length == 1) {
300 + // We have a location in the database for this remote IP
301 + var iploc = nodes[0], x = {};
302 + x.publicip = iploc.ip;
303 + x.iploc = iploc.loc + ',' + (Math.floor((new Date(command.value.date)) / 1000));
304 + ChangeAgentLocationInfo(x);
305 } else {
306 - var t = new Date((parseFloat(loc[2]) * 1000)), now = Date.now();
307 - t.setDate(t.getDate() + 20);
308 - if (t < now) { doIpLocation = 3; }
309 - }
310 - }
306 + // Check if we need to ask for the IP location
307 + var doIpLocation = 0;
308 + if (device.iploc == null) {
309 + doIpLocation = 1;
310 + } else {
311 + var loc = device.iploc.split(',');
312 + if (loc.length < 3) {
313 + doIpLocation = 2;
314 + } else {
315 + var t = new Date((parseFloat(loc[2]) * 1000)), now = Date.now();
316 + t.setDate(t.getDate() + 20);
317 + if (t < now) { doIpLocation = 3; }
318 + }
319 + }
320
312 - // If we need to ask for IP location, see if we have the quota to do it.
313 - if (doIpLocation > 0) {
314 - obj.db.getValueOfTheDay('ipLocationRequestLimitor', 10, function (ipLocationLimitor) {
315 - if (ipLocationLimitor.value > 0) {
316 - ipLocationLimitor.value--;
317 - obj.db.Set(ipLocationLimitor);
318 - obj.send(JSON.stringify({ action: 'iplocation' }));
321 + // If we need to ask for IP location, see if we have the quota to do it.
322 + if (doIpLocation > 0) {
323 + obj.db.getValueOfTheDay('ipLocationRequestLimitor', 10, function (ipLocationLimitor) {
324 + if (ipLocationLimitor.value > 0) {
325 + ipLocationLimitor.value--;
326 + obj.db.Set(ipLocationLimitor);
327 + obj.send(JSON.stringify({ action: 'iplocation' }));
328 + }
329 + });
330 }
320 - });
321 - }
331 + }
332 + });
333 });
334 });
335 }
@@ -425,11 +436,17 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
436 case 'iplocation':
437 {
438 // Sent by the agent to update location information
439 + console.log(command);
440 if ((command.type == 'publicip') && (command.value != null) && (typeof command.value == 'object') && (command.value.ip) && (command.value.loc)) {
441 var x = {};
442 x.publicip = command.value.ip;
443 x.iploc = command.value.loc + ',' + (Math.floor(Date.now() / 1000) );
444 ChangeAgentLocationInfo(x);
445 + command.value._id = 'iploc_' + command.value.ip;
446 + command.value.type = 'iploc';
447 + command.value.date = Date.now();
448 + obj.db.Set(command.value); // Store the IP to location data in the database
449 + // Sample Value: { ip: '192.55.64.246', city: 'Hillsboro', region: 'Oregon', country: 'US', loc: '45.4443,-122.9663', org: 'AS4983 Intel Corporation', postal: '97123' }
450 }
451 break;
452 }
meshcentral.js
+2 -1
@@ -54,7 +54,7 @@ function CreateMeshCentralServer() {
54 try { require('./pass').hash('test', function () { }); } catch (e) { console.log('Old version of node, must upgrade.'); return; } // TODO: Not sure if this test works or not.
55
56 // Check for invalid arguments
57 - var validArguments = ['_', 'notls', 'user', 'port', 'mpsport', 'redirport', 'cert', 'deletedomain', 'deletedefaultdomain', 'showusers', 'shownodes', 'showmeshes', 'showevents', 'showpower', 'help', 'exactports', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpsdebug', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbimport'];
57 + var validArguments = ['_', 'notls', 'user', 'port', 'mpsport', 'redirport', 'cert', 'deletedomain', 'deletedefaultdomain', 'showusers', 'shownodes', 'showmeshes', 'showevents', 'showpower', 'showiplocations', 'help', 'exactports', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpsdebug', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbimport'];
58 for (var arg in obj.args) { if (validArguments.indexOf(arg.toLocaleLowerCase()) == -1) { console.log('Invalid argument "' + arg + '", use --help.'); return; } }
59 if (obj.args.mongodb == true) { console.log('Must specify: --mongodb [connectionstring] \r\nSee https://docs.mongodb.com/manual/reference/connection-string/ for MongoDB connection string.'); return; }
60
@@ -208,6 +208,7 @@ function CreateMeshCentralServer() {
208 if (obj.args.showmeshes) { obj.db.GetAllType('mesh', function (err, docs) { console.log(docs); process.exit(); }); return; }
209 if (obj.args.showevents) { obj.db.GetAllType('event', function (err, docs) { console.log(docs); process.exit(); }); return; }
210 if (obj.args.showpower) { obj.db.GetAllType('power', function (err, docs) { console.log(docs); process.exit(); }); return; }
211 + if (obj.args.showiplocations) { obj.db.GetAllType('iploc', function (err, docs) { console.log(docs); process.exit(); }); return; }
212 if (obj.args.dbexport) {
213 // Export the entire database to a JSON file
214 if (obj.args.dbexport == true) { console.log('Use --dbexport [filename]'); process.exit(); } else { obj.db.GetAll(function (err, docs) { obj.fs.writeFileSync(obj.args.dbexport, JSON.stringify(docs)); console.log('Exported ' + docs.length + ' document(s).'); process.exit(); }); }
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.0.7-f",
3 + "version": "0.0.7-g",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",