feat: per-app awg and custom via
This commit is contained in:
43 files changed
+2674
-197
No files matched your search
@@ -111,7 +111,7 @@ in
|
||||
assert (de.conflicts or [ ]) == [ ];
|
||||
assert builtins.elem "multi-user.target" de.wantedBy;
|
||||
assert de.serviceConfig.Restart == "on-failure";
|
||||
assert hasInfix "ping -n -c 1 -I awg-de" deStart;
|
||||
assert hasInfix ''ping -n -c 1 -I "awg-de"'' deStart;
|
||||
assert !(builtins.elem "proxy-suite-awg-de.service" services.proxy-suite-tproxy.conflicts);
|
||||
assert builtins.elem "proxy-suite-awg-home.service" services.proxy-suite-tproxy.conflicts;
|
||||
assert !(builtins.elem "proxy-suite-awg-de.service" services.proxy-suite-awg-home.conflicts);
|
||||
@@ -123,9 +123,9 @@ in
|
||||
# IPv6 needs a route for sockets bound to the interface: a table of its own, looked up
|
||||
# only by them, and gone with the unit.
|
||||
(
|
||||
assert hasInfix "route replace default dev awg-de table 110" deStart;
|
||||
assert hasInfix "rule add pref 8991 oif awg-de table 110" deStart;
|
||||
assert hasInfix "rule del pref 8991 oif awg-de" deStop;
|
||||
assert hasInfix ''route replace default dev "awg-de" table "110"'' deStart;
|
||||
assert hasInfix ''rule add pref 8991 oif "awg-de" table "110"'' deStart;
|
||||
assert hasInfix ''rule del pref 8991 oif "awg-de"'' deStop;
|
||||
assert !(hasInfix "fib daddr type local" deStart);
|
||||
true
|
||||
)
|
||||
@@ -136,7 +136,7 @@ in
|
||||
homeStart;
|
||||
assert hasInfix "type route hook output priority mangle - 5" homeStart;
|
||||
assert hasInfix "nft delete table inet proxy-suite-awg-awg-home" homeStop;
|
||||
assert !(hasInfix "oif awg-home" homeStart);
|
||||
assert !(hasInfix "oif" homeStart);
|
||||
# UDP services answer from the right address even on a socket bound to every address.
|
||||
assert hasInfix "for port in 3478 49152-65535" homeStart;
|
||||
assert hasInfix ''rule add pref 8989 ipproto udp sport "$port" lookup main'' homeStart;
|
||||
|
||||
@@ -247,10 +247,12 @@ in
|
||||
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
|
||||
printf '%s\n' '● proxy-suite-awg-tunnel@old.service loaded failed failed x' \
|
||||
' proxy-suite-awg-if@gone.service loaded active exited x' > units
|
||||
: > log
|
||||
$sync
|
||||
grep -qx 'stop --no-block proxy-suite-awg-tunnel@old.service' log
|
||||
grep -qx 'stop --no-block proxy-suite-awg-if@gone.service' log
|
||||
touch "$out"
|
||||
'';
|
||||
|
||||
@@ -399,7 +401,21 @@ in
|
||||
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;
|
||||
assert pkgs.lib.hasInfix "proxy-suite-awg-$kind@$tag.service" runtimeSync;
|
||||
assert pkgs.lib.hasInfix "[[ -e /var/lib/proxy-suite/outbounds.d/$tag.iface ]] && kind=if"
|
||||
runtimeSync;
|
||||
# Or, with <tag>.iface, an interface of its own: a template like the global one's,
|
||||
# behind a direct outbound bound to it.
|
||||
assert runtimeUnits."proxy-suite-awg-if@".wantedBy == [ ];
|
||||
assert runtimeUnits."proxy-suite-awg-if@".serviceConfig.RuntimeDirectory == "proxy-suite-awg-if-%i";
|
||||
assert builtins.elem "proxy-suite-awg-if-watchdog@%i.service"
|
||||
runtimeUnits."proxy-suite-awg-if@".wants;
|
||||
assert runtimeUnits."proxy-suite-awg-if-watchdog@".bindsTo == [ "proxy-suite-awg-if@%i.service" ];
|
||||
assert pkgs.lib.hasInfix "_proxy_suite_add_awg_interface" runtimeOutboundScript;
|
||||
assert pkgs.lib.hasInfix ''bind_interface: $i, domain_resolver: ("awg-dns-" + $t)''
|
||||
runtimeOutboundScript;
|
||||
assert runtimeCtl.wrapperEnv.AWG_RUNTIME_IFACE_OUTBOUNDS == "1";
|
||||
assert runtimeCtl.wrapperEnv.AWG_RUNTIME_OUTBOUND_KIND == "userspace";
|
||||
# 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;
|
||||
|
||||
@@ -181,6 +181,10 @@ let
|
||||
;
|
||||
};
|
||||
|
||||
perAppRoutingAwgChecks = import ./per-app-routing-awg.nix {
|
||||
inherit checkLib pkgs mkPerAppUserRules;
|
||||
};
|
||||
|
||||
outboundValidationChecks = import ./outbound-validation.nix {
|
||||
inherit mkBadProxySuiteFixture mkFailingAssertions;
|
||||
};
|
||||
@@ -337,6 +341,7 @@ let
|
||||
++ subscriptionChecks.assertions
|
||||
++ routeModeChecks.assertions
|
||||
++ perAppRoutingChecks.assertions
|
||||
++ perAppRoutingAwgChecks.assertions
|
||||
++ serverModuleChecks.assertions
|
||||
);
|
||||
in
|
||||
|
||||
@@ -0,0 +1,285 @@
|
||||
{
|
||||
checkLib,
|
||||
pkgs,
|
||||
mkPerAppUserRules,
|
||||
}:
|
||||
|
||||
let
|
||||
inherit (checkLib) mkProxySuite rejectsProxySuite;
|
||||
generated = import ./read-generated.nix;
|
||||
inherit (pkgs.lib) hasInfix;
|
||||
|
||||
config = {
|
||||
enable = true;
|
||||
proxy = {
|
||||
enable = true;
|
||||
tproxy.enable = true;
|
||||
};
|
||||
killSwitch.enable = true;
|
||||
amneziaWg = {
|
||||
enable = true;
|
||||
kernelModulePackage = null;
|
||||
profiles = {
|
||||
de = {
|
||||
asOutbound = "interface";
|
||||
configFile = "/run/secrets/de.conf";
|
||||
};
|
||||
plain = {
|
||||
asOutbound = "singBox";
|
||||
configFile = "/run/secrets/plain.conf";
|
||||
};
|
||||
# Global: apps run through a copy of it, brought up apart.
|
||||
home.configFile = "/run/secrets/home.conf";
|
||||
};
|
||||
};
|
||||
perAppRouting = {
|
||||
enable = true;
|
||||
profiles = [
|
||||
{
|
||||
name = "game";
|
||||
route = "tun";
|
||||
outbound = "de";
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
fixture = mkProxySuite config;
|
||||
services = fixture.config.systemd.services;
|
||||
via = services."proxy-suite-per-app-via@";
|
||||
perAppRouting = mkPerAppUserRules fixture;
|
||||
viaUp = generated.readDerivation perAppRouting.viaUp;
|
||||
viaDns = generated.readDerivation perAppRouting.viaDns;
|
||||
appCopy = services."proxy-suite-awg-app@";
|
||||
viaFile = builtins.fromJSON (generated.readDerivation perAppRouting.perAppViaFile);
|
||||
viaUserStart = generated.readDerivation perAppRouting.viaUserStart;
|
||||
anchor = fixture.config.systemd.user.services."proxy-suite-per-app-via-anchor@";
|
||||
dePrepare = generated.readDerivation (
|
||||
builtins.head services.proxy-suite-awg-de.serviceConfig.ExecStartPre
|
||||
);
|
||||
nftr = import ../../modules/proxy-suite/nftables.nix {
|
||||
inherit (pkgs) lib;
|
||||
inherit pkgs;
|
||||
cfg = fixture.config.services.proxy-suite;
|
||||
};
|
||||
globalTproxyRules = generated.readDerivation nftr.nftablesRulesFile;
|
||||
killSwitchRules = generated.readDerivation nftr.killSwitchRulesFile;
|
||||
|
||||
withoutPerApp = mkProxySuite (config // { perAppRouting.enable = false; });
|
||||
|
||||
# Pins: any outbound, through per-app TProxy or TUN.
|
||||
pinConfig = {
|
||||
enable = true;
|
||||
proxy = {
|
||||
enable = true;
|
||||
ipv6 = false;
|
||||
tproxy.enable = true;
|
||||
outbounds = [
|
||||
{
|
||||
tag = "nl";
|
||||
url = "socks5://127.0.0.1:1080";
|
||||
}
|
||||
];
|
||||
};
|
||||
killSwitch.enable = true;
|
||||
perAppRouting = {
|
||||
enable = true;
|
||||
tproxy.enable = true;
|
||||
tun.enable = true;
|
||||
profiles = [
|
||||
{
|
||||
name = "game";
|
||||
route = "tproxy";
|
||||
outbound = "nl";
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
pinFixture = mkProxySuite pinConfig;
|
||||
pinServices = pinFixture.config.systemd.services;
|
||||
pinPerApp = mkPerAppUserRules pinFixture;
|
||||
pinTproxyUp = generated.readDerivation pinPerApp.pinUp.tproxy;
|
||||
pinTproxyDown = generated.readDerivation pinPerApp.pinDown.tproxy;
|
||||
pinTunUp = generated.readDerivation pinPerApp.pinUp.tun;
|
||||
pinUserStart = generated.readDerivation pinPerApp.viaUserStart;
|
||||
pinTproxyConfig = checkLib.mkTProxyConfig pinFixture;
|
||||
pinTunConfig = checkLib.mkPerAppTunConfig pinFixture;
|
||||
pinNft = import ../../modules/proxy-suite/nftables.nix {
|
||||
inherit (pkgs) lib;
|
||||
inherit pkgs;
|
||||
cfg = pinFixture.config.services.proxy-suite;
|
||||
};
|
||||
in
|
||||
{
|
||||
assertions = [
|
||||
# Only the "interface" outbound takes apps directly, by its table and a mark of its own.
|
||||
(
|
||||
assert
|
||||
viaFile == {
|
||||
de = {
|
||||
interface = "awg-de";
|
||||
table = 110;
|
||||
mark = 23040;
|
||||
dnsPort = 19100;
|
||||
unit = "proxy-suite-awg-de.service";
|
||||
dnsFile = "/run/proxy-suite-awg-de/dns";
|
||||
fallbackDns = [ "1.1.1.1" ];
|
||||
};
|
||||
};
|
||||
assert hasInfix "--dns-out \"/run/proxy-suite-awg-de/dns\"" dePrepare;
|
||||
true
|
||||
)
|
||||
# The instance: the app's packets marked into the table, masqueraded, DNS rewritten to the
|
||||
# profile's resolver, and turned away while the table has no route.
|
||||
(
|
||||
assert hasInfix "%i" via.serviceConfig.ExecStart;
|
||||
assert via.serviceConfig.Type == "notify";
|
||||
assert hasInfix "per_app_dns.py --port \"$dns_port\" --mark \"$mark\"" viaDns;
|
||||
assert hasInfix ''nft_table="proxy_suite_per_app_via_$mark"'' viaUp;
|
||||
assert hasInfix ''oifname "$interface" meta mark $mark masquerade'' viaUp;
|
||||
assert hasInfix "ct mark $mark meta l4proto { tcp, udp } th dport 53 redirect to :$dns_port" viaUp;
|
||||
assert hasInfix "th dport 53 goto app_mark" viaUp;
|
||||
assert hasInfix "ip daddr 192.168.0.0/16 return" viaUp;
|
||||
assert hasInfix ''rule add pref 8986 fwmark "$mark" lookup "$table"'' viaUp;
|
||||
assert hasInfix ''rule add pref 8987 fwmark "$mark" unreachable'' viaUp;
|
||||
assert hasInfix "priority mangle - 10" viaUp;
|
||||
# The tunnel's own packets carry its FwMark and still look like the app's: never taken,
|
||||
# nor marked on the way in, or they loop back into the tunnel.
|
||||
assert hasInfix "meta mark != 0 return" viaUp;
|
||||
assert hasInfix ''iifname "$interface" ct mark $mark meta mark set $mark'' viaUp;
|
||||
# Private ranges go through the tunnel, the server's own address among them.
|
||||
assert !(hasInfix "RESERVED_IP" viaUp);
|
||||
true
|
||||
)
|
||||
# Each user's apps, in the outbound's slice; the slice from a user unit.
|
||||
(
|
||||
assert hasInfix "^([0-9]+)-((awg|app|tproxy|tun)-[0-9a-f]+)$" viaUserStart;
|
||||
assert hasInfix "nft_chain=app_mark" viaUserStart;
|
||||
assert hasInfix ''-name "$slice_name"'' viaUserStart;
|
||||
assert hasInfix "meta mark set $mark ct mark set $mark" viaUserStart;
|
||||
assert anchor.serviceConfig.Slice == "proxy-suite-per-app-via-%i.slice";
|
||||
assert services ? "proxy-suite-per-app-via-user@";
|
||||
true
|
||||
)
|
||||
# Global TProxy and the kill switch let the apps' mark past.
|
||||
(
|
||||
# And the slots of the outbounds added at runtime, on interfaces of their own.
|
||||
assert hasInfix "meta mark { 23040, 23104, 23105," globalTproxyRules;
|
||||
# And the slots of the global profiles apps run through.
|
||||
assert hasInfix "23119, 23232," globalTproxyRules;
|
||||
assert hasInfix "23239 } return" globalTproxyRules;
|
||||
assert hasInfix "23040" killSwitchRules;
|
||||
true
|
||||
)
|
||||
# A global profile: a copy rendered as an "interface" outbound has it, on a slot's
|
||||
# interface, never up with the profile itself.
|
||||
(
|
||||
assert appCopy.wantedBy == [ ];
|
||||
assert appCopy.serviceConfig.Restart == "no";
|
||||
assert
|
||||
builtins.elem "proxy-suite-awg-%i.service" appCopy.conflicts
|
||||
&& builtins.elem "proxy-suite-awg@%i.service" appCopy.conflicts;
|
||||
assert builtins.elem "proxy-suite-awg-app-watchdog@%i.service" appCopy.wants;
|
||||
assert services."proxy-suite-awg-app-watchdog@".bindsTo == [ "proxy-suite-awg-app@%i.service" ];
|
||||
assert appCopy.serviceConfig.RuntimeDirectory == "proxy-suite-awg-app-%i";
|
||||
assert hasInfix ''"/run/proxy-suite-awg-app-$tag/slot"'' viaUp;
|
||||
assert hasInfix "^(awg|app)-([0-9a-f][0-9a-f]){1,64}$" viaUp;
|
||||
assert hasInfix ''iifname "psawga*" accept''
|
||||
fixture.config.networking.firewall.extraReversePathFilterRules;
|
||||
true
|
||||
)
|
||||
# Without per-app routing there is nothing to run via.
|
||||
(
|
||||
assert !(withoutPerApp.config.systemd.services ? "proxy-suite-per-app-via@");
|
||||
true
|
||||
)
|
||||
# Pin slots: a listener, a rule and a selector each in the backend, which the filter fills;
|
||||
# a unit per route, part of the backend whose selector it switches.
|
||||
(
|
||||
assert pinServices ? "proxy-suite-per-app-via-tproxy@";
|
||||
assert pinServices."proxy-suite-per-app-via-tproxy@".partOf == [ "proxy-suite-socks.service" ];
|
||||
assert pinServices."proxy-suite-per-app-via-tun@".partOf == [ "proxy-suite-per-app-tun.service" ];
|
||||
assert builtins.elem "proxy-suite-tproxy.service"
|
||||
pinServices."proxy-suite-per-app-via-tun@".conflicts;
|
||||
assert pinServices ? "proxy-suite-per-app-via-user@";
|
||||
# The kernel path's unit only with an "interface" AmneziaWG outbound to take.
|
||||
assert !(pinServices ? "proxy-suite-per-app-via@");
|
||||
assert builtins.elem {
|
||||
type = "tproxy";
|
||||
tag = "per-app-pin-tproxy-0";
|
||||
listen = "127.0.0.1";
|
||||
listen_port = 19160;
|
||||
} pinTproxyConfig.inbounds;
|
||||
assert builtins.elem {
|
||||
inbound = [ "per-app-pin-tproxy-7" ];
|
||||
outbound = "proxy-suite-pin-tproxy-7";
|
||||
} pinTproxyConfig.route.rules;
|
||||
assert builtins.elem {
|
||||
type = "selector";
|
||||
tag = "proxy-suite-pin-tproxy-0";
|
||||
outbounds = [ "block" ];
|
||||
default = "block";
|
||||
} pinTproxyConfig.outbounds;
|
||||
assert builtins.elem {
|
||||
inbound = [ "tun-in" ];
|
||||
source_ip_cidr = [ "172.20.1.2/32" ];
|
||||
outbound = "proxy-suite-pin-tun-0";
|
||||
} pinTunConfig.route.rules;
|
||||
assert pinTunConfig.experimental.clash_api.external_controller == "127.0.0.1:19180";
|
||||
assert hasInfix ''select_pin "$tag" 60'' pinTproxyUp;
|
||||
assert hasInfix "/proxies/proxy-suite-pin-tproxy-$slot" pinTproxyUp;
|
||||
assert hasInfix "/run/proxy-suite-socks/clash-secret" pinTproxyUp;
|
||||
assert hasInfix "127.0.0.1:19180" pinTunUp;
|
||||
assert hasInfix ''route replace default dev psperapptun0 table "$table"'' pinTunUp;
|
||||
# The backend tells a TUN slot by the source its packets are SNATed to.
|
||||
assert hasInfix ''oifname "psperapptun0" meta mark 23200 meta nfproto ipv4 snat ip to 172.20.1.2'' (
|
||||
generated.readDerivation (builtins.head pinNft.perAppPinTunChainFiles)
|
||||
);
|
||||
assert hasInfix ''rule add pref 8988 fwmark "$mark" table "$table"'' pinTunUp;
|
||||
assert hasInfix "select_pin block 1" pinTproxyDown;
|
||||
assert builtins.length pinNft.perAppPinTproxyRulesFiles == 8;
|
||||
assert hasInfix "tproxy ip to 127.0.0.1:19167" (
|
||||
generated.readDerivation (pkgs.lib.last pinNft.perAppPinTproxyRulesFiles)
|
||||
);
|
||||
assert hasInfix "table inet proxy_suite_per_app_via_tun_0" (
|
||||
generated.readDerivation (builtins.head pinNft.perAppPinTunChainFiles)
|
||||
);
|
||||
assert hasInfix "ct mark 23200 meta mark set 23200" (
|
||||
generated.readDerivation (builtins.head pinNft.perAppPinTunChainFiles)
|
||||
);
|
||||
# The user rule units find the slot a pin holds.
|
||||
assert hasInfix "^([0-9]+)-((awg|app|tproxy|tun)-[0-9a-f]+)$" pinUserStart;
|
||||
assert hasInfix "nft_chain=output" pinUserStart;
|
||||
assert hasInfix "23168" (checkLib.mkNftRules pinFixture "killSwitchRulesFile");
|
||||
true
|
||||
)
|
||||
# Any outbound for a tproxy or tun profile; only an "interface" one for the rest.
|
||||
(rejectsProxySuite "outbound 'nl' needs route \"tproxy\" or \"tun\"" (
|
||||
pinConfig
|
||||
// {
|
||||
perAppRouting = pinConfig.perAppRouting // {
|
||||
profiles = [
|
||||
{
|
||||
name = "game";
|
||||
route = "proxychains";
|
||||
outbound = "nl";
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
))
|
||||
(rejectsProxySuite "outbound 'plain' needs route \"tproxy\" or \"tun\"" (
|
||||
config
|
||||
// {
|
||||
perAppRouting = config.perAppRouting // {
|
||||
profiles = [
|
||||
{
|
||||
name = "game";
|
||||
route = "tun";
|
||||
outbound = "plain";
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
))
|
||||
];
|
||||
}
|
||||
@@ -114,7 +114,10 @@ in
|
||||
);
|
||||
assert !(hasInfix "\"set-property\"" (polkitConfig allFixture));
|
||||
# A user's per-app marking is theirs alone.
|
||||
assert hasInfix "if (instance !== uid)" (polkitConfig allFixture);
|
||||
assert hasInfix "if (owner !== uid)" (polkitConfig allFixture);
|
||||
assert hasInfix ''"proxy-suite-per-app-via-user@" ? instance.split("-")[0]'' (
|
||||
polkitConfig allFixture
|
||||
);
|
||||
assert groupReadsSecrets allFixture;
|
||||
assert hasInfix "chown proxy-suite-daemon:proxy-suite \"$backend_config\"" (socksStart allFixture);
|
||||
assert
|
||||
|
||||
Reference in new issue
Block a user