feat: outbound groups
This commit is contained in:
35 files changed
+2444
-210
No files matched your search
@@ -89,7 +89,7 @@ in
|
||||
]);
|
||||
in
|
||||
assert pkgs.lib.hasInfix ''--argjson ex '["primary"]' '' script;
|
||||
assert pkgs.lib.hasInfix "excluded: ($tags - $selectable)" script;
|
||||
assert pkgs.lib.hasInfix "excluded: ($top - $selectable)" script;
|
||||
true
|
||||
)
|
||||
|
||||
|
||||
@@ -197,6 +197,16 @@ let
|
||||
;
|
||||
};
|
||||
|
||||
outboundGroupsChecks = import ./outbound-groups.nix {
|
||||
inherit
|
||||
pkgs
|
||||
checkLib
|
||||
evalProxySuite
|
||||
baseModule
|
||||
rejects
|
||||
;
|
||||
};
|
||||
|
||||
amneziaWgChecks = import ./amnezia-wg.nix {
|
||||
inherit checkLib;
|
||||
inherit
|
||||
@@ -317,6 +327,7 @@ let
|
||||
++ xrayBackendChecks.assertions
|
||||
++ outboundValidationChecks.assertions
|
||||
++ chainingDnsChecks.assertions
|
||||
++ outboundGroupsChecks.assertions
|
||||
++ amneziaWgChecks.assertions
|
||||
++ amneziaWgOutboundChecks.assertions
|
||||
++ zapretChecks.assertions
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
# proxy.groups and "failover" selection, as the module evaluates them. The start-time
|
||||
# resolution itself is the outbound-disabled check (repo.nix); the watcher is proxy-ctl's.
|
||||
{
|
||||
pkgs,
|
||||
checkLib,
|
||||
evalProxySuite,
|
||||
baseModule,
|
||||
rejects,
|
||||
}:
|
||||
|
||||
let
|
||||
inherit (checkLib) startScript;
|
||||
|
||||
grouped = evalProxySuite [
|
||||
baseModule
|
||||
{
|
||||
services.proxy-suite.proxy = {
|
||||
selection = "failover";
|
||||
outbounds = [
|
||||
{
|
||||
tag = "second";
|
||||
url = "http://second.example.com:8080";
|
||||
}
|
||||
];
|
||||
groups.pool.outbounds = [
|
||||
"primary"
|
||||
"second"
|
||||
];
|
||||
priority.pool = 1;
|
||||
# A rule may name a group like any outbound.
|
||||
routing.rules = [
|
||||
{
|
||||
outbound = "pool";
|
||||
domains = [ "example.org" ];
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
];
|
||||
script = startScript grouped;
|
||||
in
|
||||
{
|
||||
assertions = [
|
||||
# The watcher runs beside the socks backend, and the start script resolves the groups.
|
||||
(
|
||||
let
|
||||
unit = grouped.config.systemd.services.proxy-suite-outbound-groups;
|
||||
in
|
||||
assert builtins.elem "proxy-suite-socks.service" unit.bindsTo;
|
||||
assert pkgs.lib.hasInfix "proxy groups watch" unit.serviceConfig.ExecStart;
|
||||
assert pkgs.lib.hasInfix ''"outbounds":["primary","second"]'' script;
|
||||
assert pkgs.lib.hasInfix "interrupt_exist_connections:true" script;
|
||||
true
|
||||
)
|
||||
|
||||
# Pure XRay has neither groups nor failover selection, nor the watcher.
|
||||
(rejects "proxy.groups requires proxy.backend" [
|
||||
{
|
||||
services.proxy-suite.proxy = {
|
||||
backend = pkgs.lib.mkForce "xray";
|
||||
groups.pool.outbounds = [ "primary" ];
|
||||
};
|
||||
}
|
||||
])
|
||||
(rejects "proxy.selection = \"failover\" requires" [
|
||||
{
|
||||
services.proxy-suite.proxy = {
|
||||
backend = pkgs.lib.mkForce "xray";
|
||||
selection = "failover";
|
||||
};
|
||||
}
|
||||
])
|
||||
(
|
||||
let
|
||||
xray = evalProxySuite [
|
||||
baseModule
|
||||
{ services.proxy-suite.proxy.backend = pkgs.lib.mkForce "xray"; }
|
||||
];
|
||||
in
|
||||
assert !(xray.config.systemd.services ? proxy-suite-outbound-groups);
|
||||
true
|
||||
)
|
||||
|
||||
# A group takes a name of its own, and has members.
|
||||
(rejects "proxy.groups names must differ" [
|
||||
{ services.proxy-suite.proxy.groups.primary.outbounds = [ "primary" ]; }
|
||||
])
|
||||
(rejects "proxy.groups names must differ" [
|
||||
{ services.proxy-suite.proxy.groups.direct.outbounds = [ "primary" ]; }
|
||||
])
|
||||
(rejects "proxy.groups need members" [
|
||||
{ services.proxy-suite.proxy.groups.empty = { }; }
|
||||
])
|
||||
(rejects "proxy.groups names may hold only" [
|
||||
{ services.proxy-suite.proxy.groups."a/b".outbounds = [ "primary" ]; }
|
||||
])
|
||||
];
|
||||
}
|
||||
+90
-36
@@ -846,46 +846,100 @@ in
|
||||
# `proxy-ctl proxy outbounds disable` markers, as the start script reads them.
|
||||
outbound-disabled =
|
||||
let
|
||||
inherit
|
||||
(import ../../modules/proxy-suite/service/script-blocks/outbounds.nix {
|
||||
lib = pkgs.lib;
|
||||
inherit pkgs;
|
||||
jq = "${pkgs.jq}/bin/jq";
|
||||
proxyCfg.selectionExclude = [ "ex" ];
|
||||
selectionMode = "urltest";
|
||||
hybridEnabled = false;
|
||||
# Relative: the build directory.
|
||||
pinnedOutboundFile = "pinned";
|
||||
runtimeOutboundsDir = "obd";
|
||||
singBoxCfg = null;
|
||||
sshProxyCfg = null;
|
||||
warpCfg = null;
|
||||
torCfg = null;
|
||||
whitelistBypassJoiners = [ ];
|
||||
awgOutbounds = null;
|
||||
constants = null;
|
||||
pureXrayEnabled = null;
|
||||
collapseNamedOutbounds = null;
|
||||
backend = null;
|
||||
backendArg = null;
|
||||
xraySidecarRoutingMark = null;
|
||||
python3 = null;
|
||||
parserScriptsPythonPath = null;
|
||||
buildOutboundPy = null;
|
||||
mkSubscriptionBlock = null;
|
||||
mkSubscriptionLoadHelperBlock = null;
|
||||
runtimeSubscriptionsBlock = null;
|
||||
})
|
||||
selectionBlocks
|
||||
;
|
||||
selection = pkgs.writeShellScript "outbound-selection" ''
|
||||
set -euo pipefail
|
||||
${selectionBlocks}
|
||||
'';
|
||||
mkSelection =
|
||||
groups:
|
||||
let
|
||||
inherit
|
||||
(import ../../modules/proxy-suite/service/script-blocks/outbounds.nix {
|
||||
lib = pkgs.lib;
|
||||
inherit pkgs;
|
||||
jq = "${pkgs.jq}/bin/jq";
|
||||
proxyCfg = {
|
||||
selectionExclude = [ "ex" ];
|
||||
inherit groups;
|
||||
priority = { };
|
||||
urlTest = {
|
||||
url = "https://t";
|
||||
interval = "3m";
|
||||
};
|
||||
};
|
||||
selectionMode = "urltest";
|
||||
hybridEnabled = false;
|
||||
# Relative: the build directory.
|
||||
pinnedOutboundFile = "pinned";
|
||||
runtimeOutboundsDir = "obd";
|
||||
singBoxCfg.urlTest.tolerance = 50;
|
||||
sshProxyCfg = null;
|
||||
warpCfg = null;
|
||||
torCfg = null;
|
||||
whitelistBypassJoiners = [ ];
|
||||
awgOutbounds = null;
|
||||
constants = null;
|
||||
pureXrayEnabled = false;
|
||||
collapseNamedOutbounds = null;
|
||||
backend = null;
|
||||
backendArg = null;
|
||||
xraySidecarRoutingMark = null;
|
||||
python3 = null;
|
||||
parserScriptsPythonPath = null;
|
||||
buildOutboundPy = null;
|
||||
mkSubscriptionBlock = null;
|
||||
mkSubscriptionLoadHelperBlock = null;
|
||||
runtimeSubscriptionsBlock = null;
|
||||
})
|
||||
selectionBlocks
|
||||
;
|
||||
in
|
||||
pkgs.writeShellScript "outbound-selection" ''
|
||||
set -euo pipefail
|
||||
${selectionBlocks}
|
||||
'';
|
||||
selection = mkSelection { };
|
||||
# Resolved at start as the runtime ones are; `g` holds b and a, in that order.
|
||||
grouped = mkSelection {
|
||||
g = {
|
||||
outbounds = [
|
||||
"b"
|
||||
"a"
|
||||
];
|
||||
subscriptions = [ ];
|
||||
match = [ ];
|
||||
strategy = "failover";
|
||||
failback = true;
|
||||
interval = null;
|
||||
};
|
||||
};
|
||||
in
|
||||
pkgs.runCommand "proxy-suite-outbound-disabled-check" { nativeBuildInputs = [ pkgs.jq ]; } ''
|
||||
mkdir obd
|
||||
export RUNTIME_DIR="$PWD" OUTBOUND_SOURCES_JSON='{}'
|
||||
# No group here: `g` names members that are not running, so it blocks, with a warning.
|
||||
export OUTBOUNDS_JSON='[{"tag":"x"}]'
|
||||
${grouped} 2> err
|
||||
grep -q "group 'g': member 'b' is not available" err
|
||||
grep -q "group 'g' has no available member" err
|
||||
jq -e '.top == ["x", "g"] and .groups.g.members == ["block"]' outbounds.json > /dev/null
|
||||
|
||||
# With its members: they leave the top level, the group takes their place and order.
|
||||
export OUTBOUNDS_JSON='[{"tag":"x"},{"tag":"a"},{"tag":"b"}]'
|
||||
${grouped}
|
||||
jq -e '.top == ["x", "g"] and .groups.g.members == ["b", "a"] and .groups.g.strategy == "failover"' outbounds.json > /dev/null
|
||||
# A group pin, a runtime group, and a runtime priority putting it first.
|
||||
mkdir group-pins
|
||||
echo a > group-pins/g
|
||||
echo '{"outbounds": ["x"], "strategy": "urltest"}' > obd/h.group
|
||||
echo '{"h": 1}' > obd/priority.json
|
||||
_proxy_suite_read_source() { cat -- "$1"; }
|
||||
export -f _proxy_suite_read_source
|
||||
${grouped}
|
||||
jq -e '.top == ["h", "g"] and .groups.g.pinned == "a" and .groups.h.runtime and .priority.h == 1' outbounds.json > /dev/null
|
||||
# A group inside itself stops the start.
|
||||
echo '{"outbounds": ["h3"]}' > obd/h2.group
|
||||
echo '{"outbounds": ["h2"]}' > obd/h3.group
|
||||
! ${grouped} 2> err
|
||||
grep -q "group 'h2' contains itself" err
|
||||
rm -r obd/h.group obd/h2.group obd/h3.group obd/priority.json group-pins
|
||||
|
||||
export OUTBOUNDS_JSON='[{"tag":"a"},{"tag":"b"},{"tag":"ex"}]'
|
||||
|
||||
# A marker takes its outbound out of selection and drops a pin on it; stale markers do nothing.
|
||||
|
||||
@@ -105,6 +105,27 @@ let
|
||||
};
|
||||
};
|
||||
|
||||
# Several devices: one registration and one profile each, and "warp" is their group.
|
||||
instances = mkProxySuite {
|
||||
enable = true;
|
||||
amneziaWg.enable = true;
|
||||
proxy.enable = true;
|
||||
warp = {
|
||||
enable = true;
|
||||
asOutbound = "interface";
|
||||
instances = 2;
|
||||
};
|
||||
};
|
||||
instancesCfg = instances.config.services.proxy-suite;
|
||||
devices = mkFixture "sing-box" {
|
||||
asOutbound = "singBox";
|
||||
devices = {
|
||||
a = { };
|
||||
b.endpoint = "162.159.193.10:500";
|
||||
};
|
||||
};
|
||||
devicesStart = startOf devices;
|
||||
|
||||
invalidAssertions = mkFailingAssertions mkBadProxySuiteFixture [
|
||||
# An IPv6 endpoint without brackets.
|
||||
{
|
||||
@@ -117,6 +138,42 @@ let
|
||||
};
|
||||
proxy.enable = true;
|
||||
}
|
||||
# instances is devices' shorthand, not an addition.
|
||||
{
|
||||
enable = true;
|
||||
amneziaWg.enable = true;
|
||||
proxy.enable = true;
|
||||
warp = {
|
||||
enable = true;
|
||||
asOutbound = "interface";
|
||||
instances = 2;
|
||||
devices.extra = { };
|
||||
};
|
||||
}
|
||||
# With two devices, "warp" is their group's name.
|
||||
{
|
||||
enable = true;
|
||||
amneziaWg.enable = true;
|
||||
proxy.enable = true;
|
||||
warp = {
|
||||
enable = true;
|
||||
asOutbound = "interface";
|
||||
devices = {
|
||||
warp = { };
|
||||
other = { };
|
||||
};
|
||||
};
|
||||
}
|
||||
# One global profile, one key.
|
||||
{
|
||||
enable = true;
|
||||
amneziaWg.enable = true;
|
||||
warp = {
|
||||
enable = true;
|
||||
asAmneziaWg = true;
|
||||
instances = 2;
|
||||
};
|
||||
}
|
||||
# Enabled but used for nothing.
|
||||
{
|
||||
enable = true;
|
||||
@@ -313,6 +370,45 @@ in
|
||||
&& amneziaWg.config.services.proxy-suite.amneziaWg.profiles.warp.endpoint == null;
|
||||
true
|
||||
)
|
||||
(
|
||||
assert
|
||||
builtins.attrNames (
|
||||
pkgs.lib.filterAttrs (n: _: pkgs.lib.hasPrefix "warp" n) instancesCfg.amneziaWg.profiles
|
||||
) == [
|
||||
"warp-1"
|
||||
"warp-2"
|
||||
];
|
||||
# warp-1 keeps the state dir a single device had; warp-2 gets one of its own.
|
||||
assert pkgs.lib.hasSuffix "/warp/wgcf-profile.conf"
|
||||
instancesCfg.amneziaWg.profiles.warp-1.configFile;
|
||||
assert pkgs.lib.hasSuffix "/warp/warp-2/wgcf-profile.conf"
|
||||
instancesCfg.amneziaWg.profiles.warp-2.configFile;
|
||||
assert instances.config.systemd.services ? proxy-suite-warp;
|
||||
assert
|
||||
instances.config.systemd.services.proxy-suite-warp-register-warp-2.serviceConfig.StateDirectory
|
||||
== "proxy-suite/warp/warp-2";
|
||||
assert builtins.elem "proxy-suite-warp-register-warp-2.service"
|
||||
instances.config.systemd.services.proxy-suite-awg-warp-2.wants;
|
||||
assert
|
||||
instancesCfg.proxy.groups.warp.outbounds == [
|
||||
"warp-1"
|
||||
"warp-2"
|
||||
];
|
||||
assert instancesCfg.proxy.groups.warp.strategy == "failover";
|
||||
true
|
||||
)
|
||||
# sing-box tunnels: the first on the usual ports, the next on its own, with its endpoint.
|
||||
(
|
||||
assert hasInfix ''"server":"127.0.0.1","server_port":18538,"tag":"a","type":"socks"'' devicesStart;
|
||||
assert hasInfix ''"server":"127.0.0.1","server_port":18900,"tag":"b","type":"socks"'' devicesStart;
|
||||
assert hasInfix "--endpoint 162.159.193.10:500" (
|
||||
generated.readDerivation devices.config.systemd.services.proxy-suite-warp-tunnel-b.serviceConfig.ExecStart
|
||||
);
|
||||
# a has the configFile, so only b registers.
|
||||
assert !(devices.config.systemd.services ? proxy-suite-warp);
|
||||
assert devices.config.systemd.services ? proxy-suite-warp-register-b;
|
||||
true
|
||||
)
|
||||
]
|
||||
++ invalidAssertions;
|
||||
}
|
||||
Reference in new issue
Block a user