Files
RouterOS/RouterOSAssistant/Core/Networking/SSHTransport.swift
T
KayandClaude Sonnet 5 87f4fa188c M3: WAN + LAN/DHCP-Wizard mit Anwenden-Logik
Neuer "Einrichten"-Tab führt durch Internet-Anschluss (DHCP/statisch/
PPPoE) und Heimnetzwerk+DHCP-Server, zeigt vor dem Anwenden eine
Klartext-Übersicht (optional mit den exakten RouterOS-Befehlen) und
erstellt automatisch eine Sicherung, bevor Änderungen geschrieben
werden. Änderungen laufen über beide Transporte: CLI-Zeile für SSH,
JSON-POST für REST — beide aus einem gemeinsamen RouterOSCommand
gebaut. RouterOS-CLI-Syntax ist Standard und langjährig stabil, aber
nicht gegen ein echtes Gerät verifiziert; deshalb die Detailanzeige
im Übersichtsschritt vor dem Anwenden.

ConnectionService ist jetzt der einzige App-weite Zustand (ersetzt
SessionStore) und wird explizit an alle drei Tabs durchgereicht.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HReLXMbmPvtQ23p1iWiJNW
2026-09-11 21:07:34 +02:00

64 lines
2.3 KiB
Swift

import Foundation
import Citadel
/// SSH+CLI transport fallback for RouterOS devices/firmware without the REST API (pre-7.1).
///
/// Known limitation (tracked for M7 hardening): host key validation currently accepts any key.
/// This is acceptable for now because REST already provides certificate TOFU on the primary
/// path and this fallback is used for local-network devices only, but it should get the same
/// trust-on-first-use treatment before wider distribution.
final class SSHTransport: RouterOSTransport {
let kind: RouterOSTransportKind = .ssh
private let credentials: RouterOSCredentials
private var client: SSHClient?
init(credentials: RouterOSCredentials) {
self.credentials = credentials
}
func connect() async throws {
do {
client = try await SSHClient.connect(
host: credentials.host,
port: credentials.sshPort,
authenticationMethod: .passwordBased(username: credentials.username, password: credentials.password),
hostKeyValidator: .acceptAnything(),
reconnect: .never
)
} catch {
throw RouterOSError.transportUnavailable("SSH-Verbindung fehlgeschlagen: \(error.localizedDescription)")
}
}
func fetchDeviceInfo() async throws -> RouterDeviceInfo {
let output = try await run("/system resource print without-paging")
return RouterOSCliParser.parseDeviceInfo(output)
}
func fetchInterfaces() async throws -> [NetworkInterface] {
let output = try await run("/interface print without-paging terse")
return RouterOSCliParser.parseInterfaces(output)
}
func disconnect() async {
try? await client?.close()
client = nil
}
/// Full human-readable config export (`/export terse`), used for local backups.
func exportConfiguration() async throws -> String {
try await run("/export terse")
}
func apply(_ command: RouterOSCommand) async throws {
_ = try await run(command.cliLine)
}
private func run(_ command: String) async throws -> String {
guard let client else { throw RouterOSError.notConnected }
let buffer = try await client.executeCommand(command)
return String(buffer: buffer)
}
}