fix: tun/tproxy routing

This commit is contained in:
FUFSoB committed 2026-09-18 08:36:45 +05:00
1 parent c62000821a
commit f7036f6075
7 files changed
+117 -17

No files matched your search

+29
View File
@@ -2752,6 +2752,34 @@ def _cleanup_slice_if_idle(slice_base, anchor_unit, user_svc, backend_svc):
systemctl("stop", backend_svc)
def _stub_resolver_nameservers(path="/etc/resolv.conf"):
"""The resolvers /etc/resolv.conf names, if every one of them is a local stub."""
try:
found = [
line.split()[1]
for line in lines(read_text(path))
if line.split()[:1] == ["nameserver"] and len(line.split()) > 1
]
except OSError:
return []
stub = [x for x in found if x.startswith("127.") or x in ("::1", "localhost")]
return stub if found and len(stub) == len(found) else []
def _warn_stub_resolver(route):
"""A wrapped app asking a local stub resolver leaks its names: the stub answers from
its own process, which is outside the app's cgroup and so outside the app's route."""
stub = _stub_resolver_nameservers()
if not stub:
return
print(
f"warning: {stub[0]} is this host's only resolver, and it answers from outside the "
f"wrapped app's cgroup, so names resolve outside route={route}. Point the app at a "
"resolver of its own to keep its DNS in the tunnel.",
file=sys.stderr,
)
def _wrap_slice(slice_base, profile, backend_svc, cmd):
"""Runs cmd in a user scope inside the route's slice, then stops what went idle."""
uid = os.getuid()
@@ -2823,6 +2851,7 @@ def cmd_apps_run(profile="", *cmd):
_check_no_global_proxy(route)
if env(enabled) != "1":
die(f"Profile '{profile}' uses route={route}, but {option} is false.")
_warn_stub_resolver(route)
_wrap_slice(slice_base, profile, f"{slice_base}.service", list(cmd))
else:
die(f"Route backend '{route}' is not implemented.")
+27
View File
@@ -44,6 +44,33 @@ def ok(fn, *args):
return out
class StubResolverTest(unittest.TestCase):
def resolv(self, content):
tmp = tempfile.NamedTemporaryFile("w", suffix=".conf", delete=False)
tmp.write(content)
tmp.close()
self.addCleanup(os.unlink, tmp.name)
return tmp.name
def test_only_stub_nameservers_are_reported(self):
path = self.resolv("nameserver 127.0.0.53\noptions edns0\n")
assert ctl._stub_resolver_nameservers(path) == ["127.0.0.53"]
def test_a_real_resolver_alongside_the_stub_is_not_a_leak(self):
path = self.resolv("nameserver 127.0.0.53\nnameserver 192.168.1.1\n")
assert ctl._stub_resolver_nameservers(path) == []
def test_missing_resolv_conf_is_quiet(self):
assert ctl._stub_resolver_nameservers("/nonexistent/resolv.conf") == []
def test_wrap_warns_once_on_the_stub(self):
with mock.patch.object(ctl, "_stub_resolver_nameservers", return_value=["127.0.0.53"]):
status, out, err = run(ctl._warn_stub_resolver, "tun")
assert status == 0, (status, out, err)
assert "route=tun" in err
assert "127.0.0.53" in err
class EnvTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()