feat: per-app awg and custom via

This commit is contained in:
FUFSoB committed 2026-10-04 15:39:56 +05:00
1 parent af5b3ff27a
commit 65082aad88
43 files changed
+2674 -197

No files matched your search

+342 -39
View File
@@ -142,6 +142,10 @@ Changes and secrets need root or the userControl group.
apps [list] per-app routing profiles
apps run <profile> -- <cmd> [args] run a command through a profile
apps run --via <outbound> -- <cmd> run a command through one outbound
(--route tun|tproxy picks the method)
apps add <name> [--route r] [--via o] add a profile without a rebuild
apps rm <name> remove one added that way
inbounds [list] server inbounds
inbounds link <tag> [user] [--onion|--variant=<name>] [--qr|--json]
@@ -308,8 +312,8 @@ def denied(path, what="read"):
die(f"Cannot {what} {path} - {ask_group()}.")
def _spool_write(path, text=""):
"""Replaces path whole, 0640, in a directory userControl's group may write to.
def _spool_write(path, text="", mode=0o640):
"""Replaces path whole, 0640 by default, in a directory userControl's group may write to.
Through a new file renamed over it: a symlink a member left at path is replaced rather
than followed, so proxy-ctl run as root never writes where it points. The file takes
@@ -319,7 +323,7 @@ def _spool_write(path, text=""):
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
f.write(text)
os.chmod(tmp, 0o640)
os.chmod(tmp, mode)
os.replace(tmp, path)
except BaseException:
try:
@@ -692,7 +696,13 @@ COMPLETE = {
"link": "its URL, QR code, JSON or client config",
}
},
"proxy outbounds add": {"flags": {"--detour": "chain it through another outbound"}},
"proxy outbounds add": {
"flags": {
"--detour": "chain it through another outbound",
"--interface": "an AmneziaWG config on an interface of its own",
"--userspace": "an AmneziaWG config in wireproxy",
}
},
"proxy outbounds link": {
"args": _outbound_choices,
"flags": {"--qr": "print a QR code", "--json": "backend JSON", "--config": "client config for this server"},
@@ -833,8 +843,23 @@ COMPLETE = {
"wl join": {"args": lambda: _names(w["name"] for w in _wl() if w["role"] == "joiner")},
"wl new": {"args": lambda: _names(w["name"] for w in _wl() if w["role"] == "creator" and not w.get("fixedLink"))},
**{f"wl {verb}": {"args": lambda: {w["name"]: w["role"] for w in _wl()}} for verb in ("on", "off", "toggle", "restart")},
"apps": {"words": {"list": "per-app routing profiles", "run": "run a command through a profile"}},
"apps run": {"args": lambda: {_s(p["name"]): _s(p.get("route") or "") for p in read_json(env("PER_APP_ROUTING_PROFILES_FILE"))}},
"apps": {
"words": {
"list": "per-app routing profiles",
"run": "run a command through a profile",
"add": "add a profile at runtime",
"rm": "remove a profile added at runtime",
}
},
"apps add": {"flags": {"--route": "how the app is routed", "--via": "the outbound it goes through"}},
"apps rm": {"args": lambda: _names(p["name"] for p in _runtime_apps())},
"apps run": {
"args": lambda: {
_s(p["name"]): _s(p.get("outbound") or p.get("route") or "")
for p in [*_runtime_apps(), *read_json(env("PER_APP_ROUTING_PROFILES_FILE"))]
},
"flags": {"--via": "through one outbound, not a profile", "--route": "tun or tproxy, for --via"},
},
"inbounds": {
"words": {
"list": "server inbounds",
@@ -890,6 +915,16 @@ def _complete_tree(*words):
while rest and f"{path} {rest[0]}".strip() in COMPLETE:
path = f"{path} {rest.pop(0)}".strip()
node = COMPLETE[path]
if path == "apps add" and rest[-1:] == ["--route"]:
return _names(APP_ROUTES)
if path in ("apps run", "apps add") and rest[-1:] == ["--via"]:
return {
**(_outbound_choices() if _pin_routes() else {}),
**_names(_via_outbounds()),
**({p: "AmneziaWG profile" for p in _awg_profiles()} if env("PER_APP_VIA_PROFILES") == "1" else {}),
}
if path == "apps run" and rest[-1:] == ["--route"]:
return _names(_pin_routes())
if rest[-1:] in (["--exits"], ["--via"], ["--detour"]):
return _outbound_choices()
if rest[-1:] == ["--by"]:
@@ -1246,7 +1281,14 @@ def cmd_outbounds(verb="list", *args):
del args[i : i + 2]
if not container:
usage("proxy outbounds add [tag] <vpn://…|file.conf|-> [--container <name>]")
_runtime_entry_add("outbound", *args, detour=detour, container=container)
awg_kind = ""
for flag in ("--interface", "--userspace"):
if flag in args:
if awg_kind:
die("Pick one of --interface and --userspace.")
awg_kind = flag[2:]
args.remove(flag)
_runtime_entry_add("outbound", *args, detour=detour, container=container, awg_kind=awg_kind)
elif verb == "chain":
cmd_outbound_chain(*args)
elif verb in ("rm", "remove", "del"):
@@ -2528,7 +2570,7 @@ def _runtime_json_outbound(text):
def _check_runtime_tag(kind, tag):
if tag in ("proxy", "direct", "block"):
if tag in ("proxy", "direct", "block") or tag.startswith("proxy-suite-"):
die(f"'{tag}' is reserved; pick another {kind} tag.")
if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._-]*", tag):
die(f"Invalid {kind} tag '{tag}': letters, digits, dot, dash and underscore only.")
@@ -2613,7 +2655,7 @@ def _unique_runtime_tag(kind, name, fallback):
return tag
def _runtime_entry_add(kind, *args, detour="", container=""):
def _runtime_entry_add(kind, *args, detour="", container="", awg_kind=""):
what = "<url|json|-> [--detour <tag>]" if kind == "outbound" else "<url>"
shape = f"proxy {_runtime_noun(kind)} add [tag] {what}"
if len(args) == 1 and _runtime_source(kind, args[0]):
@@ -2634,10 +2676,12 @@ def _runtime_entry_add(kind, *args, detour="", container=""):
if kind == "outbound" and _awg_file(url):
url = _awg_input(url)
if kind == "outbound" and _awg_source(url):
_awg_outbound_add(tag, url, detour, container)
_awg_outbound_add(tag, url, detour, container, awg_kind)
return
if container:
die("--container only applies to an AmneziaWG vpn:// link.")
if awg_kind:
die(f"--{awg_kind} only applies to an AmneziaWG config.")
if not tag:
tag = _runtime_tag_for(kind, url)
_check_runtime_tag(kind, tag)
@@ -2711,7 +2755,7 @@ def _runtime_entry_rm(kind, tag="", *_):
try:
os.unlink(path)
# Its hop, and a disable left from it: a new entry of this name would inherit them.
for extra in (".detour", ".disabled", ".port") if kind == "outbound" else ():
for extra in (".detour", ".disabled", ".port", ".iface") if kind == "outbound" else ():
if os.path.exists(os.path.join(_runtime_dir(kind), tag + extra)):
os.unlink(os.path.join(_runtime_dir(kind), tag + extra))
except FileNotFoundError:
@@ -4031,7 +4075,8 @@ def _awg_service(profile):
# --- AmneziaWG configs added at runtime -----------------------------------------
#
# A .conf or vpn:// link becomes a global profile (`awg add`, amneziawg.d/<name>.conf) or an
# outbound (`proxy outbounds add`, outbounds.d/<tag>.awg with its tunnel's port in <tag>.port).
# outbound (`proxy outbounds add`, outbounds.d/<tag>.awg with its tunnel's port in <tag>.port,
# or on an interface of its own, with its slot in <tag>.iface).
# amneziawg_config.py --import checks and normalizes it, hooks refused. The config always
# reaches it on stdin: keys in argv would show in ps.
@@ -4132,8 +4177,35 @@ def _awg_free_port():
return port
def _awg_outbound_add(tag, text, detour, container):
def _awg_iface_slots():
return range(int(env("AWG_IFACE_SLOTS", "16")))
def _awg_free_iface_slot():
"""A slot no <tag>.iface holds: the interface psawgr<slot>, and its table and marks."""
taken = set()
directory = _runtime_dir("outbound")
try:
names = os.listdir(directory)
except OSError:
names = []
for name in names:
if name.endswith(".iface"):
try:
taken.add(int(read_text(os.path.join(directory, name)).split()[0]))
except (OSError, ValueError, IndexError):
pass
slot = next((n for n in _awg_iface_slots() if n not in taken), None)
if slot is None:
die(f"No free slot for another AmneziaWG interface: all {len(_awg_iface_slots())} are taken. Remove one first.")
return slot
def _awg_outbound_add(tag, text, detour, container, kind=""):
require_enabled("AWG_RUNTIME_OUTBOUNDS", "Adding AmneziaWG outbounds at runtime")
kind = kind or env("AWG_RUNTIME_OUTBOUND_KIND", "userspace")
if kind == "interface":
require_enabled("AWG_RUNTIME_IFACE_OUTBOUNDS", "An AmneziaWG outbound on an interface of its own (root hosts only)")
if detour:
die("An AmneziaWG outbound cannot chain through another: its tunnel dials the peer itself.")
described = _awg_describe(text, container)
@@ -4144,13 +4216,17 @@ def _awg_outbound_add(tag, text, detour, container):
directory = _runtime_dir("outbound")
if not os.access(directory, os.W_OK | os.X_OK):
denied(directory, "write to")
port = _awg_free_port()
# The port, or the interface's slot, first: the config is what the start script and the
# sync unit look for.
if kind == "interface":
extra, value, stale = ".iface", _awg_free_iface_slot(), ".port"
else:
extra, value, stale = ".port", _awg_free_port(), ".iface"
try:
# The port first: the config is what the start script and the sync unit look for.
for extra in (".detour", ".disabled"):
if os.path.lexists(os.path.join(directory, tag + extra)):
os.unlink(os.path.join(directory, tag + extra)) # left from an earlier entry of this name
_spool_write(os.path.join(directory, f"{tag}.port"), f"{port}\n")
for leftover in (".detour", ".disabled", stale):
if os.path.lexists(os.path.join(directory, tag + leftover)):
os.unlink(os.path.join(directory, tag + leftover)) # left from an earlier entry of this name
_spool_write(os.path.join(directory, tag + extra), f"{value}\n")
except OSError:
denied(directory, "write to")
_awg_write(text, os.path.join(directory, f"{tag}.awg"), container)
@@ -4426,11 +4502,90 @@ SLICE_ROUTES = {
}
def _per_app_profiles():
APP_NAME = re.compile(r"[a-z0-9][a-z0-9-]*")
APP_ROUTES = ("direct", "proxychains", "tun", "tproxy", "zapret")
def _runtime_apps():
"""Profiles `apps add` added, <name>.json in RUNTIME_APPS_DIR, each marked "runtime"."""
directory = env("RUNTIME_APPS_DIR")
try:
return read_json(env("PER_APP_ROUTING_PROFILES_FILE"))
names = sorted(os.listdir(directory)) if directory else []
except OSError:
names = []
apps = []
for file in names:
name = file.removesuffix(".json")
entry = read_json_or(os.path.join(directory, file), {}) if file.endswith(".json") else {}
if APP_NAME.fullmatch(name) and _s(entry.get("route")) in APP_ROUTES:
apps.append({"name": name, "route": _s(entry["route"]), "outbound": _s(entry.get("outbound") or "") or None, "runtime": True})
return apps
def _per_app_profiles():
"""The declared profiles, then those added at runtime under a name none of them has."""
try:
declared = read_json(env("PER_APP_ROUTING_PROFILES_FILE"))
except (OSError, ValueError):
die(f"Cannot read perAppRouting profiles: {env('PER_APP_ROUTING_PROFILES_FILE')}")
names = {p.get("name") for p in declared}
return declared + [p for p in _runtime_apps() if p["name"] not in names]
APPS_ADD_USAGE = "add <name> [--route direct|proxychains|tun|tproxy|zapret] [--via <outbound>]"
def _apps_add(*args):
args, route, via = list(args), "", ""
for flag in ("--route", "--via"):
if flag in args:
i = args.index(flag)
value = args[i + 1] if i + 1 < len(args) else ""
del args[i : i + 2]
if not value:
usage(f"apps {APPS_ADD_USAGE}")
route, via = (value, via) if flag == "--route" else (route, value)
if len(args) != 1 or not (route or via):
usage(f"apps {APPS_ADD_USAGE}")
name = args[0]
if not APP_NAME.fullmatch(name):
die(f"Invalid profile name '{name}': lowercase letters, digits and dashes.")
if any(p.get("name") == name and not p.get("runtime") for p in _per_app_profiles()):
die(f"Profile '{name}' is declared in the NixOS configuration; pick another name.")
if route and route not in APP_ROUTES:
die(f"--route is one of {', '.join(APP_ROUTES)}, not '{route}'.")
# An "interface" outbound or a global profile takes the app directly; `apps run` sorts out
# a name that is both.
direct = via in _via_outbounds() or via.startswith("awg:") or (env("PER_APP_VIA_PROFILES") == "1" and via in _awg_profiles())
if via and not direct:
# Any other outbound goes through a pin slot of the route.
route = route or next(iter(_pin_routes()), "")
if route not in _pin_routes():
die(f"--via '{via}' needs --route tun or tproxy with pin slots here, or an \"interface\" AmneziaWG outbound.")
directory = env("RUNTIME_APPS_DIR")
if not directory or not os.access(directory, os.W_OK | os.X_OK):
denied(directory or "RUNTIME_APPS_DIR", "write to")
try:
# Readable by everyone: whoever runs apps reads the profiles.
_spool_write(os.path.join(directory, f"{name}.json"), json.dumps({"route": route or "direct", "outbound": via or None}) + "\n", 0o644)
except OSError:
denied(directory, "write to")
print(f"Added app profile: {name}")
def _apps_rm(name="", *_):
if not name:
usage("apps rm <name>")
path = os.path.join(env("RUNTIME_APPS_DIR"), f"{name}.json")
if not APP_NAME.fullmatch(name) or not os.path.exists(path):
if any(p.get("name") == name for p in _per_app_profiles()):
die(f"Profile '{name}' is declared in the NixOS configuration; remove it there.")
die(f"No app profile added at runtime named '{name}'.")
try:
os.unlink(path)
except OSError:
denied(path, "remove")
print(f"Removed app profile: {name}")
def _ensure_app_routing():
@@ -4446,13 +4601,21 @@ def _has_units(*args):
return bool(re.search(".", systemctl(*args, capture=True)[1]))
def _cleanup_slice_if_idle(slice_base, anchor_unit, user_svc, backend_svc):
def _units_active(*globs):
return _has_units("list-units", "--type=service", "--state=active", "--plain", "--no-legend", *globs)
def _cleanup_slice_if_idle(slice_base, anchor_unit, user_svc, backend_svc, users_glob="", busy=(), then=None):
"""busy: globs of other units that keep the backend up; then: what to do once it stopped."""
if _has_units("--user", "list-units", "--type=scope", "--state=running", "--plain", "--no-legend", f"{slice_base}-*"):
return
systemctl("stop", user_svc)
systemctl("--user", "stop", anchor_unit)
if not _has_units("list-units", "--type=service", "--state=active", "--plain", "--no-legend", f"{slice_base}-user@*.service"):
users_glob = users_glob or f"{slice_base}-user@*.service"
if not _units_active(users_glob, *busy):
systemctl("stop", backend_svc)
if then:
then()
def _stub_resolver_nameservers(path="/etc/resolv.conf"):
@@ -4483,16 +4646,22 @@ def _warn_stub_resolver(route):
)
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."""
def _wrap_slice(slice_base, profile, backend_svc, cmd, units=None, busy=(), then=None, before=()):
"""Runs cmd in a user scope inside the route's slice, then stops what went idle.
units: (anchor, this user's marking unit, a glob of every user's) when they are not
named after the slice. busy and then: as _cleanup_slice_if_idle takes them. before:
units the backend needs up first.
"""
uid = os.getuid()
scope_unit = f"{slice_base}-{profile}-{os.getpid()}"
anchor_unit = f"{slice_base}-anchor.service"
user_svc = f"{slice_base}-user@{uid}.service"
anchor_unit, user_svc, users_glob = units or (f"{slice_base}-anchor.service", f"{slice_base}-user@{uid}.service", "")
# SIGTERM still cleans up, as it did behind bash's EXIT trap.
signal.signal(signal.SIGTERM, lambda *_: sys.exit(143))
try:
status = systemctl("--user", "start", anchor_unit)[0]
for unit in before:
status = status or systemctl("start", unit)[0]
status = status or systemctl("start", backend_svc)[0]
status = status or systemctl("start", user_svc)[0]
if not status:
@@ -4510,10 +4679,115 @@ def _wrap_slice(slice_base, profile, backend_svc, cmd):
]
)
finally:
_cleanup_slice_if_idle(slice_base, anchor_unit, user_svc, backend_svc)
_cleanup_slice_if_idle(slice_base, anchor_unit, user_svc, backend_svc, users_glob, busy, then)
sys.exit(status)
def _via_outbounds():
"""What `apps run --via` takes: {tag: {...}}, the "interface" AmneziaWG outbounds, which
the app's packets enter directly: declared ones, and those added at runtime."""
outbounds = read_json_or(env("PER_APP_VIA_FILE"), {})
if env("PER_APP_VIA_RUNTIME") == "1":
try:
names = os.listdir(_runtime_dir("outbound"))
except OSError:
names = []
for name in names:
if name.endswith(".iface"):
outbounds.setdefault(name[: -len(".iface")], {"runtime": True})
return outbounds
def _via_key(tag):
"""The units' instance for tag. In hex: a "-" in a tag would nest one slice in another's."""
return "awg-" + tag.encode().hex()
# Routes that take any outbound through a pin slot, and the flag that says this host has them.
PIN_ROUTES = {"tun": "PER_APP_PIN_TUN", "tproxy": "PER_APP_PIN_TPROXY"}
def _pin_routes():
return [route for route, flag in PIN_ROUTES.items() if env(flag) == "1"]
def _stop_per_app_tun_if_idle():
"""The per-app TUN backend, once neither a tun profile nor a pin of it uses it."""
if not _units_active("proxy-suite-per-app-tun-user@*.service", "proxy-suite-per-app-via-tun@*.service"):
systemctl("stop", "proxy-suite-per-app-tun.service")
def _run_via(tag, label, cmd, route=""):
"""Runs cmd with all its traffic through tag, past the routing rules: into an "interface"
AmneziaWG outbound's interface directly, with its DNS; else through a pin slot of per-app
TUN or TProxy (route; TUN when both are there), whose selector sends it to tag. A global
AmneziaWG profile is brought up apart for the apps (proxy-suite-awg-app@), as an
"interface" outbound has it, unless it is up globally already.
tag may say which it is, "awg:<profile>" or "outbound:<tag>"; a bare name that is both
is refused. Neither a tag nor a profile name has a colon."""
kind, name = tag.split(":", 1) if ":" in tag else ("", tag)
if kind not in ("", "awg", "outbound") or not name:
die(f"--via takes an outbound, awg:<profile> or outbound:<tag>, not '{tag}'.")
label = name if label == tag else label
profiles = _awg_profiles() if env("PER_APP_VIA_PROFILES") == "1" else []
is_profile = kind != "outbound" and name in profiles
if kind == "awg" and not is_profile:
die(f"No global AmneziaWG profile '{name}' to run apps through. See: proxy-ctl awg")
if not kind and is_profile and (name in _via_outbounds() or name in _outbound_tags()):
die(f"'{name}' is both a global AmneziaWG profile and an outbound: say --via awg:{name} or --via outbound:{name}.")
tag = name
before = ()
if not is_profile and tag in _via_outbounds():
key = _via_key(tag)
unit, global_units, then = f"proxy-suite-per-app-via@{key}.service", ("proxy-suite-tun",), None
elif is_profile:
key = f"app-{tag.encode().hex()}"
unit = f"proxy-suite-per-app-via@{key}.service"
# Up globally, it carries the app already.
global_units = ("proxy-suite-tun", _awg_service(tag))
before = (f"proxy-suite-awg-app@{tag}.service",)
then = lambda: systemctl("stop", *before) # noqa: E731
else:
routes = _pin_routes()
route = route or next(iter(routes), "")
if route not in routes:
interfaces = ", ".join(sorted(_via_outbounds())) or "none"
why = (
f"no pin slots of per-app {route} here (perAppRouting.{route}.enable, the sing-box or hybrid backend)"
if route
else "this configuration has no pin slots: per-app TUN or TProxy, with the sing-box or hybrid backend"
)
die(f"Cannot run via '{tag}': {why}. \"interface\" AmneziaWG outbounds: {interfaces}.")
tags = _outbound_tags()
if tags and tag not in {*tags, *_outbound_groups(), "proxy", "direct", "block"}:
die(f"Unknown outbound: {tag}. See: proxy-ctl proxy outbounds")
key = f"{route}-{tag.encode().hex()}"
unit = f"proxy-suite-per-app-via-{route}@{tag.encode().hex()}.service"
global_units = ("proxy-suite-tun", "proxy-suite-tproxy")
then = _stop_per_app_tun_if_idle if route == "tun" else None
# A global mode takes the app past the per-app rules; run it as it is rather than refuse,
# like the other per-app routes.
active = next((u for u in global_units if svc_active(f"{u}.service")), "")
if active:
print(f"note: {active}.service is active; running without --via {tag}.", file=sys.stderr)
_exec(list(cmd))
uid = os.getuid()
_wrap_slice(
f"proxy-suite-per-app-via-{key}",
label,
unit,
list(cmd),
(
f"proxy-suite-per-app-via-anchor@{key}.service",
f"proxy-suite-per-app-via-user@{uid}-{key}.service",
f"proxy-suite-per-app-via-user@*-{key}.service",
),
then=then,
before=before,
)
def cmd_apps(verb="list", *args):
_ensure_app_routing()
if verb == "list":
@@ -4521,24 +4795,51 @@ def cmd_apps(verb="list", *args):
if not profiles:
print("No perAppRouting profiles configured.")
return
print(f" {'PROFILE':<24} ROUTE")
print(f" {'PROFILE':<24} {'ROUTE':<12} {'VIA':<16} SOURCE")
for p in profiles:
print(f" {_s(p.get('name')):<24} {_s(p.get('route'))}")
source = "runtime" if p.get("runtime") else "declared"
print(f" {_s(p.get('name')):<24} {_s(p.get('route')):<12} {_s(p.get('outbound') or '-'):<16} {source}")
elif verb == "run":
cmd_apps_run(*args)
elif verb == "add":
_apps_add(*args)
elif verb in ("rm", "remove", "del"):
_apps_rm(*args)
else:
usage("apps [list] | run <profile> -- <cmd> [args]")
usage(f"apps [list] | {APPS_RUN_USAGE} | {APPS_ADD_USAGE} | rm <name>")
def cmd_apps_run(profile="", *cmd):
if cmd[:1] == ("--",):
cmd = cmd[1:]
if not profile or not cmd:
usage("apps run <profile> -- <cmd> [args]")
APPS_RUN_USAGE = "run <profile> -- <cmd> [args] | run --via <outbound> [--route tun|tproxy] -- <cmd> [args]"
route = next((_s(p.get("route")) for p in _per_app_profiles() if p.get("name") == profile), "")
if not route:
def cmd_apps_run(*args):
profile, via, route = "", "", ""
args = list(args)
# --via <outbound> [--route tun|tproxy], before the command.
while args[:1] in (["--via"], ["--route"]):
if len(args) < 2 or not args[1]:
usage(f"apps {APPS_RUN_USAGE}")
if args[0] == "--via":
via = args[1]
else:
route = args[1]
del args[:2]
if route and route not in PIN_ROUTES:
die(f"--route is tun or tproxy, not '{route}'.")
if not via:
profile, args = (args[0], args[1:]) if args and not route else ("", args)
cmd = args[1:] if args[:1] == ["--"] else args
if not (profile or via) or not cmd:
usage(f"apps {APPS_RUN_USAGE}")
if via:
_run_via(via, via, cmd, route)
entry = next((p for p in _per_app_profiles() if p.get("name") == profile), None)
if entry is None:
die(f"Unknown perAppRouting profile: {profile}")
route = _s(entry.get("route"))
if entry.get("outbound"):
_run_via(_s(entry["outbound"]), profile, cmd, route if route in PIN_ROUTES else "")
if route == "direct":
_exec(list(cmd))
@@ -4560,7 +4861,9 @@ def cmd_apps_run(profile="", *cmd):
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))
# Pins of per-app TUN (apps run --via) keep its backend up too.
busy = ("proxy-suite-per-app-via-tun@*.service",) if route == "tun" else ()
_wrap_slice(slice_base, profile, f"{slice_base}.service", list(cmd), busy=busy)
else:
die(f"Route backend '{route}' is not implemented.")
+29 -2
View File
@@ -477,11 +477,27 @@ def _user_add_argv(text):
def app_rows(_):
return [
{"key": ctl._s(p.get("name")), "profile": ctl._s(p.get("name")), "route": ctl._s(p.get("route"))}
{
"key": ctl._s(p.get("name")),
"profile": ctl._s(p.get("name")),
"route": ctl._s(p.get("route")),
"via": ctl._s(p.get("outbound") or ""),
"source": "runtime" if p.get("runtime") else "declared",
}
for p in ctl._per_app_profiles()
]
def _via_run_argv(text):
"""<outbound> <command> [args], as typed, to `apps run --via`."""
words = shlex.split(text)
return ["apps", "run", "--via", *words[:1], "--", *words[1:]]
def _via_offered():
return bool(ctl._via_outbounds())
def snapshot(states):
"""proxy-ctl's status snapshot for these unit states; None when it cannot be read."""
return _safe(ctl._status_snapshot, states)
@@ -729,6 +745,14 @@ TABS = [
prompt="[tag] <url, JSON or vpn://> - e.g. de-1 vless://… or just vless://…",
stdin=lambda r, t: _add_stdin(t),
),
Action(
"N",
"add an AmneziaWG outbound on an interface of its own…",
lambda r, t, _: ["proxy", "outbounds", "add", *_add_args(t), "--interface"],
prompt="[tag] <vpn://… or a .conf path> - apps can then run through it (proxy-ctl apps run --via)",
stdin=lambda r, t: _add_stdin(t),
offered=lambda: ctl.env("AWG_RUNTIME_IFACE_OUTBOUNDS") == "1",
),
Action(
"g",
"add a group…",
@@ -921,10 +945,13 @@ TABS = [
"Apps",
# `apps run` is per user (its uid, systemctl --user): nothing a root session can use.
lambda states: not is_root() and _enabled("PER_APP_ROUTING_ENABLED")(states),
[("profile", "Profile"), ("route", "Route")],
[("profile", "Profile"), ("route", "Route"), ("via", "Via"), ("source", "Source")],
app_rows,
actions=[
Action("n", "add a profile…", lambda r, t, _: ["apps", "add", *shlex.split(t)], prompt="<name> [--route tun|tproxy|…] [--via <outbound>] - e.g. game --via de"),
Action("d", "remove it", lambda r, *_: ["apps", "rm", r["profile"]], when=lambda r: r["source"] == "runtime", confirm=True),
Action("space", "run a command through it…", lambda r, t, _: ["apps", "run", r["profile"], "--", *shlex.split(t)], when=ROW, prompt="<command> [args]", mode="pause"),
Action("v", "run a command via an outbound…", lambda r, t, _: _via_run_argv(t), prompt="<outbound> <command> [args] - e.g. de firefox", mode="pause", offered=_via_offered),
],
),
]
+181 -1
View File
@@ -1293,6 +1293,35 @@ class AmneziaWgRuntimeTest(RuntimeSpoolTest):
os.environ["AWG_RUNTIME_OUTBOUNDS"] = "0"
self.assertIn("not enabled", run(ctl.cmd_outbounds, "add", "fr", AWG_CONF)[2])
def test_interface_outbounds(self):
os.environ.update(AWG_RUNTIME_IFACE_OUTBOUNDS="1", AWG_IFACE_SLOTS="2", PER_APP_VIA_RUNTIME="1")
# --interface: a slot in <tag>.iface instead of a port, which frees nothing of the tunnels'.
self.assertIn("Added outbound: de", ok(ctl.cmd_outbounds, "add", "de", AWG_CONF, "--interface"))
self.assertEqual(ctl.read_text(self.path("outbounds.d/de.iface")), "0\n")
self.assertFalse(os.path.exists(self.path("outbounds.d/de.port")))
self.assertEqual(ctl._awg_free_port(), 18800)
# The configuration's default kind, unless the command says.
os.environ["AWG_RUNTIME_OUTBOUND_KIND"] = "interface"
ok(ctl.cmd_outbounds, "add", "nl", AWG_CONF)
self.assertEqual(ctl.read_text(self.path("outbounds.d/nl.iface")), "1\n")
self.assertIn("No free slot", run(ctl.cmd_outbounds, "add", "fr", AWG_CONF)[2])
ok(ctl.cmd_outbounds, "add", "fr", AWG_CONF, "--userspace")
self.assertEqual(ctl.read_text(self.path("outbounds.d/fr.port")), "18800\n")
# Apps can run through the interface ones.
self.assertEqual(sorted(ctl._via_outbounds()), ["de", "nl"])
# rm takes the slot along.
ok(ctl.cmd_outbounds, "rm", "de")
self.assertFalse(os.path.exists(self.path("outbounds.d/de.iface")))
self.assertEqual(ctl._awg_free_iface_slot(), 0)
for args, why in [
(("x", AWG_CONF, "--interface", "--userspace"), "Pick one"),
(("x", "vless://u@x.test:443", "--interface"), "--interface only applies"),
]:
with self.subTest(args=args[-1]):
self.assertIn(why, run(ctl.cmd_outbounds, "add", *args)[2])
os.environ["AWG_RUNTIME_IFACE_OUTBOUNDS"] = "0"
self.assertIn("root hosts only", run(ctl.cmd_outbounds, "add", "x", AWG_CONF, "--interface")[2])
class ZapretAutoTest(EnvTest):
@unittest.skipIf(os.geteuid() == 0, "root writes anything")
@@ -1450,8 +1479,10 @@ class AppsRunTest(EnvTest):
PROXYCHAINS_CONFIG=self.write("proxychains.conf", ""),
PER_APP_ROUTING_PROFILES_FILE=self.write(
"profiles.json",
[{"name": n, "route": n} for n in ("direct", "proxychains", "tun", "tproxy", "zapret")],
[{"name": n, "route": n} for n in ("direct", "proxychains", "tun", "tproxy", "zapret")]
+ [{"name": "game", "route": "tun", "outbound": "de-2"}],
),
PER_APP_VIA_FILE=self.write("via.json", {"de-2": {"interface": "awg-de", "mark": 23040}}),
)
def systemctl(*args, **kw):
@@ -1508,6 +1539,155 @@ class AppsRunTest(EnvTest):
self.assertIn("proxy-suite-tun.service is active", err)
self.assertFalse(any(c[0] == "systemd-run" for c in self.calls))
def test_via_interface_outbound(self):
# Its own slice and units, keyed by the tag in hex: "de-2" must not nest under "de".
key = "awg-" + "de-2".encode().hex()
uid = os.getuid()
for args, label in ((("--via", "de-2", "--", "curl", "x"), "de-2"), (("game", "--", "curl", "x"), "game")):
self.calls.clear()
status, _, _ = run(ctl.cmd_apps, "run", *args)
self.assertEqual(status, 0)
base = f"proxy-suite-per-app-via-{key}"
scope = next(c for c in self.calls if c[0] == "systemd-run")
self.assertIn(f"--unit={base}-{label}-{os.getpid()}", scope)
self.assertIn(f"--slice={base}", scope)
self.assertEqual(scope[-2:], ("curl", "x"))
self.assertIn(("--user", "start", f"proxy-suite-per-app-via-anchor@{key}.service"), self.calls)
self.assertIn(("start", f"proxy-suite-per-app-via@{key}.service"), self.calls)
self.assertIn(("start", f"proxy-suite-per-app-via-user@{uid}-{key}.service"), self.calls)
self.assertIn(("stop", f"proxy-suite-per-app-via@{key}.service"), self.calls)
listed = next(c for c in self.calls if c[0] == "list-units" and c[-1].startswith("proxy-suite-per-app-via-user@"))
self.assertEqual(listed[-1], f"proxy-suite-per-app-via-user@*-{key}.service")
def test_via_without_pin_slots(self):
status, _, err = run(ctl.cmd_apps, "run", "--via", "nl", "--", "curl")
self.assertNotEqual(status, 0)
self.assertIn("this configuration has no pin slots", err)
self.assertIn('"interface" AmneziaWG outbounds: de-2.', err)
self.assertFalse(any(c[0] == "systemd-run" for c in self.calls))
def test_via_pin_slots(self):
os.environ.update(PER_APP_PIN_TUN="1", PER_APP_PIN_TPROXY="1")
self.patch("_outbound_tags", lambda: ["nl", "de-2"])
self.patch("_outbound_groups", lambda: {})
hexed = "nl".encode().hex()
uid = os.getuid()
# TUN when both are there; a profile's route, or --route, picks.
for args, route, label in (
(("--via", "nl", "--", "curl"), "tun", "nl"),
(("--via", "nl", "--route", "tproxy", "--", "curl"), "tproxy", "nl"),
(("--route", "tproxy", "--via", "nl", "curl"), "tproxy", "nl"),
):
with self.subTest(args=args):
self.calls.clear()
status, _, _ = run(ctl.cmd_apps, "run", *args)
self.assertEqual(status, 0)
key = f"{route}-{hexed}"
scope = next(c for c in self.calls if c[0] == "systemd-run")
self.assertIn(f"--slice=proxy-suite-per-app-via-{key}", scope)
self.assertIn(f"--unit=proxy-suite-per-app-via-{key}-{label}-{os.getpid()}", scope)
self.assertIn(("start", f"proxy-suite-per-app-via-{route}@{hexed}.service"), self.calls)
self.assertIn(("start", f"proxy-suite-per-app-via-user@{uid}-{key}.service"), self.calls)
self.assertIn(("stop", f"proxy-suite-per-app-via-{route}@{hexed}.service"), self.calls)
# The per-app TUN backend goes too once nothing uses it; the socks backend stays.
self.assertEqual(("stop", "proxy-suite-per-app-tun.service") in self.calls, route == "tun")
self.assertIn("Unknown outbound: fr", run(ctl.cmd_apps, "run", "--via", "fr", "--", "curl")[2])
self.assertIn("--route is tun or tproxy", run(ctl.cmd_apps, "run", "--via", "nl", "--route", "zapret", "--", "curl")[2])
os.environ["PER_APP_PIN_TUN"] = "0"
self.assertIn("no pin slots of per-app tun here", run(ctl.cmd_apps, "run", "--via", "nl", "--route", "tun", "--", "curl")[2])
# A global TProxy takes pinned apps past their route, as it does the other routes.
self.active = {"proxy-suite-tproxy.service"}
run(ctl.cmd_apps, "run", "--via", "nl", "--", "curl", "x")
self.assertEqual(self.execed, ["curl", "x"])
def test_runtime_profiles(self):
os.environ.update(PER_APP_PIN_TPROXY="1", RUNTIME_APPS_DIR=self.path("apps.d"))
os.makedirs(self.path("apps.d"))
self.patch("_outbound_tags", lambda: ["nl"])
self.patch("_outbound_groups", lambda: {})
self.assertIn("Added app profile: play", ok(ctl.cmd_apps, "add", "play", "--via", "nl"))
ok(ctl.cmd_apps, "add", "chat", "--route", "proxychains")
ok(ctl.cmd_apps, "add", "vpn", "--via", "de-2")
# Readable by whoever runs apps; the pin route chosen now, an interface outbound needs none.
self.assertEqual(os.stat(self.path("apps.d/play.json")).st_mode & 0o777, 0o644)
added = {p["name"]: p for p in ctl._per_app_profiles() if p.get("runtime")}
self.assertEqual((added["play"]["route"], added["play"]["outbound"]), ("tproxy", "nl"))
self.assertEqual((added["chat"]["route"], added["chat"]["outbound"]), ("proxychains", None))
self.assertEqual(added["vpn"]["outbound"], "de-2")
listing = ok(ctl.cmd_apps, "list")
self.assertRegex(listing, r"play\s+tproxy\s+nl\s+runtime")
self.assertRegex(listing, r"tun\s+tun\s+-\s+declared")
self.assertIn("play", ctl._complete_tree("apps", "rm"))
# Run like a declared one.
self.calls.clear()
self.assertEqual(run(ctl.cmd_apps, "run", "play", "--", "curl")[0], 0)
self.assertIn(("start", f"proxy-suite-per-app-via-tproxy@{'nl'.encode().hex()}.service"), self.calls)
for args, why in [
(("tun", "--route", "tun"), "declared in the NixOS configuration"),
(("Bad", "--route", "tun"), "Invalid profile name"),
(("x", "--route", "warp"), "--route is one of"),
(("x", "--via", "nl", "--route", "proxychains"), "needs --route tun or tproxy"),
(("x",), "usage"),
]:
with self.subTest(args=args):
status, out, err = run(ctl.cmd_apps, "add", *args)
self.assertNotEqual(status, 0)
self.assertIn(why, (out + err).lower() if why == "usage" else err)
self.assertIn("remove it there", run(ctl.cmd_apps, "rm", "tun")[2])
self.assertIn("Removed app profile: play", ok(ctl.cmd_apps, "rm", "play"))
self.assertFalse(os.path.exists(self.path("apps.d/play.json")))
self.assertIn("No app profile", run(ctl.cmd_apps, "rm", "play")[2])
def test_via_global_awg_profile(self):
# Brought up apart for the app, first, and taken down with the last app through it.
os.environ.update(PER_APP_VIA_PROFILES="1", AWG_PROFILES_FILE=self.write("awg.json", ["netcup"]))
key = "app-" + "netcup".encode().hex()
status, _, _ = run(ctl.cmd_apps, "run", "--via", "netcup", "--", "curl", "x")
self.assertEqual(status, 0)
starts = [c[1] for c in self.calls if c[0] == "start"]
self.assertEqual(starts[:2], ["proxy-suite-awg-app@netcup.service", f"proxy-suite-per-app-via@{key}.service"])
scope = next(c for c in self.calls if c[0] == "systemd-run")
self.assertIn(f"--slice=proxy-suite-per-app-via-{key}", scope)
self.assertIn(("stop", "proxy-suite-awg-app@netcup.service"), self.calls)
self.assertIn("netcup", ctl._complete_tree("apps", "run", "--via"))
# Up globally, it carries the app as it is.
self.calls.clear()
self.active = {"proxy-suite-awg-netcup.service"}
status, _, err = run(ctl.cmd_apps, "run", "--via", "netcup", "--", "curl", "x")
self.assertEqual(self.execed, ["curl", "x"])
self.assertIn("proxy-suite-awg-netcup.service is active", err)
self.assertNotIn(("start", "proxy-suite-awg-app@netcup.service"), self.calls)
def test_via_names_both_a_profile_and_an_outbound(self):
# "nl" is an outbound and a global profile: a bare name is refused, a prefix picks.
os.environ.update(PER_APP_VIA_PROFILES="1", PER_APP_PIN_TPROXY="1", AWG_PROFILES_FILE=self.write("awg.json", ["nl"]))
self.patch("_outbound_tags", lambda: ["nl"])
self.patch("_outbound_groups", lambda: {})
status, _, err = run(ctl.cmd_apps, "run", "--via", "nl", "--", "curl")
self.assertNotEqual(status, 0)
self.assertIn("say --via awg:nl or --via outbound:nl", err)
hexed = "nl".encode().hex()
for via, unit in (("awg:nl", f"proxy-suite-per-app-via@app-{hexed}.service"), ("outbound:nl", f"proxy-suite-per-app-via-tproxy@{hexed}.service")):
with self.subTest(via=via):
self.calls.clear()
self.assertEqual(run(ctl.cmd_apps, "run", "--via", via, "--", "curl")[0], 0)
self.assertIn(("start", unit), self.calls)
scope = next(c for c in self.calls if c[0] == "systemd-run")
self.assertTrue(any(a.startswith("--unit=") and ":" not in a for a in scope))
self.assertIn("No global AmneziaWG profile 'fr'", run(ctl.cmd_apps, "run", "--via", "awg:fr", "--", "curl")[2])
self.assertIn("--via takes an outbound", run(ctl.cmd_apps, "run", "--via", "wg:nl", "--", "curl")[2])
def test_tun_profile_keeps_backend_for_pins(self):
self.calls.clear()
run(ctl.cmd_apps, "run", "tun", "--", "curl")
listed = next(c for c in self.calls if c[0] == "list-units" and "proxy-suite-per-app-tun-user@*.service" in c)
self.assertIn("proxy-suite-per-app-via-tun@*.service", listed)
def test_via_under_global_tun_runs_plain(self):
self.active = {"proxy-suite-tun.service"}
run(ctl.cmd_apps, "run", "--via", "de-2", "--", "curl", "x")
self.assertEqual(self.execed, ["curl", "x"])
def test_disabled_route(self):
os.environ["PER_APP_ROUTING_ZAPRET_ENABLED"] = "0"
self.assertIn("perAppRouting.zapret.enable is false", run(ctl.cmd_apps, "run", "zapret", "--", "curl")[2])