feat: awg runtime add

This commit is contained in:
fufsob committed 2026-09-28 18:51:47 +00:00
1 parent cd3aa5a53f
commit 364bed8d51
39 files changed
+1581 -190

No files matched your search

+36
View File
@@ -134,6 +134,42 @@ pkgs.testers.runNixOSTest {
userspace.succeed("systemctl stop proxy-suite-awg-test.service")
userspace.fail("ip link show awg-test")
with subtest("a profile added at runtime"):
# The same client as "test", as a .conf pasted in rather than declared.
userspace.succeed("""umask 077; cat > /root/rt.conf <<'EOF'
[Interface]
PrivateKey = ${keys.peer1.privateKey}
Address = 10.23.42.2/32
Jc = ${toString obfuscation.jc}
Jmin = ${toString obfuscation.jmin}
Jmax = ${toString obfuscation.jmax}
S1 = ${toString obfuscation.s1}
S2 = ${toString obfuscation.s2}
[Peer]
PublicKey = ${keys.peer0.publicKey}
Endpoint = 192.168.0.1:23542
AllowedIPs = 10.23.42.1/32
PersistentKeepalive = 5
EOF""")
userspace.succeed("proxy-ctl awg add rt /root/rt.conf")
userspace.succeed("test $(stat -c %a /var/lib/proxy-suite/amneziawg.d/rt.conf) = 600")
userspace.succeed("proxy-ctl awg list | grep -E 'rt +inactive +runtime'")
userspace.succeed("proxy-ctl awg on rt")
userspace.succeed("ip link show awg-rt")
userspace.succeed("ping -c 5 10.23.42.1")
userspace.succeed("systemctl is-active proxy-suite-awg-watchdog@rt.service")
# A declared profile takes over: they share the peer, and only one global tunnel runs.
userspace.succeed("systemctl start proxy-suite-awg-test.service")
userspace.fail("systemctl is-active proxy-suite-awg@rt.service")
userspace.fail("ip link show awg-rt")
userspace.succeed("systemctl stop proxy-suite-awg-test.service")
# Hooks are refused whoever adds the config, and nothing is written.
userspace.fail("sed 's/^Address/PostUp = touch \\/pwned\\nAddress/' /root/rt.conf | proxy-ctl awg add hooked -")
userspace.fail("test -e /var/lib/proxy-suite/amneziawg.d/hooked.conf")
userspace.succeed("proxy-ctl awg rm rt")
userspace.fail("test -e /var/lib/proxy-suite/amneziawg.d/rt.conf")
with subtest("failed handshake rolls back userspace routing"):
userspace.fail("systemctl start proxy-suite-awg-broken.service")
userspace.fail("ip link show awg-broken")
+172 -4
View File
@@ -67,6 +67,15 @@ let
}
];
homeService = awgOnly.config.systemd.services.proxy-suite-awg-home;
awgOnlyNoRuntime = mkProxySuite {
enable = true;
amneziaWg = {
enable = true;
kernelModulePackage = null;
runtime.enable = false;
profiles.home.configFile = "/run/secrets/awg.conf";
};
};
workService = awgOnly.config.systemd.services.proxy-suite-awg-work;
ctl = mkProxyCtlDerived awgOnly;
homeWatchdog = awgOnly.config.systemd.services.proxy-suite-awg-home-watchdog;
@@ -146,10 +155,105 @@ let
];
withGlobalAwgService = withGlobalTun.config.systemd.services.proxy-suite-awg-home;
withGlobalAwgStartPre = withGlobalAwgService.serviceConfig.ExecStartPre;
withGlobalAwgBypassUp = generated.readDerivation (builtins.elemAt withGlobalAwgStartPre 1);
withGlobalAwgBypassUp = generated.readDerivation (builtins.elemAt withGlobalAwgStartPre 2);
# Nothing declared: every profile and AmneziaWG outbound comes from proxy-ctl.
runtimeOnly = evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = {
enable = true;
killSwitch.enable = true;
userControl = {
enable = true;
scopes = [ "amneziaWg" ];
};
proxy = {
enable = true;
backend = "sing-box";
outbounds = [
{
tag = "primary";
url = "http://proxy.example.com:8080";
}
];
};
amneziaWg = {
enable = true;
kernelModulePackage = null;
};
};
}
];
runtimeUnits = runtimeOnly.config.systemd.services;
# The scripts that list units, run against a systemctl that lists what the check says.
stubSystemctl = pkgs.writeShellScript "systemctl" ''
echo "$*" >> "$SYSTEMCTL_LOG"
if [ "$1" = list-units ]; then cat "$UNITS"; fi
'';
stubbed = evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = {
enable = true;
host.systemctl = pkgs.lib.mkForce "${stubSystemctl}";
proxy = {
enable = true;
backend = "sing-box";
outbounds = [
{
tag = "primary";
url = "http://proxy.example.com:8080";
}
];
};
amneziaWg = {
enable = true;
kernelModulePackage = null;
profiles.home.configFile = "/run/secrets/awg.conf";
};
};
}
];
stubbedUnits = stubbed.config.systemd.services;
runtimeTemplate = runtimeUnits."proxy-suite-awg@";
runtimeTunnel = runtimeUnits."proxy-suite-awg-tunnel@";
runtimeSync = generated.readDerivation runtimeUnits.proxy-suite-awg-runtime-sync.serviceConfig.ExecStart;
runtimeKillSwitchRules = checkLib.mkNftRules runtimeOnly "killSwitchRulesFile";
runtimeCtl = mkProxyCtlDerived runtimeOnly;
runtimeOutboundScript = generated.readDerivation runtimeUnits.proxy-suite-socks.serviceConfig.ExecStart;
withGlobalAwgBypassDown = generated.readDerivation withGlobalAwgService.serviceConfig.ExecStopPost;
in
{
# Nothing listed is no failure (grep finding nothing, under pipefail, once was); what is
# listed is stopped, a failed unit's glyph and all, but never the unit itself.
unitScripts = pkgs.runCommand "amneziawg-unit-scripts-check" { } ''
export SYSTEMCTL_LOG=$PWD/log UNITS=$PWD/units
stop=${builtins.elemAt stubbedUnits.proxy-suite-awg-home.serviceConfig.ExecStartPre 1}
sync=${stubbedUnits.proxy-suite-awg-runtime-sync.serviceConfig.ExecStart}
: > units
$stop
$stop self
$sync
! grep -q '^stop' log
printf '%s\n' \
'● proxy-suite-awg@gone.service loaded failed failed x' \
' proxy-suite-awg@self.service loaded active exited x' > units
: > log
$stop self
grep -qx 'stop proxy-suite-awg@gone.service' log
! grep -q 'stop proxy-suite-awg@self.service' log
printf '%s\n' '● proxy-suite-awg-tunnel@old.service loaded failed failed x' > units
: > log
$sync
grep -qx 'stop --no-block proxy-suite-awg-tunnel@old.service' log
touch "$out"
'';
manifest = pkgs.runCommand "amneziawg-secret-manifest-check" { } ''
${pkgs.python3}/bin/python3 - <<'PY'
import json
@@ -185,7 +289,11 @@ in
assert builtins.elem "proxy-suite-per-app-zapret.service" homeService.conflicts;
assert workService.wantedBy == [ ];
assert homeService.serviceConfig.NoNewPrivileges;
assert builtins.length homeService.serviceConfig.ExecStartPre == 1;
# Rendering, then stopping any profile added at runtime: they share an interface.
assert builtins.length homeService.serviceConfig.ExecStartPre == 2;
assert pkgs.lib.hasInfix "proxy-suite-awg@*.service" (
generated.readDerivation (builtins.elemAt homeService.serviceConfig.ExecStartPre 1)
);
assert !(homeService.serviceConfig ? ExecStopPost);
# A silent handshake moves the interface to a new port: at start, and from the watchdog.
assert pkgs.lib.hasInfix "set \"$interface\" listen-port 0" homeStart;
@@ -255,7 +363,7 @@ in
withGlobalTun.config.systemd.services.proxy-suite-per-app-zapret.conflicts;
assert builtins.elem "proxy-suite-socks.service" withGlobalAwgService.after;
assert builtins.elem "proxy-suite-socks.service" withGlobalAwgService.wants;
assert builtins.length withGlobalAwgStartPre == 2;
assert builtins.length withGlobalAwgStartPre == 3;
assert pkgs.lib.hasInfix "rule add" withGlobalAwgBypassUp;
assert pkgs.lib.hasInfix "pref 8998" withGlobalAwgBypassUp;
assert pkgs.lib.hasInfix "fwmark 2 lookup main" withGlobalAwgBypassUp;
@@ -266,11 +374,71 @@ in
assert pkgs.lib.hasInfix "fwmark 2 lookup main" withGlobalAwgBypassDown;
true
)
# Profiles added with proxy-ctl: a template sharing one interface, which the kill switch
# knows; a tunnel template per outbound, started by a sync unit the reload pulls in.
(
assert runtimeTemplate.wantedBy == [ ];
assert builtins.elem "proxy-suite-awg-watchdog@%i.service" runtimeTemplate.wants;
assert builtins.elem "proxy-suite-killswitch.service" runtimeTemplate.before;
assert builtins.elem "proxy-suite-killswitch.service" runtimeTemplate.wants;
assert builtins.elem "proxy-suite-tun.service" runtimeTemplate.conflicts;
assert runtimeTemplate.serviceConfig.Group == "proxy-suite-awg";
assert runtimeTemplate.serviceConfig.RuntimeDirectory == "proxy-suite-awg-rt-%i";
assert pkgs.lib.hasSuffix " %i" runtimeTemplate.serviceConfig.ExecStart;
assert builtins.all (pkgs.lib.hasSuffix " %i") (
pkgs.lib.take 2 runtimeTemplate.serviceConfig.ExecStartPre
);
assert runtimeUnits."proxy-suite-awg-watchdog@".bindsTo == [ "proxy-suite-awg@%i.service" ];
assert runtimeUnits."proxy-suite-awg-watchdog@".wantedBy == [ ];
assert runtimeTunnel.wantedBy == [ ];
assert runtimeTunnel.serviceConfig.Restart == "on-failure";
assert runtimeTunnel.serviceConfig.RuntimeDirectory == "proxy-suite-awg-tunnel-%i";
assert builtins.elem "proxy-suite-outbound-reload.service"
runtimeUnits.proxy-suite-awg-runtime-sync.wantedBy;
assert pkgs.lib.hasInfix "proxy-suite-awg-tunnel@$tag.service" runtimeSync;
# The backend dials each one's tunnel on the port in <tag>.port.
assert pkgs.lib.hasInfix ''"/var/lib/proxy-suite/outbounds.d"/*.awg'' runtimeOutboundScript;
assert pkgs.lib.hasInfix "_proxy_suite_add_socks_hop" runtimeOutboundScript;
assert pkgs.lib.hasInfix ''oifname { "awg-rt" } accept'' runtimeKillSwitchRules;
assert pkgs.lib.hasInfix ''meta skgid "proxy-suite-awg" accept'' runtimeKillSwitchRules;
assert builtins.elem "d /var/lib/proxy-suite/amneziawg.d 2775 root proxy-suite -"
runtimeOnly.config.systemd.tmpfiles.rules;
assert builtins.elem "d /var/lib/proxy-suite/outbounds.d 0700 root root -"
runtimeOnly.config.systemd.tmpfiles.rules;
assert builtins.elem runtimeOnly.config.services.proxy-suite.amneziaWg.wireproxyPackage
runtimeOnly.config.environment.systemPackages;
assert pkgs.lib.hasInfix "proxy-suite-awg-tunnel@" runtimeOnly.config.security.polkit.extraConfig;
assert runtimeCtl.wrapperEnv.AWG_RUNTIME_GLOBAL == "1";
assert runtimeCtl.wrapperEnv.AWG_RUNTIME_OUTBOUNDS == "1";
assert runtimeCtl.wrapperEnv.AWG_RUNTIME_DIR == "/var/lib/proxy-suite/amneziawg.d";
assert pkgs.lib.hasSuffix "/amneziawg_config.py" runtimeCtl.wrapperEnv.AWG_CONFIG_TOOL;
assert runtimeCtl.awgProfiles == [ ];
# Declared profiles only in a configuration that turns the runtime ones off.
assert !(awgOnlyNoRuntime.config.systemd.services ? "proxy-suite-awg@");
assert
builtins.length awgOnlyNoRuntime.config.systemd.services.proxy-suite-awg-home.serviceConfig.ExecStartPre
== 1;
true
)
]
++ mkFailingAssertions mkBadProxySuiteFixture [
{
enable = true;
amneziaWg.enable = true;
amneziaWg = {
enable = true;
runtime.enable = false;
};
}
{
enable = true;
amneziaWg = {
enable = true;
kernelModulePackage = null;
profiles.home = {
interfaceName = "awg-rt";
configFile = "/run/home.conf";
};
};
}
{
enable = true;
+2 -1
View File
@@ -151,8 +151,9 @@ in
assert cfg.users.groups ? proxy-suite-awg;
assert pkgs.lib.hasInfix "--fwmark 51820" awgKillSwitchPrepare;
assert pkgs.lib.hasInfix ''meta skgid "proxy-suite-awg" accept'' rules;
# And the one the profiles added at runtime share.
assert pkgs.lib.hasInfix
''oifname { "${cfg.services.proxy-suite.amneziaWg.profiles.home.interfaceName}" } accept''
''oifname { "${cfg.services.proxy-suite.amneziaWg.profiles.home.interfaceName}", "awg-rt" } accept''
rules;
assert pkgs.lib.hasInfix "meta mark { 1, 2, 51820 } accept" rules;
assert !(pkgs.lib.hasInfix "skgid" killSwitchNftRules);
+30
View File
@@ -165,6 +165,18 @@ let
];
smCfg = systemManager.config;
# AmneziaWG with nothing declared: outbounds added with proxy-ctl run as user units.
hmRuntimeAwg = evalHost "homeManager" [
rootlessSettings
{
services.proxy-suite.amneziaWg = {
enable = true;
kernelModulePackage = null;
};
}
];
hmRuntimeAwgUnits = hmRuntimeAwg.config.systemd.user.services;
nixOnDroid = evalHost "nixOnDroid" [ rootlessSettings ];
nodCfg = nixOnDroid.config;
@@ -200,6 +212,17 @@ in
));
true
)
(
# Rootless: AmneziaWG outbounds added at runtime, never a global profile.
assert failedAssertions hmRuntimeAwg == [ ];
assert hmRuntimeAwgUnits ? "proxy-suite-awg-tunnel@";
assert hmRuntimeAwgUnits ? proxy-suite-awg-runtime-sync;
assert !(hmRuntimeAwgUnits ? "proxy-suite-awg@");
assert
!(lib.hasInfix "setpriv" (toString hmRuntimeAwgUnits."proxy-suite-awg-tunnel@".Service.ExecStart));
assert forced hmRuntimeAwgUnits;
true
)
(
# Rootless, the socks unit only listens: no TProxy inbound, no SO_MARK.
assert lib.any (inbound: inbound.type == "mixed") hmSocksConfig.inbounds;
@@ -244,6 +267,13 @@ in
perAppRouting.zapret.enable = true;
} "perAppRouting.zapret needs root";
assert rejects "nixOnDroid" { gui.enable = true; } "the GUI needs a desktop session";
# No templates in proxy-suitectl, and no root for a global profile: nothing to add to.
assert rejects "nixOnDroid" {
amneziaWg = {
enable = true;
kernelModulePackage = null;
};
} "amneziaWg.enable needs a profile";
assert rejects "nixOnDroid" {
inbounds.listeners.vless.port = lib.mkForce 443;
} "inbounds.listeners.vless.port = 443 is a privileged port";
+1
View File
@@ -332,6 +332,7 @@ in
{
proxy-suite-module = builtins.seq validated (pkgs.writeText "proxy-suite-module-check" "ok");
amneziawg-secret-manifest = amneziaWgChecks.manifest;
amneziawg-unit-scripts = amneziaWgChecks.unitScripts;
xray-jq-filter-runtime = xrayBackendChecks.runtime;
tor-guard-runtime = torChecks.runtime;
per-app-zapret-runtime = perAppRoutingChecks.runtime;
+35 -1
View File
@@ -122,6 +122,19 @@ let
};
ipNamedConfig = mkInboundsConfig ipNamedFixture;
# Other names and IPs of this host, beside the one in share links.
aliasedFixture = mkInbounds {
serverAddress = "vpn.example.com";
serverAliases = [
"turn.example.com"
"203.0.113.10"
"2001:db8::10"
"vpn.example.com"
];
listeners.vless-in = realityListener;
};
aliasedConfig = mkInboundsConfig aliasedFixture;
unguardedFixture = mkInbounds {
routing = {
blockRu = false;
@@ -812,7 +825,28 @@ let
(ok ((ruleByTag blockedConfig "inbound-server-address-direct").inboundTag == [ "open" ]))
# No address to exempt when it is detected at runtime instead.
(ok (!builtins.elem "inbound-server-address-direct" (ruleTags relayConfig)))
(ok ((ruleByTag ipNamedConfig "inbound-server-address-direct").ip == [ "82.146.44.102" ]))
(ok ((ruleByTag ipNamedConfig "inbound-server-address-direct-ip").ip == [ "82.146.44.102" ]))
(ok (!builtins.elem "inbound-server-address-direct" (ruleTags ipNamedConfig)))
(ok (!builtins.elem "inbound-server-address-direct-ip" (ruleTags namedConfig)))
# Aliases join serverAddress, names and IPs apart, as XRay ANDs one rule's fields.
(
assert
(ruleByTag aliasedConfig "inbound-server-address-direct").domain == [
"full:vpn.example.com"
"full:turn.example.com"
];
true
)
(
assert
(ruleByTag aliasedConfig "inbound-server-address-direct-ip").ip == [
"203.0.113.10"
"2001:db8::10"
];
true
)
(ok ((ruleByTag aliasedConfig "inbound-server-address-direct-ip").port == "443"))
(ok ((ruleByTag aliasedConfig "inbound-server-address-direct-ip").outboundTag == "direct"))
(ok (!builtins.elem "inbound-block-ru-domain" (ruleTags unguardedConfig)))
# A per-listener via becomes its own rule; listeners on the default do not.
+4 -1
View File
@@ -73,7 +73,10 @@ pkgs.lib.mapAttrs mkPythonCheck {
proxy-ctl-unit = {
drv = "proxy-suite-proxy-ctl-unit";
file = "${proxyCtl}/test_proxy_ctl.py";
env = singBox;
# `awg add` and AmneziaWG outbounds hand their configs to it.
env = singBox // {
AWG_CONFIG_TOOL = "${scripts}/amneziawg_config.py";
};
};
# proxy-suitectl, nix-on-droid's service manager: real processes restarted, timed and stopped.
proxy-suite-supervisor-unit = {
+2
View File
@@ -503,6 +503,8 @@ in
has exclude zapret auto
has proxy-suite-awg-work logs
has work awg on
has add awg
has rm awg
has torrent apps run
has unpin proxy
has newnym tor