1
0
Fork 0
mirror of https://github.com/Ylianst/MeshCentral.git synced 2025-02-12 11:01:52 +00:00

Completed support for web relay with multiple DNS names.

This commit is contained in:
Ylian Saint-Hilaire 2022-07-10 11:32:59 -07:00
parent 1a72126c4f
commit 5eca4eecee

View file

@ -6612,21 +6612,58 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
const port = parseInt(req.query.p); const port = parseInt(req.query.p);
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 try {
// TODO: Check that we have an exact session on any of the relay DNS names
const xrelaySessionId = req.session.userid + '/' + req.session.x + '/' + req.hostname; // Check that we have an exact session on any of the relay DNS names
const xrelaySession = webRelaySessions[xrelaySessionId]; var xrelaySessionId, xrelaySession, freeRelayHost, oldestRelayTime, oldestRelayHost;
if ((xrelaySession != null) && (xrelaySession.domain.id == domain.id) && (xrelaySession.userid == userid) && (xrelaySession.nodeid == nodeid) && (xrelaySession.addr == addr) && (xrelaySession.port == port) && (xrelaySession.appid == appid)) { for (var hostIndex in obj.args.relaydns) {
// We found an exact match, we are all setup already, redirect to root const host = obj.args.relaydns[hostIndex];
xrelaySessionId = req.session.userid + '/' + req.session.x + '/' + host;
xrelaySession = webRelaySessions[xrelaySessionId];
if (xrelaySession == null) {
// We found an unused hostname, save this as it could be useful.
if (freeRelayHost == null) { freeRelayHost = host; }
} else {
// Check if we already have a relay session that matches exactly what we want
if ((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 of that DNS name
if (host == req.hostname) {
// Request was made on the same host, redirect to root.
res.redirect('/'); res.redirect('/');
} else {
// Request was made to a different host
const httpport = ((args.aliasport != null) ? args.aliasport : args.port);
res.redirect('https://' + host + ((httpport != 443) ? (':' + httpport) : '') + '/');
}
return; return;
} }
// TODO: Check if there is a free relay DNS name we can use // Keep a record of the oldest web relay session, this could be useful.
if (oldestRelayHost == null) {
// Oldest host not set yet, set it
oldestRelayHost = host;
oldestRelayTime = xrelaySession.lastOperation;
} else {
// Check if this host is older then oldest so far
if (oldestRelayTime > xrelaySession.lastOperation) {
oldestRelayHost = host;
oldestRelayTime = xrelaySession.lastOperation;
}
}
}
}
// There is a relay session, but it's not correct, close it. // Check if there is a free relay DNS name we can use
// TODO: Do this on the session that got the olders request var selectedHost = null;
if (xrelaySession != null) { if (freeRelayHost != null) {
// There is a free one, use it.
selectedHost = freeRelayHost;
xrelaySessionId = req.session.userid + '/' + req.session.x + '/' + selectedHost;
} else {
// No free ones, close the oldest one
selectedHost = oldestRelayHost;
xrelaySessionId = req.session.userid + '/' + req.session.x + '/' + selectedHost;
xrelaySession = webRelaySessions[xrelaySessionId];
xrelaySession.close(); xrelaySession.close();
delete webRelaySessions[xrelaySessionId]; delete webRelaySessions[xrelaySessionId];
} }
@ -6646,8 +6683,16 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
// 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); }
// Redirect to root if (selectedHost == req.hostname) {
// Request was made on the same host, redirect to root.
res.redirect('/'); res.redirect('/');
} else {
// Request was made to a different host
const httpport = ((args.aliasport != null) ? args.aliasport : args.port);
res.redirect('https://' + selectedHost + ((httpport != 443) ? (':' + httpport) : '') + '/');
}
} catch (ex) { console.log(ex); }
}); });
// Handle all incoming requests as web relays // Handle all incoming requests as web relays