Standardmäßig aus (Toggle wie VLAN) -- höchstes Risiko aller bisherigen Schritte, falsche Regeln können Fernzugriff kappen. Preset ist Mikrotiks eigener Standard-Ansatz (unverändert seit Jahren in RouterOS-Werkskonfigurationen): NAT/Masquerade auf WAN, established/ related erlauben, invalid verwerfen, unaufgeforderte WAN-Verbindungen zu LAN-Geräten blocken (außer explizitem Port-Forward via connection-nat-state=!dstnat). Jede neue Regel bekommt ein place-before mit aufsteigendem Index, damit sie vor eventuell schon vorhandenen Regeln des Routers landet -- sonst könnte eine bereits vorhandene "alles blocken"-Regel unsere neuen Regeln wirkungslos machen. NAT und Filter sind getrennte, unabhängig nummerierte RouterOS-Listen. Vor dem Anwenden zeigt der Schritt die Anzahl bereits vorhandener Filter-/NAT-Regeln (neuer fetchFirewallRuleCounts()-Aufruf in RouterOSTransport/RestTransport/SSHTransport/ConnectionService) -- Transparenz, bevor auf einem möglicherweise schon konfigurierten Router weitere Regeln landen. Nutzer-Entscheidung, extra Lese-Aufruf in Kauf zu nehmen statt nur Warntext. Build + Test-Compile (build-for-testing) sind grün. Der eigentliche Testlauf (xcodebuild test) hängt aktuell an einem macOS-Gatekeeper- Netzwerk-Check für ad-hoc-signierte Binaries (amfid: "adhoc signed or signed by an unknown certificate chain", GK performScan über syspolicyd) -- kein Code-Bug, tritt nur bei CLI-Testläufen auf, nicht beim normalen Xcode-Cmd+R-Weg. Nutzer verifiziert M6 deshalb direkt in Xcode. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HReLXMbmPvtQ23p1iWiJNW
85 lines
3.4 KiB
Swift
85 lines
3.4 KiB
Swift
import Foundation
|
|
|
|
struct FirewallRuleCounts: Equatable {
|
|
var filterRuleCount: Int
|
|
var natRuleCount: Int
|
|
}
|
|
|
|
/// A safe-default firewall + NAT preset, standard Mikrotik best practice (matches the ruleset
|
|
/// shipped in RouterOS' own factory-default home-router configurations, unchanged across
|
|
/// RouterOS versions for well over a decade): NAT/masquerade the WAN interface, allow
|
|
/// established/related traffic, drop invalid packets, and drop unsolicited connections
|
|
/// arriving from the WAN that aren't destination-NATed (i.e. not an explicit port forward).
|
|
///
|
|
/// Every rule gets an incrementing `place-before` so it lands ahead of whatever the router
|
|
/// already has in that chain (filter and NAT are separate, independently numbered lists) —
|
|
/// otherwise, on a device that already has firewall rules, a pre-existing catch-all rule
|
|
/// earlier in the chain could make our rules unreachable.
|
|
struct FirewallConfig: Equatable {
|
|
var wanInterface: String
|
|
|
|
func buildCommands() -> [RouterOSCommand] {
|
|
let natCommand = RouterOSCommand.add(
|
|
menuPath: "/ip firewall nat",
|
|
restPath: "ip/firewall/nat",
|
|
arguments: [
|
|
"chain": "srcnat",
|
|
"out-interface": wanInterface,
|
|
"action": "masquerade",
|
|
"place-before": "0"
|
|
],
|
|
summary: "Internetfreigabe (NAT/Masquerade) über \(wanInterface) einrichten"
|
|
)
|
|
|
|
let filterRules: [(arguments: [String: String], summary: String)] = [
|
|
(
|
|
["chain": "input", "connection-state": "established,related", "action": "accept"],
|
|
"Bestehende Verbindungen zum Router erlauben"
|
|
),
|
|
(
|
|
["chain": "input", "connection-state": "invalid", "action": "drop"],
|
|
"Ungültige Pakete zum Router verwerfen"
|
|
),
|
|
(
|
|
["chain": "input", "in-interface": wanInterface, "protocol": "icmp", "action": "accept"],
|
|
"Ping (ICMP) vom Internet zum Router erlauben"
|
|
),
|
|
(
|
|
["chain": "input", "in-interface": wanInterface, "action": "drop"],
|
|
"Restlichen Zugriff vom Internet auf den Router blockieren"
|
|
),
|
|
(
|
|
["chain": "forward", "connection-state": "established,related", "action": "accept"],
|
|
"Bestehende Verbindungen durch den Router erlauben"
|
|
),
|
|
(
|
|
["chain": "forward", "connection-state": "invalid", "action": "drop"],
|
|
"Ungültige Pakete verwerfen"
|
|
),
|
|
(
|
|
[
|
|
"chain": "forward",
|
|
"connection-state": "new",
|
|
"connection-nat-state": "!dstnat",
|
|
"in-interface": wanInterface,
|
|
"action": "drop"
|
|
],
|
|
"Unaufgeforderte Verbindungen aus dem Internet zu Geräten im Heimnetz blockieren"
|
|
)
|
|
]
|
|
|
|
let filterCommands = filterRules.enumerated().map { index, rule -> RouterOSCommand in
|
|
var arguments = rule.arguments
|
|
arguments["place-before"] = "\(index)"
|
|
return RouterOSCommand.add(
|
|
menuPath: "/ip firewall filter",
|
|
restPath: "ip/firewall/filter",
|
|
arguments: arguments,
|
|
summary: rule.summary
|
|
)
|
|
}
|
|
|
|
return [natCommand] + filterCommands
|
|
}
|
|
}
|