Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

20.9 KB
Raw Blame
import Foundation
@preconcurrency import NIOCore
@preconcurrency import NIOConcurrencyHelpers
@preconcurrency import NIOPosix
@preconcurrency import NIOSSH

/// Result of an SSH command execution
public struct SSHResult: Sendable {
    public let exitCode: Int32
    public let output: String

    public init(exitCode: Int32, output: String) {
        self.exitCode = exitCode
        self.output = output
    }
}

/// SSH client using SwiftNIO SSH for typed, versioned API
public actor SSHClient {
    private let host: String
    private let port: Int
    private let user: String
    private let password: String
    private let connectTimeout: TimeInterval

    /// Networking infrastructure has process lifetime, not client lifetime.
    private static let sharedEventLoopGroup = MultiThreadedEventLoopGroup.singleton

    public init(
        host: String,
        port: UInt16 = 22,
        user: String = "lume",
        password: String = "lume",
        connectTimeout: TimeInterval = 30
    ) {
        self.host = host
        self.port = Int(port)
        self.user = user
        self.password = password
        self.connectTimeout = connectTimeout
    }

    /// Execute a command on the remote host
    public func execute(command: String, timeout: TimeInterval = 60) async throws -> SSHResult {
        let channel = try await connect()

        // Create a promise for the result
        let resultPromise = channel.eventLoop.makePromise(of: SSHResult.self)
        let resultState = CommandResultState(promise: resultPromise)

        // Create the child promise only after locating NIOSSHHandler. If the
        // connection closes first, flatMap is skipped; a promise created outside
        // this closure would then be released unfulfilled and SwiftNIO traps in
        // EventLoopFuture.deinit.
        let childChannel: Channel
        do {
            childChannel = try await Self.makeChildChannelFuture(
                handlerFuture: channel.pipeline.handler(type: NIOSSHHandler.self),
                eventLoop: channel.eventLoop
            ) { sshHandler, childChannelPromise in
                sshHandler.createChannel(childChannelPromise) { childChannel, channelType in
                    guard channelType == .session else {
                        return channel.eventLoop.makeFailedFuture(
                            SSHError.connectionFailed("Invalid channel type")
                        )
                    }

                    return childChannel.eventLoop.makeCompletedFuture {
                        let execHandler = CommandExecHandler(
                            command: command,
                            resultState: resultState
                        )
                        try childChannel.pipeline.syncOperations.addHandler(execHandler)
                    }
                }
            }.get()
        } catch {
            // The command handler never took ownership of resultState, so resolve
            // its promise before unwinding this operation.
            resultState.fail(error)
            try? await channel.close().get()
            throw error
        }

        // Set up timeout if specified
        if timeout > 0 {
            let timeoutTask = channel.eventLoop.scheduleTask(in: .seconds(Int64(timeout))) {
                resultState.fail(SSHError.timeout)
                childChannel.close(promise: nil)
            }

            // Cancel timeout when result is received
            resultState.futureResult.whenComplete { _ in
                timeoutTask.cancel()
            }
        }

        do {
            let result = try await resultState.futureResult.get()
            try? await childChannel.closeFuture.get()
            try? await channel.close().get()
            return result
        } catch {
            try? await childChannel.close().get()
            try? await channel.close().get()
            throw error
        }
    }

    /// Start an interactive SSH session
    public func interactive() async throws {
        let channel = try await connect()

        // As in execute(), do not allocate the child promise until handler
        // lookup succeeds or a disconnect race can leak an unfulfilled promise.
        let sessionCompletePromise = channel.eventLoop.makePromise(of: Void.self)
        let childChannel: Channel
        do {
            childChannel = try await Self.makeChildChannelFuture(
                handlerFuture: channel.pipeline.handler(type: NIOSSHHandler.self),
                eventLoop: channel.eventLoop
            ) { sshHandler, childChannelPromise in
                sshHandler.createChannel(childChannelPromise) { childChannel, channelType in
                    guard channelType == .session else {
                        return channel.eventLoop.makeFailedFuture(
                            SSHError.connectionFailed("Invalid channel type")
                        )
                    }

                    return childChannel.eventLoop.makeCompletedFuture {
                        let interactiveHandler = InteractiveSessionHandler(
                            completePromise: sessionCompletePromise
                        )
                        try childChannel.pipeline.syncOperations.addHandler(interactiveHandler)
                    }
                }
            }.get()
        } catch {
            sessionCompletePromise.fail(error)
            try? await channel.close().get()
            throw error
        }

        // Wait for the session to complete
        try await sessionCompletePromise.futureResult.get()

        // Clean up
        try? await childChannel.closeFuture.get()
        try? await channel.close().get()
    }

    /// Creates the child promise only after handler lookup succeeds. SwiftNIO
    /// deliberately traps when an unfulfilled promise is deallocated, so callers
    /// must not allocate this promise before the prerequisite future completes.
    static func makeChildChannelFuture(
        handlerFuture: EventLoopFuture<NIOSSHHandler>,
        eventLoop: EventLoop,
        initialize: @escaping @Sendable (NIOSSHHandler, EventLoopPromise<Channel>) -> Void
    ) -> EventLoopFuture<Channel> {
        handlerFuture.flatMap { sshHandler in
            let childChannelPromise = eventLoop.makePromise(of: Channel.self)
            initialize(sshHandler, childChannelPromise)
            return childChannelPromise.futureResult
        }
    }

    /// Connect to the SSH server and return the channel
    private func connect() async throws -> Channel {
        let user = self.user
        let password = self.password
        let bootstrap = ClientBootstrap(group: Self.sharedEventLoopGroup)
            .channelInitializer { channel in
                channel.eventLoop.makeCompletedFuture {
                    let sshHandler = NIOSSHHandler(
                        role: .client(
                            .init(
                                userAuthDelegate: PasswordAuthDelegate(
                                    username: user,
                                    password: password
                                ),
                                serverAuthDelegate: AcceptAllHostKeysDelegate()
                            )
                        ),
                        allocator: channel.allocator,
                        inboundChildChannelInitializer: nil
                    )
                    try channel.pipeline.syncOperations.addHandler(sshHandler)
                    try channel.pipeline.syncOperations.addHandler(SSHErrorHandler())
                }
            }
            .channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
            .channelOption(ChannelOptions.socket(SocketOptionLevel(IPPROTO_TCP), TCP_NODELAY), value: 1)
            .connectTimeout(.seconds(max(1, Int64(connectTimeout.rounded(.up)))))

        do {
            return try await bootstrap.connect(host: host, port: port).get()
        } catch {
            throw SSHError.connectionFailed(error.localizedDescription)
        }
    }
}

