diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/ProxyChain.kt b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/ProxyChain.kt index 2bc3167..517e548 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/ProxyChain.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/ProxyChain.kt @@ -4,6 +4,7 @@ import androidx.fragment.app.FragmentActivity import io.github.lordofpolls.shellwave.core.crypto.CredentialVault import io.github.lordofpolls.shellwave.core.db.dao.HostDao import io.github.lordofpolls.shellwave.core.db.entities.HostEntity +import java.io.IOException import net.schmizz.sshj.SSHClient import net.schmizz.sshj.connection.channel.direct.DirectConnection import net.schmizz.sshj.transport.verification.HostKeyVerifier @@ -43,6 +44,11 @@ class ProxyChainResources internal constructor( * * A failure partway through tears down everything already opened, so a caller that never sees a * successful return owns nothing but [finalSsh]. + * + * [isClosing] is polled before each hop is dialled, so a caller that was torn down mid-chain stops + * at the next hop boundary instead of authenticating (and prompting) through the rest. It cannot + * interrupt a hop already in flight. It throws [IOException], which the failure handling above + * cleans up after. */ internal fun connectChainAndAuthenticate( finalSsh: SSHClient, @@ -52,6 +58,7 @@ internal fun connectChainAndAuthenticate( targetUsername: String, targetAuthMethod: AuthMethod, hostKeyVerifier: HostKeyVerifier, + isClosing: () -> Boolean = { false }, ): ProxyChainResources { if (hops.isEmpty()) { connectAndAuthenticate( @@ -71,6 +78,7 @@ internal fun connectChainAndAuthenticate( try { var previous: SSHClient? = null for (hop in hops) { + if (isClosing()) throw IOException("Connection closed while connecting") val client = SSHClient() val prev = previous if (prev == null) { @@ -96,6 +104,7 @@ internal fun connectChainAndAuthenticate( clients += client previous = client } + if (isClosing()) throw IOException("Connection closed while connecting") val finalChannel = previous!!.newDirectConnection(targetHost, targetPort) channels += finalChannel connectViaAndAuthenticate( diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshAuth.kt b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshAuth.kt index 048c1d1..53cb9c3 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshAuth.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshAuth.kt @@ -21,6 +21,10 @@ internal const val KEEPALIVE_INTERVAL_SECONDS = 15 // Only affects a server that completes TCP but never finishes KEX; socket connect is separate. private const val KEX_TIMEOUT_MS = 180_000 +// Bounds the TCP connect only. Without it a black-holed host holds the attempt for the OS's own +// SYN retry limit, minutes on Android, with the session stuck in CONNECTING. +internal const val CONNECT_TIMEOUT_MS = 15_000 + /** * Connect and authenticate one [SSHClient] - the half of "open an SSH connection" that has nothing * to do with what happens afterwards (a PTY + shell for SshConnection, a headless `exec` for @@ -46,6 +50,7 @@ internal fun connectAndAuthenticate( // connect() itself, only if the interval is already non-zero at that point. ssh.connection.keepAlive.keepAliveInterval = KEEPALIVE_INTERVAL_SECONDS ssh.transport.timeoutMs = KEX_TIMEOUT_MS + ssh.connectTimeout = CONNECT_TIMEOUT_MS ssh.connect(host, port) authenticate(ssh, username, authMethod) } diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt index 0e1d03f..5661369 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt @@ -278,12 +278,17 @@ open class SshConnection( port, username, authMethod, - hostKeyVerifier + hostKeyVerifier, + isClosing = { closing.get() } ) // Everything below runs on an authenticated transport nobody else owns yet. A failure here // would otherwise leave it, its shell and any forwarded agent live until Retry or tab close. try { + // A disconnect() that ran before ssh.connect() started had no socket to close, and one + // that ran during a proxy chain had no chain resources to tear down yet. The catch + // below finishes that teardown now that they exist. + if (closing.get()) throw IOException("Connection closed while connecting") val newSession: Session = if (agentKeys.isEmpty()) { ssh.startSession() diff --git a/app/src/test/java/io/github/lordofpolls/shellwave/ssh/ConnectTimeoutTest.kt b/app/src/test/java/io/github/lordofpolls/shellwave/ssh/ConnectTimeoutTest.kt new file mode 100644 index 0000000..2ce754b --- /dev/null +++ b/app/src/test/java/io/github/lordofpolls/shellwave/ssh/ConnectTimeoutTest.kt @@ -0,0 +1,30 @@ +package io.github.lordofpolls.shellwave.ssh + +import java.io.IOException +import java.net.ServerSocket +import net.schmizz.sshj.SSHClient +import net.schmizz.sshj.transport.verification.PromiscuousVerifier +import org.junit.Assert.assertEquals +import org.junit.Test + +class ConnectTimeoutTest { + + @Test + fun connectAndAuthenticate_boundsTheSocketConnect() { + // A port that was just free and is now closed refuses at once, so nothing waits. + val port = ServerSocket(0).use { it.localPort } + val ssh = SSHClient() + try { + connectAndAuthenticate( + ssh, + "127.0.0.1", + port, + "user", + AuthMethod.Password("pw"), + PromiscuousVerifier(), + ) + } catch (_: IOException) { + } + assertEquals(CONNECT_TIMEOUT_MS, ssh.connectTimeout) + } +}