feat: awg inbounds
This commit is contained in:
31 files changed
+2876
-133
No files matched your search
@@ -52,6 +52,9 @@ let
|
||||
proxyInboundsRuntime = import ./checks/proxy-inbounds-runtime.nix {
|
||||
inherit pkgs proxySuiteModule;
|
||||
};
|
||||
amneziaWgInboundsRuntime = import ./checks/amnezia-wg-inbound-runtime.nix {
|
||||
inherit pkgs proxySuiteModule;
|
||||
};
|
||||
in
|
||||
moduleSuiteChecks
|
||||
// {
|
||||
@@ -62,6 +65,7 @@ moduleSuiteChecks
|
||||
// pkgs.lib.optionalAttrs (system == "x86_64-linux") {
|
||||
amneziawg-runtime = amneziaWgRuntime;
|
||||
proxy-inbounds-runtime = proxyInboundsRuntime;
|
||||
amneziawg-inbounds-runtime = amneziaWgInboundsRuntime;
|
||||
}
|
||||
// parserChecks
|
||||
// repoChecks
|
||||
@@ -0,0 +1,213 @@
|
||||
{
|
||||
pkgs,
|
||||
proxySuiteModule,
|
||||
}:
|
||||
|
||||
let
|
||||
# Clients reach the server on VLAN 1, and nothing else but through it. Public addresses:
|
||||
# private destinations never reach XRay.
|
||||
serverAddress = "11.0.0.1";
|
||||
# VLAN 2: the internet, as seen from the server, and the server's LAN in one.
|
||||
serverUplink = "11.0.1.1";
|
||||
outsideAddress = "11.0.1.3";
|
||||
serverLanAddress = "192.168.2.1";
|
||||
lanAddress = "192.168.2.2";
|
||||
|
||||
# Forced: the test driver's own 192.168.<vlan>.<node> addresses would share subnets.
|
||||
network = interface: addresses: {
|
||||
networking.useDHCP = false;
|
||||
networking.interfaces.${interface}.ipv4.addresses = pkgs.lib.mkForce (
|
||||
map (address: {
|
||||
inherit address;
|
||||
prefixLength = 24;
|
||||
}) addresses
|
||||
);
|
||||
};
|
||||
|
||||
users = names: map (name: { inherit name; }) names;
|
||||
|
||||
# Each client imports what the server hands out: the .conf, or the vpn:// link.
|
||||
client =
|
||||
address:
|
||||
{ ... }:
|
||||
{
|
||||
imports = [
|
||||
proxySuiteModule
|
||||
(network "eth1" [ address ])
|
||||
];
|
||||
services.proxy-suite = {
|
||||
enable = true;
|
||||
amneziaWg = {
|
||||
enable = true;
|
||||
# The server covers the kernel module.
|
||||
kernelModulePackage = null;
|
||||
profiles.home = {
|
||||
interfaceName = "awg-home";
|
||||
configFile = "/run/awg/home.conf";
|
||||
};
|
||||
profiles.roam = {
|
||||
interfaceName = "awg-roam";
|
||||
vpnFile = "/run/awg/roam.vpn";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
in
|
||||
pkgs.testers.runNixOSTest {
|
||||
name = "proxy-suite-amneziawg-inbounds-runtime";
|
||||
|
||||
nodes = {
|
||||
server =
|
||||
{ ... }:
|
||||
{
|
||||
imports = [
|
||||
proxySuiteModule
|
||||
(network "eth1" [ serverAddress ])
|
||||
(network "eth2" [
|
||||
serverUplink
|
||||
serverLanAddress
|
||||
])
|
||||
];
|
||||
virtualisation.vlans = [
|
||||
1
|
||||
2
|
||||
];
|
||||
|
||||
services.proxy-suite = {
|
||||
enable = true;
|
||||
proxy.enable = false;
|
||||
inbounds = {
|
||||
enable = true;
|
||||
inherit serverAddress;
|
||||
routing.via = "direct";
|
||||
openFirewall = true;
|
||||
listeners.home = {
|
||||
type = "amneziawg";
|
||||
port = 51820;
|
||||
users = users [
|
||||
"phone"
|
||||
"laptop"
|
||||
];
|
||||
amneziaWg = {
|
||||
mode = "lan";
|
||||
subnet6 = "fd66:66::/64";
|
||||
dns = [ ];
|
||||
};
|
||||
};
|
||||
listeners.roam = {
|
||||
type = "amneziawg";
|
||||
port = 51821;
|
||||
users = users [
|
||||
"tablet"
|
||||
"watch"
|
||||
];
|
||||
amneziaWg = {
|
||||
subnet = "10.67.0.0/24";
|
||||
dns = [ ];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
environment.systemPackages = [ pkgs.jq ];
|
||||
};
|
||||
|
||||
# The internet and the server's LAN in one: a web server answering with the caller's address.
|
||||
outside =
|
||||
{ ... }:
|
||||
{
|
||||
imports = [
|
||||
(network "eth1" [
|
||||
outsideAddress
|
||||
lanAddress
|
||||
])
|
||||
];
|
||||
virtualisation.vlans = [ 2 ];
|
||||
services.nginx = {
|
||||
enable = true;
|
||||
virtualHosts.default = {
|
||||
default = true;
|
||||
locations."/".return = "200 '$remote_addr'";
|
||||
};
|
||||
};
|
||||
networking.firewall.allowedTCPPorts = [ 80 ];
|
||||
};
|
||||
|
||||
alice = client "11.0.0.2";
|
||||
bob = client "11.0.0.4";
|
||||
};
|
||||
|
||||
testScript = ''
|
||||
start_all()
|
||||
server.wait_for_unit("proxy-suite-inbounds-awg.service")
|
||||
server.wait_for_unit("proxy-suite-inbounds.service")
|
||||
outside.wait_for_unit("nginx.service")
|
||||
|
||||
def import_profiles(machine, home_user, roam_user):
|
||||
home = server.succeed(f"proxy-ctl inbounds link home {home_user} --config")
|
||||
roam = server.succeed(f"proxy-ctl inbounds link roam {roam_user}").strip()
|
||||
assert roam.startswith("vpn://"), roam
|
||||
machine.succeed("mkdir -p -m 700 /run/awg")
|
||||
machine.succeed(f"cat > /run/awg/home.conf <<'EOF'\n{home}\nEOF")
|
||||
machine.succeed(f"echo '{roam}' > /run/awg/roam.vpn")
|
||||
|
||||
def fetch(machine, host):
|
||||
return machine.succeed(f"curl -sS --fail --max-time 10 http://{host}/").strip()
|
||||
|
||||
# XRay dials out, so the web server sees the server.
|
||||
def through_server(machine, timeout=60):
|
||||
machine.wait_until_succeeds(
|
||||
"test \"$(curl -sS --fail --max-time 10 http://${outsideAddress}/)\" = ${serverUplink}",
|
||||
timeout=timeout,
|
||||
)
|
||||
|
||||
with subtest("state and links are generated"):
|
||||
server.succeed("test $(stat -c %a /var/lib/proxy-suite/awg-inbounds/home/state.json) = 600")
|
||||
server.succeed("ip link show awgi-home")
|
||||
server.succeed("ip link show awgi-roam")
|
||||
server.succeed("nft list table inet proxy_suite_awg_inbounds")
|
||||
server.succeed("test $(jq '[.[] | select(.type == \"amneziawg\")] | length' /run/proxy-suite-inbounds/links.json) = 4")
|
||||
server.succeed("test -n \"$(proxy-ctl inbounds link home phone --config --qr)\"")
|
||||
import_profiles(alice, "phone", "tablet")
|
||||
import_profiles(bob, "laptop", "watch")
|
||||
|
||||
with subtest("lan: the internet through the listener's routing"):
|
||||
for machine in (alice, bob):
|
||||
machine.succeed("systemctl start proxy-suite-awg-home.service")
|
||||
through_server(alice)
|
||||
# Nothing but TCP and UDP can follow via.
|
||||
alice.fail("ping -c 2 -W 2 ${outsideAddress}")
|
||||
|
||||
with subtest("lan: this host, its LAN and the other peers"):
|
||||
alice.succeed("ping -c 2 -W 2 10.66.0.1")
|
||||
alice.succeed("ping -c 2 -W 2 ${serverLanAddress}")
|
||||
# Masqueraded, so the LAN needs no route back.
|
||||
assert fetch(alice, "${lanAddress}") == "${serverLanAddress}"
|
||||
bob_address = bob.succeed("ip -4 -o addr show awg-home | awk '{print $4}' | cut -d/ -f1").strip()
|
||||
alice.succeed(f"ping -c 2 -W 2 {bob_address}")
|
||||
alice.succeed("ping -6 -c 2 -W 2 fd66:66::1")
|
||||
|
||||
with subtest("usage and presence are recorded"):
|
||||
server.succeed("proxy-ctl inbounds online | grep phone | grep -q online")
|
||||
server.succeed("proxy-ctl inbounds online | grep laptop | grep -q online")
|
||||
server.succeed("proxy-ctl inbounds stats --by user | grep -q phone")
|
||||
|
||||
with subtest("proxy: nothing but the internet"):
|
||||
for machine in (alice, bob):
|
||||
machine.succeed("systemctl start proxy-suite-awg-roam.service")
|
||||
machine.fail("ip link show awg-home")
|
||||
through_server(alice)
|
||||
alice.fail("ping -c 2 -W 2 10.67.0.1")
|
||||
alice.fail("curl -sS --fail --max-time 5 http://${lanAddress}/")
|
||||
alice.fail("ping -c 2 -W 2 ${serverLanAddress}")
|
||||
bob_address = bob.succeed("ip -4 -o addr show awg-roam | awk '{print $4}' | cut -d/ -f1").strip()
|
||||
alice.fail(f"ping -c 2 -W 2 {bob_address}")
|
||||
server.succeed("proxy-ctl inbounds online | grep tablet | grep -q online")
|
||||
|
||||
with subtest("keys survive a restart"):
|
||||
before = server.succeed("jq -S .users /var/lib/proxy-suite/awg-inbounds/roam/state.json")
|
||||
server.succeed("systemctl restart proxy-suite-inbounds-awg.service")
|
||||
server.wait_for_unit("proxy-suite-inbounds.service")
|
||||
assert server.succeed("jq -S .users /var/lib/proxy-suite/awg-inbounds/roam/state.json") == before
|
||||
through_server(alice, timeout=90)
|
||||
'';
|
||||
}
|
||||
@@ -45,4 +45,13 @@
|
||||
python ${../../scripts/test-amneziawg-config.py}
|
||||
touch "$out"
|
||||
'';
|
||||
|
||||
awg-inbound-state =
|
||||
pkgs.runCommand "awg-inbound-state-check" { nativeBuildInputs = [ pkgs.python3 ]; }
|
||||
''
|
||||
export PYTHONDONTWRITEBYTECODE=1
|
||||
export PYTHONPATH=${../../scripts}:$PYTHONPATH
|
||||
python ${../../scripts/test-awg-inbound.py}
|
||||
touch "$out"
|
||||
'';
|
||||
}
|
||||
@@ -173,6 +173,59 @@ let
|
||||
listeners.vless-in = realityListener;
|
||||
};
|
||||
|
||||
awgListener = {
|
||||
type = "amneziawg";
|
||||
port = 51820;
|
||||
users = [
|
||||
{ name = "phone"; }
|
||||
{ name = "laptop"; }
|
||||
];
|
||||
};
|
||||
awgFixture = mkInbounds {
|
||||
listeners.vless-in = realityListener;
|
||||
listeners.home = awgListener // {
|
||||
amneziaWg = {
|
||||
mode = "lan";
|
||||
subnet6 = "fd66:66::/64";
|
||||
};
|
||||
};
|
||||
listeners.roam = awgListener // {
|
||||
port = 51821;
|
||||
amneziaWg.subnet = "10.67.0.0/24";
|
||||
};
|
||||
};
|
||||
awgSpec = mkInboundsSpec awgFixture;
|
||||
awgSpecListener = tag: lib.head (builtins.filter (l: l.tag == tag) awgSpec.listeners);
|
||||
awgProxyFixture = mkInbounds {
|
||||
listeners.roam = awgListener;
|
||||
};
|
||||
readInboundsStart =
|
||||
fixture: (import ./read-generated.nix).readDerivation (service fixture).serviceConfig.ExecStart;
|
||||
awgService = fixture: fixture.config.systemd.services."proxy-suite-inbounds-awg";
|
||||
awgStartScript =
|
||||
fixture: (import ./read-generated.nix).readDerivation (awgService fixture).serviceConfig.ExecStart;
|
||||
# The nft rules file is a writeText the start script names; a writeText that returns its text
|
||||
# puts the rules into the script itself, readable without import-from-derivation.
|
||||
awgRules =
|
||||
fixture:
|
||||
let
|
||||
cfg = fixture.config.services.proxy-suite;
|
||||
nftr = import ../../modules/proxy-suite/nftables.nix { inherit lib pkgs cfg; };
|
||||
module = import ../../modules/proxy-suite/amnezia-wg-inbounds.nix {
|
||||
inherit lib cfg;
|
||||
pkgs = pkgs // {
|
||||
writeText = _: text: text;
|
||||
};
|
||||
derived = import ../../modules/proxy-suite/derived.nix { inherit lib cfg; };
|
||||
proxyInboundsSpecFile = "/spec.json";
|
||||
inherit (nftr) reservedIpBlock;
|
||||
ip = "ip";
|
||||
nft = "nft";
|
||||
};
|
||||
in
|
||||
(import ./read-generated.nix).readDerivation
|
||||
module.services.proxy-suite.internal.services."proxy-suite-inbounds-awg".serviceConfig.ExecStart;
|
||||
|
||||
ruleTags = config: map (rule: rule.ruleTag) config.routing.rules;
|
||||
ruleByTag =
|
||||
config: tag: builtins.head (builtins.filter (rule: rule.ruleTag == tag) config.routing.rules);
|
||||
@@ -417,6 +470,43 @@ let
|
||||
})
|
||||
"collides with proxy.listener.port"
|
||||
)
|
||||
|
||||
# AmneziaWG listeners.
|
||||
(mkRejectsListener (awgListener // { tls.enable = true; }) "AmneziaWG takes no tls")
|
||||
# A long tag makes a long default name.
|
||||
(mkRejects (
|
||||
baseProxy
|
||||
// {
|
||||
inbounds = {
|
||||
enable = true;
|
||||
listeners.far-too-long = awgListener;
|
||||
};
|
||||
}
|
||||
) "is longer than the kernel's 15 characters")
|
||||
(mkRejectsListener (awgListener // { users = [ { } ]; }) "users each need a name")
|
||||
(mkRejectsListener (
|
||||
awgListener
|
||||
// {
|
||||
users = [
|
||||
{
|
||||
name = "a";
|
||||
address = "10.66.1.2";
|
||||
}
|
||||
];
|
||||
}
|
||||
) "must be a host address inside amneziaWg.subnet")
|
||||
(mkRejects (
|
||||
baseProxy
|
||||
// {
|
||||
inbounds = {
|
||||
enable = true;
|
||||
listeners.one = awgListener;
|
||||
listeners.two = awgListener // {
|
||||
port = 51821;
|
||||
};
|
||||
};
|
||||
}
|
||||
) "must each use a subnet of their own")
|
||||
];
|
||||
|
||||
assertions = [
|
||||
@@ -585,6 +675,82 @@ let
|
||||
true
|
||||
)
|
||||
|
||||
# AmneziaWG: the spec carries each listener's loopback inbound, in listener order.
|
||||
(assert (awgSpecListener "home").amneziaWg.internalPort == 18700; true)
|
||||
(assert (awgSpecListener "roam").amneziaWg.internalPort == 18701; true)
|
||||
(assert (awgSpecListener "home").amneziaWg.internalListen == "::"; true)
|
||||
(assert (awgSpecListener "roam").amneziaWg.internalListen == "127.0.0.1"; true)
|
||||
(assert (awgSpecListener "home").amneziaWg.fwmark == 2; true)
|
||||
(assert (awgSpecListener "home").amneziaWg.interfaceName == "awgi-home"; true)
|
||||
(
|
||||
assert
|
||||
(awgSpecListener "roam").amneziaWg.stateFile == "/var/lib/proxy-suite/awg-inbounds/roam/state.json";
|
||||
true
|
||||
)
|
||||
(assert !((awgSpecListener "vless-in") ? amneziaWg); true)
|
||||
# UDP only, and the interfaces past the host firewall.
|
||||
(assert awgFixture.config.networking.firewall.allowedTCPPorts == [ 443 ]; true)
|
||||
(
|
||||
assert
|
||||
lib.sort lib.lessThan awgFixture.config.networking.firewall.allowedUDPPorts == [
|
||||
51820
|
||||
51821
|
||||
];
|
||||
true
|
||||
)
|
||||
(
|
||||
assert builtins.elem "awgi-home" awgFixture.config.networking.firewall.trustedInterfaces;
|
||||
assert builtins.elem "awgi-roam" awgFixture.config.networking.firewall.trustedInterfaces;
|
||||
true
|
||||
)
|
||||
(
|
||||
assert
|
||||
lib.hasInfix ''iifname "awgi-home" accept'' awgFixture.config.networking.firewall.extraReversePathFilterRules;
|
||||
true
|
||||
)
|
||||
# The interfaces come up before XRay renders their links.
|
||||
(assert builtins.elem "proxy-suite-inbounds-awg.service" (service awgFixture).after; true)
|
||||
(assert builtins.elem "proxy-suite-inbounds-awg.service" (service awgFixture).wants; true)
|
||||
(assert !(relayFixture.config.systemd.services ? "proxy-suite-inbounds-awg"); true)
|
||||
(
|
||||
assert lib.hasInfix "rule add pref 8990 fwmark 20 table 103" (awgStartScript awgFixture);
|
||||
assert lib.hasInfix "awg_inbound.py prepare" (awgStartScript awgFixture);
|
||||
true
|
||||
)
|
||||
# IPv6 routing only where a listener has IPv6.
|
||||
(assert lib.hasInfix "-6 rule add pref 8990" (awgStartScript awgFixture); true)
|
||||
(assert !lib.hasInfix "-6 rule add pref 8990" (awgStartScript awgProxyFixture); true)
|
||||
# Transparent sockets only where there is something to divert.
|
||||
(
|
||||
assert lib.hasInfix "net_admin" (readInboundsStart awgFixture);
|
||||
assert !lib.hasInfix "net_admin" (readInboundsStart relayFixture);
|
||||
true
|
||||
)
|
||||
# Forwarding only for "lan" listeners.
|
||||
(assert awgFixture.config.boot.kernel.sysctl."net.ipv4.ip_forward" == 1; true)
|
||||
(assert awgFixture.config.boot.kernel.sysctl."net.ipv6.conf.all.forwarding" == 1; true)
|
||||
(assert !(awgProxyFixture.config.services.proxy-suite.internal.sysctl ? "net.ipv4.ip_forward"); true)
|
||||
# "lan" clients reach private networks and each other natively, "proxy" clients nothing but via.
|
||||
(
|
||||
let
|
||||
rules = awgRules awgFixture;
|
||||
in
|
||||
assert lib.hasInfix "ip daddr $RESERVED_IP return" rules;
|
||||
assert lib.hasInfix "ip daddr 192.168.0.0/16 return" rules;
|
||||
assert lib.hasInfix "ip daddr 192.168.0.0/16 drop" rules;
|
||||
assert lib.hasInfix "ip daddr 10.67.0.0/24 drop" rules;
|
||||
assert lib.hasInfix "ip6 daddr fd66:66::/64 return" rules;
|
||||
assert lib.hasInfix "tproxy ip to 127.0.0.1:18700 meta mark set 20 accept" rules;
|
||||
assert lib.hasInfix "tproxy ip6 to [::1]:18700 meta mark set 20 accept" rules;
|
||||
assert !lib.hasInfix "tproxy ip6 to [::1]:18701" rules;
|
||||
assert lib.hasInfix ''iifname "awgi-roam" jump listener_1'' rules;
|
||||
assert lib.hasInfix "th dport { 18700, 18701 } fib daddr type local drop" rules;
|
||||
assert lib.hasInfix ''ip saddr 10.66.0.0/24 oifname != "awgi-home" masquerade'' rules;
|
||||
assert !lib.hasInfix "10.67.0.0/24 oifname" rules;
|
||||
true
|
||||
)
|
||||
(assert !lib.hasInfix "masquerade" (awgRules awgProxyFixture); true)
|
||||
|
||||
]
|
||||
++ failing;
|
||||
in
|
||||
|
||||
+15
-1
@@ -640,7 +640,7 @@
|
||||
''
|
||||
add=${../../modules/proxy-suite/inbound-stats-add.jq}
|
||||
now=1789135690 # 2026-09-11
|
||||
a() { jq -c --argjson q "$1" --argjson online "''${3:-{\}}" --arg day 2026-09-11 --argjson now "$now" -f "$add" <<<"$2"; }
|
||||
a() { jq -c --argjson q "$1" --argjson online "''${3:-{\}}" --argjson awg "''${4:-[]}" --arg day 2026-09-11 --argjson now "$now" -f "$add" <<<"$2"; }
|
||||
|
||||
# XRay gives values as strings and leaves a zero counter without one.
|
||||
r='{"stat":[{"name":"user>>>fufsob>>>traffic>>>downlink","value":"3000000"},
|
||||
@@ -665,6 +665,20 @@
|
||||
old="$(jq -c '.days["2024-01-01"] = {user: {fufsob: {up: 1}}}' <<<"$s")"
|
||||
jq -e '.days["2024-01-01"] == null' <<<"$(a '{}' "$old")" > /dev/null
|
||||
|
||||
# AmneziaWG peers come with raw interface counters, of which only the growth is added.
|
||||
p() { echo '[{"interface":"awgi-home","key":"K","tag":"home","user":"tablet","rx":'"$1"',"tx":'"$2"',"handshake":'"$3"',"endpoint":"203.0.113.9:4000"}]'; }
|
||||
s="$(a '{}' "$s" '{}' "$(p 1000 5000 1789135500)")"
|
||||
jq -e '.days["2026-09-11"].user.tablet == {up: 1000, down: 5000} and .seen.tablet == 1789135500
|
||||
and .awgPeers.tablet == {tag: "home", handshake: 1789135500, endpoint: "203.0.113.9:4000"}' <<<"$s" > /dev/null
|
||||
s="$(a '{}' "$s" '{}' "$(p 1500 5200 1789135600)")"
|
||||
jq -e '.days["2026-09-11"].user.tablet == {up: 1500, down: 5200} and .seen.tablet == 1789135600' <<<"$s" > /dev/null
|
||||
# A counter below the last one restarted with its interface.
|
||||
s="$(a '{}' "$s" '{}' "$(p 100 100 0)")"
|
||||
jq -e '.days["2026-09-11"].user.tablet == {up: 1600, down: 5300} and .seen.tablet == 1789135600
|
||||
and .awgPeers == {}' <<<"$s" > /dev/null
|
||||
# A peer gone from the listeners leaves no counters behind.
|
||||
jq -e '.awgCounters == {}' <<<"$(a '{}' "$s")" > /dev/null
|
||||
|
||||
touch "$out"
|
||||
'';
|
||||
|
||||
|
||||
Reference in new issue
Block a user