forked from kay/RouterOS
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
94 lines
3.4 KiB
Swift
94 lines
3.4 KiB
Swift
import Foundation
|
|
|
|
/// Drives the REST-first, SSH-fallback connection flow and holds the connected device's state.
|
|
@MainActor
|
|
final class ConnectionService: ObservableObject {
|
|
enum State: Equatable {
|
|
case idle
|
|
case connecting
|
|
case connected(kind: RouterOSTransportKind)
|
|
case needsCertificateConfirmation(fingerprint: String)
|
|
case failed(String)
|
|
}
|
|
|
|
@Published private(set) var state: State = .idle
|
|
@Published private(set) var deviceInfo: RouterDeviceInfo?
|
|
@Published private(set) var interfaces: [NetworkInterface] = []
|
|
/// Credentials of the current (or last attempted) connection, shared with features
|
|
/// that need their own dedicated connection, e.g. BackupService's SSH export.
|
|
@Published private(set) var credentials: RouterOSCredentials?
|
|
|
|
private let certificateTrust: CertificateTrustStore
|
|
private var activeTransport: RouterOSTransport?
|
|
|
|
init(certificateTrust: CertificateTrustStore = CertificateTrustStore()) {
|
|
self.certificateTrust = certificateTrust
|
|
}
|
|
|
|
/// Injection point for tests: bypasses the real REST/SSH transports.
|
|
func connect(with credentials: RouterOSCredentials, makeRestTransport: () -> RouterOSTransport, makeSSHTransport: () -> RouterOSTransport) async {
|
|
state = .connecting
|
|
self.credentials = credentials
|
|
|
|
let rest = makeRestTransport()
|
|
do {
|
|
try await rest.connect()
|
|
await finishConnecting(using: rest)
|
|
return
|
|
} catch RouterOSError.untrustedCertificate(let fingerprint) {
|
|
state = .needsCertificateConfirmation(fingerprint: fingerprint)
|
|
return
|
|
} catch {
|
|
// REST fehlgeschlagen (z.B. altes RouterOS ohne REST-API) -> SSH-Fallback versuchen.
|
|
}
|
|
|
|
let ssh = makeSSHTransport()
|
|
do {
|
|
try await ssh.connect()
|
|
await finishConnecting(using: ssh)
|
|
} catch {
|
|
state = .failed(error.localizedDescription)
|
|
}
|
|
}
|
|
|
|
func connect(with credentials: RouterOSCredentials) async {
|
|
await connect(
|
|
with: credentials,
|
|
makeRestTransport: { RestTransport(credentials: credentials, certificateTrust: self.certificateTrust) },
|
|
makeSSHTransport: { SSHTransport(credentials: credentials) }
|
|
)
|
|
}
|
|
|
|
func trustCurrentCertificateAndRetry(fingerprint: String) async {
|
|
guard let credentials else { return }
|
|
certificateTrust.trust(host: credentials.host, fingerprint: fingerprint)
|
|
await connect(with: credentials)
|
|
}
|
|
|
|
/// Applies a single configuration change on the active transport (REST or SSH).
|
|
func apply(_ command: RouterOSCommand) async throws {
|
|
guard let activeTransport else { throw RouterOSError.notConnected }
|
|
try await activeTransport.apply(command)
|
|
}
|
|
|
|
private func finishConnecting(using transport: RouterOSTransport) async {
|
|
activeTransport = transport
|
|
do {
|
|
deviceInfo = try await transport.fetchDeviceInfo()
|
|
interfaces = try await transport.fetchInterfaces()
|
|
state = .connected(kind: transport.kind)
|
|
} catch {
|
|
state = .failed(error.localizedDescription)
|
|
}
|
|
}
|
|
|
|
func disconnect() async {
|
|
await activeTransport?.disconnect()
|
|
activeTransport = nil
|
|
deviceInfo = nil
|
|
interfaces = []
|
|
credentials = nil
|
|
state = .idle
|
|
}
|
|
}
|