Fixed web relay when used with local device group.

Ylian Saint-Hilaire committed Nov 1, 2022 at 14:23 UTC 0c2c55726e4d7d5ac881ea6c4b958910224495df
3 files changed +11 -8
apprelays.js
+9 -6
@@ -69,7 +69,7 @@ function SerialTunnel(options) {
69 }
70
71 // Construct a Web relay object
72 -module.exports.CreateWebRelaySession = function (parent, db, req, args, domain, userid, nodeid, addr, port, appid, sessionid, expire) {
72 +module.exports.CreateWebRelaySession = function (parent, db, req, args, domain, userid, nodeid, addr, port, appid, sessionid, expire, mtype) {
73 const obj = {};
74 obj.parent = parent;
75 obj.lastOperation = Date.now();
@@ -81,6 +81,7 @@ module.exports.CreateWebRelaySession = function (parent, db, req, args, domain,
81 obj.appid = appid;
82 obj.sessionid = sessionid;
83 obj.expireTimer = null;
84 + obj.mtype = mtype;
85 var pendingRequests = [];
86 var nextTunnelId = 1;
87 var tunnels = {};
@@ -164,7 +165,7 @@ module.exports.CreateWebRelaySession = function (parent, db, req, args, domain,
165 // Launch a new tunnel
166 if (obj.closed == true) return;
167 parent.parent.debug('webrelay', 'launchNewTunnel');
167 - const tunnel = module.exports.CreateWebRelay(obj, db, args, domain);
168 + const tunnel = module.exports.CreateWebRelay(obj, db, args, domain, obj.mtype);
169 tunnel.onclose = function (tunnelId, processedCount) {
170 if (tunnels == null) return;
171 parent.parent.debug('webrelay', 'tunnel-onclose');
@@ -238,7 +239,7 @@ module.exports.CreateWebRelaySession = function (parent, db, req, args, domain,
239
240
241 // Construct a Web relay object
241 -module.exports.CreateWebRelay = function (parent, db, args, domain) {
242 +module.exports.CreateWebRelay = function (parent, db, args, domain, mtype) {
243 //const Net = require('net');
244 const WebSocket = require('ws')
245
@@ -249,6 +250,7 @@ module.exports.CreateWebRelay = function (parent, db, args, domain) {
250 obj.isWebSocket = false; // If true, this request will not close and so, it can't be allowed to hold up other requests
251 obj.isStreaming = false; // If true, this request will not close and so, it can't be allowed to hold up other requests
252 obj.processedRequestCount = 0;
253 + obj.mtype = mtype;
254 const constants = (require('crypto').constants ? require('crypto').constants : require('constants')); // require('constants') is deprecated in Node 11.10, use require('crypto').constants instead.
255
256 // Events
@@ -260,7 +262,7 @@ module.exports.CreateWebRelay = function (parent, db, args, domain) {
262 // Called when we need to close the tunnel because the response stream has closed
263 function handleResponseClosure() { obj.close(); }
264
263 - // Return copkie name and values
265 + // Return cookie name and values
266 function parseRequestCookies(cookiesString) {
267 var r = {};
268 if (typeof cookiesString != 'string') return r;
@@ -437,7 +439,7 @@ module.exports.CreateWebRelay = function (parent, db, args, domain) {
439 obj.relayActive = false;
440 };
441
440 - // Start the looppback server
442 + // Start the loopback server
443 obj.connect = function (userid, nodeid, addr, port, appid) {
444 if (obj.relayActive || obj.closed) return;
445 obj.addr = addr;
@@ -687,7 +689,8 @@ module.exports.CreateWebRelay = function (parent, db, args, domain) {
689
690 // If there is a header, send it
691 if (header != null) {
690 - obj.res.status(parseInt(header.Directive[1])); // Set the status
692 + const statusCode = parseInt(header.Directive[1]);
693 + if ((!isNaN(statusCode)) && (statusCode > 0) && (statusCode <= 999)) { obj.res.status(statusCode); } // Set the status
694 const blockHeaders = ['Directive', 'sec-websocket-extensions', 'connection', 'transfer-encoding', 'last-modified', 'content-security-policy', 'cache-control']; // We do not forward these headers
695 for (var i in header) {
696 if (i == 'set-cookie') {
webrelayserver.js
+1 -1
@@ -259,7 +259,7 @@ module.exports.CreateWebRelayServer = function (parent, db, args, certificates,
259 if (xrelaySession != null) { xrelaySession.close(); delete relaySessions[webSessionId]; }
260
261 // Create a web relay session
262 - const relaySession = require('./apprelays.js').CreateWebRelaySession(obj, db, req, args, domain, userid, nodeid, addr, port, appid, webSessionId, expire);
262 + const relaySession = require('./apprelays.js').CreateWebRelaySession(obj, db, req, args, domain, userid, nodeid, addr, port, appid, webSessionId, expire, node.mtype);
263 relaySession.xpublicid = publicid;
264 relaySession.onclose = function (sessionId) {
265 // Remove the relay session
webserver.js
+1 -1
@@ -6865,7 +6865,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
6865 if (xrelaySession != null) { xrelaySession.close(); delete webRelaySessions[xrelaySessionId]; }
6866
6867 // Create a web relay session
6868 - const relaySession = require('./apprelays.js').CreateWebRelaySession(obj, db, req, args, domain, userid, nodeid, addr, port, appid, xrelaySessionId, expire);
6868 + const relaySession = require('./apprelays.js').CreateWebRelaySession(obj, db, req, args, domain, userid, nodeid, addr, port, appid, xrelaySessionId, expire, node.mtype);
6869 relaySession.xpublicid = publicid;
6870 relaySession.onclose = function (sessionId) {
6871 // Remove the relay session