feat: better inbounds user management

This commit is contained in:
FUFSoB committed 2026-10-14 21:41:49 +05:00
1 parent 2f8414455d
commit 9f7ad91f39
47 files changed
+6767 -78

No files matched your search

+3 -1
View File
@@ -223,9 +223,11 @@ pkgs.testers.runNixOSTest {
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"):
with subtest("a runtime user becomes a peer, synced in: the interfaces do not restart"):
since = server.succeed("systemctl show -p ActiveEnterTimestampMonotonic --value proxy-suite-inbounds-awg.service").strip()
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)
assert server.succeed("systemctl show -p ActiveEnterTimestampMonotonic --value proxy-suite-inbounds-awg.service").strip() == since
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.
+5
View File
@@ -52,6 +52,11 @@ let
enable = true;
serverAddress = "vpn.example.com";
users.user.uuidFile = "/run/secrets/uuid";
# A policy level per speed slot and the API services the manager uses: XRay must take them.
manager = {
enable = true;
speedLimits.enable = true;
};
listeners.vless-in = {
type = "vless";
port = 443;
+11 -2
View File
@@ -13,6 +13,10 @@ let
serverAddress = "11.0.0.1";
# XRay refuses plain VLESS to a public IP, so the client dials a private one.
serverPrivateAddress = "10.0.0.1";
# The inbounds' own XRay, for its API.
xrayApi = "${
import ../../pkgs/xray.nix { inherit pkgs; }
}/bin/xray api inbounduser --server=unix:///run/proxy-suite-inbounds/api/stats.sock -tag=vless-in";
network = public: private: {
networking.useDHCP = false;
@@ -191,10 +195,12 @@ pkgs.testers.runNixOSTest {
with subtest("runtime users and listeners"):
cfg = "/run/proxy-suite-inbounds/config.json"
# A user comes through XRay's API: the same XRay, config.json kept in step with it.
pid = server.succeed("systemctl show -p MainPID --value proxy-suite-inbounds.service").strip()
server.succeed("proxy-ctl inbounds users add friend --listener vless-in")
# The reload's restart returns once the start script is forked: config.json is
# gone until it renders it again.
server.wait_until_succeeds(f"jq -e '.inbounds[] | select(.tag == \"vless-in\") | .settings.clients | map(.email) == [\"tester\", \"friend\"]' {cfg}")
server.succeed("${xrayApi} | grep -q friend")
assert server.succeed("systemctl show -p MainPID --value proxy-suite-inbounds.service").strip() == pid
server.succeed("proxy-ctl inbounds add rt vless --port 9001 --user friend")
server.wait_for_open_port(9001)
server.succeed("proxy-ctl inbounds users | grep friend | grep -q rt")
@@ -225,7 +231,10 @@ pkgs.testers.runNixOSTest {
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")
pid = server.succeed("systemctl show -p MainPID --value proxy-suite-inbounds.service").strip()
server.succeed("proxy-ctl inbounds users rm friend")
server.wait_until_succeeds(f"jq -e '.inbounds[] | select(.tag == \"vless-in\") | .settings.clients | map(.email) == [\"tester\"]' {cfg}")
server.fail("${xrayApi} | grep -q friend")
assert server.succeed("systemctl show -p MainPID --value proxy-suite-inbounds.service").strip() == pid
'';
}
+132 -1
View File
@@ -251,10 +251,58 @@ let
);
runtimeNoPortsFixture = mkInbounds (runtimeInbounds // { runtime.enable = true; });
# inbounds.manager: the daemon, its spec and the XRay config it changes in place.
managerInbounds = runtimeInbounds // {
manager = {
enable = true;
speedLimits = {
enable = true;
slots = 3;
};
api = {
listen = "127.0.0.1:9180";
tokensFile = "/run/secrets/manager-tokens";
};
webhooks = [ { url = "http://127.0.0.1:8088/hook"; } ];
};
};
managerFixture = mkInbounds managerInbounds;
managerConfig = mkInboundsConfig managerFixture;
managerSpec = mkInboundsSpec managerFixture;
managerService = managerFixture.config.systemd.services."proxy-suite-inbounds-manager";
policyWithoutManagerFixture = evalWithUsers [
baseModule
{
services.proxy-suite.inbounds = {
enable = true;
listeners.vless-in = realityListener;
users.user.policy.trafficLimit = "10G";
};
}
];
speedWithoutLimitsFixture = mkInbounds (
lib.recursiveUpdate managerInbounds {
manager.speedLimits.enable = false;
users.user.policy.speedLimit = "20M/5M";
}
);
unpatchedXrayFixture = mkInbounds (managerInbounds // { package = pkgs.xray; });
tokenlessApiFixture = mkInbounds (
lib.recursiveUpdate managerInbounds { manager.api.tokensFile = null; }
);
# subscriptions.keyFile: null generates one, a path is the config's, false is none.
keySpec =
keyFile:
mkInboundsSpec (
mkInbounds (lib.recursiveUpdate runtimeInbounds { subscriptions.keyFile = keyFile; })
);
unguardedFixture = mkInbounds {
routing = {
blockRu = false;
blockPrivate = false;
blockTorrent = false;
blockPorts = [ ];
};
listeners.vless-in = realityListener;
};
@@ -915,14 +963,23 @@ let
(ok ((ruleByTag relayConfig "inbound-block-ru-domain").domain == [ "geosite:category-ru" ]))
(ok ((ruleByTag relayConfig "inbound-block-ru-ip").ip == [ "geoip:ru" ]))
(ok (!builtins.elem "inbound-block-private" (ruleTags unguardedConfig)))
# What hosters close servers over, ahead of every exception: no listener or via lets it by.
(ok ((ruleByTag relayConfig "inbound-block-torrent").protocol == [ "bittorrent" ]))
(ok (!(ruleByTag relayConfig "inbound-block-torrent") ? inboundTag))
(ok ((ruleByTag relayConfig "inbound-block-ports").port == "25"))
(ok ((ruleByTag relayConfig "inbound-block-ports").outboundTag == "block"))
(ok (!builtins.elem "inbound-block-torrent" (ruleTags unguardedConfig)))
(ok (!builtins.elem "inbound-block-ports" (ruleTags unguardedConfig)))
# serverAddress is exempt after blockPrivate, before the country blocks. Sniffing is
# routeOnly, so a connection to another IP naming this host in its handshake would match
# the name: the guard before it sends those the listener's way.
(
assert
lib.take 5 (ruleTags namedConfig) == [
lib.take 7 (ruleTags namedConfig) == [
"inbound-stats-api"
"inbound-block-private"
"inbound-block-torrent"
"inbound-block-ports"
"inbound-server-address-sniffed-proxy"
"inbound-server-address-direct"
"inbound-block-ru-domain"
@@ -1502,6 +1559,80 @@ let
)
(ok (!lib.hasInfix "masquerade" (awgRules awgProxyFixture)))
(
let
generated = keySpec null;
own = keySpec "/run/secrets/sub-key";
none = keySpec false;
in
assert generated.subscriptionKeyFile == null;
assert generated.subscriptionKeyStateFile == "/var/lib/proxy-suite/inbounds/subscription-key";
assert own.subscriptionKeyFile == "/run/secrets/sub-key";
assert own.subscriptionKeyStateFile == null;
assert none.subscriptionKeyFile == null;
assert none.subscriptionKeyStateFile == null;
true
)
# inbounds.manager
(
assert
managerSpec.manager == {
policyFile = "/var/lib/proxy-suite/inbounds-manager/policy.json";
stateFile = "/var/lib/proxy-suite/inbounds-manager/state.json";
statsFile = "/var/lib/proxy-suite/inbound-stats.json";
speedLimits = true;
};
assert runtimeSpec.manager == null;
# A level per speed slot, and the API services that change users and rules in place.
assert
builtins.attrNames managerConfig.policy.levels == [
"0"
"1"
"2"
"3"
];
assert builtins.attrNames relayConfig.policy.levels == [ "0" ];
assert builtins.elem "HandlerService" relayConfig.api.services;
assert builtins.elem "RoutingService" relayConfig.api.services;
true
)
(
let
start = readInboundsStart managerFixture;
in
assert lib.hasInfix "rendered.json" start;
assert lib.hasInfix "render-args.json" start;
assert lib.hasInfix "--argjson exit_mark 1140850688" start;
assert lib.hasInfix "--argjson exit_mark null" (readInboundsStart relayFixture);
true
)
(
assert builtins.elem "multi-user.target" managerService.wantedBy;
assert builtins.elem "proxy-suite-inbounds.service" managerService.after;
assert managerService.serviceConfig.RuntimeDirectory == "proxy-suite-inbounds-manager";
assert lib.hasInfix "inbound_manager.py" (
(import ./read-generated.nix).readDerivation managerService.serviceConfig.ExecStart
);
assert !(runtimeFixture.config.systemd.services ? "proxy-suite-inbounds-manager");
# Runtime changes go through the API now, a restart only where it cannot.
assert lib.hasInfix "inbound_apply.py" (
(import ./read-generated.nix).readDerivation
runtimeFixture.config.systemd.services."proxy-suite-inbounds-reload".serviceConfig.ExecStart
);
assert builtins.elem "-/run/proxy-suite-inbounds" (
runtimeFixture.config.systemd.services."proxy-suite-inbounds-reload".serviceConfig.ReadWritePaths
);
true
)
(ok (failedMessages managerFixture == [ ]))
(ok (hasFailed policyWithoutManagerFixture "policy needs inbounds.manager.enable"))
(ok (
hasFailed speedWithoutLimitsFixture "policy.speedLimit needs inbounds.manager.speedLimits.enable"
))
(ok (hasFailed unpatchedXrayFixture "needs proxy-suite's XRay"))
(ok (hasFailed tokenlessApiFixture "api.listen needs inbounds.manager.api.tokensFile"))
]
++ failing;
in
+29
View File
@@ -67,6 +67,35 @@ pkgs.lib.mapAttrs mkPythonCheck {
file = "${scripts}/test-inbound-runtime.py";
env = withPath scripts;
};
# inbounds.manager: policies and the traffic they are measured by, the statuses the
# renderers take; changes through XRay's API, against proxy-suite's own XRay; the daemon,
# its API and subscriptions, with apply and nftables stubbed.
inbound-policy = {
file = "${scripts}/test-inbound-policy.py";
env = withPath scripts;
};
inbound-apply = {
file = "${scripts}/test-inbound-apply.py";
env = withPath scripts // {
XRAY = "${import ../../pkgs/xray.nix { inherit pkgs; }}/bin/xray";
};
};
inbound-manager = {
file = "${scripts}/test-inbound-manager.py";
env = withPath scripts;
};
# The subscription in each client's format: sing-box checks its config; node runs the page's
# QR encoder and zbar reads what it drew.
inbound-subscription = {
file = "${scripts}/test-inbound-subscription.py";
env =
withPath scripts
// singBox
// {
NODE = "${pkgs.nodejs-slim}/bin/node";
ZBARIMG = "${pkgs.zbar}/bin/zbarimg";
};
};
# The NFQWS_OPT rewrites the zapret v1 package build applies.
patch-zapret-config = {
file = "${scripts}/test-patch-zapret-config.py";