feat: runtime routing

This commit is contained in:
fufsob committed 2026-10-10 16:33:24 +00:00
1 parent d70bcb317f
commit 584c8ba0a0
34 files changed
+1926 -112

No files matched your search

+11
View File
@@ -80,6 +80,15 @@ let
;
};
runtimeRoutingChecks = import ./runtime-routing.nix {
inherit
checkLib
pkgs
minimalProxyCtlWrapper
minimalProxyCtlScript
;
};
subscriptionChecks = import ./subscriptions.nix {
inherit checkLib;
inherit
@@ -344,6 +353,7 @@ let
++ guiChecks.assertions
++ subscriptionChecks.assertions
++ routeModeChecks.assertions
++ runtimeRoutingChecks.assertions
++ optionTypeChecks.assertions
++ perAppRoutingChecks.assertions
++ perAppRoutingAwgChecks.assertions
@@ -356,6 +366,7 @@ in
amneziawg-unit-scripts = amneziaWgChecks.unitScripts;
xray-jq-filter-runtime = xrayBackendChecks.runtime;
tor-guard-runtime = torChecks.runtime;
runtime-routing = runtimeRoutingChecks.runtime;
per-app-zapret-runtime = perAppRoutingChecks.runtime;
zapret-hostlist-rules = zapretChecks.rules;
zapret2-config = zapret2Checks.runtime;
+169
View File
@@ -0,0 +1,169 @@
# Routing rules added at runtime (`proxy-ctl proxy rules`): the units, spool and environment
# they need, the configuration's sections proxy-ctl lists, and (runtime) the rules composed into
# real configs, which the backends then accept.
{
checkLib,
pkgs,
minimalProxyCtlWrapper,
minimalProxyCtlScript,
}:
let
inherit (checkLib)
ok
evalProxySuite
baseModule
mkRouteModeRules
shellValueByPrefix
startScript
unitScript
;
inherit (pkgs) lib;
fillTemplate = import ../../modules/proxy-suite/lib/fill-template.nix;
routing = {
rules = [
{
outbound = "primary";
domains = [ "custom.example" ];
}
];
proxy.domains = [ "proxied.example" ];
block.domains = [ "blocked.example" ];
};
fixture =
backend:
evalProxySuite [
baseModule
{
services.proxy-suite = {
proxy = {
backend = lib.mkForce backend;
inherit routing;
};
userControl = {
enable = true;
scopes = [ "routing" ];
};
};
}
];
singBoxFixture = fixture "sing-box";
xrayFixture = fixture "xray";
services = singBoxFixture.config.systemd.services;
execStart = f: f.config.systemd.services.proxy-suite-socks.serviceConfig.ExecStart;
socksScript = startScript singBoxFixture;
applyScript = unitScript singBoxFixture "proxy-suite-routing-apply";
buckets = mkRouteModeRules singBoxFixture;
polkitRules = singBoxFixture.config.security.polkit.extraConfig;
sections = (checkLib.mkRouting singBoxFixture).routingSections;
# The inbounds' guard, alone: where it lands around runtime direct rules.
guardFilter = pkgs.writeText "proxy-suite-runtime-routing-guard.jq" (
import ../../modules/proxy-suite/service/script-blocks/backend-jq-filter.nix {
inherit lib;
pureXrayEnabled = false;
selectionMode = "first";
proxyInboundsGuardPrivate = true;
}
);
runtime =
pkgs.runCommand "proxy-suite-runtime-routing-check"
{
nativeBuildInputs = [
pkgs.jq
pkgs.gnugrep
];
}
(
fillTemplate ./runtime-routing/compose.template.sh {
singBoxStart = execStart singBoxFixture;
xrayStart = execStart xrayFixture;
singBox = "${singBoxFixture.config.services.proxy-suite.proxy.singBox.package}/bin/sing-box";
xray = "${xrayFixture.config.services.proxy-suite.proxy.xray.package}/bin/xray";
xrayAssets = "${xrayFixture.config.services.proxy-suite.geodata.xray.assets}/share/v2ray";
inherit guardFilter;
}
);
in
{
inherit runtime;
assertions = [
# proxy-ctl reads the configuration's sections, the spool and the rendered copy, and checks
# geosites against the geodata the backend would load.
(
assert
shellValueByPrefix minimalProxyCtlWrapper "export RUNTIME_ROUTING_DIR="
== "/var/lib/proxy-suite/routing.d";
assert
shellValueByPrefix minimalProxyCtlWrapper "export ROUTING_RULES_DIR="
== "/var/lib/proxy-suite/routing";
assert lib.hasPrefix "/nix/store/" (
shellValueByPrefix minimalProxyCtlWrapper "export ROUTING_RULES_FILE="
);
assert lib.hasSuffix "/share/sing-box/rule-set" (
shellValueByPrefix minimalProxyCtlWrapper "export GEODATA_GEOSITE_DIR="
);
assert lib.hasInfix "proxy rules add <target> <match...>" minimalProxyCtlScript;
true
)
# The sections, at the priorities runtime rules sort among, with the tags as written.
(ok (
map (s: s.id) sections == [
"rules"
"proxy"
"block"
"direct"
"proxyGeo"
]
&&
map (s: s.priority) sections == [
100
200
300
400
500
]
&& (builtins.head (builtins.head sections).entries).target == "primary"
&& (builtins.head (builtins.elemAt sections 2).entries).domains == [ "blocked.example" ]
))
# The DNS mirror by section adds up to the configuration's own, in its order.
(ok (
lib.concatMap (c: c.dns) buckets.custom
++ buckets.dns.proxyPrimary
++ buckets.dns.direct
++ buckets.dns.proxyGeo == (checkLib.mkRouting singBoxFixture).singBoxDnsRules
))
# The apply unit: root, over the routing scope's spool, which polkit lets that scope start.
(
let
apply = services.proxy-suite-routing-apply.serviceConfig;
in
assert apply.RemainAfterExit == false;
assert apply.ProtectSystem == "strict";
assert apply.StateDirectory == "proxy-suite";
assert lib.hasInfix "systemctl restart proxy-suite-socks" (
builtins.replaceStrings [ "\n" ] [ " " ] applyScript
);
assert lib.hasInfix "/run/proxy-suite-tun/routing-structure" applyScript;
assert lib.hasInfix "proxy-suite-routing-apply" polkitRules;
assert builtins.elem "d /var/lib/proxy-suite/routing.d 2770 root proxy-suite -"
singBoxFixture.config.systemd.tmpfiles.rules;
true
)
# Every start renders the spool and composes the route, the runtime rules among the
# configuration's, before the filter.
(
assert lib.hasInfix "routing-convert.jq" socksScript;
assert lib.hasInfix "routing-compose.jq" socksScript;
assert lib.hasInfix "--slurpfile user_rule_sets" socksScript;
assert lib.hasInfix ''"$RUNTIME_DIR/routing-structure"'' socksScript;
true
)
];
}
@@ -0,0 +1,188 @@
# Runtime routing rules through the real generated programs: the start script's convert and
# compose steps, then the backend filter, over a spool with good and bad entries; the result
# through the backend's own check. Paths are read out of the start script, as it runs them.
set -eu
export HOME=$PWD
# $1 start script: the store paths it hands jq.
paths() {
START=$1
FILTER=$(grep -o 'BACKEND_JQ_FILTER=[^ ]*' "$START" | head -n 1 | cut -d= -f2)
CONFIG=$(grep -o '"/nix/store/[a-z0-9]*-proxy-suite-core" > "$RUNTIME_DIR/config.json"' "$START" | grep -o '/nix/store/[a-z0-9]*-proxy-suite-core')
BUCKETS=$(grep -o 'routing-compose.jq "/nix/store/[^"]*' "$START" | grep -o '/nix/store/.*')
CONVERT=$(grep -o '/nix/store/[^ ]*routing-convert.jq' "$START" | head -n 1)
COMPOSE=$(grep -o '/nix/store/[^ ]*routing-compose.jq' "$START" | head -n 1)
GEOSITE_DIR=$(grep -o -- '--arg geosite_dir [^ ]*' "$START" | head -n 1 | cut -d' ' -f3)
GEOIP_DIR=$(grep -o -- '--arg geoip_dir [^ ]*' "$START" | head -n 1 | cut -d' ' -f3)
URLTEST=$(grep -o -- '--argjson urltest [a-z]*' "$START" | head -n 1 | cut -d' ' -f3)
BACKEND=$(grep -o -- '--arg backend [a-z-]*' "$START" | head -n 1 | cut -d' ' -f3)
for v in FILTER CONFIG BUCKETS CONVERT COMPOSE GEOSITE_DIR GEOIP_DIR URLTEST BACKEND; do
if [ -z "$(eval "printf %s \"\$$v\"")" ]; then
echo "no $v in $START" >&2
exit 1
fi
done
}
# The spool as a member might leave it: a name twice, a path for a name, junk in the lists.
cat > spool.json <<'JSON'
{"rules": [
{"name": "bank", "target": "direct", "priority": 50, "domains": [".Bank.Example", "bad domain"],
"ips": ["192.0.2.1", "nope"], "geosites": ["category-gov-ru", "../x"]},
{"name": "bank", "target": "proxy"},
{"name": "../evil", "target": "proxy"},
{"name": "ads", "target": "block", "priority": 10, "domains": ["ads2.example"]},
{"name": "work", "target": "primary", "priority": 450, "domains": ["work.example"],
"ips": ["198.51.100.0/24", "999.1.1.1", "198.51.100.1/33", "010.0.0.1", "1::2::3"]},
{"name": "gone", "target": "removed-outbound", "domains": ["x.example"]},
{"name": "nogeo", "target": "proxy", "geosites": ["no-such-list"]},
{"name": "empty", "target": "proxy", "priority": 250},
{"name": "off", "target": "proxy", "disabled": true, "domains": ["off.example"]}
]}
JSON
# The render script root runs, as the start script calls it, into ./routing.
RENDER=$(grep -A1 "^RUNTIME_ROUTING_SKIPPED='\[\]'" @singBoxStart@ | tail -n 1)
mkdir spool
cp spool.json spool/rules.json
mkdir -p routing/rs
echo '{}' > routing/rs/old.json
"$RENDER" "$PWD/spool" "$PWD/routing"
cp routing/rules.json rendered.json
jq -e '
map(.name) == ["ads", "bank", "gone", "nogeo", "off", "empty", "work"]
and (.[] | select(.name == "bank") | .domains == ["bank.example"] and .ips == ["192.0.2.1/32"]
and .geosites == ["category-gov-ru"] and .target == "direct")
' rendered.json > /dev/null
# A rule set per rule, domains and addresses for routing, domains alone for lookups; an address
# the backends would refuse (and with it the whole start) is dropped.
jq -e '.rules == [{domain_suffix: ["work.example"]}, {ip_cidr: ["198.51.100.0/24"]}]' routing/rs/work.json > /dev/null
jq -e '.rules == [{domain_suffix: ["work.example"]}]' routing/dns/work.json > /dev/null
jq -e '.rules == []' routing/rs/empty.json > /dev/null
# What restarts the backends: everything but the domains and addresses of enabled rules.
jq -e --slurpfile r rendered.json '. == ($r[0] | map(select(.disabled | not) | del(.domains, .ips)))' routing/structure > /dev/null
# A rule gone: its rule set stays until apply sweeps, once the backends restarted.
[ -e routing/rs/old.json ]
"$RENDER" --sweep "$PWD/spool" "$PWD/routing"
[ ! -e routing/rs/old.json ] && [ -e routing/rs/work.json ]
# Only a change is renamed in: sing-box reloads a rule set on every new file.
before=$(stat -c %i routing/rs/work.json)
"$RENDER" "$PWD/spool" "$PWD/routing"
[ "$(stat -c %i routing/rs/work.json)" = "$before" ]
# A member's symlink, what is not JSON, an empty file or two documents are no rule at all, and
# no failure.
mkdir bad-link bad-json bad-empty bad-two
ln -s "$PWD/spool/rules.json" bad-link/rules.json
echo 'not json' > bad-json/rules.json
: > bad-empty/rules.json
cat spool.json spool.json > bad-two/rules.json
for spool in bad-link bad-json bad-empty bad-two; do
"$RENDER" "$PWD/$spool" "$PWD/out-$spool"
jq -e '. == []' "out-$spool/rules.json" > /dev/null
done
# $1 start script, $2 mode ("": none), $3 the outbounds the start script would add, $4 out.
compose() {
paths "$1"
MODE=$2 OBS=$3 OUT=$4
MISSING=$(jq -r '[.[] | select(.disabled | not) | (.geosites[] | "geosite-" + .), (.geoips[] | "geoip-" + .)] | unique[]' rendered.json \
| while read -r geo; do
case "$geo" in
geosite-*) [ -e "$GEOSITE_DIR/$geo.srs" ] || printf '%s\n' "$geo" ;;
*) [ -e "$GEOIP_DIR/$geo.srs" ] || printf '%s\n' "$geo" ;;
esac
done | jq -R . | jq -cs .)
TAGS=$(jq -c --argjson obs "$OBS" '[($obs[]?, .outbounds[]?) | .tag? | strings] | unique' "$CONFIG")
jq -c --arg backend "$BACKEND" --slurpfile tags <(echo "$TAGS") --argjson rule_sets '[]' \
--slurpfile missing_geo <(echo "$MISSING") --arg dir "$PWD/routing" --arg geosite_dir "$GEOSITE_DIR" \
--arg geoip_dir "$GEOIP_DIR" --argjson urltest "$URLTEST" -f "$CONVERT" rendered.json > converted.json
jq -c --arg mode "${MODE:-blacklist}" --slurpfile runtime <(jq -c .sections converted.json) -f "$COMPOSE" "$BUCKETS" > composed.json
FINAL=proxy DNS_FINAL=remote CLEAR=false
case "$MODE" in
whitelist) FINAL=direct DNS_FINAL=local ;;
all-proxy) CLEAR=true ;;
all-bypass) FINAL=direct DNS_FINAL=local CLEAR=true ;;
esac
jq --slurpfile obs <(echo "$OBS") --slurpfile probe_inbounds <(echo '[]') --slurpfile probe_pin_rules <(echo '[]') \
--slurpfile autoproxy_rule_sets <(echo '[]') --slurpfile autoproxy_rules <(echo '[]') \
--argjson auth_enabled false --arg user "" --arg password "" \
--argjson route_enabled "${ENABLED:-true}" --slurpfile route_rules <(jq -c .rules composed.json) \
--slurpfile route_dns <(jq -c .dns composed.json) --argjson static_dns_count "$(jq .staticDnsCount composed.json)" \
--slurpfile user_rule_sets <(jq -c .rule_sets converted.json) \
--arg route_final "$FINAL" --arg dns_final "$DNS_FINAL" --argjson clear_dns_rules "$CLEAR" \
--arg xray_loglevel "" --arg xray_single_proxy_tag "" --slurpfile xray_selectable <(echo '[]') \
--argjson xray_tun_dns_runtime false --argjson host_addresses '[]' \
-f "$FILTER" "$CONFIG" > "$OUT"
}
# --- sing-box --------------------------------------------------------------------------------
SB_OBS='[{"type":"selector","tag":"proxy","outbounds":["primary"]},{"type":"http","tag":"primary","server":"proxy.example.com","server_port":8080}]'
compose @singBoxStart@ "" "$SB_OBS" none.json
# Left out, and why: an outbound that is gone, a geosite this host does not have.
jq -e 'map(.name) == ["gone", "nogeo"] and (.[0].why | test("removed-outbound"))' <(jq -c .skipped converted.json) > /dev/null
jq -e '
[.route.rules[] | (.rule_set[0]? // .domain_suffix[0]? // .action // (.ip_is_private | tostring))] as $order
| def at($x): $order | index($x);
# Runtime ads (10) and bank (50) ahead of the configuration'"'"'s rules (100), empty (250) after
# its proxy lists (200), work (450) after the direct ones (400); disabled off nowhere.
at("user:ads") < at("user:bank") and at("user:bank") < at("custom.example")
and at("proxied.example") < at("user:empty") and at("user:empty") < at("blocked.example")
and at("true") < at("user:work") and at("user:off") == null and at("user:gone") == null
# The rule sets, each once: geodata the configuration has too is not declared twice.
and ([.route.rule_set[].tag] | length == (unique | length))
and ([.route.rule_set[] | select(.tag == "user:bank" or .tag == "user-dns:bank")] | length == 2)
and any(.route.rule_set[]; .tag == "geosite-category-gov-ru")
# Lookups follow: bank locally, ahead of the configuration'"'"'s; a block rule none.
and ([.dns.rules[] | .rule_set[0]? // .domain_suffix[0]?] as $dns
| ($dns | index("user-dns:bank")) < ($dns | index("custom.example"))
and ($dns | index("user-dns:ads")) == null and ($dns | index("user-dns:work")) != null)
and (.dns.rules[] | select(.rule_set[0]? == "user-dns:bank") | .server) == "local"
' none.json > /dev/null
@singBox@ check -c none.json
# Composed with no runtime rule, the route is the configuration's own, lookups too.
cp rendered.json full.json
echo '[]' > rendered.json
compose @singBoxStart@ "" "$SB_OBS" composed-default.json
ENABLED=false compose @singBoxStart@ "" "$SB_OBS" static.json
jq -e --slurpfile static static.json '.route == $static[0].route and .dns == $static[0].dns' composed-default.json > /dev/null
cp full.json rendered.json
for mode in whitelist all-proxy all-bypass; do
compose @singBoxStart@ "$mode" "$SB_OBS" "$mode.json"
@singBox@ check -c "$mode.json"
done
# all-proxy keeps no direct list, all-bypass nothing but the blocks.
jq -e '[.route.rules[].rule_set[0]?] | index("user:bank") == null and index("user:work") != null' all-proxy.json > /dev/null
jq -e '[.route.rules[].rule_set[0]?] | index("user:ads") != null and index("user:work") == null' all-bypass.json > /dev/null
# --- xray ------------------------------------------------------------------------------------
jq 'map(.ruleSets = [])' full.json > rendered.json
compose @xrayStart@ "" '[{"protocol":"freedom","tag":"primary"}]' xray.json
jq -e '
[.routing.rules[].ruleTag] as $tags
| ($tags | index("user:bank")) < ($tags | index("user:work"))
and ($tags | index("user:work:ip")) == ($tags | index("user:work")) + 1
and ($tags | index("dns-remote-bridge")) != null
' xray.json > /dev/null
XRAY_LOCATION_ASSET=@xrayAssets@ @xray@ run -test -c xray.json
# --- the inbounds' guard ---------------------------------------------------------------------
# A runtime direct rule above the configuration's gets a guard of its own, for what it matches;
# the guard proper stays before the configuration's first direct rule.
echo '{"inbounds":[],"outbounds":[],"dns":{"rules":[]},"route":{"final":"proxy","rules":[
{"action":"sniff"},{"rule_set":["user:bank"],"outbound":"direct"},
{"domain_suffix":["proxied.example"],"outbound":"proxy"},{"domain_suffix":["ru"],"outbound":"direct"}]}}' |
jq --slurpfile obs <(echo '[]') --argjson auth_enabled false --arg user "" --arg password "" \
--argjson route_enabled false --slurpfile route_rules <(echo '[]') \
--arg route_final proxy --arg dns_final remote --argjson clear_dns_rules false \
--slurpfile probe_inbounds <(echo '[]') --slurpfile autoproxy_rule_sets <(echo '[]') \
--slurpfile autoproxy_rules <(echo '[]') --slurpfile probe_pin_rules <(echo '[]') \
-f @guardFilter@ > guard.json
jq -e '
.route.rules as $r
| ($r | map(.type? == "logical" and .action == "resolve" and .rules[-1].rule_set == ["user:bank"]) | index(true)) as $scoped
| ($r | map(.rule_set? == ["user:bank"]) | index(true)) as $bank
| ($r | map(.inbound? == ["mixed-in"] and .action == "resolve") | index(true)) as $guard
| ($r | map(.domain_suffix? == ["proxied.example"]) | index(true)) as $proxied
| ($r | map(.domain_suffix? == ["ru"]) | index(true)) as $ru
| $scoped < $bank and $proxied < $guard and $guard < $ru
' guard.json > /dev/null
touch "$out"