feat: runtime users and inbounds
This commit is contained in:
36 files changed
+2932
-83
No files matched your search
@@ -112,6 +112,11 @@ pkgs.testers.runNixOSTest {
|
||||
dns = [ ];
|
||||
};
|
||||
};
|
||||
# A peer added with proxy-ctl.
|
||||
runtime = {
|
||||
enable = true;
|
||||
ports = [ "9000-9001" ];
|
||||
};
|
||||
};
|
||||
};
|
||||
environment.systemPackages = [ pkgs.jq ];
|
||||
@@ -215,5 +220,18 @@ pkgs.testers.runNixOSTest {
|
||||
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)
|
||||
|
||||
with subtest("a runtime user becomes a peer, and only its listener's interfaces restart"):
|
||||
server.succeed("proxy-ctl inbounds users add ring --listener roam")
|
||||
server.wait_until_succeeds("awg show awgi-roam peers | wc -l | grep -qx 3", timeout=30)
|
||||
server.succeed("jq -e '.users.ring.publicKey' /var/lib/proxy-suite/awg-inbounds/roam/state.json")
|
||||
server.succeed("proxy-ctl inbounds link roam ring --config | grep -q '^PrivateKey'")
|
||||
# A change that leaves the peers alone restarts only XRay.
|
||||
since = server.succeed("systemctl show -p ActiveEnterTimestampMonotonic --value proxy-suite-inbounds-awg.service").strip()
|
||||
server.succeed("proxy-ctl inbounds users order ring 9")
|
||||
assert server.succeed("systemctl show -p ActiveEnterTimestampMonotonic --value proxy-suite-inbounds-awg.service").strip() == since
|
||||
server.succeed("proxy-ctl inbounds users rm ring")
|
||||
server.wait_until_succeeds("awg show awgi-roam peers | wc -l | grep -qx 2", timeout=30)
|
||||
server.succeed("jq -e '.users.ring == null' /var/lib/proxy-suite/awg-inbounds/roam/state.json")
|
||||
'';
|
||||
}
|
||||
@@ -63,6 +63,11 @@ pkgs.testers.runNixOSTest {
|
||||
method = "2022-blake3-aes-128-gcm";
|
||||
users = [ "ss-user" ];
|
||||
};
|
||||
# Users and listeners proxy-ctl adds below.
|
||||
runtime = {
|
||||
enable = true;
|
||||
ports = [ "9000-9010" ];
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
@@ -159,5 +164,38 @@ pkgs.testers.runNixOSTest {
|
||||
with subtest("a listener keeps serving after a restart"):
|
||||
server.succeed("systemctl restart proxy-suite-inbounds.service")
|
||||
server.wait_for_open_port(8443)
|
||||
|
||||
with subtest("runtime users and listeners"):
|
||||
cfg = "/run/proxy-suite-inbounds/config.json"
|
||||
server.succeed("proxy-ctl inbounds users add friend --listener vless-in")
|
||||
server.succeed(f"jq -e '.inbounds[] | select(.tag == \"vless-in\") | .settings.clients | map(.email) == [\"tester\", \"friend\"]' {cfg}")
|
||||
server.succeed("proxy-ctl inbounds add rt vless --port 9001 --user friend --user tester")
|
||||
server.wait_for_open_port(9001)
|
||||
server.succeed("proxy-ctl inbounds users | grep friend | grep -q rt")
|
||||
# Refused, and nothing else disturbed.
|
||||
server.fail("proxy-ctl inbounds add far vless --port 30000 --user friend")
|
||||
server.fail("proxy-ctl inbounds bind tester vless-in")
|
||||
server.succeed("test ! -e /var/lib/proxy-suite/inbounds.d/listeners/far.json")
|
||||
# The runtime user's link works from the client, as a runtime outbound there. XRay
|
||||
# refuses plain VLESS to a public IP, so it dials the private one.
|
||||
link = server.succeed("proxy-ctl inbounds link rt friend").strip()
|
||||
assert link.startswith("vless://") and ":9001" in link, link
|
||||
link = link.replace("${serverAddress}", "${serverPrivateAddress}")
|
||||
client.succeed(f"proxy-ctl proxy outbounds add rt-out '{link}'")
|
||||
client.succeed("proxy-ctl proxy pin rt-out")
|
||||
client.wait_until_succeeds(
|
||||
"curl -sS --fail --max-time 20 --proxy socks5h://127.0.0.1:1080"
|
||||
f" http://{'${serverAddress}'}/ | grep -q served-from-origin",
|
||||
timeout=60,
|
||||
)
|
||||
# A spool file that no longer holds up is left out; the declared listeners stay.
|
||||
server.succeed("echo '{' > /var/lib/proxy-suite/inbounds.d/listeners/rt.json")
|
||||
server.succeed("systemctl start proxy-suite-inbounds-reload.service")
|
||||
server.wait_for_open_port(8443)
|
||||
server.succeed(f"jq -e '[.inbounds[].tag] | index(\"rt\") == null' {cfg}")
|
||||
server.succeed("journalctl -u proxy-suite-inbounds | grep -q \"ignoring runtime listener 'rt'\"")
|
||||
server.succeed("rm /var/lib/proxy-suite/inbounds.d/listeners/rt.json")
|
||||
server.succeed("proxy-ctl inbounds users rm friend")
|
||||
server.succeed(f"jq -e '.inbounds[] | select(.tag == \"vless-in\") | .settings.clients | map(.email) == [\"tester\"]' {cfg}")
|
||||
'';
|
||||
}
|
||||
@@ -211,6 +211,46 @@ let
|
||||
lib.recursiveUpdate selfSourceInbounds { routing.serverSource.ipv4 = "10.78.0.5/24"; }
|
||||
);
|
||||
|
||||
# inbounds.runtime: the rules runtime listeners join are marked, the runtime users'
|
||||
# serverSource rules have their anchors, and the rest of the system is ready for them.
|
||||
runtimeInbounds = selfSourceInbounds // {
|
||||
runtime = {
|
||||
enable = true;
|
||||
ports = [
|
||||
9443
|
||||
"20000-20010"
|
||||
];
|
||||
vias = [ "direct" ];
|
||||
};
|
||||
};
|
||||
runtimeFixture = mkInbounds runtimeInbounds;
|
||||
runtimeConfig = mkInboundsConfig runtimeFixture;
|
||||
runtimeSpec = mkInboundsSpec runtimeFixture;
|
||||
hasFailed =
|
||||
fixture: message:
|
||||
builtins.any (a: !a.assertion && lib.hasInfix message a.message) fixture.config.assertions;
|
||||
# Only this module's: a fixture has no root file system or boot loader.
|
||||
failedMessages =
|
||||
fixture:
|
||||
builtins.filter (lib.hasPrefix "proxy-suite:") (
|
||||
map (a: a.message) (builtins.filter (a: !a.assertion) fixture.config.assertions)
|
||||
);
|
||||
# A listener meant only for runtime users, and nothing but runtime listeners.
|
||||
runtimeOnlyFixture = mkInbounds {
|
||||
serverAddress = "vpn.example.com";
|
||||
runtime = {
|
||||
enable = true;
|
||||
ports = [ "20000-20010" ];
|
||||
};
|
||||
listeners.waiting = realityListener // {
|
||||
users = [ ];
|
||||
};
|
||||
};
|
||||
runtimeClashFixture = mkInbounds (
|
||||
lib.recursiveUpdate runtimeInbounds { runtime.ports = [ "400-500" ]; }
|
||||
);
|
||||
runtimeNoPortsFixture = mkInbounds (runtimeInbounds // { runtime.enable = true; });
|
||||
|
||||
unguardedFixture = mkInbounds {
|
||||
routing = {
|
||||
blockRu = false;
|
||||
@@ -995,6 +1035,118 @@ let
|
||||
) badSelfSourceFixture.config.assertions;
|
||||
true
|
||||
)
|
||||
# The declared users are in the spec without inbounds.runtime too, for `inbounds users`.
|
||||
(
|
||||
let
|
||||
spec = mkInboundsSpec selfSourceFixture;
|
||||
in
|
||||
assert spec.runtime == null;
|
||||
assert
|
||||
builtins.attrNames spec.users == [
|
||||
"a"
|
||||
"alice"
|
||||
"anon"
|
||||
"b"
|
||||
"bob"
|
||||
"far"
|
||||
"h3"
|
||||
"hy2"
|
||||
"laptop"
|
||||
"phone"
|
||||
"second"
|
||||
"ss"
|
||||
"user"
|
||||
"ws"
|
||||
];
|
||||
assert
|
||||
(builtins.head spec.serverSource.declared) == {
|
||||
name = "bob";
|
||||
number = 1;
|
||||
};
|
||||
true
|
||||
)
|
||||
# Without inbounds.runtime nothing is marked for it.
|
||||
(ok (!builtins.any (rule: rule ? _members || rule ? _anchor) selfSourceConfig.routing.rules))
|
||||
(
|
||||
let
|
||||
rules = runtimeConfig.routing.rules;
|
||||
anchors = builtins.filter (rule: rule ? _anchor) rules;
|
||||
tags = ruleTags runtimeConfig;
|
||||
at = tag: lib.lists.findFirstIndex (t: t == tag) null tags;
|
||||
anchorAt = name: lib.lists.findFirstIndex (rule: (rule._anchor or null) == name) null rules;
|
||||
startScript = (import ./read-generated.nix).readDerivation (service runtimeFixture)
|
||||
.serviceConfig.ExecStart;
|
||||
fw = runtimeFixture.config.networking.firewall;
|
||||
in
|
||||
assert
|
||||
map (rule: rule._anchor) anchors == [
|
||||
"selfIp"
|
||||
"selfName"
|
||||
];
|
||||
# Each after the declared users' own, before the host's.
|
||||
assert anchorAt "selfIp" > at "inbound-server-address-self-ip4-6";
|
||||
assert anchorAt "selfIp" < at "inbound-server-address-direct-ip";
|
||||
assert anchorAt "selfName" > at "inbound-server-address-self-6";
|
||||
assert anchorAt "selfName" < at "inbound-server-address-direct";
|
||||
assert (builtins.head anchors).ip4 == [ "203.0.113.10" ];
|
||||
assert (builtins.head anchors).port == "8443,443,9443,20000-20010";
|
||||
assert
|
||||
(ruleByTag runtimeConfig "inbound-server-address-direct")._members == { notVia = [ "block" ]; };
|
||||
# A guard for every exit a runtime listener may take, even with nobody on it yet.
|
||||
assert (ruleByTag runtimeConfig "inbound-server-address-sniffed-direct").inboundTag == [ ];
|
||||
assert
|
||||
(ruleByTag runtimeConfig "inbound-server-address-sniffed-direct")._members == {
|
||||
via = [ "direct" ];
|
||||
};
|
||||
assert !builtins.elem "inbound-server-address-sniffed-block" tags;
|
||||
assert
|
||||
runtimeSpec.runtime.vias == [
|
||||
"proxy"
|
||||
"direct"
|
||||
"block"
|
||||
];
|
||||
assert
|
||||
runtimeSpec.serverSource.declared == [
|
||||
{
|
||||
name = "bob";
|
||||
number = 1;
|
||||
}
|
||||
{
|
||||
name = "alice";
|
||||
number = 5;
|
||||
}
|
||||
{
|
||||
name = "anon";
|
||||
number = 6;
|
||||
}
|
||||
];
|
||||
assert runtimeSpec.runtime.spool == "/var/lib/proxy-suite/inbounds.d";
|
||||
assert !(runtimeSpec.runtime.listenerDefaults ? users);
|
||||
assert runtimeSpec.runtime.listenerDefaults.port == 443;
|
||||
assert
|
||||
runtimeSpec.runtime.selfFinalRules
|
||||
== (builtins.head (builtins.filter (ob: ob.tag == "direct-self4-1") runtimeConfig.outbounds))
|
||||
.settings.finalRules;
|
||||
assert builtins.elem 9443 fw.allowedTCPPorts && builtins.elem 9443 fw.allowedUDPPorts;
|
||||
assert
|
||||
fw.allowedTCPPortRanges == [
|
||||
{
|
||||
from = 20000;
|
||||
to = 20010;
|
||||
}
|
||||
];
|
||||
assert lib.hasInfix "--template" startScript;
|
||||
assert runtimeFixture.config.systemd.services ? proxy-suite-inbounds-reload;
|
||||
assert builtins.elem "d /var/lib/proxy-suite/inbounds.d 0700 root root -"
|
||||
runtimeFixture.config.systemd.tmpfiles.rules;
|
||||
assert !(selfSourceFixture.config.systemd.services ? proxy-suite-inbounds-reload);
|
||||
true
|
||||
)
|
||||
(ok (failedMessages runtimeOnlyFixture == [ ]))
|
||||
(ok (
|
||||
hasFailed runtimeClashFixture "inbounds.runtime.ports cover a port a declared listener or proxy-suite itself uses (443)"
|
||||
))
|
||||
(ok (hasFailed runtimeNoPortsFixture "inbounds.runtime.enable needs inbounds.runtime.ports"))
|
||||
(ok ((ruleByTag aliasedConfig "inbound-server-address-direct").port == "443,993,7882-7885"))
|
||||
(ok ((ruleByTag aliasedConfig "inbound-server-address-direct-ip").port == "443,993,7882-7885"))
|
||||
(ok ((ruleByTag aliasedConfig "inbound-server-address-direct-ip").outboundTag == "direct"))
|
||||
|
||||
@@ -62,6 +62,11 @@ pkgs.lib.mapAttrs mkPythonCheck {
|
||||
file = "${scripts}/test-awg-inbound.py";
|
||||
env = withPath scripts;
|
||||
};
|
||||
# inbounds.runtime: the spool, its fences, the merge and the rules it expands.
|
||||
inbound-runtime = {
|
||||
file = "${scripts}/test-inbound-runtime.py";
|
||||
env = withPath scripts;
|
||||
};
|
||||
# The NFQWS_OPT rewrites the zapret v1 package build applies.
|
||||
patch-zapret-config = {
|
||||
file = "${scripts}/test-patch-zapret-config.py";
|
||||
@@ -76,6 +81,7 @@ pkgs.lib.mapAttrs mkPythonCheck {
|
||||
# `awg add` and AmneziaWG outbounds hand their configs to it.
|
||||
env = singBox // {
|
||||
AWG_CONFIG_TOOL = "${scripts}/amneziawg_config.py";
|
||||
INBOUNDS_RUNTIME_TOOL = "${scripts}/inbound_runtime.py";
|
||||
};
|
||||
};
|
||||
# proxy-suitectl, nix-on-droid's service manager: real processes restarted, timed and stopped.
|
||||
|
||||
Reference in new issue
Block a user