forked from kay/RouterOS
Kritischer Fund beim M6-Live-Test: RouterOSCliParser.parseInterfaces
splittete nur auf "\n", aber die SSH-Ausgabe eines echten hEX-Routers
trennt Zeilen anders -- alle Interface-Zeilen wurden zu EINER Zeile
zusammengefasst. Beim key=value-Parsen dieser einen Riesenzeile
überschrieb jedes Feld (name=, type=, ...) den vorherigen Wert, sodass
am Ende nur das letzte Interface im Text ("lo", Loopback) übrig blieb
-- mit den Feldwerten aller anderen Interfaces vermischt.
Folge: WAN-Schritt zeigte nur "lo" zur Auswahl, wodurch alle
WAN-Interface-Referenzen (NAT-Masquerade, ICMP-Regel, WAN-Block-Regel,
finale Anti-Spoofing-Regel) fälschlich auf "lo" statt den echten
WAN-Port zeigten. Auf diesem Testgerät blieb es folgenlos, weil RouterOS
schon eine vollständige eigene Standard-Firewall (defconf) mitbrachte,
die den echten Schutz weiterhin übernahm -- auf einem Gerät ohne
bestehende Firewall hätte das eine wirkungslose Firewall bedeutet, die
sich als aktiv ausgegeben hätte.
Fix: split(whereSeparator: \.isNewline) statt split(separator: "\n"),
robust gegen \n/\r/\r\n. Zusätzliches Sicherheitsnetz in SetupView:
Loopback-Interfaces werden aus allen WAN/LAN/VLAN-Auswahllisten
gefiltert, damit ein ähnlicher Parser-Fehler künftig nicht erneut zu
einer sinnlosen Interface-Auswahl führen kann. Regressionstest mit
realen \r\n-getrennten hEX-Daten ergänzt.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HReLXMbmPvtQ23p1iWiJNW
65 lines
3.1 KiB
Swift
65 lines
3.1 KiB
Swift
import Foundation
|
|
|
|
/// Best-effort parser for RouterOS CLI output over SSH.
|
|
/// Not verified against a live device yet — check against real router output during the M2 dry-run pass.
|
|
enum RouterOSCliParser {
|
|
/// Parses `/system resource print` output (colon-separated "key: value" lines).
|
|
static func parseDeviceInfo(_ raw: String) -> RouterDeviceInfo {
|
|
var fields: [String: String] = [:]
|
|
for line in raw.split(whereSeparator: \.isNewline) {
|
|
guard let colonIndex = line.firstIndex(of: ":") else { continue }
|
|
let key = String(line[line.startIndex..<colonIndex]).trimmingCharacters(in: .whitespaces)
|
|
let value = String(line[line.index(after: colonIndex)...]).trimmingCharacters(in: .whitespaces)
|
|
fields[key] = value
|
|
}
|
|
return RouterDeviceInfo(
|
|
boardName: fields["board-name"] ?? "unbekannt",
|
|
routerOSVersion: fields["version"] ?? "unbekannt",
|
|
architecture: fields["architecture-name"] ?? "unbekannt",
|
|
uptime: fields["uptime"] ?? "-"
|
|
)
|
|
}
|
|
|
|
/// Parses `/interface print terse` output (one line per interface, `key=value` pairs).
|
|
///
|
|
/// Splits on any newline-like character, not just "\n" — a real hEX device's SSH output
|
|
/// turned out not to split on bare "\n", collapsing every interface into a single "line"
|
|
/// whose repeated keys (name=, type=, ...) then overwrote each other in the fields
|
|
/// dictionary, leaving only the last interface (`lo`) in the result. Found via live testing.
|
|
static func parseInterfaces(_ raw: String) -> [NetworkInterface] {
|
|
raw.split(whereSeparator: \.isNewline).compactMap { line in
|
|
let fields = keyValues(from: String(line))
|
|
guard let name = fields["name"] else { return nil }
|
|
return NetworkInterface(
|
|
name: name,
|
|
type: fields["type"] ?? "unbekannt",
|
|
running: isTrue(fields["running"]),
|
|
disabled: isTrue(fields["disabled"]),
|
|
macAddress: fields["mac-address"]
|
|
)
|
|
}
|
|
}
|
|
|
|
/// RouterOS CLI output mixes "true"/"false" and "yes"/"no" for booleans depending on field/version.
|
|
private static func isTrue(_ value: String?) -> Bool {
|
|
value == "true" || value == "yes"
|
|
}
|
|
|
|
private static func keyValues(from text: String) -> [String: String] {
|
|
var result: [String: String] = [:]
|
|
let pattern = #"([a-zA-Z0-9-]+)=("[^"]*"|\S+)"#
|
|
guard let regex = try? NSRegularExpression(pattern: pattern) else { return result }
|
|
let nsText = text as NSString
|
|
let matches = regex.matches(in: text, range: NSRange(location: 0, length: nsText.length))
|
|
for match in matches {
|
|
let key = nsText.substring(with: match.range(at: 1))
|
|
var value = nsText.substring(with: match.range(at: 2))
|
|
if value.hasPrefix("\""), value.hasSuffix("\""), value.count >= 2 {
|
|
value = String(value.dropFirst().dropLast())
|
|
}
|
|
result[key] = value
|
|
}
|
|
return result
|
|
}
|
|
}
|