Merge main: resolve conflict, keep WebRTC NAT fix + all new features

Resolved conflict in switch_backend.py — kept both:
- Our branch: backup/restore, firewall matrix, services, scheduling,
  topology, PoE, port forwarding, WireGuard DNS profiles
- Main: OPNsense NAT WebRTC/Mattermost calls fix (symmetric NAT →
  hybrid outbound NAT + static port rules)

https://claude.ai/code/session_01Do9bsN39MTuy2GVv7yzSrE
This commit is contained in:
Claude
2026-03-28 14:20:08 +00:00
2 changed files with 636 additions and 0 deletions
+135
View File
@@ -5525,3 +5525,138 @@ def poe_budget():
})
return result
# ══════════════════════════════════════════════════════════════════════
# OPNSENSE NAT — WEBRTC / MATTERMOST CALLS FIX
# ══════════════════════════════════════════════════════════════════════
#
# Problem: OPNsense uses symmetric NAT by default (port address translation).
# Each UDP flow to a *different* destination gets a *different* external source
# port. WebRTC ICE relies on STUN to discover the external address, but with
# symmetric NAT the STUN server sees a different port than the TURN or peer
# server will see — ICE candidate matching fails and calls drop. A commercial
# VPN "fixes" it because the VPN encapsulates UDP inside a single TCP/UDP
# tunnel that is full-cone from OPNsense's perspective.
#
# Fix: add a "static port" outbound NAT rule for each VLAN subnet.
# "Static port" (fixedport in pfSense/OPNsense) preserves the source port
# number through NAT. The external port equals the internal port, so every
# STUN server sees the same address:port — ICE succeeds.
#
# This requires switching outbound NAT from "Automatic" to "Hybrid" mode
# (hybrid = keep automatic rules, also honour manual ones). The static-port
# rules are added for UDP only; TCP and other protocols are unaffected.
# ══════════════════════════════════════════════════════════════════════
@app.post("/api/opnsense/nat/fix-webrtc")
def opnsense_nat_fix_webrtc(body: dict):
"""
Fix WebRTC / Mattermost Calls / STUN failures caused by symmetric NAT.
Steps:
1. Switch outbound NAT mode to Hybrid (preserves automatic rules).
2. For each VLAN subnet in body.vlans, add a UDP static-port outbound
NAT rule on the WAN interface. Static port = source port preserved
through NAT so STUN candidates are consistent across servers.
3. Apply changes.
Body: { token, vlans: [{id, name, subnet}], wan_interface: "wan" }
Returns: { success, mode_set, rules_added: [...], rules_failed: [...] }
"""
require_session(body.get("token", ""))
cfg = _load_opnsense_cfg()
if not cfg.get("host"):
raise HTTPException(503, "OPNsense API not configured")
vlans = body.get("vlans", [])
wan_iface = body.get("wan_interface", "wan")
rules_added = []
rules_failed = []
# Step 1 — switch to Hybrid outbound NAT mode
try:
_opnsense_request(cfg, "firewall/nat/outbound/setMode",
method="POST", body={"mode": "hybrid"})
mode_set = True
except Exception as e:
raise HTTPException(500, f"Could not set outbound NAT to hybrid: {e}")
# Step 2 — add a static-port UDP rule for each VLAN
for vlan in vlans:
subnet = vlan.get("subnet", "").strip()
name = vlan.get("name", f"VLAN{vlan.get('id','')}")
if not subnet:
continue
rule = {
"rule": {
"enabled": "1",
"sequence": "1",
"interface": wan_iface,
"ipprotocol": "inet",
"protocol": "UDP",
"source": {"network": subnet, "port": ""},
"sourceport": "",
"destination": {"network": "any", "port": ""},
"destinationport": "",
"target": "",
"targetip": "",
"targetip_subnet": "32",
"nonat": "0",
"staticnatport": "1",
"descr": f"Static port UDP — {name} WebRTC/STUN fix",
}
}
try:
resp = _opnsense_request(cfg, "firewall/nat/outbound/addRule",
method="POST", body=rule)
rules_added.append({"vlan": name, "subnet": subnet, "uuid": resp.get("uuid","")})
except Exception as e:
rules_failed.append({"vlan": name, "subnet": subnet, "error": str(e)})
# Step 3 — apply
try:
_opnsense_request(cfg, "firewall/nat/outbound/apply", method="POST")
except Exception as e:
rules_failed.append({"vlan": "apply", "error": str(e)})
return {
"success": len(rules_added) > 0 and not rules_failed,
"mode_set": mode_set,
"rules_added": rules_added,
"rules_failed": rules_failed,
"explanation": (
"Static-port NAT preserves UDP source ports through NAT. "
"STUN now sees the same external address regardless of destination server. "
"WebRTC ICE candidates match — calls work without VPN."
),
}
@app.get("/api/opnsense/nat/webrtc-status")
def opnsense_nat_webrtc_status():
"""
Check whether static-port outbound NAT rules exist for WebRTC.
Returns current outbound NAT mode and any existing static-port rules.
"""
cfg = _load_opnsense_cfg()
if not cfg.get("host"):
raise HTTPException(503, "OPNsense API not configured")
try:
data = _opnsense_request(cfg, "firewall/nat/outbound/get")
mode = data.get("natoutbound", {}).get("mode", "unknown")
rules = data.get("natoutbound", {}).get("rule", {})
static_rules = [
{"uuid": uid, "descr": r.get("descr",""), "source": r.get("source",{})}
for uid, r in (rules.items() if isinstance(rules, dict) else {}.items())
if str(r.get("staticnatport","0")) == "1"
]
return {
"mode": mode,
"hybrid": mode == "hybrid",
"static_rules": static_rules,
"webrtc_ready": mode == "hybrid" and len(static_rules) > 0,
}
except Exception as e:
raise HTTPException(500, str(e))