Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -52,6 +58,7 @@ internal fun connectChainAndAuthenticate(
targetUsername: String,
targetAuthMethod: AuthMethod,
hostKeyVerifier: HostKeyVerifier,
isClosing: () -> Boolean = { false },
): ProxyChainResources {
if (hops.isEmpty()) {
connectAndAuthenticate(
Expand All @@ -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) {
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading