mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2025-03-09 15:40:18 +00:00
Server peering fixes.
This commit is contained in:
parent
c080a55e4b
commit
e0977eac22
3 changed files with 64 additions and 47 deletions
|
@ -121,7 +121,7 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us
|
|||
};
|
||||
|
||||
function performRelay() {
|
||||
if (obj.id == null) { try { obj.close(); } catch (e) { } return null; } // Attempt to connect without id, drop this.
|
||||
if (obj.id == null) { try { obj.close(); } catch (e) { } return; } // Attempt to connect without id, drop this.
|
||||
if (obj.ws != null) { obj.ws._socket.setKeepAlive(true, 240000); } // Set TCP keep alive
|
||||
|
||||
// Check the peer connection status
|
||||
|
@ -137,7 +137,7 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us
|
|||
delete obj.id;
|
||||
delete obj.ws;
|
||||
delete obj.peer;
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Connect to peer
|
||||
|
@ -160,9 +160,9 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us
|
|||
parent.parent.debug('relay', 'FileRelay connected: ' + obj.id + ' (' + obj.req.clientIp + ' --> ' + obj.peer.req.clientIp + ')');
|
||||
|
||||
// Log the connection
|
||||
if (sessionUser != null) {
|
||||
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 };
|
||||
parent.parent.DispatchEvent(['*', sessionUser._id], obj, event);
|
||||
if (obj.user != null) {
|
||||
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 };
|
||||
parent.parent.DispatchEvent(['*', obj.user._id], obj, event);
|
||||
}
|
||||
} else {
|
||||
// Connected already, drop this connection.
|
||||
|
@ -171,60 +171,76 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us
|
|||
delete obj.id;
|
||||
delete obj.ws;
|
||||
delete obj.peer;
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// Wait for other relay connection
|
||||
parent.wsrelays[obj.id] = { peer1: obj, state: 1, timeout: setTimeout(closeBothSides, 30000) };
|
||||
parent.parent.debug('relay', 'FileRelay holding: ' + obj.id + ' (' + obj.req.clientIp + ') ' + (obj.authenticated ? 'Authenticated' : ''));
|
||||
if (obj.ws != null) {
|
||||
// Websocket connection
|
||||
obj.ws._socket.pause();
|
||||
|
||||
// Check if a peer server has this connection
|
||||
if (parent.parent.multiServer != null) {
|
||||
var rsession = parent.wsPeerRelays[obj.id];
|
||||
if ((rsession != null) && (rsession.serverId > parent.parent.serverId)) {
|
||||
// We must initiate the connection to the peer
|
||||
parent.parent.multiServer.createPeerRelay(ws, req, rsession.serverId, obj.req.session.userid);
|
||||
delete parent.wsrelays[obj.id];
|
||||
} else {
|
||||
// Send message to other peers that we have this connection
|
||||
parent.parent.multiServer.DispatchMessage(JSON.stringify({ action: 'relay', id: obj.id }));
|
||||
// Check if a peer server has this connection
|
||||
if (parent.parent.multiServer != null) {
|
||||
var rsession = parent.wsPeerRelays[obj.id];
|
||||
if ((rsession != null) && (rsession.serverId > parent.parent.serverId)) {
|
||||
// We must initiate the connection to the peer
|
||||
parent.parent.multiServer.createPeerRelay(ws, req, rsession.serverId, obj.req.session.userid);
|
||||
delete parent.wsrelays[obj.id];
|
||||
return;
|
||||
} else {
|
||||
|
||||
// Unexpected connection, drop it
|
||||
if (obj.ws) { obj.ws.close(); }
|
||||
parent.parent.debug('relay', 'FileRelay unexpected connection: ' + obj.id + ' (' + obj.req.clientIp + ')');
|
||||
delete obj.id;
|
||||
delete obj.ws;
|
||||
delete obj.peer;
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// HTTP connection, Send message to other peers that we have this connection
|
||||
parent.parent.multiServer.DispatchMessage(JSON.stringify({ action: 'relay', id: obj.id }));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Websocket handling
|
||||
if (obj.ws != null) {
|
||||
// When data is received from the mesh relay web socket
|
||||
obj.ws.on('message', function (data) {
|
||||
if (typeof data == 'string') {
|
||||
var cmd = null;
|
||||
try { cmd = JSON.parse(data); } catch (ex) { }
|
||||
if ((cmd == null) || (typeof cmd.op == 'string')) {
|
||||
if (cmd.op == 'ok') {
|
||||
setContentDispositionHeader(this.res, 'application/octet-stream', this.file, cmd.size, 'file.bin');
|
||||
} else {
|
||||
try { this.res.sendStatus(401); } catch (ex) { }
|
||||
// Websocket handling
|
||||
if (obj.ws != null) {
|
||||
// When data is received from the mesh relay web socket
|
||||
obj.ws.on('message', function (data) {
|
||||
if (this.res == null) { return; } // File download websocket does not have an HTTP peer, should not happen.
|
||||
if (typeof data == 'string') {
|
||||
var cmd = null;
|
||||
try { cmd = JSON.parse(data); } catch (ex) { }
|
||||
if ((cmd == null) || (typeof cmd.op == 'string')) {
|
||||
if (cmd.op == 'ok') {
|
||||
setContentDispositionHeader(this.res, 'application/octet-stream', this.file, cmd.size, 'file.bin');
|
||||
} else {
|
||||
try { this.res.sendStatus(401); } catch (ex) { }
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var unpause = function unpauseFunc(err) { try { unpauseFunc.s.resume(); } catch (ex) { } }
|
||||
unpause.s = this._socket;
|
||||
this._socket.pause();
|
||||
try { this.res.write(data, unpause); } catch (ex) { }
|
||||
}
|
||||
} else {
|
||||
var unpause = function unpauseFunc(err) { try { unpauseFunc.s.resume(); } catch (ex) { } }
|
||||
unpause.s = this._socket;
|
||||
this._socket.pause();
|
||||
try { this.res.write(data, unpause); } catch (ex) { }
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// If error, close both sides of the relay.
|
||||
obj.ws.on('error', function (err) {
|
||||
parent.relaySessionErrorCount++;
|
||||
//console.log('FileRelay error from ' + obj.req.clientIp + ', ' + err.toString().split('\r')[0] + '.');
|
||||
closeBothSides();
|
||||
});
|
||||
// If error, close both sides of the relay.
|
||||
obj.ws.on('error', function (err) {
|
||||
parent.relaySessionErrorCount++;
|
||||
//console.log('FileRelay error from ' + obj.req.clientIp + ', ' + err.toString().split('\r')[0] + '.');
|
||||
closeBothSides();
|
||||
});
|
||||
|
||||
// If the relay web socket is closed, close both sides.
|
||||
obj.ws.on('close', function (req) { closeBothSides(); });
|
||||
// If the relay web socket is closed, close both sides.
|
||||
obj.ws.on('close', function (req) { closeBothSides(); });
|
||||
}
|
||||
}
|
||||
|
||||
// Close both our side and the peer side.
|
||||
|
@ -278,7 +294,7 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us
|
|||
// Set the content disposition header for a HTTP response.
|
||||
// Because the filename can't have any special characters in it, we need to be extra careful.
|
||||
function setContentDispositionHeader(res, type, name, size, altname) {
|
||||
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('');
|
||||
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; }
|
||||
try {
|
||||
var x = { 'Cache-Control': 'no-store', 'Content-Type': type, 'Content-Disposition': 'attachment; filename="' + name + '"' };
|
||||
if (typeof size == 'number') { x['Content-Length'] = size; }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue