Server peering fixes.
Ylian Saint-Hilaire committed
Nov 12, 2020 at 13:16 UTC
e0977eac2261af6f1c89b0718800811b4ef689ab
3 files changed
+65
-48
meshcentral.js
+2
-1
@@ -138,7 +138,7 @@ function CreateMeshCentralServer(config, args) {
138
try { require('./pass').hash('test', function () { }, 0); } catch (e) { console.log('Old version of node, must upgrade.'); return; } // TODO: Not sure if this test works or not.
139
140
// Check for invalid arguments
141
- var validArguments = ['_', 'user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'rediraliasport', 'cert', 'mpscert', 'deletedomain', 'deletedefaultdomain', 'showall', 'showusers', 'showitem', 'listuserids', 'showusergroups', 'shownodes', 'showallmeshes', 'showmeshes', 'showevents', 'showsmbios', 'showpower', 'clearpower', 'showiplocations', 'help', 'exactports', 'xinstall', 'xuninstall', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbexportmin', 'dbimport', 'dbmerge', 'dbfix', 'dbencryptkey', 'selfupdate', 'tlsoffload', 'userallowedip', 'userblockedip', 'swarmallowedip', 'agentallowedip', 'agentblockedip', 'fastcert', 'swarmport', 'logintoken', 'logintokenkey', 'logintokengen', 'mailtokengen', 'admin', 'unadmin', 'sessionkey', 'sessiontime', 'minify', 'minifycore', 'dblistconfigfiles', 'dbshowconfigfile', 'dbpushconfigfiles', 'dbpullconfigfiles', 'dbdeleteconfigfiles', 'vaultpushconfigfiles', 'vaultpullconfigfiles', 'vaultdeleteconfigfiles', 'configkey', 'loadconfigfromdb', 'npmpath', 'serverid', 'recordencryptionrecode', 'vault', 'token', 'unsealkey', 'name', 'log', 'dbstats', 'translate', 'createaccount', 'resetaccount', 'pass', 'adminaccount', 'removeaccount', 'domain', 'email'];
141
+ var validArguments = ['_', 'user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'rediraliasport', 'cert', 'mpscert', 'deletedomain', 'deletedefaultdomain', 'showall', 'showusers', 'showitem', 'listuserids', 'showusergroups', 'shownodes', 'showallmeshes', 'showmeshes', 'showevents', 'showsmbios', 'showpower', 'clearpower', 'showiplocations', 'help', 'exactports', 'xinstall', 'xuninstall', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbexportmin', 'dbimport', 'dbmerge', 'dbfix', 'dbencryptkey', 'selfupdate', 'tlsoffload', 'userallowedip', 'userblockedip', 'swarmallowedip', 'agentallowedip', 'agentblockedip', 'fastcert', 'swarmport', 'logintoken', 'logintokenkey', 'logintokengen', 'mailtokengen', 'admin', 'unadmin', 'sessionkey', 'sessiontime', 'minify', 'minifycore', 'dblistconfigfiles', 'dbshowconfigfile', 'dbpushconfigfiles', 'dbpullconfigfiles', 'dbdeleteconfigfiles', 'vaultpushconfigfiles', 'vaultpullconfigfiles', 'vaultdeleteconfigfiles', 'configkey', 'loadconfigfromdb', 'npmpath', 'serverid', 'recordencryptionrecode', 'vault', 'token', 'unsealkey', 'name', 'log', 'dbstats', 'translate', 'createaccount', 'resetaccount', 'pass', 'adminaccount', 'removeaccount', 'domain', 'email', 'configfile'];
142
for (var arg in obj.args) { obj.args[arg.toLocaleLowerCase()] = obj.args[arg]; if (validArguments.indexOf(arg.toLocaleLowerCase()) == -1) { console.log('Invalid argument "' + arg + '", use --help.'); return; } }
143
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; }
144
for (i in obj.config.settings) { obj.args[i] = obj.config.settings[i]; } // Place all settings into arguments, arguments have already been placed into settings so arguments take precedence.
@@ -2621,6 +2621,7 @@ function getConfig(createSampleConfig) {
2621
2622
// Read configuration file if present and change arguments.
2623
var config = {}, configFilePath = path.join(datapath, 'config.json');
2624
+ if (args.configfile) { configFilePath = path.join(datapath, args.configfile); }
2625
if (fs.existsSync(configFilePath)) {
2626
// Load and validate the configuration file
2627
try { config = require(configFilePath); } catch (e) { console.log('ERROR: Unable to parse ' + configFilePath + '.'); return null; }
meshdevicefile.js
+62
-46
@@ -121,7 +121,7 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us
121
};
122
123
function performRelay() {
124
- if (obj.id == null) { try { obj.close(); } catch (e) { } return null; } // Attempt to connect without id, drop this.
124
+ if (obj.id == null) { try { obj.close(); } catch (e) { } return; } // Attempt to connect without id, drop this.
125
if (obj.ws != null) { obj.ws._socket.setKeepAlive(true, 240000); } // Set TCP keep alive
126
127
// Check the peer connection status
@@ -137,7 +137,7 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us
137
delete obj.id;
138
delete obj.ws;
139
delete obj.peer;
140
- return null;
140
+ return;
141
}
142
143
// Connect to peer
@@ -160,9 +160,9 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us
160
parent.parent.debug('relay', 'FileRelay connected: ' + obj.id + ' (' + obj.req.clientIp + ' --> ' + obj.peer.req.clientIp + ')');
161
162
// Log the connection
163
- if (sessionUser != null) {
164
- var event = { etype: 'relay', action: 'relaylog', domain: domain.id, userid: sessionUser._id, username: sessionUser.name, msg: "Started file transfer session" + ' \"' + obj.id + '\" from ' + obj.peer.req.clientIp + ' to ' + req.clientIp, protocol: req.query.p, nodeid: req.query.nodeid };
165
- parent.parent.DispatchEvent(['*', sessionUser._id], obj, event);
163
+ if (obj.user != null) {
164
+ var event = { etype: 'relay', action: 'relaylog', domain: domain.id, userid: obj.user._id, username: obj.user.name, msg: "Started file transfer session" + ' \"' + obj.id + '\" from ' + obj.peer.req.clientIp + ' to ' + req.clientIp, protocol: req.query.p, nodeid: req.query.nodeid };
165
+ parent.parent.DispatchEvent(['*', obj.user._id], obj, event);
166
}
167
} else {
168
// Connected already, drop this connection.
@@ -171,60 +171,76 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us
171
delete obj.id;
172
delete obj.ws;
173
delete obj.peer;
174
- return null;
174
+ return;
175
}
176
} else {
177
// Wait for other relay connection
178
parent.wsrelays[obj.id] = { peer1: obj, state: 1, timeout: setTimeout(closeBothSides, 30000) };
179
parent.parent.debug('relay', 'FileRelay holding: ' + obj.id + ' (' + obj.req.clientIp + ') ' + (obj.authenticated ? 'Authenticated' : ''));
180
-
181
- // Check if a peer server has this connection
182
- if (parent.parent.multiServer != null) {
183
- var rsession = parent.wsPeerRelays[obj.id];
184
- if ((rsession != null) && (rsession.serverId > parent.parent.serverId)) {
185
- // We must initiate the connection to the peer
186
- parent.parent.multiServer.createPeerRelay(ws, req, rsession.serverId, obj.req.session.userid);
187
- delete parent.wsrelays[obj.id];
188
- } else {
189
- // Send message to other peers that we have this connection
190
- parent.parent.multiServer.DispatchMessage(JSON.stringify({ action: 'relay', id: obj.id }));
180
+ if (obj.ws != null) {
181
+ // Websocket connection
182
+ obj.ws._socket.pause();
183
+
184
+ // Check if a peer server has this connection
185
+ if (parent.parent.multiServer != null) {
186
+ var rsession = parent.wsPeerRelays[obj.id];
187
+ if ((rsession != null) && (rsession.serverId > parent.parent.serverId)) {
188
+ // We must initiate the connection to the peer
189
+ parent.parent.multiServer.createPeerRelay(ws, req, rsession.serverId, obj.req.session.userid);
190
+ delete parent.wsrelays[obj.id];
191
+ return;
192
+ } else {
193
+
194
+ // Unexpected connection, drop it
195
+ if (obj.ws) { obj.ws.close(); }
196
+ parent.parent.debug('relay', 'FileRelay unexpected connection: ' + obj.id + ' (' + obj.req.clientIp + ')');
197
+ delete obj.id;
198
+ delete obj.ws;
199
+ delete obj.peer;
200
+ return;
201
+ }
202
}
203
+ } else {
204
+ // HTTP connection, Send message to other peers that we have this connection
205
+ parent.parent.multiServer.DispatchMessage(JSON.stringify({ action: 'relay', id: obj.id }));
206
+ return;
207
}
208
}
209
}
195
- }
210
197
- // Websocket handling
198
- if (obj.ws != null) {
199
- // When data is received from the mesh relay web socket
200
- obj.ws.on('message', function (data) {
201
- if (typeof data == 'string') {
202
- var cmd = null;
203
- try { cmd = JSON.parse(data); } catch (ex) { }
204
- if ((cmd == null) || (typeof cmd.op == 'string')) {
205
- if (cmd.op == 'ok') {
206
- setContentDispositionHeader(this.res, 'application/octet-stream', this.file, cmd.size, 'file.bin');
207
- } else {
208
- try { this.res.sendStatus(401); } catch (ex) { }
211
+ // Websocket handling
212
+ if (obj.ws != null) {
213
+ // When data is received from the mesh relay web socket
214
+ obj.ws.on('message', function (data) {
215
+ if (this.res == null) { return; } // File download websocket does not have an HTTP peer, should not happen.
216
+ if (typeof data == 'string') {
217
+ var cmd = null;
218
+ try { cmd = JSON.parse(data); } catch (ex) { }
219
+ if ((cmd == null) || (typeof cmd.op == 'string')) {
220
+ if (cmd.op == 'ok') {
221
+ setContentDispositionHeader(this.res, 'application/octet-stream', this.file, cmd.size, 'file.bin');
222
+ } else {
223
+ try { this.res.sendStatus(401); } catch (ex) { }
224
+ }
225
}
226
+ } else {
227
+ var unpause = function unpauseFunc(err) { try { unpauseFunc.s.resume(); } catch (ex) { } }
228
+ unpause.s = this._socket;
229
+ this._socket.pause();
230
+ try { this.res.write(data, unpause); } catch (ex) { }
231
}
211
- } else {
212
- var unpause = function unpauseFunc(err) { try { unpauseFunc.s.resume(); } catch (ex) { } }
213
- unpause.s = this._socket;
214
- this._socket.pause();
215
- try { this.res.write(data, unpause); } catch (ex) { }
216
- }
217
- });
232
+ });
233
219
- // If error, close both sides of the relay.
220
- obj.ws.on('error', function (err) {
221
- parent.relaySessionErrorCount++;
222
- //console.log('FileRelay error from ' + obj.req.clientIp + ', ' + err.toString().split('\r')[0] + '.');
223
- closeBothSides();
224
- });
234
+ // If error, close both sides of the relay.
235
+ obj.ws.on('error', function (err) {
236
+ parent.relaySessionErrorCount++;
237
+ //console.log('FileRelay error from ' + obj.req.clientIp + ', ' + err.toString().split('\r')[0] + '.');
238
+ closeBothSides();
239
+ });
240
226
- // If the relay web socket is closed, close both sides.
227
- obj.ws.on('close', function (req) { closeBothSides(); });
241
+ // If the relay web socket is closed, close both sides.
242
+ obj.ws.on('close', function (req) { closeBothSides(); });
243
+ }
244
}
245
246
// Close both our side and the peer side.
@@ -278,7 +294,7 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us
294
// Set the content disposition header for a HTTP response.
295
// Because the filename can't have any special characters in it, we need to be extra careful.
296
function setContentDispositionHeader(res, type, name, size, altname) {
281
- var name = require('path').basename(name).split('\\').join('').split('/').join('').split(':').join('').split('*').join('').split('?').join('').split('"').join('').split('<').join('').split('>').join('').split('|').join('').split(' ').join('').split('\'').join('');
297
+ if (name != null) { name = require('path').basename(name).split('\\').join('').split('/').join('').split(':').join('').split('*').join('').split('?').join('').split('"').join('').split('<').join('').split('>').join('').split('|').join('').split(' ').join('').split('\'').join(''); } else { name = altname; }
298
try {
299
var x = { 'Cache-Control': 'no-store', 'Content-Type': type, 'Content-Disposition': 'attachment; filename="' + name + '"' };
300
if (typeof size == 'number') { x['Content-Length'] = size; }
meshuser.js
+1
-1
@@ -240,7 +240,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
240
var routing = parent.parent.GetRoutingServerId(command.nodeid, 1); // 1 = MeshAgent routing type
241
if (routing != null) {
242
// Check if we have permission to send a message to that node
243
- parent.GetNodeWithRights(domain, user, agent.dbNodeKey, function (node, rights, visible) {
243
+ parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
244
if ((requiredRights != null) && ((rights & requiredRights) == 0)) { if (func) { func(false); return; } } // Check Required Rights
245
if ((requiredNonRights != null) && (rights != MESHRIGHT_ADMIN) && ((rights & requiredNonRights) != 0)) { if (func) { func(false); return; } } // Check Required None Rights
246