Net core: re-create WireGuard tunnels after a network change (rebind)

mihomo's WireGuard outbounds keep their UDP socket bound to the address they started on:
after moving from Planet9 to Saga the Amnezia sockets were still sending from
192.168.68.x, so every Amnezia (and client) tunnel looked dead while the AmneziaVPN app,
connecting afresh, worked. Client tunnels now live in a file provider (keys/clients.yaml,
groups select them by filter) like the Amnezia keys; netctl rebind touches both files and
reloads the providers, which re-creates the tunnels on the current address without
touching other connections. BroV calls it on every network change before re-testing.
Tested on a scratch core: groups, selection across a rebind, health check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maksarsanjeev
2026-10-14 15:54:16 +03:00
parent 7c4e9abc46
commit 101a3860a9
4 changed files with 51 additions and 10 deletions
+11 -3
View File
@@ -176,7 +176,12 @@ def main():
with open(os.open(prov, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600), "w") as f:
f.write(y({"proxies": awg}) + "\n")
clients = {n: c for n, c in CLIENTS.items() if os.path.exists(os.path.join(SRC, c[0]))}
proxies = [wg(n, f) for n, (f, _, _) in clients.items()]
# Client tunnels live in a provider file too: after a network change BroV re-creates
# them (netctl rebind) so their WireGuard sockets bind to the new address.
client_proxies = [wg(n, f) for n, (f, _, _) in clients.items()]
with open(os.open(os.path.join(KEYS, "clients.yaml"), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600), "w") as f:
f.write(y({"proxies": client_proxies}) + "\n")
proxies = []
sub = open(os.path.join(SRC, "vless.sub")).read().strip()
rules = ([] if "home" in clients else [f"IP-CIDR,{HOME_NET},DIRECT,no-resolve"]) + [
@@ -200,6 +205,8 @@ def main():
# Single VLESS links added in BroV: a provider of their own (only when there are any —
# mihomo refuses an empty provider file).
extra_prov = {}
clients_prov = {"clients": {"type": "file", "path": "./keys/clients.yaml",
"health-check": {"enable": False, "url": TEST_URL}}} if clients else {}
vless_file = os.path.join(KEYS, "vless.txt")
if os.path.exists(vless_file) and any(l.strip().startswith("vless://") for l in open(vless_file)):
extra_prov["vless-keys"] = {"type": "file", "path": "./keys/vless.txt",
@@ -223,7 +230,7 @@ def main():
"nameserver": ["https://1.1.1.1/dns-query#exit", "https://8.8.8.8/dns-query#exit"],
"direct-nameserver": ["77.88.8.8", "77.88.8.1"]},
"proxies": proxies,
"proxy-providers": {"amnezia-keys": awg_prov, "vless-cluster": vless, **extra_prov},
"proxy-providers": {"amnezia-keys": awg_prov, "vless-cluster": vless, **extra_prov, **clients_prov},
"proxy-groups": [
# What traffic actually uses: the exit picked in BroV while it answers, "auto"
# when it doesn't (a pinned connection that dies no longer takes the Mac offline).
@@ -246,7 +253,8 @@ def main():
] + [
# Client networks: off (REJECT) until switched on in BroV.
# Client networks: first option = "off" (REJECT, or DIRECT for home).
{"name": f"{n}-sw", "type": "select", "proxies": [off, n]} for n, (_, _, off) in clients.items()
{"name": f"{n}-sw", "type": "select", "proxies": [off], "use": ["clients"], "filter": f"^{n}$"}
for n, (_, _, off) in clients.items()
],
"rules": rules,
}