133 lines
5.3 KiB
Swift
133 lines
5.3 KiB
Swift
// SPDX-FileCopyrightText: 2026 Kaede Akino
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
import Foundation
|
|
import Logging
|
|
import TermindApplication
|
|
import TermindDomain
|
|
|
|
/// Application-owned construction and one-shot operation boundary for all
|
|
/// SSH/SFTP transports. Features receive this service instead of constructing
|
|
/// infrastructure clients in views.
|
|
public nonisolated final class RemoteConnectionCoordinator: Sendable {
|
|
private final class AgentConfiguration: @unchecked Sendable {
|
|
let lock = NSLock()
|
|
var factory: SSHAgentForwardingFactory?
|
|
func resolve(_ request: SSHConnectionRequest) async throws -> (any SSHAgentForwarding)? {
|
|
let factory = lock.withLock { self.factory }
|
|
return try await factory?(request)
|
|
}
|
|
}
|
|
private let agentConfiguration = AgentConfiguration()
|
|
public func updateAgentForwarding(_ factory: SSHAgentForwardingFactory?) {
|
|
agentConfiguration.lock.withLock { agentConfiguration.factory = factory }
|
|
}
|
|
private var agentFactory: SSHAgentForwardingFactory {
|
|
{ [agentConfiguration] request in try await agentConfiguration.resolve(request) }
|
|
}
|
|
private let keyboardInteractive: SSHKeyboardInteractiveHandler?
|
|
private let maxSessionsPolicy: SSHMaxSessionsPolicy
|
|
private let transportEngines: ResumableTerminalEngineRegistry
|
|
let logger: Logger
|
|
|
|
public init(
|
|
keyboardInteractive: SSHKeyboardInteractiveHandler? = nil,
|
|
logger: Logger = ApplicationLogger.disabled("termind.remote-connection"),
|
|
maxSessionsPolicy: SSHMaxSessionsPolicy = SSHMaxSessionsPolicy(),
|
|
transportEngines: ResumableTerminalEngineRegistry = .live
|
|
) {
|
|
self.keyboardInteractive = keyboardInteractive
|
|
self.logger = logger
|
|
self.maxSessionsPolicy = maxSessionsPolicy
|
|
self.transportEngines = transportEngines
|
|
}
|
|
|
|
public func updateSSHPreferences(_ preferences: SSHPreferences) {
|
|
maxSessionsPolicy.update(preferences)
|
|
}
|
|
|
|
public func withKeyboardInteractive(_ handler: @escaping SSHKeyboardInteractiveHandler)
|
|
-> RemoteConnectionCoordinator
|
|
{
|
|
RemoteConnectionCoordinator(
|
|
keyboardInteractive: handler, logger: logger,
|
|
maxSessionsPolicy: maxSessionsPolicy, transportEngines: transportEngines)
|
|
}
|
|
public func makePTYChannel(
|
|
output: @escaping SSHPTYChannel.OutputHandler,
|
|
exit: @escaping SSHPTYChannel.ExitHandler,
|
|
shellIntegration: @escaping SSHPTYChannel.ShellIntegrationHandler = { _ in }
|
|
) -> SSHPTYChannel {
|
|
SSHPTYChannel(
|
|
output: output, exit: exit, shellIntegration: shellIntegration, keyboardInteractive: keyboardInteractive,
|
|
agentForwardingFactory: agentFactory,
|
|
logger: logger, maxSessionsPolicy: maxSessionsPolicy, transportEngines: transportEngines
|
|
)
|
|
}
|
|
|
|
public func makeSFTPClient() -> any SFTPClientProtocol {
|
|
SFTPClient(
|
|
keyboardInteractive: keyboardInteractive, logger: logger,
|
|
maxSessionsPolicy: maxSessionsPolicy)
|
|
}
|
|
|
|
public func makeConnection() -> any SSHConnectionProtocol {
|
|
SSHConnection(
|
|
keyboardInteractive: keyboardInteractive, agentForwardingFactory: agentFactory, logger: logger,
|
|
maxSessionsPolicy: maxSessionsPolicy)
|
|
}
|
|
|
|
public func connectConnection(
|
|
using request: SSHConnectionRequest, progress: SSHRouteProgressHandler? = nil
|
|
) async throws -> any SSHConnectionProtocol {
|
|
let connection = SSHConnection(
|
|
keyboardInteractive: keyboardInteractive, agentForwardingFactory: agentFactory, logger: logger,
|
|
maxSessionsPolicy: maxSessionsPolicy)
|
|
do {
|
|
try await connection.connect(using: request, progress: progress, connectionFailure: nil)
|
|
return connection
|
|
} catch {
|
|
await connection.disconnect()
|
|
throw error
|
|
}
|
|
}
|
|
|
|
public func connectSFTP(
|
|
using request: SSHConnectionRequest,
|
|
progress: SSHRouteProgressHandler? = nil,
|
|
connectionFailure: (@Sendable (String) -> Void)? = nil
|
|
) async throws -> any SFTPClientProtocol {
|
|
let client = makeSFTPClient()
|
|
do {
|
|
try await client.connect(
|
|
using: request, progress: progress,
|
|
connectionFailure: connectionFailure
|
|
)
|
|
return client
|
|
} catch {
|
|
await client.disconnect()
|
|
throw error
|
|
}
|
|
}
|
|
|
|
public func keyExportServices() -> KeyExportServices {
|
|
KeyExportServices(
|
|
derivePublicKey: { key, credential, passphrase in
|
|
try await SSHKeyExportSession.derivePublicKey(key: key, credential: credential, passphrase: passphrase)
|
|
},
|
|
connect: { [self] request, progress in
|
|
let connection = SSHConnection(
|
|
keyboardInteractive: keyboardInteractive, logger: logger, maxSessionsPolicy: maxSessionsPolicy)
|
|
do {
|
|
try await connection.connect(using: request, progress: progress, connectionFailure: nil)
|
|
try Task.checkCancellation()
|
|
return SSHKeyExportSession(connection: connection)
|
|
} catch {
|
|
await connection.disconnect()
|
|
throw error
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|