Net core: clean restart when the Mac changes networks, instead of a leaky rebind
Re-creating the WireGuard tunnels through a provider reload leaked every old tunnel: on 16.10 the core held 652 UDP sockets to the Amnezia servers and 677 MB, and the servers stopped answering the handshake flood. netctl rebind is now a clean core restart (the move drops the TCP connections anyway), and BroV calls it only when the IPv4 address of a physical adapter actually changed, not on every path update. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+17
-26
@@ -12,8 +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)
|
||||
rebind {} the Mac moved to another network: clean core restart (WireGuard
|
||||
tunnels bind to the new address, nothing leaks)
|
||||
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
|
||||
@@ -419,36 +419,27 @@ def cmd_refresh(_):
|
||||
|
||||
|
||||
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
|
||||
# A new network may run on another adapter (Wi-Fi → Ethernet): its DNS goes into the
|
||||
# core too, while capture is on.
|
||||
"""After the Mac moved to another network (its address changed): a clean core restart.
|
||||
mihomo's WireGuard outbounds stay bound to the old address; re-creating them through a
|
||||
provider reload leaked every old tunnel (16.10: 652 UDP sockets, 677 MB, the Amnezia
|
||||
servers stopped answering the handshake flood). A restart leaves nothing behind, and
|
||||
the move has dropped the open TCP connections anyway. TUN comes back as tun.json says;
|
||||
the DNS follows the newly active adapter."""
|
||||
subprocess.run(["/bin/launchctl", "kickstart", "-k", "system/" + CORE_LABEL], capture_output=True, timeout=20)
|
||||
for _ in range(40):
|
||||
time.sleep(0.25)
|
||||
try:
|
||||
api("GET", "/configs", timeout=2)
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
with open(os.path.join(CORE, "tun.json")) as f:
|
||||
if json.load(f).get("on", True):
|
||||
dns_to_core()
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
return {"ok": True, "rebound": done}
|
||||
return {"ok": True, "restarted": True}
|
||||
|
||||
|
||||
def cmd_hide(r):
|
||||
|
||||
Reference in New Issue
Block a user