Fix for #2687. Relay would not work for very long expire times.

Ylian Saint-Hilaire committed May 25, 2021 at 12:35 UTC a1cb07670dc488bf913936ba533675989854b4c1
1 file changed +13 -3
meshrelay.js
+13 -3
@@ -144,7 +144,7 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) {
144 delete obj.id;
145 delete obj.ws;
146 delete obj.peer;
147 - delete obj.expireTimer;
147 + if (obj.expireTimer != null) { clearTimeout(obj.expireTimer); delete obj.expireTimer; }
148
149 // Unsubscribe
150 if (obj.pid != null) { parent.parent.RemoveAllEventDispatch(obj); }
@@ -295,6 +295,13 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) {
295 }
296 }
297
298 + // Check that both sides have websocket connections
299 + if ((obj.ws == null) || (relayinfo.peer1.ws == null)) {
300 + relayinfo.peer1.close();
301 + obj.close();
302 + return null;
303 + }
304 +
305 // Connect to peer
306 obj.peer = relayinfo.peer1;
307 obj.peer.peer = obj;
@@ -767,7 +774,10 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) {
774 }
775
776 // If this session has a expire time, setup the expire timer now.
770 - if (cookie && (typeof cookie.expire == 'number')) { obj.expireTimer = setTimeout(closeBothSides, cookie.expire - currentTime); }
777 + if (cookie && (typeof cookie.expire == 'number')) {
778 + const timeToExpire = (cookie.expire - currentTime);
779 + if (timeToExpire < 0x7FFFFFFF) { obj.expireTimer = setTimeout(closeBothSides, timeToExpire); } // Only set the timeout if it fits in 32bit signed integer.
780 + }
781
782 // Mark this relay session as authenticated if this is the user end.
783 obj.authenticated = (user != null);
@@ -1097,7 +1107,7 @@ function CreateLocalRelayEx(parent, ws, req, domain, user, cookie) {
1107 delete obj.nodeid;
1108 delete obj.meshid;
1109 delete obj.tcpport;
1100 - delete obj.expireTimer;
1110 + if (obj.expireTimer != null) { clearTimeout(obj.expireTimer); delete obj.expireTimer; }
1111 if (obj.client != null) { obj.client.destroy(); delete obj.client; } // Close the client socket
1112 if (obj.pingtimer != null) { clearInterval(obj.pingtimer); delete obj.pingtimer; }
1113 if (obj.pongtimer != null) { clearInterval(obj.pongtimer); delete obj.pongtimer; }