Net core: with capture on, the Mac's DNS goes into the core
The home router's DNS (192.168.10.1) doesn't answer; a LAN DNS server is reached around the TUN, so with AmneziaVPN off nothing resolved. netctl now points every network service at 198.18.0.2 (hijacked inside the TUN) when capture is on, restores the previous servers when it goes off, and syncs this at boot. uninstall.sh puts DNS back too. Adds scripts/netcore/diag.sh (one-minute TUN diagnosis, run by hand with sudo). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -117,6 +117,66 @@ def cmd_delay(r):
|
||||
return {"ok": False, "error": "замер не разрешён"}
|
||||
|
||||
|
||||
# MARK: - System DNS
|
||||
#
|
||||
# With capture on, the Mac's DNS must go into the core (198.18.0.2 sits inside the TUN and
|
||||
# is hijacked). A DNS server in the local network — e.g. the home router — is reached
|
||||
# around the TUN, and when it doesn't answer nothing resolves. The previous servers are
|
||||
# saved and put back when capture goes off.
|
||||
|
||||
CORE_DNS = "198.18.0.2"
|
||||
DNS_BACKUP = os.path.join(CORE, "dns-backup.json")
|
||||
|
||||
|
||||
def network_services():
|
||||
out = subprocess.run(["/usr/sbin/networksetup", "-listallnetworkservices"],
|
||||
capture_output=True, text=True, timeout=10).stdout.splitlines()
|
||||
return [l for l in out[1:] if l and not l.startswith("*")]
|
||||
|
||||
|
||||
def dns_of(service):
|
||||
out = subprocess.run(["/usr/sbin/networksetup", "-getdnsservers", service],
|
||||
capture_output=True, text=True, timeout=10).stdout.splitlines()
|
||||
ip = r"\d{1,3}(\.\d{1,3}){3}|[0-9a-fA-F]*:[0-9a-fA-F:]+"
|
||||
return [x.strip() for x in out if re.fullmatch(ip, x.strip())]
|
||||
|
||||
|
||||
def set_dns(service, servers):
|
||||
subprocess.run(["/usr/sbin/networksetup", "-setdnsservers", service] + (servers or ["Empty"]),
|
||||
capture_output=True, timeout=10)
|
||||
|
||||
|
||||
def dns_to_core():
|
||||
try:
|
||||
with open(DNS_BACKUP) as f:
|
||||
backup = json.load(f)
|
||||
except (OSError, ValueError):
|
||||
backup = {}
|
||||
for svc in network_services():
|
||||
cur = dns_of(svc)
|
||||
if cur != [CORE_DNS]:
|
||||
backup[svc] = cur
|
||||
set_dns(svc, [CORE_DNS])
|
||||
fd = os.open(DNS_BACKUP, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, "w") as f:
|
||||
json.dump(backup, f)
|
||||
|
||||
|
||||
def dns_restore():
|
||||
try:
|
||||
with open(DNS_BACKUP) as f:
|
||||
backup = json.load(f)
|
||||
except (OSError, ValueError):
|
||||
backup = {}
|
||||
for svc in network_services():
|
||||
if dns_of(svc) == [CORE_DNS]:
|
||||
set_dns(svc, [x for x in backup.get(svc, []) if x != CORE_DNS])
|
||||
try:
|
||||
os.remove(DNS_BACKUP)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def save_tun(on):
|
||||
tmp = os.path.join(CORE, "tun.json.tmp")
|
||||
fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o600)
|
||||
@@ -139,6 +199,7 @@ def cmd_tun(r):
|
||||
save_tun(on)
|
||||
gen()
|
||||
if not on:
|
||||
dns_restore()
|
||||
api("PATCH", "/configs", {"tun": {"enable": False}})
|
||||
return {"ok": True}
|
||||
subprocess.run(["/bin/launchctl", "kickstart", "-k", "system/" + CORE_LABEL], capture_output=True, timeout=20)
|
||||
@@ -146,6 +207,7 @@ def cmd_tun(r):
|
||||
time.sleep(0.25)
|
||||
try:
|
||||
if api("GET", "/configs", timeout=2).get("tun", {}).get("enable"):
|
||||
dns_to_core()
|
||||
return {"ok": True}
|
||||
except Exception:
|
||||
pass
|
||||
@@ -311,9 +373,24 @@ def handle(conn):
|
||||
conn.close()
|
||||
|
||||
|
||||
def sync_dns_at_start():
|
||||
"""At boot the core starts with capture as the user left it: DNS follows."""
|
||||
on = True
|
||||
try:
|
||||
with open(os.path.join(CORE, "tun.json")) as f:
|
||||
on = bool(json.load(f).get("on", True))
|
||||
except (OSError, ValueError, AttributeError):
|
||||
pass
|
||||
try:
|
||||
dns_to_core() if on else dns_restore()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
if ALLOWED_UID < 0:
|
||||
sys.exit("usage: netctl.py <uid allowed to connect>")
|
||||
threading.Thread(target=sync_dns_at_start, daemon=True).start()
|
||||
try:
|
||||
os.unlink(SOCK)
|
||||
except FileNotFoundError:
|
||||
|
||||
Reference in New Issue
Block a user