// MARK: - Host Key Validation Delegate

/// Accepts all host keys (for internal VM use only - not for production)
private final class AcceptAllHostKeysDelegate: NIOSSHClientServerAuthenticationDelegate, Sendable {
    func validateHostKey(
        hostKey: NIOSSHPublicKey,
        validationCompletePromise: EventLoopPromise<Void>
    ) {
        // Accept all host keys for internal VM connections
        // In production, you should validate against known hosts
        validationCompletePromise.succeed(())
    }
}

// MARK: - Password Authentication Delegate

/// Handles password-based authentication
private final class PasswordAuthDelegate: NIOSSHClientUserAuthenticationDelegate, Sendable {
    private let username: String
    private let password: String
    private let didAttempt: NIOLockedValueBox<Bool>

    init(username: String, password: String) {
        self.username = username
        self.password = password
        self.didAttempt = NIOLockedValueBox(false)
    }

    func nextAuthenticationType(
        availableMethods: NIOSSHAvailableUserAuthenticationMethods,
        nextChallengePromise: EventLoopPromise<NIOSSHUserAuthenticationOffer?>
    ) {
        // Only attempt password auth once
        let alreadyAttempted = didAttempt.withLockedValue { attempted in
            if attempted {
                return true
            }
            attempted = true
            return false
        }

        if alreadyAttempted {
            // We already tried, authentication failed
            nextChallengePromise.fail(SSHError.authenticationFailed)
            return
        }

        guard availableMethods.contains(.password) else {
            nextChallengePromise.fail(SSHError.authenticationFailed)
            return
        }

        nextChallengePromise.succeed(
            NIOSSHUserAuthenticationOffer(
                username: username,
                serviceName: "",
                offer: .password(.init(password: password))
            )
        )
    }
}

// MARK: - Command Execution Handler

private final class CommandResultState: @unchecked Sendable {
    private let promise: EventLoopPromise<SSHResult>
    private let completed = NIOLockedValueBox(false)

    init(promise: EventLoopPromise<SSHResult>) {
        self.promise = promise
    }

    var futureResult: EventLoopFuture<SSHResult> { promise.futureResult }

    func succeed(_ result: SSHResult) {
        guard claimCompletion() else { return }
        promise.succeed(result)
    }

    func fail(_ error: Error) {
        guard claimCompletion() else { return }
        promise.fail(error)
    }

    private func claimCompletion() -> Bool {
        completed.withLockedValue { completed in
            guard !completed else { return false }
            completed = true
            return true
        }
    }
}

