feat: cahining + stats

This commit is contained in:
fufsob committed 2026-09-15 11:53:49 +00:00
1 parent 9c9658baa7
commit ea1922243a
28 files changed
+898 -72

No files matched your search

+144
View File
@@ -0,0 +1,144 @@
{
pkgs,
evalProxySuite,
baseModule,
mkTunConfig,
mkTProxyConfig,
mkBadProxySuiteFixture,
mkFailingAssertions,
}:
let
generated = import ./read-generated.nix;
startScript =
fixture: generated.readDerivation fixture.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart;
chained = evalProxySuite [
baseModule
{
services.proxy-suite.proxy = {
outbounds = [
{
tag = "exit";
url = "http://exit.example.com:8080";
detour = "primary";
}
];
subscriptions = [
{
tag = "sub";
url = "https://example.com/sub";
detour = "primary";
}
];
};
}
];
dnsModule = {
services.proxy-suite.proxy = {
tun.enable = true;
dns = {
strategy = "ipv4_only";
fakeIp.enable = true;
singBox = {
servers = [
{
tag = "corp";
type = "udp";
server = "10.0.0.53";
}
];
rules = [
{
domain_suffix = [ "corp.example" ];
server = "corp";
}
];
};
};
};
};
dnsFixture = evalProxySuite [
baseModule
dnsModule
];
tunDns = (mkTunConfig dnsFixture).dns;
tproxyDns = (mkTProxyConfig dnsFixture).dns;
in
{
assertions = [
# Detours are resolved at start, once subscription entries exist; without any, nothing is added.
(
assert pkgs.lib.hasInfix ''{"outbounds":{"exit":"primary"},"subscriptions":{"sub":"primary"}}'' (startScript chained);
assert pkgs.lib.hasInfix "proxy-suite-outbound-detours" (startScript chained);
assert !(pkgs.lib.hasInfix "outbound chaining" (startScript (evalProxySuite [ baseModule ])));
true
)
# User rules first, fake IP last and only for what comes through the TUN.
(
assert builtins.head tunDns.rules == { domain_suffix = [ "corp.example" ]; server = "corp"; };
assert pkgs.lib.last tunDns.rules == {
inbound = [ "tun-in" ];
query_type = [ "A" "AAAA" ];
server = "fakeip";
};
assert builtins.any (s: s.tag == "fakeip") tunDns.servers;
assert builtins.any (s: s.tag == "corp") tunDns.servers;
assert tunDns.strategy == "ipv4_only";
true
)
# TProxy has no TUN to hand fake addresses to.
(
assert !(builtins.any (s: s.tag == "fakeip") tproxyDns.servers);
assert builtins.head tproxyDns.rules == { domain_suffix = [ "corp.example" ]; server = "corp"; };
true
)
]
++ mkFailingAssertions mkBadProxySuiteFixture [
# An outbound cannot chain through itself...
{
enable = true;
proxy = {
enable = true;
outbounds = [
{
tag = "loop";
url = "http://proxy.example.com:8080";
detour = "loop";
}
];
};
}
# ...nor through the selector it belongs to.
{
enable = true;
proxy = {
enable = true;
subscriptions = [
{
tag = "sub";
url = "https://example.com/sub";
detour = "proxy";
}
];
};
}
# The sing-box DNS options need sing-box.
{
enable = true;
proxy = {
enable = true;
backend = "xray";
outbounds = [
{
tag = "primary";
url = "http://proxy.example.com:8080";
}
];
dns.fakeIp.enable = true;
};
}
];
}
+12
View File
@@ -157,6 +157,17 @@ let
outboundValidationChecks = import ./outbound-validation.nix {
inherit mkBadProxySuiteFixture mkFailingAssertions;
};
chainingDnsChecks = import ./chaining-dns.nix {
inherit
pkgs
evalProxySuite
baseModule
mkTunConfig
mkTProxyConfig
mkBadProxySuiteFixture
mkFailingAssertions
;
};
amneziaWgChecks = import ./amnezia-wg.nix {
inherit
@@ -264,6 +275,7 @@ let
++ tgWsProxyChecks.assertions
++ xrayBackendChecks.assertions
++ outboundValidationChecks.assertions
++ chainingDnsChecks.assertions
++ amneziaWgChecks.assertions
++ amneziaWgOutboundChecks.assertions
++ zapretChecks.assertions
+48 -5
View File
@@ -578,25 +578,68 @@
''
add=${../../modules/proxy-suite/inbound-stats-add.jq}
now=1789135690 # 2026-09-11
a() { jq -c --argjson q "$1" --arg day 2026-09-11 --argjson now "$now" -f "$add" <<<"$2"; }
a() { jq -c --argjson q "$1" --argjson online "''${3:-{\}}" --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"},
{"name":"user>>>fufsob>>>traffic>>>uplink","value":"2000"},
{"name":"user>>>phone>>>traffic>>>uplink"}]}'
{"name":"user>>>phone>>>traffic>>>uplink"},
{"name":"inbound>>>vless-in>>>traffic>>>downlink","value":5},
{"name":"outbound>>>direct>>>traffic>>>uplink","value":6}]}'
s="$(a "$r" '{}')"
s="$(a "$r" "$s")"
jq -e '.days["2026-09-11"].fufsob == {down: 6000000, up: 4000}
and .days["2026-09-11"].phone.up == 0 and .at == 1789135690' <<<"$s" > /dev/null
jq -e '.days["2026-09-11"] | .user.fufsob == {down: 6000000, up: 4000}
and .user.phone.up == 0 and .inbound["vless-in"].down == 10 and .outbound.direct.up == 12' <<<"$s" > /dev/null
jq -e '.at == 1789135690' <<<"$s" > /dev/null
# Nothing counted since the last reading: nothing changes.
jq -e --argjson s "$s" '.days == $s.days' <<<"$(a '{}' "$s")" > /dev/null
# An online user's last seen time is kept, and never goes back.
o='{"users":[{"email":"phone","ips":[{"ip":"203.0.113.7","lastSeen":1789135000},{"ip":"203.0.113.8","lastSeen":1789135600}]}]}'
s="$(a '{}' "$s" "$o")"
jq -e '.seen.phone == 1789135600' <<<"$s" > /dev/null
s="$(a '{}' "$s" '{"users":[{"email":"phone","ips":[{"ip":"203.0.113.7","lastSeen":1}]}]}')"
jq -e '.seen.phone == 1789135600' <<<"$s" > /dev/null
# Days more than a year old are let go.
old="$(jq -c '.days["2024-01-01"] = {fufsob: {up: 1}}' <<<"$s")"
old="$(jq -c '.days["2024-01-01"] = {user: {fufsob: {up: 1}}}' <<<"$s")"
jq -e '.days["2024-01-01"] == null' <<<"$(a '{}' "$old")" > /dev/null
touch "$out"
'';
# Proxy chains, resolved at start once subscription entries exist.
outbound-detours =
pkgs.runCommand "proxy-suite-outbound-detours-check"
{
nativeBuildInputs = [ pkgs.jq ];
}
''
f=${../../modules/proxy-suite/outbound-detours.jq}
d() { jq -c -f "$f" --argjson d "$1" --argjson sources "$2" --arg kind "$3" <<<"$4"; }
# sing-box: a declared outbound and every entry of a subscription.
r="$(d '{"outbounds":{"de":"ru"},"subscriptions":{"s":"warp"}}' '{"ru":"static","de":"static","s-a":"sub:s","warp":"warp"}' sing-box \
'{"outbounds":[{"tag":"ru"},{"tag":"de"},{"tag":"s-a"},{"tag":"warp"}],"xray":[]}')"
jq -e '.errors == [] and ([.outbounds[] | .detour] == [null, "ru", "warp", null])' <<<"$r" > /dev/null
# XRay: dialerProxy, next to what sockopt already had.
r="$(d '{"outbounds":{"de":"ru"},"subscriptions":{}}' '{"ru":"static","de":"static"}' xray \
'{"outbounds":[{"tag":"ru"},{"tag":"de","streamSettings":{"sockopt":{"mark":1}}}],"xray":[]}')"
jq -e '.outbounds[1].streamSettings.sockopt == {mark: 1, dialerProxy: "ru"}' <<<"$r" > /dev/null
# Hybrid: sidecar outbounds chain in the sidecar, sing-box ones through anything.
r="$(d '{"outbounds":{"x1":"x2","sb":"x1"},"subscriptions":{}}' '{"x1":"static","x2":"static","sb":"static"}' hybrid \
'{"outbounds":[{"tag":"x1"},{"tag":"x2"},{"tag":"sb"}],"xray":[{"tag":"x1"},{"tag":"x2"}]}')"
jq -e '.errors == [] and .xray[0].streamSettings.sockopt.dialerProxy == "x2"
and .outbounds[0].detour == null and .outbounds[2].detour == "x1"' <<<"$r" > /dev/null
r="$(d '{"outbounds":{"x1":"sb"},"subscriptions":{}}' '{"x1":"static","sb":"static"}' hybrid \
'{"outbounds":[{"tag":"x1"},{"tag":"sb"}],"xray":[{"tag":"x1"}]}')"
jq -e '.errors == ["outbound '"'x1'"' runs on XRay and can only chain through another XRay outbound, not '"'sb'"'"]' <<<"$r" > /dev/null
# A missing hop and a loop are errors, and nothing is rewritten.
r="$(d '{"outbounds":{"a":"b","b":"a","c":"gone"},"subscriptions":{}}' '{"a":"static","b":"static","c":"static"}' sing-box \
'{"outbounds":[{"tag":"a"},{"tag":"b"},{"tag":"c"}],"xray":[]}')"
jq -e '(.errors | length) == 3 and all(.outbounds[]; .detour == null)' <<<"$r" > /dev/null
touch "$out"
'';
proxy-ctl-amneziawg =
pkgs.runCommand "proxy-suite-proxy-ctl-amneziawg-check"
{