fix(transport): recover rover feedback and retained WireGuard routes

This commit is contained in:
DCCONSTRUCTIONS
2026-11-04 00:42:24 +03:00
parent f4cdf3744a
commit 1e77b198cb
12 changed files with 833 additions and 30 deletions
+65 -9
View File
@@ -94,6 +94,8 @@ class Manager:
self.path = self.root / "profiles.json"
self.profiles = json.loads(self.path.read_text()) if self.path.exists() else {}
self.children = {}
self.next_physical_route_check = 0.0
self.restore_retry = {}
def persist(self):
save(self.path, self.profiles)
@@ -146,14 +148,47 @@ class Manager:
return
# A migrated VPS has a new destination, so it needs a new /32 even when
# this profile owns a route to its previous VPS. Reboots may remove routes.
record = {"endpoint": endpoint, "gateway": gateway, "source": source, "interface": interface}
if is_host and prior == record:
return # Stable DHCP state: no route rewrite or tunnel reconfiguration.
command = "change" if is_host and prior else "add"
run("/sbin/route", "-n", command, "-host", endpoint, gateway, "-ifa", source)
record = {"endpoint": endpoint, "gateway": gateway, "source": source, "interface": interface}
owned[endpoint] = record
row["host_route"] = record
row["physical_endpoint"] = endpoint
self.persist()
def refresh_physical_routes(self):
"""Repair only our explicit VPS /32 after the selected Mac LAN changes.
WireGuard keepalives cannot repair a macOS route whose cached source is
an expired DHCP address. Leave peer keys, tunnel state, default routes,
foreign routes and the selected physical interface unchanged.
"""
if not DARWIN:
return
with self.lock:
now = time.monotonic()
if now < self.next_physical_route_check:
return
self.next_physical_route_check = now + 5
for row in self.profiles.values():
config = row.get("config")
endpoint = row.get("physical_endpoint")
if not config or not config.get("interface") or row.get("pending") or not endpoint:
continue
owned = row.get("host_routes", {})
legacy = row.get("host_route") or {}
if endpoint not in owned and legacy.get("endpoint") != endpoint:
continue
try:
self.physical_route(config, endpoint, row)
except (OSError, ValueError, IndexError, subprocess.SubprocessError):
# No DHCP lease, a foreign route or a transient failure must
# not disable the tunnel or prevent another profile's check.
# Retry on the next bounded pass without touching the VPN.
continue
def retire_physical_routes(self, row):
if not DARWIN:
return
@@ -238,7 +273,17 @@ class Manager:
if DARWIN:
result = run("/sbin/route", "-n", "get", route[:-3], check=False).stdout.decode()
fields = dict(line.strip().split(":", 1) for line in result.splitlines() if ":" in line)
exists = "HOST" in fields.get("flags", "")
flags = fields.get("flags", "")
# A scoped cache created from another VPN's default route is
# not an administrator's explicit host route. Retire only this
# exact cached fleet destination before adding our /32.
if "HOST" in flags and "WASCLONED" in flags:
cached_interface = fields.get("interface", "").strip()
if not re.fullmatch(r"(?:utun|en)[0-9]+", cached_interface):
raise ValueError("Unrecognized cached route interface")
run("/sbin/route", "-n", "delete", "-host", route[:-3], "-ifscope", cached_interface, check=False)
flags = ""
exists = "HOST" in flags
own = exists and fields.get("interface", "").strip() == name
else:
entries = json.loads(run("/usr/sbin/ip", "-j", "route", "show", "exact", route).stdout)
@@ -348,14 +393,24 @@ class Manager:
def restore(self, startup=False):
with self.lock:
for row in self.profiles.values():
for identity, row in self.profiles.items():
pending = row.get("pending")
if pending and (startup or pending["deadline"] <= time.time()):
self.apply(row, pending["previous"])
row["pending"] = None
self.persist()
elif startup and row.get("config"):
self.apply(row, row["config"])
rollback = pending and (startup or pending["deadline"] <= time.time())
if not (rollback or (row.get("config") and (startup or identity in self.restore_retry))):
continue
if not startup and time.monotonic() < self.restore_retry.get(identity, 0):
continue
try:
self.apply(row, pending["previous"] if rollback else row["config"])
if rollback:
row["pending"] = None
self.persist()
except Exception:
# A late DHCP lease or transient route failure at boot must
# not leave an apparently running helper permanently partial.
self.restore_retry[identity] = time.monotonic() + 5
else:
self.restore_retry.pop(identity, None)
class Handler(socketserver.StreamRequestHandler):
@@ -405,6 +460,7 @@ def main():
manager.restore()
except Exception:
pass
manager.refresh_physical_routes()
if __name__ == "__main__":