From 716fe4b18d4896363d5778898dc6096ed107cf2d Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Sat, 18 Apr 2026 15:12:55 +0500 Subject: [PATCH] other improvements --- README.md | 4 +- docs/options.md | 5 +- modules/proxy-suite/options/types.nix | 5 +- modules/proxy-suite/service-assertions.nix | 5 + modules/proxy-suite/service/context.nix | 4 +- modules/proxy-suite/service/control.nix | 4 +- modules/proxy-suite/service/default.nix | 3 +- modules/proxy-suite/service/scripts.nix | 46 ++++++-- nix/checks.nix | 125 +++++++++++++++++++++ pkgs/proxy-ctl.nix | 4 +- pkgs/proxy-ctl.sh | 13 ++- 11 files changed, 192 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 4d95ed3..99303c5 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ Usage: proxy-ctl [args] Commands: help show this help message status [--tray] show status of all proxy-suite services - proxy on|off enable/disable the core SOCKS proxy + proxy on|off enable/disable the sing-box proxy stack tproxy on|off enable/disable TProxy transparent mode tun on|off enable/disable TUN mode zapret on|off enable/disable zapret-discord-youtube @@ -144,6 +144,6 @@ Commands: apps list configured per-app routing profiles wrap -- run a command via a perAppRouting profile subscription list show subscriptions, cache age, and proxy count - subscription update force-refresh all subscription caches and restart + subscription update force-refresh all subscription caches and restart active sing-box services ``` diff --git a/docs/options.md b/docs/options.md index 1468a92..bee3588 100644 --- a/docs/options.md +++ b/docs/options.md @@ -2566,10 +2566,13 @@ Unique identifier for this subscription\. Used as a prefix for all outbound tags generated from its proxy list, e\.g\. “my-sub” -> tags like “my-sub-Server-DE”\. +This value is also used as the subscription cache filename stem under +/var/lib/proxy-suite/subscriptions/, so it must be a safe identifier\. + *Type:* -string +string matching the pattern ^\[A-Za-z0-9]\[A-Za-z0-9\._-]\*$ diff --git a/modules/proxy-suite/options/types.nix b/modules/proxy-suite/options/types.nix index ea46180..bba3ff4 100644 --- a/modules/proxy-suite/options/types.nix +++ b/modules/proxy-suite/options/types.nix @@ -76,11 +76,14 @@ let subscriptionType = types.submodule { options = { tag = mkOption { - type = types.str; + type = types.strMatching "^[A-Za-z0-9][A-Za-z0-9._-]*$"; description = '' Unique identifier for this subscription. Used as a prefix for all outbound tags generated from its proxy list, e.g. "my-sub" -> tags like "my-sub-Server-DE". + + This value is also used as the subscription cache filename stem under + /var/lib/proxy-suite/subscriptions/, so it must be a safe identifier. ''; example = "community-list"; }; diff --git a/modules/proxy-suite/service-assertions.nix b/modules/proxy-suite/service-assertions.nix index 5e41951..14d1319 100644 --- a/modules/proxy-suite/service-assertions.nix +++ b/modules/proxy-suite/service-assertions.nix @@ -9,6 +9,7 @@ perAppZapretCfg, builtinTags, outboundTags, + subscriptionTags, invalidRoutingTargets, effectivePerAppRoutingProfileNames, hasProxychainsProfiles, @@ -30,6 +31,10 @@ in assertion = !singBoxCfg.enable || builtins.length outboundTags == builtins.length (lib.unique outboundTags); message = "proxy-suite: outbound tags must be unique"; } + { + assertion = !singBoxCfg.enable || builtins.length subscriptionTags == builtins.length (lib.unique subscriptionTags); + message = "proxy-suite: subscription tags must be unique because they are used as cache keys and outbound tag prefixes"; + } { assertion = !singBoxCfg.enable || builtins.all (tag: !builtins.elem tag builtinTags) outboundTags; message = "proxy-suite: outbound tags must not use reserved names: proxy, direct, block"; diff --git a/modules/proxy-suite/service/context.nix b/modules/proxy-suite/service/context.nix index 3e6bd1c..277acbc 100644 --- a/modules/proxy-suite/service/context.nix +++ b/modules/proxy-suite/service/context.nix @@ -26,6 +26,7 @@ let builtinTags = [ "proxy" "direct" "block" ]; outboundTags = map (ob: ob.tag) singBoxCfg.outbounds; + subscriptionTags = map (sub: sub.tag) singBoxCfg.subscriptions; invalidRoutingTargets = lib.unique ( map (rule: rule.outbound) ( builtins.filter (rule: !builtins.elem rule.outbound (builtinTags ++ outboundTags)) singBoxCfg.routing.rules @@ -69,7 +70,7 @@ let control = import ./control.nix { inherit packages singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy perAppZapretCfg; zapretEnabled = cfg.zapret.enable; - inherit (scripts) subscriptionTagsList; + inherit (scripts) subscriptionTagsFile; inherit (perAppRouting) perAppRoutingProfilesFile proxychainsConfigFile @@ -89,6 +90,7 @@ in userControlCfg builtinTags outboundTags + subscriptionTags invalidRoutingTargets polkit scripts diff --git a/modules/proxy-suite/service/control.nix b/modules/proxy-suite/service/control.nix index d88b713..26ae1d8 100644 --- a/modules/proxy-suite/service/control.nix +++ b/modules/proxy-suite/service/control.nix @@ -7,7 +7,7 @@ perAppRoutingTproxy, perAppZapretCfg, zapretEnabled, - subscriptionTagsList, + subscriptionTagsFile, perAppRoutingProfilesFile, proxychainsConfigFile, proxychainsQuietArg, @@ -17,7 +17,7 @@ clashApi = "http://127.0.0.1:${toString singBoxCfg.clashApiPort}"; selection = singBoxCfg.selection; inherit - subscriptionTagsList + subscriptionTagsFile perAppRoutingProfilesFile proxychainsConfigFile proxychainsQuietArg diff --git a/modules/proxy-suite/service/default.nix b/modules/proxy-suite/service/default.nix index 38560d1..4caf9d8 100644 --- a/modules/proxy-suite/service/default.nix +++ b/modules/proxy-suite/service/default.nix @@ -45,6 +45,7 @@ let userControlCfg builtinTags outboundTags + subscriptionTags invalidRoutingTargets polkit scripts @@ -101,7 +102,7 @@ in inherit singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy; perAppZapretCfg = perAppZapretCfg; tgWsProxyCfg = cfg.tgWsProxy; - inherit builtinTags outboundTags invalidRoutingTargets; + inherit builtinTags outboundTags subscriptionTags invalidRoutingTargets; inherit (perAppRouting) effectivePerAppRoutingProfileNames hasProxychainsProfiles diff --git a/modules/proxy-suite/service/scripts.nix b/modules/proxy-suite/service/scripts.nix index 187e989..d2fe406 100644 --- a/modules/proxy-suite/service/scripts.nix +++ b/modules/proxy-suite/service/scripts.nix @@ -17,6 +17,9 @@ let globalTproxy = singBoxCfg.tproxy; + subscriptionCacheDir = "/var/lib/proxy-suite/subscriptions"; + + mkSubscriptionCacheFile = sub: "${subscriptionCacheDir}/${sub.tag}.json"; mkSubscriptionUrlSource = sub: @@ -76,10 +79,13 @@ let # Fetches subscription into the cache on first run; subsequent starts use the cache. mkSubscriptionBlock = sub: routingMark: + let + cacheFile = mkSubscriptionCacheFile sub; + in '' # subscription: ${sub.tag} - CACHE_DIR="/var/lib/proxy-suite/subscriptions" - CACHE_FILE="$CACHE_DIR/${lib.escapeShellArg sub.tag}.json" + CACHE_DIR="${subscriptionCacheDir}" + CACHE_FILE="${cacheFile}" if [ ! -f "$CACHE_FILE" ]; then mkdir -p "$CACHE_DIR" if ${mkSubscriptionFetchCommand sub} > "$CACHE_FILE.tmp"; then @@ -195,13 +201,15 @@ let # Shell code to fetch a single subscription into the cache (used in update service). mkSubscriptionFetchBlock = sub: + let + cacheFile = mkSubscriptionCacheFile sub; + in '' - if ${mkSubscriptionFetchCommand sub} > "$CACHE_DIR/${lib.escapeShellArg sub.tag}.json.tmp"; then - mv "$CACHE_DIR/${lib.escapeShellArg sub.tag}.json.tmp" \ - "$CACHE_DIR/${lib.escapeShellArg sub.tag}.json" + if ${mkSubscriptionFetchCommand sub} > "${cacheFile}.tmp"; then + mv "${cacheFile}.tmp" "${cacheFile}" echo "Updated subscription: ${sub.tag}" else - rm -f "$CACHE_DIR/${lib.escapeShellArg sub.tag}.json.tmp" + rm -f "${cacheFile}.tmp" echo "proxy-suite: failed to update subscription '${sub.tag}'" >&2 FAILED=1 fi @@ -209,23 +217,39 @@ let subscriptionUpdateScript = pkgs.writeShellScript "proxy-suite-subscription-update" '' set -euo pipefail - CACHE_DIR="/var/lib/proxy-suite/subscriptions" + CACHE_DIR="${subscriptionCacheDir}" mkdir -p "$CACHE_DIR" FAILED=0 + SOCKS_WAS_ACTIVE=0 + TUN_WAS_ACTIVE=0 + PER_APP_TUN_WAS_ACTIVE=0 + + systemctl is-active --quiet proxy-suite-socks && SOCKS_WAS_ACTIVE=1 || true + systemctl is-active --quiet proxy-suite-tun && TUN_WAS_ACTIVE=1 || true + systemctl is-active --quiet proxy-suite-per-app-tun && PER_APP_TUN_WAS_ACTIVE=1 || true ${lib.concatMapStrings mkSubscriptionFetchBlock singBoxCfg.subscriptions} if [ "$FAILED" -eq 0 ]; then - systemctl restart proxy-suite-socks - systemctl is-active --quiet proxy-suite-tun && systemctl restart proxy-suite-tun || true + if [ "$SOCKS_WAS_ACTIVE" -eq 1 ]; then + systemctl restart proxy-suite-socks + fi + if [ "$TUN_WAS_ACTIVE" -eq 1 ]; then + systemctl restart proxy-suite-tun + fi + if [ "$PER_APP_TUN_WAS_ACTIVE" -eq 1 ]; then + systemctl restart proxy-suite-per-app-tun + fi fi exit "$FAILED" ''; hasSubscriptions = singBoxCfg.subscriptions != [ ]; - subscriptionTagsList = lib.concatStringsSep " " (map (sub: lib.escapeShellArg sub.tag) singBoxCfg.subscriptions); + subscriptionTagsFile = pkgs.writeText "proxy-suite-subscription-tags.json" ( + builtins.toJSON (map (sub: sub.tag) singBoxCfg.subscriptions) + ); in { inherit startSocks startTun startPerAppTun; - inherit subscriptionUpdateScript hasSubscriptions subscriptionTagsList; + inherit subscriptionUpdateScript hasSubscriptions subscriptionTagsFile; } diff --git a/nix/checks.nix b/nix/checks.nix index c6f6afe..fd031be 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -641,6 +641,24 @@ let }; } ]; + subscriptionOnlyProxyCtl = + packageByPattern subscriptionOnlyFixture.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"; + subscriptionOnlyWrapper = + builtins.readFile "${subscriptionOnlyProxyCtl}/bin/proxy-ctl"; + subscriptionOnlyScript = + subscriptionOnlyWrapper + + "\n" + + builtins.readFile "${subscriptionOnlyProxyCtl}/bin/.proxy-ctl-wrapped"; + subscriptionOnlyTags = + builtins.fromJSON ( + builtins.readFile ( + shellValueByPrefix subscriptionOnlyWrapper "export SUB_TAGS_FILE=" + ) + ); + subscriptionOnlyStartScript = + builtins.readFile subscriptionOnlyFixture.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart; + subscriptionOnlyUpdateScript = + builtins.readFile subscriptionOnlyFixture.config.systemd.services."proxy-suite-subscription-update".serviceConfig.ExecStart; subscriptionWithStaticFixture = evalProxySuite [ { @@ -666,6 +684,43 @@ let }; } ]; + duplicateSubscriptionTags = forceEval ( + (evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + singBox.subscriptions = [ + { + tag = "dup"; + url = "https://example.com/sub/one"; + } + { + tag = "dup"; + url = "https://example.com/sub/two"; + } + ]; + }; + } + ]).config.system.build.toplevel.drvPath + ); + + unsafeSubscriptionTag = forceEval ( + (evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + singBox.subscriptions = [ + { + tag = "bad/tag"; + url = "https://example.com/sub/token"; + } + ]; + }; + } + ]).config.system.build.toplevel.drvPath + ); # selection=first with subscriptions only – start script must rename first # subscription outbound to "proxy" so routing rules resolve at runtime. @@ -686,6 +741,28 @@ let }; } ]; + subscriptionPerAppTunFixture = evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + singBox = { + subscriptions = [ + { + tag = "community"; + url = "https://example.com/sub/token"; + } + ]; + tun.perApp.enable = true; + }; + perAppRouting.enable = true; + }; + } + ]; + subscriptionPerAppTunUpdateScript = + builtins.readFile ( + subscriptionPerAppTunFixture.config.systemd.services."proxy-suite-subscription-update".serviceConfig.ExecStart + ); subscriptionUrlFileFixture = evalProxySuite [ { @@ -1697,6 +1774,14 @@ let true ) + # -- subscription: wrapper exposes tags through a JSON file, not shell word splitting -- + ( + assert subscriptionOnlyTags == [ "community" ]; + assert pkgs.lib.hasInfix "export SUB_TAGS_FILE=" subscriptionOnlyScript; + assert !(pkgs.lib.hasInfix "SUB_TAGS_RAW" subscriptionOnlyScript); + true + ) + # -- subscription: update service is created when subscriptions are configured -- ( assert subscriptionOnlyFixture.config.systemd.services ? "proxy-suite-subscription-update"; @@ -1725,6 +1810,18 @@ let true ) + # -- subscription: duplicate tags fail because they are cache keys and tag prefixes -- + ( + assert duplicateSubscriptionTags.success == false; + true + ) + + # -- subscription: tag must be a filesystem-safe identifier -- + ( + assert unsafeSubscriptionTag.success == false; + true + ) + # -- subscription: selection=first renames first subscription outbound to "proxy" -- ( let @@ -1758,6 +1855,32 @@ let true ) + # -- subscription/runtime: generated cache paths use the raw validated tag, not shell-escaped quotes -- + ( + assert pkgs.lib.hasInfix ''CACHE_FILE="/var/lib/proxy-suite/subscriptions/community.json"'' subscriptionOnlyStartScript; + assert pkgs.lib.hasInfix ''/var/lib/proxy-suite/subscriptions/community.json.tmp'' subscriptionOnlyUpdateScript; + assert !(pkgs.lib.hasInfix "subscriptions/'community'.json" subscriptionOnlyStartScript); + assert !(pkgs.lib.hasInfix "subscriptions/'community'.json" subscriptionOnlyUpdateScript); + true + ) + + # -- subscription/runtime: refresh only restarts sing-box units that were already active -- + ( + assert pkgs.lib.hasInfix ''SOCKS_WAS_ACTIVE=0'' subscriptionOnlyUpdateScript; + assert pkgs.lib.hasInfix ''if [ "$SOCKS_WAS_ACTIVE" -eq 1 ]; then'' subscriptionOnlyUpdateScript; + assert pkgs.lib.hasInfix ''if [ "$TUN_WAS_ACTIVE" -eq 1 ]; then'' subscriptionOnlyUpdateScript; + assert pkgs.lib.hasInfix ''systemctl restart proxy-suite-socks'' subscriptionOnlyUpdateScript; + true + ) + + # -- subscription/runtime: refresh also handles the per-app TUN sing-box service when present -- + ( + assert pkgs.lib.hasInfix ''PER_APP_TUN_WAS_ACTIVE=0'' subscriptionPerAppTunUpdateScript; + assert pkgs.lib.hasInfix ''systemctl is-active --quiet proxy-suite-per-app-tun'' subscriptionPerAppTunUpdateScript; + assert pkgs.lib.hasInfix ''systemctl restart proxy-suite-per-app-tun'' subscriptionPerAppTunUpdateScript; + true + ) + # -- subscription: no update service/timer without subscriptions -- ( assert !(minimal.config.systemd.services ? "proxy-suite-subscription-update"); @@ -1839,6 +1962,8 @@ let ( assert pkgs.lib.hasInfix "help)" perAppRoutingProxychainsScript; assert pkgs.lib.hasInfix "show this help message" perAppRoutingProxychainsScript; + assert pkgs.lib.hasInfix "enable/disable the sing-box proxy stack" perAppRoutingProxychainsScript; + assert pkgs.lib.hasInfix "restart active sing-box services" perAppRoutingProxychainsScript; assert pkgs.lib.hasInfix "wrap -- " perAppRoutingProxychainsScript; assert pkgs.lib.hasInfix "apps" perAppRoutingProxychainsScript; true diff --git a/pkgs/proxy-ctl.nix b/pkgs/proxy-ctl.nix index c5fc619..3c926cc 100644 --- a/pkgs/proxy-ctl.nix +++ b/pkgs/proxy-ctl.nix @@ -6,7 +6,7 @@ { clashApi, selection, - subscriptionTagsList, + subscriptionTagsFile, perAppRoutingEnabled, perAppRoutingProxychainsEnabled, perAppRoutingTunEnabled, @@ -37,7 +37,7 @@ pkgs.symlinkJoin { ]}" \ --set CLASH_API ${lib.escapeShellArg clashApi} \ --set SELECTION ${lib.escapeShellArg selection} \ - --set SUB_TAGS_RAW ${lib.escapeShellArg subscriptionTagsList} \ + --set SUB_TAGS_FILE ${lib.escapeShellArg (toString subscriptionTagsFile)} \ --set PER_APP_ROUTING_ENABLED ${lib.escapeShellArg perAppRoutingEnabled} \ --set PER_APP_ROUTING_PROXYCHAINS_ENABLED ${lib.escapeShellArg perAppRoutingProxychainsEnabled} \ --set PER_APP_ROUTING_TUN_ENABLED ${lib.escapeShellArg perAppRoutingTunEnabled} \ diff --git a/pkgs/proxy-ctl.sh b/pkgs/proxy-ctl.sh index 9904af8..23afe33 100644 --- a/pkgs/proxy-ctl.sh +++ b/pkgs/proxy-ctl.sh @@ -1,10 +1,13 @@ set -euo pipefail # Wrapped binary provides runtime config through env vars. -SUB_TAGS_RAW="${SUB_TAGS_RAW:-}" +SUB_TAGS_FILE="${SUB_TAGS_FILE:-}" PROXYCHAINS_QUIET_ARG="${PROXYCHAINS_QUIET_ARG:-}" -# shellcheck disable=SC2206 -SUB_TAGS=(${SUB_TAGS_RAW}) +SUB_TAGS=() + +if [ -n "$SUB_TAGS_FILE" ] && [ -f "$SUB_TAGS_FILE" ]; then + mapfile -t SUB_TAGS < <(jq -r '.[]' "$SUB_TAGS_FILE") +fi ALL_SERVICES=( proxy-suite-socks @@ -22,7 +25,7 @@ _usage() { echo "Commands:" echo " help show this help message" echo " status [--tray] show status of all proxy-suite services" - echo " proxy on|off enable/disable the core SOCKS proxy" + echo " proxy on|off enable/disable the sing-box proxy stack" echo " tproxy on|off enable/disable TProxy transparent mode" echo " tun on|off enable/disable TUN mode" echo " zapret on|off enable/disable zapret-discord-youtube" @@ -33,7 +36,7 @@ _usage() { echo " apps list configured per-app routing profiles" echo " wrap -- run a command via a perAppRouting profile" echo " subscription list show subscriptions, cache age, and proxy count" - echo " subscription update force-refresh all subscription caches and restart" + echo " subscription update force-refresh all subscription caches and restart active sing-box services" exit "$status" }