Put in the groundwork for web relay with multiple relay DNS names.
This commit is contained in:
parent
1bec66a241
commit
a0ea6ead09
|
@ -69,7 +69,7 @@ function SerialTunnel(options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct a Web relay object
|
// Construct a Web relay object
|
||||||
module.exports.CreateWebRelaySession = function (parent, db, req, args, domain, userid, nodeid, addr, port, appid) {
|
module.exports.CreateWebRelaySession = function (parent, db, req, args, domain, userid, nodeid, addr, port, appid, sessionid) {
|
||||||
const obj = {};
|
const obj = {};
|
||||||
obj.parent = parent;
|
obj.parent = parent;
|
||||||
obj.lastOperation = Date.now();
|
obj.lastOperation = Date.now();
|
||||||
|
@ -79,6 +79,7 @@ module.exports.CreateWebRelaySession = function (parent, db, req, args, domain,
|
||||||
obj.addr = addr;
|
obj.addr = addr;
|
||||||
obj.port = port;
|
obj.port = port;
|
||||||
obj.appid = appid;
|
obj.appid = appid;
|
||||||
|
obj.sessionid = sessionid;
|
||||||
var pendingRequests = [];
|
var pendingRequests = [];
|
||||||
var nextTunnelId = 1;
|
var nextTunnelId = 1;
|
||||||
var tunnels = {};
|
var tunnels = {};
|
||||||
|
@ -199,7 +200,7 @@ module.exports.CreateWebRelaySession = function (parent, db, req, args, domain,
|
||||||
for (var i in pendingRequests) { if (pendingRequests[i][2] == true) { pendingRequests[i][1].close(); } else { pendingRequests[i][1].end(); } }
|
for (var i in pendingRequests) { if (pendingRequests[i][2] == true) { pendingRequests[i][1].close(); } else { pendingRequests[i][1].end(); } }
|
||||||
|
|
||||||
// Notify of session closure
|
// Notify of session closure
|
||||||
if (obj.onclose) { obj.onclose(obj.userid + '/' + obj.sessionId); }
|
if (obj.onclose) { obj.onclose(obj.sessionid); }
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
delete obj.userid;
|
delete obj.userid;
|
||||||
|
|
|
@ -1649,8 +1649,12 @@ function CreateMeshCentralServer(config, args) {
|
||||||
obj.webserver = require('./webserver.js').CreateWebServer(obj, obj.db, obj.args, obj.certificates, obj.StartEx5);
|
obj.webserver = require('./webserver.js').CreateWebServer(obj, obj.db, obj.args, obj.certificates, obj.StartEx5);
|
||||||
if (obj.redirserver != null) { obj.redirserver.hookMainWebServer(obj.certificates); }
|
if (obj.redirserver != null) { obj.redirserver.hookMainWebServer(obj.certificates); }
|
||||||
|
|
||||||
|
// Change RelayDNS to a array of strings
|
||||||
|
if (typeof obj.args.relaydns == 'string') { obj.args.relaydns = [obj.args.relaydns]; }
|
||||||
|
if (obj.common.validateStrArray(obj.args.relaydns, 1) == false) { delete obj.args.relaydns; }
|
||||||
|
|
||||||
// Start the HTTP relay web server if needed
|
// Start the HTTP relay web server if needed
|
||||||
if ((typeof obj.args.relaydns != 'string') && (typeof obj.args.relayport == 'number') && (obj.args.relayport != 0)) {
|
if ((obj.args.relaydns != null) && (typeof obj.args.relayport == 'number') && (obj.args.relayport != 0)) {
|
||||||
obj.webrelayserver = require('./webrelayserver.js').CreateWebRelayServer(obj, obj.db, obj.args, obj.certificates, function () { });
|
obj.webrelayserver = require('./webrelayserver.js').CreateWebRelayServer(obj, obj.db, obj.args, obj.certificates, function () { });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -207,7 +207,7 @@ module.exports.CreateWebRelayServer = function (parent, db, args, certificates,
|
||||||
if (xrelaySession != null) { xrelaySession.close(); delete relaySessions[req.session.userid + '/' + req.session.x]; }
|
if (xrelaySession != null) { xrelaySession.close(); delete relaySessions[req.session.userid + '/' + req.session.x]; }
|
||||||
|
|
||||||
// Create a web relay session
|
// Create a web relay session
|
||||||
const relaySession = require('./apprelays.js').CreateWebRelaySession(obj, db, req, args, domain, userid, nodeid, addr, port, appid);
|
const relaySession = require('./apprelays.js').CreateWebRelaySession(obj, db, req, args, domain, userid, nodeid, addr, port, appid, xrelaySession);
|
||||||
relaySession.onclose = function (sessionId) {
|
relaySession.onclose = function (sessionId) {
|
||||||
// Remove the relay session
|
// Remove the relay session
|
||||||
delete relaySessions[sessionId];
|
delete relaySessions[sessionId];
|
||||||
|
|
31
webserver.js
31
webserver.js
|
@ -88,7 +88,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
|
|
||||||
// Web relay sessions
|
// Web relay sessions
|
||||||
var webRelayNextSessionId = 1;
|
var webRelayNextSessionId = 1;
|
||||||
var webRelaySessions = {} // RelayID --> Web Mutli-Tunnel
|
var webRelaySessions = {} // UserId/SessionId/Host --> Web Relay Session
|
||||||
var webRelayCleanupTimer = null;
|
var webRelayCleanupTimer = null;
|
||||||
|
|
||||||
// Mesh Rights
|
// Mesh Rights
|
||||||
|
@ -2871,8 +2871,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
webstate: encodeURIComponent(webstate).replace(/'/g, '%27'),
|
webstate: encodeURIComponent(webstate).replace(/'/g, '%27'),
|
||||||
amtscanoptions: amtscanoptions,
|
amtscanoptions: amtscanoptions,
|
||||||
pluginHandler: (parent.pluginHandler == null) ? 'null' : parent.pluginHandler.prepExports(),
|
pluginHandler: (parent.pluginHandler == null) ? 'null' : parent.pluginHandler.prepExports(),
|
||||||
webRelayPort: ((typeof args.relaydns == 'string') ? ((typeof args.aliasport == 'number') ? args.aliasport : args.port) : ((parent.webrelayserver != null) ? ((typeof args.relayaliasport == 'number') ? args.relayaliasport : parent.webrelayserver.port) : 0)),
|
webRelayPort: ((args.relaydns != null) ? ((typeof args.aliasport == 'number') ? args.aliasport : args.port) : ((parent.webrelayserver != null) ? ((typeof args.relayaliasport == 'number') ? args.relayaliasport : parent.webrelayserver.port) : 0)),
|
||||||
webRelayDns: ((typeof args.relaydns == 'string') ? args.relaydns : '')
|
webRelayDns: ((args.relaydns != null) ? args.relaydns[0] : '')
|
||||||
}, dbGetFunc.req, domain), user);
|
}, dbGetFunc.req, domain), user);
|
||||||
}
|
}
|
||||||
xdbGetFunc.req = req;
|
xdbGetFunc.req = req;
|
||||||
|
@ -5772,7 +5772,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
|
|
||||||
// Handle all incoming web sockets, see if some need to be handled as web relays
|
// Handle all incoming web sockets, see if some need to be handled as web relays
|
||||||
obj.app.ws('/*', function (ws, req, next) {
|
obj.app.ws('/*', function (ws, req, next) {
|
||||||
if ((obj.webRelayRouter != null) && (req.hostname == obj.args.relaydns)) { handleWebRelayWebSocket(ws, req); return; }
|
if ((obj.webRelayRouter != null) && (obj.args.relaydns.indexOf(req.hostname) >= 0)) { handleWebRelayWebSocket(ws, req); return; }
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -5876,7 +5876,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
}
|
}
|
||||||
|
|
||||||
// If this is a web relay connection, handle it here.
|
// If this is a web relay connection, handle it here.
|
||||||
if ((obj.webRelayRouter != null) && (req.hostname == obj.args.relaydns)) {
|
if ((obj.webRelayRouter != null) && (obj.args.relaydns.indexOf(req.hostname) >= 0)) {
|
||||||
if (['GET', 'POST', 'PUT', 'HEAD'].indexOf(req.method) >= 0) { return obj.webRelayRouter(req, res); } else { res.sendStatus(404); return; }
|
if (['GET', 'POST', 'PUT', 'HEAD'].indexOf(req.method) >= 0) { return obj.webRelayRouter(req, res); } else { res.sendStatus(404); return; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6580,12 +6580,12 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
|
|
||||||
// Setup web relay on this web server if needed
|
// Setup web relay on this web server if needed
|
||||||
// We set this up when a DNS name is used as a web relay instead of a port
|
// We set this up when a DNS name is used as a web relay instead of a port
|
||||||
if (typeof obj.args.relaydns == 'string') {
|
if (obj.args.relaydns != null) {
|
||||||
obj.webRelayRouter = require('express').Router();
|
obj.webRelayRouter = require('express').Router();
|
||||||
|
|
||||||
// This is the magic URL that will setup the relay session
|
// This is the magic URL that will setup the relay session
|
||||||
obj.webRelayRouter.get('/control-redirect.ashx', function (req, res, next) {
|
obj.webRelayRouter.get('/control-redirect.ashx', function (req, res, next) {
|
||||||
if (req.headers.host != obj.args.relaydns) { res.sendStatus(404); return; }
|
if (obj.args.relaydns.indexOf(req.hostname) == -1) { res.sendStatus(404); return; }
|
||||||
if ((req.session.userid == null) && obj.args.user && obj.users['user//' + obj.args.user.toLowerCase()]) { req.session.userid = 'user//' + obj.args.user.toLowerCase(); } // Use a default user if needed
|
if ((req.session.userid == null) && obj.args.user && obj.users['user//' + obj.args.user.toLowerCase()]) { req.session.userid = 'user//' + obj.args.user.toLowerCase(); } // Use a default user if needed
|
||||||
res.set({ 'Cache-Control': 'no-store' });
|
res.set({ 'Cache-Control': 'no-store' });
|
||||||
parent.debug('web', 'webRelaySetup');
|
parent.debug('web', 'webRelaySetup');
|
||||||
|
@ -6613,7 +6613,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
const appid = parseInt(req.query.appid);
|
const appid = parseInt(req.query.appid);
|
||||||
|
|
||||||
// Check to see if we already have a multi-relay session that matches exactly this device and port for this user
|
// Check to see if we already have a multi-relay session that matches exactly this device and port for this user
|
||||||
const xrelaySession = webRelaySessions[req.session.userid + '/' + req.session.x];
|
const xrelaySessionId = req.session.userid + '/' + req.session.x + '/' + req.hostname;
|
||||||
|
const xrelaySession = webRelaySessions[xrelaySessionId];
|
||||||
if ((xrelaySession != null) && (xrelaySession.domain.id == domain.id) && (xrelaySession.userid == userid) && (xrelaySession.nodeid == nodeid) && (xrelaySession.addr == addr) && (xrelaySession.port == port) && (xrelaySession.appid == appid)) {
|
if ((xrelaySession != null) && (xrelaySession.domain.id == domain.id) && (xrelaySession.userid == userid) && (xrelaySession.nodeid == nodeid) && (xrelaySession.addr == addr) && (xrelaySession.port == port) && (xrelaySession.appid == appid)) {
|
||||||
// We found an exact match, we are all setup already, redirect to root
|
// We found an exact match, we are all setup already, redirect to root
|
||||||
res.redirect('/');
|
res.redirect('/');
|
||||||
|
@ -6623,11 +6624,11 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
// There is a relay session, but it's not correct, close it.
|
// There is a relay session, but it's not correct, close it.
|
||||||
if (xrelaySession != null) {
|
if (xrelaySession != null) {
|
||||||
xrelaySession.close();
|
xrelaySession.close();
|
||||||
delete webRelaySessions[req.session.userid + '/' + req.session.x];
|
delete webRelaySessions[xrelaySessionId];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a web relay session
|
// Create a web relay session
|
||||||
const relaySession = require('./apprelays.js').CreateWebRelaySession(obj, db, req, args, domain, userid, nodeid, addr, port, appid);
|
const relaySession = require('./apprelays.js').CreateWebRelaySession(obj, db, req, args, domain, userid, nodeid, addr, port, appid, xrelaySessionId);
|
||||||
relaySession.onclose = function (sessionId) {
|
relaySession.onclose = function (sessionId) {
|
||||||
// Remove the relay session
|
// Remove the relay session
|
||||||
delete webRelaySessions[sessionId];
|
delete webRelaySessions[sessionId];
|
||||||
|
@ -6636,7 +6637,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the multi-tunnel session
|
// Set the multi-tunnel session
|
||||||
webRelaySessions[userid + '/' + req.session.x] = relaySession;
|
webRelaySessions[xrelaySessionId] = relaySession;
|
||||||
|
|
||||||
// Setup the cleanup timer if needed
|
// Setup the cleanup timer if needed
|
||||||
if (obj.cleanupTimer == null) { webRelayCleanupTimer = setInterval(checkWebRelaySessionsTimeout, 10000); }
|
if (obj.cleanupTimer == null) { webRelayCleanupTimer = setInterval(checkWebRelaySessionsTimeout, 10000); }
|
||||||
|
@ -6701,7 +6702,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
// Handle an incoming request as a web relay
|
// Handle an incoming request as a web relay
|
||||||
function handleWebRelayRequest(req, res) {
|
function handleWebRelayRequest(req, res) {
|
||||||
if ((req.session.userid != null) && (req.session.x != null) && (obj.destroyedSessions[req.session.userid + '/' + req.session.x] == null)) {
|
if ((req.session.userid != null) && (req.session.x != null) && (obj.destroyedSessions[req.session.userid + '/' + req.session.x] == null)) {
|
||||||
var relaySession = webRelaySessions[req.session.userid + '/' + req.session.x];
|
var relaySession = webRelaySessions[req.session.userid + '/' + req.session.x + '/' + req.hostname];
|
||||||
if (relaySession != null) {
|
if (relaySession != null) {
|
||||||
// The web relay session is valid, use it
|
// The web relay session is valid, use it
|
||||||
relaySession.handleRequest(req, res);
|
relaySession.handleRequest(req, res);
|
||||||
|
@ -6718,7 +6719,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
// Handle an incoming websocket connection as a web relay
|
// Handle an incoming websocket connection as a web relay
|
||||||
function handleWebRelayWebSocket(ws, req) {
|
function handleWebRelayWebSocket(ws, req) {
|
||||||
if ((req.session.userid != null) && (req.session.x != null) && (obj.destroyedSessions[req.session.userid + '/' + req.session.x] == null)) {
|
if ((req.session.userid != null) && (req.session.x != null) && (obj.destroyedSessions[req.session.userid + '/' + req.session.x] == null)) {
|
||||||
var relaySession = webRelaySessions[req.session.userid + '/' + req.session.x];
|
var relaySession = webRelaySessions[req.session.userid + '/' + req.session.x + '/' + req.hostname];
|
||||||
if (relaySession != null) {
|
if (relaySession != null) {
|
||||||
// The multi-tunnel session is valid, use it
|
// The multi-tunnel session is valid, use it
|
||||||
relaySession.handleWebSocket(ws, req);
|
relaySession.handleWebSocket(ws, req);
|
||||||
|
@ -7184,7 +7185,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
} else {
|
} else {
|
||||||
obj.tcpServer = obj.tlsServer.listen(port, addr, function () {
|
obj.tcpServer = obj.tlsServer.listen(port, addr, function () {
|
||||||
console.log('MeshCentral HTTPS server running on ' + certificates.CommonName + ':' + port + ((typeof args.aliasport == 'number') ? (', alias port ' + args.aliasport) : '') + '.');
|
console.log('MeshCentral HTTPS server running on ' + certificates.CommonName + ':' + port + ((typeof args.aliasport == 'number') ? (', alias port ' + args.aliasport) : '') + '.');
|
||||||
if (typeof args.relaydns == 'string') { console.log('MeshCentral HTTPS relay server running on ' + args.relaydns + ':' + port + ((typeof args.aliasport == 'number') ? (', alias port ' + args.aliasport) : '') + '.'); }
|
if (args.relaydns != null) { console.log('MeshCentral HTTPS relay server running on ' + args.relaydns[0] + ':' + port + ((typeof args.aliasport == 'number') ? (', alias port ' + args.aliasport) : '') + '.'); }
|
||||||
});
|
});
|
||||||
obj.parent.updateServerState('servername', certificates.CommonName);
|
obj.parent.updateServerState('servername', certificates.CommonName);
|
||||||
}
|
}
|
||||||
|
@ -7194,7 +7195,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
||||||
} else {
|
} else {
|
||||||
obj.tcpServer = obj.app.listen(port, addr, function () {
|
obj.tcpServer = obj.app.listen(port, addr, function () {
|
||||||
console.log('MeshCentral HTTP server running on port ' + port + ((typeof args.aliasport == 'number') ? (', alias port ' + args.aliasport) : '') + '.');
|
console.log('MeshCentral HTTP server running on port ' + port + ((typeof args.aliasport == 'number') ? (', alias port ' + args.aliasport) : '') + '.');
|
||||||
if (typeof args.relaydns == 'string') { console.log('MeshCentral HTTP relay server running on ' + args.relaydns + ':' + port + ((typeof args.aliasport == 'number') ? (', alias port ' + args.aliasport) : '') + '.'); }
|
if (args.relaydns != null) { console.log('MeshCentral HTTP relay server running on ' + args.relaydns[0] + ':' + port + ((typeof args.aliasport == 'number') ? (', alias port ' + args.aliasport) : '') + '.'); }
|
||||||
});
|
});
|
||||||
obj.parent.updateServerState('http-port', port);
|
obj.parent.updateServerState('http-port', port);
|
||||||
if (args.aliasport != null) { obj.parent.updateServerState('http-aliasport', args.aliasport); }
|
if (args.aliasport != null) { obj.parent.updateServerState('http-aliasport', args.aliasport); }
|
||||||
|
|
Loading…
Reference in New Issue