/// Handles command execution on an SSH channel
/// Note: @unchecked Sendable is safe because this handler is only used on a single event loop
private final class CommandExecHandler: ChannelDuplexHandler, @unchecked Sendable {
    typealias InboundIn = SSHChannelData
    typealias InboundOut = ByteBuffer
    typealias OutboundIn = ByteBuffer
    typealias OutboundOut = SSHChannelData

    private let command: String
    private let resultState: CommandResultState
    private var outputBuffer: ByteBuffer
    private var exitStatus: Int32?
    private var channelClosed = false

    init(command: String, resultState: CommandResultState) {
        self.command = command
        self.resultState = resultState
        self.outputBuffer = ByteBuffer()
    }

    func handlerAdded(context: ChannelHandlerContext) {
        // nonisolated(unsafe) is safe here because we're on the channel's event loop
        nonisolated(unsafe) let ctx = context
        context.channel.setOption(ChannelOptions.allowRemoteHalfClosure, value: true).whenFailure { error in
            ctx.fireErrorCaught(error)
        }
    }

    func channelActive(context: ChannelHandlerContext) {
        // Send exec request
        // nonisolated(unsafe) is safe here because we're on the channel's event loop
        nonisolated(unsafe) let ctx = context
        let execRequest = SSHChannelRequestEvent.ExecRequest(command: command, wantReply: true)
        context.triggerUserOutboundEvent(execRequest).whenFailure { [weak self] error in
            self?.failWithError(error, context: ctx)
        }
    }

    func channelRead(context: ChannelHandlerContext, data: NIOAny) {
        let channelData = unwrapInboundIn(data)

        switch channelData.data {
        case .byteBuffer(let buffer):
            // Collect all output (stdout and stderr)
            var mutableBuffer = buffer
            outputBuffer.writeBuffer(&mutableBuffer)
        case .fileRegion:
            // Not expected, ignore
            break
        }
    }

    func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
        switch event {
        case let status as SSHChannelRequestEvent.ExitStatus:
            exitStatus = Int32(status.exitStatus)
            checkCompletion(context: context)

        case let channelEvent as ChannelEvent where channelEvent == .inputClosed:
            channelClosed = true
            checkCompletion(context: context)

        default:
            context.fireUserInboundEventTriggered(event)
        }
    }

    func channelInactive(context: ChannelHandlerContext) {
        channelClosed = true
        if exitStatus == nil {
            failForMissingExitStatus()
        } else {
            checkCompletion(context: context)
        }
    }

    func errorCaught(context: ChannelHandlerContext, error: Error) {
        failWithError(error, context: context)
    }

    func handlerRemoved(context: ChannelHandlerContext) {
        if exitStatus == nil {
            failForMissingExitStatus()
        } else {
            checkCompletion(context: context)
        }
    }

    private func checkCompletion(context: ChannelHandlerContext) {
        if let status = exitStatus, channelClosed {
            let output = outputBuffer.readString(length: outputBuffer.readableBytes) ?? ""
            resultState.succeed(SSHResult(exitCode: status, output: output))
        }
    }

    private func failForMissingExitStatus() {
        resultState.fail(
            SSHError.commandFailed(
                exitCode: -1,
                message: "SSH channel closed without an exit status"
            )
        )
    }

    private func failWithError(_ error: Error, context: ChannelHandlerContext) {
        if let sshError = error as? SSHError {
            resultState.fail(sshError)
        } else {
            resultState.fail(SSHError.commandFailed(exitCode: -1, message: String(describing: error)))
        }
        context.close(promise: nil)
    }
}

// MARK: - Interactive Session Handler

/// Handles interactive SSH sessions with stdin/stdout passthrough
/// Note: @unchecked Sendable is safe because this handler is only used on a single event loop
private final class InteractiveSessionHandler: ChannelDuplexHandler, @unchecked Sendable {
    typealias InboundIn = SSHChannelData
    typealias InboundOut = ByteBuffer
    typealias OutboundIn = ByteBuffer
    typealias OutboundOut = SSHChannelData

    private var completePromise: EventLoopPromise<Void>?
    private var stdinSource: DispatchSourceRead?
    private var originalTermios: termios?
    private let stdinFD = FileHandle.standardInput.fileDescriptor

    init(completePromise: EventLoopPromise<Void>) {
        self.completePromise = completePromise
    }

    func handlerAdded(context: ChannelHandlerContext) {
        // nonisolated(unsafe) is safe here because we're on the channel's event loop
        nonisolated(unsafe) let ctx = context
        context.channel.setOption(ChannelOptions.allowRemoteHalfClosure, value: true).whenFailure { error in
            ctx.fireErrorCaught(error)
        }
    }

