Updated tunnel idle timeout, to close the tunnel after a preset timeout if a command isn't received. Idle timeout is reset after a command is received.
Bryan Roe committed
Jul 26, 2022 at 20:03 UTC
f628c57eea0178ba5b2b50a3146493797c3b0b8d
1 file changed
+40
-8
agents/meshcore.js
+40
-8
@@ -1885,9 +1885,12 @@ function onTunnelUpgrade(response, s, head) {
1885
s.tunnel = this;
1886
s.descriptorMetadata = "MeshAgent_relayTunnel";
1887
1888
- if (require('MeshAgent').idleTimeout != null) {
1888
+
1889
+ if (this.tcpport != null || this.udpport != null)
1890
+ {
1891
s.setTimeout(require('MeshAgent').idleTimeout * 1000);
1890
- s.on('timeout', function () {
1892
+ s.on('timeout', function ()
1893
+ {
1894
this.ping();
1895
this.setTimeout(require('MeshAgent').idleTimeout * 1000);
1896
});
@@ -1895,7 +1898,8 @@ function onTunnelUpgrade(response, s, head) {
1898
1899
//sendConsoleText('onTunnelUpgrade - ' + this.tcpport + ' - ' + this.udpport);
1900
1898
- if (this.tcpport != null) {
1901
+ if (this.tcpport != null)
1902
+ {
1903
// This is a TCP relay connection, pause now and try to connect to the target.
1904
s.pause();
1905
s.data = onTcpRelayServerTunnelData;
@@ -1911,7 +1915,9 @@ function onTunnelUpgrade(response, s, head) {
1915
try { mesh.SendCommand({ action: 'sessions', type: 'tcp', value: tunnelUserCount.tcp }); } catch (ex) { }
1916
broadcastSessionsToRegisteredApps();
1917
}
1914
- } if (this.udpport != null) {
1918
+ }
1919
+ if (this.udpport != null)
1920
+ {
1921
// This is a UDP relay connection, get the UDP socket setup. // TODO: ***************
1922
s.data = onUdpRelayServerTunnelData;
1923
s.udprelay = require('dgram').createSocket({ type: 'udp4' });
@@ -1929,7 +1935,9 @@ function onTunnelUpgrade(response, s, head) {
1935
try { mesh.SendCommand({ action: 'sessions', type: 'udp', value: tunnelUserCount.tcp }); } catch (ex) { }
1936
broadcastSessionsToRegisteredApps();
1937
}
1932
- } else {
1938
+ }
1939
+ else
1940
+ {
1941
// This is a normal connect for KVM/Terminal/Files
1942
s.data = onTunnelData;
1943
}
@@ -2072,10 +2080,34 @@ function onTunnelData(data) {
2080
return;
2081
}
2082
2075
- if (this.httprequest.state == 0) {
2083
+ if (this.httprequest.state == 0)
2084
+ {
2085
// Check if this is a relay connection
2077
- if ((data == 'c') || (data == 'cr')) { this.httprequest.state = 1; /*sendConsoleText("Tunnel #" + this.httprequest.index + " now active", this.httprequest.sessionid);*/ }
2078
- } else {
2086
+ if ((data == 'c') || (data == 'cr'))
2087
+ {
2088
+ this.httprequest.state = 1; /*sendConsoleText("Tunnel #" + this.httprequest.index + " now active", this.httprequest.sessionid);*/
2089
+ this.setTimeout(global._tunnelTimeout == null ? 5000 : global._tunnelTimeout); // Once we receive 'c', we will only wait the tunnel timeout (5 seconds) before we close the tunnel
2090
+ this.on('timeout', function ()
2091
+ {
2092
+ this.end();
2093
+ });
2094
+ }
2095
+ }
2096
+ else
2097
+ {
2098
+ // We received some data, so we will reset the idle timeout of the websocket
2099
+ this.removeAllListeners('timeout');
2100
+
2101
+ if (require('MeshAgent').idleTimeout != null)
2102
+ {
2103
+ this.setTimeout(require('MeshAgent').idleTimeout * 1000);
2104
+ this.on('timeout', function ()
2105
+ {
2106
+ this.ping();
2107
+ this.setTimeout(require('MeshAgent').idleTimeout * 1000);
2108
+ });
2109
+ }
2110
+
2111
// Handle tunnel data
2112
if (this.httprequest.protocol == 0) { // 1 = Terminal (admin), 2 = Desktop, 5 = Files, 6 = PowerShell (admin), 7 = Plugin Data Exchange, 8 = Terminal (user), 9 = PowerShell (user), 10 = FileTransfer
2113
// Take a look at the protocol