fix: awg & warp fixes

This commit is contained in:
fufsob committed 2026-09-28 02:52:13 +00:00
1 parent 08b00cf449
commit d25fcb5468
17 files changed
+277 -7

No files matched your search

+7 -2
View File
@@ -143,10 +143,15 @@ let
'') sysctl
)}
# Diverted packets are for the local XRay sockets, whatever their destination.
# Diverted packets are for the local XRay sockets, whatever their destination. Only as
# they come in: with src_valid_mark on (awg-quick turns it on for a global profile and
# leaves it), the kernel checks a packet's source by its mark too, and the local route
# here would make every client's IPv4 address a martian.
${lib.concatMapStrings (family: ''
${ip} ${family} route replace local default dev lo table ${toString awgInboundRouteTable}
${ip} ${family} rule add pref ${toString awgInboundRulePriority} fwmark ${toString awgInboundFwmark} table ${toString awgInboundRouteTable}
${lib.concatMapStrings (listener: ''
${ip} ${family} rule add pref ${toString awgInboundRulePriority} iif ${lib.escapeShellArg listener.interface} fwmark ${toString awgInboundFwmark} table ${toString awgInboundRouteTable}
'') listeners}
'') ([ "-4" ] ++ lib.optional ipv6 "-6")}
${nft} -f ${nftRulesFile}
+113 -3
View File
@@ -11,7 +11,12 @@ let
profiles = awgCfg.profiles;
profileNames = builtins.attrNames profiles;
globalProfileNames = builtins.attrNames derived.awgGlobalProfiles;
inherit (derived.constants) awgGlobalFwmark awgGlobalGroup;
inherit (derived.constants)
awgGlobalFwmark
awgGlobalGroup
awgOutboundRulePriority
awgServerUdpRulePriority
;
killSwitchUnit = "proxy-suite-killswitch.service";
# Profiles behind a loopback SOCKS hop, in a tunnel unit rather than awg-quick.
tunnelOutbounds = derived.awgTunnelOutbounds;
@@ -128,6 +133,95 @@ let
}
'';
# An outbound interface runs with Table=off. A socket bound to it reaches IPv4 without a
# route (the kernel takes the destination as on-link), but IPv6 is unreachable, so the
# interface gets a table of its own, which only sockets bound to it look up.
outboundRuleCleanup = name: profile: ''
for family in -4 -6; do
while ${pkgs.iproute2}/bin/ip "$family" rule del pref ${toString awgOutboundRulePriority} oif ${lib.escapeShellArg profile.interfaceName} 2>/dev/null; do :; done
${pkgs.iproute2}/bin/ip "$family" route flush table ${toString (outboundRouteTable name)} 2>/dev/null || true
done
'';
outboundRoutesUp = name: profile: ''
${outboundRuleCleanup name profile}
for family in -4 -6; do
if [[ -n $(${pkgs.iproute2}/bin/ip "$family" -o addr show dev ${lib.escapeShellArg profile.interfaceName} scope global) ]]; then
${pkgs.iproute2}/bin/ip "$family" route replace default dev ${lib.escapeShellArg profile.interfaceName} table ${toString (outboundRouteTable name)}
${pkgs.iproute2}/bin/ip "$family" rule add pref ${toString awgOutboundRulePriority} oif ${lib.escapeShellArg profile.interfaceName} table ${toString (outboundRouteTable name)}
fi
done
'';
outboundRouteTable =
name: (lib.findFirst (ob: ob.name == name) null derived.awgInterfaceOutbounds).routeTable;
# A global profile sends everything without its fwmark into the tunnel, replies to
# connections made to this host from elsewhere (SSH, a web server, inbound listeners)
# included, which then never reach the client. Those connections get the fwmark, so
# their packets take the main table and leave the way they came. Before the
# reverse-path filter, which then finds the route back as well.
#
# A UDP socket bound to every address is routed before any packet exists to mark, so the
# tunnel's address would become its source. The ports the host serves UDP on are sent to
# the main table by rule instead, which the kernel checks before it picks the source.
repliesTable = profile: "proxy-suite-awg-${profile.interfaceName}";
serverUdpPorts = lib.unique (
map toString (awgCfg.serverUdpPorts ++ derived.proxyInboundFirewallUdpPorts)
++ cfg.host.openUdpPorts
);
serverUdpRulesDown = ''
for family in -4 -6; do
while ${pkgs.iproute2}/bin/ip "$family" rule del pref ${toString awgServerUdpRulePriority} 2>/dev/null; do :; done
done
'';
serverUdpRulesUp = ''
${serverUdpRulesDown}
for family in -4 -6; do
for port in ${lib.escapeShellArgs serverUdpPorts}; do
# IPv6 may be off.
${pkgs.iproute2}/bin/ip "$family" rule add pref ${toString awgServerUdpRulePriority} ipproto udp sport "$port" lookup main \
|| [[ $family == -6 ]]
done
done
'';
repliesUp = profile: ''
fwmark=$(${awgCfg.toolsPackage}/bin/awg show ${lib.escapeShellArg profile.interfaceName} fwmark)
# Off when the profile routes nothing by default (a table of its own, or Table=off).
if [[ "$fwmark" != off ]]; then
${serverUdpRulesUp}
${pkgs.nftables}/bin/nft -f - <<EOF
table inet ${repliesTable profile} {
chain prerouting {
type filter hook prerouting priority mangle - 5; policy accept;
ct state new iifname != { "lo", "${profile.interfaceName}" } fib daddr type local ct mark set $fwmark
ct mark $fwmark meta mark set $fwmark
}
chain output {
type route hook output priority mangle - 5; policy accept;
ct mark $fwmark meta mark set $fwmark
}
}
EOF
fi
'';
repliesDown = profile: ''
${pkgs.nftables}/bin/nft delete table inet ${repliesTable profile} 2>/dev/null || true
${serverUdpRulesDown}
'';
# awg-quick turns src_valid_mark on for a default route and never back off; left on, it
# changes how the kernel checks every marked packet's source. Put back as it was.
srcValidMark = "/proc/sys/net/ipv4/conf/all/src_valid_mark";
savedSrcValidMark = name: "${runtimeDir name}/src_valid_mark";
saveSrcValidMark = name: ''
[[ -e ${savedSrcValidMark name} ]] || cat ${srcValidMark} > ${savedSrcValidMark name}
'';
restoreSrcValidMark = name: ''
if [[ -s ${savedSrcValidMark name} ]]; then
cat ${savedSrcValidMark name} > ${srcValidMark} || true
rm -f ${savedSrcValidMark name}
fi
'';
# `ping` for the handshake probe. An outbound interface has no route to the probe address, so
# the probe is bound to it.
pingVia =
@@ -220,6 +314,12 @@ let
fi
${cfg.host.resolvconfPackage}/bin/resolvconf -d "${profile.interfaceName}" -f 2>/dev/null || true
${pkgs.iproute2}/bin/ip link delete dev ${lib.escapeShellArg profile.interfaceName} 2>/dev/null || true
${
if outbound then
outboundRuleCleanup name profile
else
repliesDown profile + restoreSrcValidMark name
}
}
trap cleanup ERR
@@ -227,7 +327,9 @@ let
read -r implementation probe _ < <(${pkgs.python3}/bin/python3 ${configTool} \
--inspect ${lib.escapeShellArg configPath})
${lib.optionalString (!outbound) (saveSrcValidMark name)}
${awgCommon.awgQuickUp "$implementation" (lib.escapeShellArg configPath)}
${if outbound then outboundRoutesUp name profile else repliesUp profile}
${mkHandshakeHelpers profile}
# A handshake is retried every 5 seconds: each retry after the first gets a new port.
@@ -246,8 +348,16 @@ let
false
'';
stop = pkgs.writeShellScript "proxy-suite-awg" ''
set -euo pipefail
exec ${awgCfg.toolsPackage}/bin/awg-quick down ${lib.escapeShellArg configPath}
set -uo pipefail
status=0
${awgCfg.toolsPackage}/bin/awg-quick down ${lib.escapeShellArg configPath} || status=$?
${
if outbound then
outboundRuleCleanup name profile
else
repliesDown profile + restoreSrcValidMark name
}
exit "$status"
'';
in
(
+8
View File
@@ -152,6 +152,8 @@ let
# (only sing-box listens on directPort).
tunnelPort = awgTunnelBasePort + 2 * index;
directPort = awgTunnelBasePort + 1 + 2 * index;
# Only an "interface" one routes by it.
routeTable = constants.awgOutboundRouteTableBase + index;
}
) (builtins.filter (name: awgProfiles.${name}.asOutbound != null) (builtins.attrNames awgProfiles));
awgInterfaceOutbounds = builtins.filter (ob: ob.kind == "interface") awgOutbounds;
@@ -424,6 +426,12 @@ let
# (an Endpoint's name) get past the kill switch.
awgGlobalFwmark = 51820;
awgGlobalGroup = "proxy-suite-awg";
# An "interface" AmneziaWG outbound's table (the base plus its index) and the rule
# sending sockets bound to the interface there, clear of the tables above.
awgOutboundRouteTableBase = 110;
awgOutboundRulePriority = 8991;
# A global AmneziaWG profile's rules keeping the host's UDP services on the main table.
awgServerUdpRulePriority = 8989;
# sing-box's fake IP caches, one per TUN config; the start script hands it to the backend.
fakeIpCacheDir = "${stateDir}/fakeip";
# Downloaded proxy.routing.ruleSets, one file each, written by the service user.
+11
View File
@@ -30,6 +30,17 @@ in
inherit (config.networking) enableIPv6;
inherit (config.boot) kernelPackages;
firewallPackage = config.networking.firewall.package;
openUdpPorts =
let
firewall = config.networking.firewall;
in
lib.unique (
lib.concatMap (
open:
map toString open.allowedUDPPorts
++ map (range: "${toString range.from}-${toString range.to}") open.allowedUDPPortRanges
) ([ firewall ] ++ lib.attrValues firewall.interfaces)
);
resolvconfPackage = config.networking.resolvconf.package;
};
# NixOS's place for helper functions; set even while disabled, so a config can
@@ -51,6 +51,21 @@ in
description = "AWG 3.1 kernel module package. `null`: userspace only.";
};
serverUdpPorts = mkOption {
type = types.listOf (types.either types.port (types.strMatching "[0-9]+-[0-9]+"));
default = [ ];
description = ''
UDP ports of services on this host that others reach, whose packets a global profile
leaves on the host's own routes. A reply from a socket bound to every address would
otherwise leave through the tunnel, from the tunnel's address. The UDP inbound listeners
and, on NixOS, the ports the firewall opens are included already.
'';
example = [
3478
"49152-65535"
];
};
profiles = mkOption {
type = types.attrsOf t.profileType;
default = { };
+2
View File
@@ -43,6 +43,8 @@ in
enableIPv6 = internalOption types.bool true;
kernelPackages = internalOption (types.nullOr types.raw) null;
firewallPackage = internalOption (types.nullOr types.package) null;
# UDP ports the host's firewall opens, as "port" or "from-to".
openUdpPorts = internalOption (types.listOf types.str) [ ];
resolvconfPackage = internalOption (types.nullOr types.package) null;
};
+9
View File
@@ -67,5 +67,14 @@ in
`amneziaWg.enable`. Cannot be used with `asOutbound`.
'';
};
autostart = mkOption {
type = types.bool;
default = false;
description = ''
Bring the "warp" AmneziaWG profile up at boot, as `autostart` of an `amneziaWg` profile
does. Needs `asAmneziaWg`. Only one global mode can autostart.
'';
};
};
}
@@ -204,6 +204,9 @@ let
(requireEnabled (
cfg.warp.enable && cfg.warp.asAmneziaWg
) cfg.amneziaWg.enable "proxy-suite: warp.asAmneziaWg requires amneziaWg.enable = true")
(requireEnabled (cfg.warp.enable && cfg.warp.autostart) cfg.warp.asAmneziaWg
"proxy-suite: warp.autostart starts the global AmneziaWG profile and requires warp.asAmneziaWg = true"
)
(requireEnabled proxyCfg.autoProxy.enable proxyEnabled
"proxy-suite: proxy.autoProxy.enable requires proxy.enable = true"
)
+1
View File
@@ -96,6 +96,7 @@ in
configFile = w.profilePath;
inherit (w) endpoint;
asOutbound = lib.mkIf viaAmneziaWg w.asOutbound;
autostart = lib.mkIf w.autostart true;
};
};