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:
@@ -12,6 +12,8 @@ allows exactly these operations:
|
||||
tun {on} traffic capture on (fresh core start) / off; remembered
|
||||
add_key {text} vpn:// Amnezia key (checked by gen.py) or a vless:// link: stored, core updated
|
||||
refresh {} re-download the VLESS subscription now (new cluster nodes), no reload
|
||||
rebind {} after a network change: re-create the Amnezia and client WireGuard
|
||||
tunnels so they bind to the new address (other connections stay)
|
||||
hide {name} take an Amnezia connection or a VLESS node out of the list and
|
||||
out of "auto" (the key / subscription stay, nothing is deleted)
|
||||
unhide {name} put it back
|
||||
@@ -84,7 +86,7 @@ def cmd_state(_):
|
||||
for name, p in proxies.items():
|
||||
keep[name] = {"now": p.get("now"), "all": p.get("all"), "history": (p.get("history") or [])[-1:]}
|
||||
providers = {}
|
||||
for prov in ("vless-cluster", "vless-keys", "amnezia-keys"):
|
||||
for prov in ("vless-cluster", "vless-keys", "amnezia-keys", "clients"):
|
||||
try:
|
||||
lst = api("GET", "/providers/proxies/" + prov).get("proxies", [])
|
||||
except Exception:
|
||||
@@ -111,7 +113,8 @@ def cmd_delay(r):
|
||||
return {"ok": True, "delays": api("GET", "/group/%s/delay?%s" % (q(r["group"]), test), timeout=10)}
|
||||
if r.get("proxy") in CLIENT_TUNNELS:
|
||||
try:
|
||||
d = api("GET", "/proxies/%s/delay?%s" % (q(r["proxy"]), test), timeout=10).get("delay", 0)
|
||||
d = api("GET", "/providers/proxies/clients/%s/healthcheck?%s" % (q(r["proxy"]), test),
|
||||
timeout=10).get("delay", 0)
|
||||
except Exception:
|
||||
d = 0
|
||||
return {"ok": True, "delays": {r["proxy"]: d}}
|
||||
@@ -395,6 +398,31 @@ def cmd_refresh(_):
|
||||
"removed": sorted(before - set(after))}
|
||||
|
||||
|
||||
def cmd_rebind(_):
|
||||
"""mihomo's WireGuard outbounds keep their UDP socket bound to the local address they
|
||||
started on: after moving from one Wi-Fi to another (Planet9 → Saga) they keep sending
|
||||
from the old address and every Amnezia / client tunnel looks dead. A provider whose file
|
||||
changed is re-parsed, so its tunnels are created anew and bind to the current address."""
|
||||
done = []
|
||||
with gen_lock:
|
||||
for prov, path in (("amnezia-keys", os.path.join(KEYS, "amnezia.yaml")),
|
||||
("clients", os.path.join(KEYS, "clients.yaml"))):
|
||||
if not os.path.isfile(path):
|
||||
continue
|
||||
body = [l for l in open(path).read().splitlines() if not l.startswith("# rebind ")]
|
||||
tmp = path + ".tmp"
|
||||
fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, "w") as f:
|
||||
f.write("# rebind %d\n" % int(time.time()) + "\n".join(body) + "\n")
|
||||
os.rename(tmp, path)
|
||||
try:
|
||||
api("PUT", "/providers/proxies/" + prov)
|
||||
done.append(prov)
|
||||
except Exception:
|
||||
pass
|
||||
return {"ok": True, "rebound": done}
|
||||
|
||||
|
||||
def cmd_hide(r):
|
||||
return change_hidden(r.get("name"), True)
|
||||
|
||||
@@ -405,7 +433,7 @@ def cmd_unhide(r):
|
||||
|
||||
COMMANDS = {"state": cmd_state, "select": cmd_select, "delay": cmd_delay, "tun": cmd_tun,
|
||||
"add_key": cmd_add_key, "hide": cmd_hide, "unhide": cmd_unhide,
|
||||
"refresh": cmd_refresh}
|
||||
"refresh": cmd_refresh, "rebind": cmd_rebind}
|
||||
|
||||
|
||||
# MARK: - Socket server
|
||||
|
||||
Reference in New Issue
Block a user