    func channelActive(context: ChannelHandlerContext) {
        // nonisolated(unsafe) is safe here because we're on the channel's event loop
        nonisolated(unsafe) let ctx = context

        // Request a pseudo-terminal
        let ptyRequest = SSHChannelRequestEvent.PseudoTerminalRequest(
            wantReply: true,
            term: "xterm-256color",
            terminalCharacterWidth: 80,
            terminalRowHeight: 24,
            terminalPixelWidth: 0,
            terminalPixelHeight: 0,
            terminalModes: .init([:])
        )

        context.triggerUserOutboundEvent(ptyRequest).flatMap {
            // Request a shell
            let shellRequest = SSHChannelRequestEvent.ShellRequest(wantReply: true)
            return ctx.triggerUserOutboundEvent(shellRequest)
        }.whenComplete { [weak self] result in
            // Ensure we're on the event loop for NIOLoopBound creation
            guard let self = self else { return }
            ctx.eventLoop.execute { [weak self] in
                switch result {
                case .success:
                    self?.setupTerminalAndStdin(context: ctx)
                case .failure(let error):
                    self?.failWithError(error, context: ctx)
                }
            }
        }
    }

    private func setupTerminalAndStdin(context: ChannelHandlerContext) {
        // Save and set raw terminal mode
        var rawTermios = termios()
        if tcgetattr(stdinFD, &rawTermios) == 0 {
            originalTermios = rawTermios
            cfmakeraw(&rawTermios)
            tcsetattr(stdinFD, TCSANOW, &rawTermios)
        }

        // Set up stdin reading
        let source = DispatchSource.makeReadSource(fileDescriptor: stdinFD, queue: .global())
        stdinSource = source

        // Capture channel and eventLoop directly (both are Sendable)
        // Don't use NIOLoopBound as accessing .value requires being on the event loop
        let channel = context.channel
        let eventLoop = context.eventLoop
        let fd = stdinFD

        source.setEventHandler { [weak self] in
            guard self != nil else { return }

            var buffer = [UInt8](repeating: 0, count: 1024)
            let bytesRead = Darwin.read(fd, &buffer, buffer.count)

            if bytesRead > 0 {
                let data = Data(buffer[0..<bytesRead])
                eventLoop.execute {
                    var byteBuffer = channel.allocator.buffer(capacity: bytesRead)
                    byteBuffer.writeBytes(data)
                    let channelData = SSHChannelData(type: .channel, data: .byteBuffer(byteBuffer))
                    channel.writeAndFlush(NIOAny(channelData), promise: nil)
                }
            } else if bytesRead == 0 {
                // EOF on stdin
                eventLoop.execute {
                    channel.close(mode: .output, promise: nil)
                }
            }
        }

        source.setCancelHandler { [weak self] in
            self?.restoreTerminal()
        }

        source.resume()
    }

    func channelRead(context: ChannelHandlerContext, data: NIOAny) {
        let channelData = unwrapInboundIn(data)

        switch channelData.data {
        case .byteBuffer(let buffer):
            // Write to stdout
            let stdoutFD = FileHandle.standardOutput.fileDescriptor
            buffer.withUnsafeReadableBytes { ptr in
                _ = Darwin.write(stdoutFD, ptr.baseAddress, ptr.count)
            }
        case .fileRegion:
            break
        }
    }

    func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
        if let channelEvent = event as? ChannelEvent, channelEvent == .inputClosed {
            complete(context: context)
        } else {
            context.fireUserInboundEventTriggered(event)
        }
    }

    func channelInactive(context: ChannelHandlerContext) {
        complete(context: context)
    }

    func errorCaught(context: ChannelHandlerContext, error: Error) {
        failWithError(error, context: context)
    }

    func handlerRemoved(context: ChannelHandlerContext) {
        cleanup()
    }

    private func complete(context: ChannelHandlerContext) {
        cleanup()
        if let promise = completePromise {
            completePromise = nil
            promise.succeed(())
        }
    }

    private func failWithError(_ error: Error, context: ChannelHandlerContext) {
        cleanup()
        if let promise = completePromise {
            completePromise = nil
            promise.fail(error)
        }
        context.close(promise: nil)
    }

    private func cleanup() {
        stdinSource?.cancel()
        stdinSource = nil
        restoreTerminal()
    }

    private func restoreTerminal() {
        if var original = originalTermios {
            tcsetattr(stdinFD, TCSANOW, &original)
            originalTermios = nil
        }
    }
}

// MARK: - Error Handler

/// Handles errors in the SSH pipeline
/// Note: @unchecked Sendable is safe because this handler is only used on a single event loop
private final class SSHErrorHandler: ChannelInboundHandler, @unchecked Sendable {
    typealias InboundIn = Any

    func errorCaught(context: ChannelHandlerContext, error: Error) {
        // Log or handle the error appropriately
        context.close(promise: nil)
    }
}