From 431acc505e1cf0d6fd9c22d6dc182b2c6419df62 Mon Sep 17 00:00:00 2001 From: greysoh Date: Thu, 30 May 2024 16:39:17 -0400 Subject: [PATCH] feature: Adds code to reinitialize SSH after disconnection. --- api/src/backendimpl/ssh.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/api/src/backendimpl/ssh.ts b/api/src/backendimpl/ssh.ts index b07113d..028f529 100644 --- a/api/src/backendimpl/ssh.ts +++ b/api/src/backendimpl/ssh.ts @@ -103,6 +103,29 @@ export class SSHBackendProvider implements BackendBaseClass { return false; } + if (this.sshInstance.connection) { + this.sshInstance.connection.on("end", async() => { + if (this.state != "started") return; + this.logs.push("We disconnected from the SSH server. Restarting..."); + + // Create a new array from the existing list of proxies, so we have a backup of the proxy list before + // we wipe the list of all proxies and clients (as we're disconnected anyways) + const proxies = Array.from(this.proxies); + + this.proxies.splice(0, this.proxies.length); + this.clients.splice(0, this.clients.length); + + await this.start(); + + if (this.state != "started") return; + + for (const proxy of proxies) { + if (!proxy.enabled) continue; + this.addConnection(proxy.sourceIP, proxy.sourcePort, proxy.destPort, "tcp"); + } + }); + }; + this.state = "started"; this.logs.push("Successfully started SSHBackendProvider.");