Files
RouterOS/RouterOSAssistant/Core/Models/RouterOSCommand.swift
T
KayandClaude Sonnet 5 c9ecd3a0a9 M9/M10: Einfach/Experte-Modus + Experte-Tab (generischer RouterOS-Zugriff)
M9: Einrichten-Wizard bekommt einen Einfach/Experte-Modusschalter
(ModeStepView). Einfach überspringt VLAN, erlaubt nur ein LAN-Netzwerk
ohne Isolation, Firewall-Grundschutz fest an.

M10: neuer "Experte"-Tab mit generischem Motor (RouterOSMenuItem,
RouterOSCommand.remove, ConnectionService.fetchMenuItems, freies
"eigener Menüpfad"-Feld) plus kuratierten Formularen mit Tooltips
(RouterOSSchemaCatalog) für Firewall/NAT/Mangle/Raw/Adress-Listen,
Interfaces, IP, VPN, WLAN, Queues, System, Werkzeuge.

Live gegen einen hEX-Testrouter verifiziert (erst per SSH, dann vom
Nutzer selbst in der App), dabei 7 reale Bugs gefunden und gefixt —
der wichtigste: RouterOS' SSH-CLI gibt bei fehlgeschlagenen Befehlen
Exit-Code 0 zurück, wodurch apply() app-weit Fehler verschluckte statt
sie zu melden. Danach ergänzt: Bestätigungsdialog vor Anlegen/Ändern
+ Auto-Backup vor dem ersten Experte-Tab-Schreibvorgang je Sitzung
(Angleichung an den Wizard), sowie ein Dauer-Editor (Tage/Std/Min/Sek)
für Lease-/Ablaufzeit-Felder statt Freitext.

Details zu allen Bugs/Fixes: HANDOFF.md, CHATLOG.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EW3r6rW1xCf6UT5jNvt6rn
2026-09-14 00:03:50 +02:00

108 lines
4.3 KiB
Swift

import Foundation
/// A single RouterOS configuration change, expressed once and executed over either transport
/// (rendered as a CLI line for SSH, as a JSON body — plus a lookup for `.set` — for REST).
struct RouterOSCommand: Equatable, Identifiable {
enum Operation: Equatable {
/// Creates a new item under `restPath` (SSH: `<menuPath> add ...`, REST: `POST`).
case add
/// Modifies an existing item matched by one field's value (SSH: `<menuPath> set [find
/// field=value] ...`; REST has no such inline lookup, so it needs a GET first to find
/// the item's `.id`, then `PATCH restPath/<id>`).
case set(matchField: String, matchValue: String)
/// Deletes an existing item matched by one field's value (SSH: `<menuPath> remove [find
/// field=value]`; REST again needs a GET-for-id first, then `DELETE restPath/<id>`).
case remove(matchField: String, matchValue: String)
}
var id: String {
switch operation {
case .add:
return "add:\(menuPath):\(summary)"
case .set(let field, let value):
return "set:\(menuPath):\(field)=\(value):\(summary)"
case .remove(let field, let value):
return "remove:\(menuPath):\(field)=\(value):\(summary)"
}
}
/// RouterOS menu path without the action word, e.g. "/ip address" or "/interface wireless".
let menuPath: String
/// REST resource path under `/rest/`, e.g. "ip/address" or "interface/wireless".
let restPath: String
let operation: Operation
/// RouterOS "words" (key=value fields) to add or change.
let arguments: [String: String]
/// Human-readable description shown on the review screen before applying.
let summary: String
static func add(menuPath: String, restPath: String, arguments: [String: String], summary: String) -> RouterOSCommand {
RouterOSCommand(menuPath: menuPath, restPath: restPath, operation: .add, arguments: arguments, summary: summary)
}
static func set(
menuPath: String,
restPath: String,
matchField: String,
matchValue: String,
arguments: [String: String],
summary: String
) -> RouterOSCommand {
RouterOSCommand(
menuPath: menuPath,
restPath: restPath,
operation: .set(matchField: matchField, matchValue: matchValue),
arguments: arguments,
summary: summary
)
}
static func remove(
menuPath: String,
restPath: String,
matchField: String,
matchValue: String,
summary: String
) -> RouterOSCommand {
RouterOSCommand(
menuPath: menuPath,
restPath: restPath,
operation: .remove(matchField: matchField, matchValue: matchValue),
arguments: [:],
summary: summary
)
}
/// Renders as a RouterOS CLI line, e.g. `/ip address add address=192.168.88.1/24 interface=bridge`
/// or `/interface wireless set [find name=wlan1] ssid=Home`.
var cliLine: String {
let args = renderedArguments
switch operation {
case .add:
return args.isEmpty ? "\(menuPath) add" : "\(menuPath) add \(args)"
case .set(let field, let value):
// Empty matchField = singleton menu (e.g. "/ip dns", "/system identity"): these hold
// exactly one item and are "set" directly, with no "[find ...]" — there's nothing to
// find. Confirmed live against RouterOS 7.24.2.
guard !field.isEmpty else {
return args.isEmpty ? "\(menuPath) set" : "\(menuPath) set \(args)"
}
let finder = "[find \(field)=\(Self.quoteIfNeeded(value))]"
return args.isEmpty ? "\(menuPath) set \(finder)" : "\(menuPath) set \(finder) \(args)"
case .remove(let field, let value):
return "\(menuPath) remove [find \(field)=\(Self.quoteIfNeeded(value))]"
}
}
private var renderedArguments: String {
arguments
.sorted { $0.key < $1.key }
.map { "\($0.key)=\(Self.quoteIfNeeded($0.value))" }
.joined(separator: " ")
}
private static func quoteIfNeeded(_ value: String) -> String {
value.contains(" ") ? "\"\(value)\"" : value
}
}