From af2121412b43b4959555700ef118cfa5097d6777 Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Sat, 18 Apr 2026 12:47:38 +0500 Subject: [PATCH] proxy-ctl movement, generate help message --- README.md | 30 +- flake.nix | 49 ++- modules/proxy-suite/service/control.nix | 38 ++ modules/proxy-suite/service/default.nix | 20 +- .../proxy-suite/service/per-app-routing.nix | 382 +----------------- nix/checks.nix | 108 +++-- nix/readme-doc.nix | 70 ++++ pkgs/proxy-ctl.nix | 50 +++ pkgs/proxy-ctl.sh | 354 ++++++++++++++++ 9 files changed, 661 insertions(+), 440 deletions(-) create mode 100644 modules/proxy-suite/service/control.nix create mode 100644 nix/readme-doc.nix create mode 100644 pkgs/proxy-ctl.nix create mode 100644 pkgs/proxy-ctl.sh diff --git a/README.md b/README.md index a825644..4d95ed3 100644 --- a/README.md +++ b/README.md @@ -118,4 +118,32 @@ services.proxy-suite = { To see options and documentation, check out the [options reference](./docs/options.md). -Options are autogenerated from [options/default.nix](./modules/proxy-suite/options/default.nix) using `nix run .#update-options-doc`. +Options and generated README help are refreshed with `nix run .#update-docs`. + +--- + +## `proxy-ctl help` + +Generated from current `proxy-ctl help` output. + + +```text +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 + tproxy on|off enable/disable TProxy transparent mode + tun on|off enable/disable TUN mode + zapret on|off enable/disable zapret-discord-youtube + restart restart active global proxy-suite services + logs [service] follow service logs (default: proxy-suite-socks) + outbounds list outbounds and current selection + select switch to a specific outbound (selector mode) + 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 +``` + diff --git a/flake.nix b/flake.nix index d9cdfe6..979a818 100644 --- a/flake.nix +++ b/flake.nix @@ -26,6 +26,9 @@ mkOptionsDoc = import ./nix/options-doc.nix { inherit nixpkgs pkgsFor proxySuiteModule; }; + mkReadmeDoc = import ./nix/readme-doc.nix { + inherit nixpkgs pkgsFor proxySuiteModule; + }; in { # Main module – bakes in the zapret flake so consumers don't need it as a separate input. @@ -46,15 +49,8 @@ let pkgs = pkgsFor system; suitePkgs = import ./pkgs/default.nix { inherit pkgs; }; - in - { - inherit (suitePkgs) - tg-ws-proxy - proxy-suite-tray - ; - optionsDoc = mkOptionsDoc system; - update-options-doc = pkgs.writeShellApplication { - name = "update-options-doc"; + updateDocs = pkgs.writeShellApplication { + name = "update-docs"; runtimeInputs = [ pkgs.coreutils pkgs.git @@ -70,21 +66,45 @@ exit 1 fi - output_path="$(nix build --no-link --print-out-paths "''${repo_root}#optionsDoc")" - target_path="''${repo_root}/docs/options.md" + options_output_path="$(nix build --no-link --print-out-paths "''${repo_root}#optionsDoc")" + readme_output_path="$(nix build --no-link --print-out-paths "''${repo_root}#readmeDoc")" - install -Dm644 "''${output_path}" "''${target_path}" - echo "updated ''${target_path}" + install -Dm644 "''${options_output_path}" "''${repo_root}/docs/options.md" + install -Dm644 "''${readme_output_path}" "''${repo_root}/README.md" + + echo "updated ''${repo_root}/docs/options.md" + echo "updated ''${repo_root}/README.md" + ''; + }; + in + { + inherit (suitePkgs) + tg-ws-proxy + proxy-suite-tray + ; + optionsDoc = mkOptionsDoc system; + readmeDoc = mkReadmeDoc system; + update-docs = updateDocs; + update-options-doc = pkgs.writeShellApplication { + name = "update-options-doc"; + runtimeInputs = [ updateDocs ]; + text = '' + exec ${updateDocs}/bin/update-docs "$@" ''; }; } ); apps = forAll (system: { + update-docs = { + type = "app"; + program = "${self.packages.${system}.update-docs}/bin/update-docs"; + meta.description = "Update generated docs and README help from current NixOS module outputs"; + }; update-options-doc = { type = "app"; program = "${self.packages.${system}.update-options-doc}/bin/update-options-doc"; - meta.description = "Update docs/options.md from the current NixOS module option declarations"; + meta.description = "Compatibility alias for update-docs"; }; }); @@ -98,6 +118,7 @@ zapret ; generatedOptionsDoc = mkOptionsDoc system; + generatedReadmeDoc = mkReadmeDoc system; } ); }; diff --git a/modules/proxy-suite/service/control.nix b/modules/proxy-suite/service/control.nix new file mode 100644 index 0000000..00892d0 --- /dev/null +++ b/modules/proxy-suite/service/control.nix @@ -0,0 +1,38 @@ +# Builds proxy-ctl from global proxy-suite state and per-app routing metadata. +{ + lib, + pkgs, + singBoxCfg, + perAppRoutingCfg, + perAppRoutingTun, + perAppRoutingTproxy, + perAppZapretCfg, + zapretEnabled, + subscriptionTagsList, + perAppRoutingProfilesFile, + proxychainsConfigFile, + proxychainsQuietArg, +}: + +let + mkProxyCtl = import ../../../pkgs/proxy-ctl.nix { + inherit lib pkgs; + }; +in +{ + proxyCtl = mkProxyCtl { + clashApi = "http://127.0.0.1:${toString singBoxCfg.clashApiPort}"; + selection = singBoxCfg.selection; + inherit + subscriptionTagsList + perAppRoutingProfilesFile + proxychainsConfigFile + proxychainsQuietArg + ; + perAppRoutingEnabled = if perAppRoutingCfg.enable then "1" else "0"; + perAppRoutingProxychainsEnabled = if perAppRoutingCfg.proxychains.enable then "1" else "0"; + perAppRoutingTunEnabled = if perAppRoutingTun.enable then "1" else "0"; + perAppRoutingTproxyEnabled = if perAppRoutingTproxy.enable then "1" else "0"; + perAppRoutingZapretEnabled = if (perAppZapretCfg.enable && zapretEnabled) then "1" else "0"; + }; +} diff --git a/modules/proxy-suite/service/default.nix b/modules/proxy-suite/service/default.nix index ccb493b..9cb7f46 100644 --- a/modules/proxy-suite/service/default.nix +++ b/modules/proxy-suite/service/default.nix @@ -37,11 +37,6 @@ let jq = "${pkgs.jq}/bin/jq"; python3 = "${pkgs.python3}/bin/python3"; singBox = "${pkgs.sing-box}/bin/sing-box"; - proxychains4 = "${pkgs.proxychains-ng}/bin/proxychains4"; - systemdRun = "${pkgs.systemd}/bin/systemd-run"; - systemctl = "${pkgs.systemd}/bin/systemctl"; - journalctl = "${pkgs.systemd}/bin/journalctl"; - idBin = "${pkgs.coreutils}/bin/id"; grepBin = "${pkgs.gnugrep}/bin/grep"; awk = "${pkgs.gawk}/bin/awk"; sleepBin = "${pkgs.coreutils}/bin/sleep"; @@ -70,14 +65,21 @@ let inherit perAppTunChainFile perAppTproxyRulesFile perAppZapretRulesFile; inherit ip nft; inherit awk grepBin findBin headBin seqBin sleepBin; - inherit jq proxychains4 systemdRun systemctl journalctl idBin; - clashApi = "http://127.0.0.1:${toString singBoxCfg.clashApiPort}"; - selection = singBoxCfg.selection; + }; + + control = import ./control.nix { + inherit lib pkgs singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy perAppZapretCfg; + zapretEnabled = cfg.zapret.enable; inherit (scripts) subscriptionTagsList; + inherit (perAppRouting) + perAppRoutingProfilesFile + proxychainsConfigFile + proxychainsQuietArg + ; }; in { - environment.systemPackages = [ perAppRouting.proxyCtl ]; + environment.systemPackages = [ control.proxyCtl ]; # nftables must be on for TProxy to work. networking.nftables.enable = lib.mkIf ( diff --git a/modules/proxy-suite/service/per-app-routing.nix b/modules/proxy-suite/service/per-app-routing.nix index 0abbf5d..9ec265b 100644 --- a/modules/proxy-suite/service/per-app-routing.nix +++ b/modules/proxy-suite/service/per-app-routing.nix @@ -1,4 +1,4 @@ -# App routing backend infrastructure (TUN, TProxy, zapret) and the proxy-ctl management tool. +# App routing backend infrastructure (TUN, TProxy, zapret). { lib, pkgs, @@ -19,15 +19,6 @@ headBin, seqBin, sleepBin, - jq, - proxychains4, - systemdRun, - systemctl, - journalctl, - idBin, - clashApi, - selection, - subscriptionTagsList, }: let @@ -257,376 +248,6 @@ let ExecStop = "${pkgs.coreutils}/bin/true"; }; }; - - proxyCtl = pkgs.writeShellApplication { - name = "proxy-ctl"; - runtimeInputs = with pkgs; [ - coreutils - curl - gawk - gnugrep - jq - proxychains-ng - systemd - ]; - text = '' - # Embedded at build time from module config - CLASH_API="${clashApi}" - SELECTION="${selection}" - SUB_TAGS=(${subscriptionTagsList}) - PER_APP_ROUTING_ENABLED="${if perAppRoutingCfg.enable then "1" else "0"}" - PER_APP_ROUTING_PROXYCHAINS_ENABLED="${if perAppRoutingCfg.proxychains.enable then "1" else "0"}" - PER_APP_ROUTING_TUN_ENABLED="${if perAppRoutingTun.enable then "1" else "0"}" - PER_APP_ROUTING_TPROXY_ENABLED="${if perAppRoutingTproxy.enable then "1" else "0"}" - PER_APP_ROUTING_ZAPRET_ENABLED="${if (perAppZapretCfg.enable && cfg.zapret.enable) then "1" else "0"}" - PER_APP_ROUTING_PROFILES_FILE="${perAppRoutingProfilesFile}" - PROXYCHAINS_CONFIG="${proxychainsConfigFile}" - PER_APP_TUN_SLICE_BASE="proxy-suite-per-app-tun" - PER_APP_TPROXY_SLICE_BASE="proxy-suite-per-app-tproxy" - PER_APP_ZAPRET_SLICE_BASE="proxy-suite-per-app-zapret" - - ALL_SERVICES=( - proxy-suite-socks - proxy-suite-tproxy - proxy-suite-tun - proxy-suite-tg-ws-proxy - proxy-suite-zapret-vm-exempt - zapret-discord-youtube - ) - - _usage() { - echo "Usage: proxy-ctl [args]" - echo "" - echo "Commands:" - echo " status [--tray] show status of all proxy-suite services" - echo " proxy on|off enable/disable the core SOCKS proxy" - 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" - echo " restart restart active global proxy-suite services" - echo " logs [service] follow service logs (default: proxy-suite-socks)" - echo " outbounds list outbounds and current selection" - echo " select switch to a specific outbound (selector mode)" - echo " apps list configured per-app routing profiles" - echo " wrap -- run a command via an perAppRouting profile" - echo " subscription list show subscriptions, cache age, and proxy count" - echo " subscription update force-refresh all subscription caches and restart" - exit 1 - } - - _svc_status() { - local svc="$1" - if ! systemctl cat "$svc" &>/dev/null; then - return - fi - local state - state=$(systemctl is-active "$svc" 2>/dev/null || true) - printf " %-44s %s\n" "$svc" "$state" - } - - _bool() { - if "$@"; then - printf true - else - printf false - fi - } - - _svc_exists() { - systemctl cat "$1" &>/dev/null - } - - _svc_active() { - systemctl is-active --quiet "$1" - } - - _status_tray() { - printf 'socks_available=%s\n' "$(_bool _svc_exists proxy-suite-socks)" - printf 'socks_active=%s\n' "$(_bool _svc_active proxy-suite-socks)" - printf 'tproxy_available=%s\n' "$(_bool _svc_exists proxy-suite-tproxy)" - printf 'tproxy_active=%s\n' "$(_bool _svc_active proxy-suite-tproxy)" - printf 'tun_available=%s\n' "$(_bool _svc_exists proxy-suite-tun)" - printf 'tun_active=%s\n' "$(_bool _svc_active proxy-suite-tun)" - printf 'zapret_available=%s\n' "$(_bool _svc_exists zapret-discord-youtube)" - printf 'zapret_active=%s\n' "$(_bool _svc_active zapret-discord-youtube)" - printf 'subscription_update_available=%s\n' "$(_bool _svc_exists proxy-suite-subscription-update)" - } - - _ensure_app_routing() { - if [ "$PER_APP_ROUTING_ENABLED" != "1" ]; then - echo "perAppRouting is not enabled in services.proxy-suite.perAppRouting." - exit 1 - fi - } - - _profile_route() { - local profile="$1" - ${jq} -r --arg name "$profile" '.[] | select(.name == $name) | .route' "$PER_APP_ROUTING_PROFILES_FILE" - } - - _scope_running() { - ${systemctl} --user list-units --type=scope --state=running --plain --no-legend "$1-*" \ - | ${grepBin} -q . - } - - _any_user_active() { - ${systemctl} list-units --type=service --state=active --plain --no-legend "$1*.service" \ - | ${grepBin} -q . - } - - _check_no_global_proxy() { - if ${systemctl} is-active --quiet proxy-suite-tun.service; then - echo "Global proxy-suite-tun.service is active. Stop it before using route=$1 profiles." - exit 1 - fi - if ${systemctl} is-active --quiet proxy-suite-tproxy.service; then - echo "Global proxy-suite-tproxy.service is active. Stop it before using route=$1 profiles." - exit 1 - fi - } - - # _wrap_slice [cmd...] - _wrap_slice() { - local slice_base="$1" enabled="$2" disabled_msg="$3" backend_svc="$4" - shift 4 - if [ "$enabled" != "1" ]; then - echo "$disabled_msg" - exit 1 - fi - local uid; uid="$(${idBin} -u)" - local scope_unit="$slice_base-''${profile}-$$" - local anchor_unit="$slice_base-anchor.service" - local user_svc="$slice_base-user@$uid.service" - - ${systemctl} --user start "$anchor_unit" - ${systemctl} start "$backend_svc" - ${systemctl} start "$user_svc" - - local status=0 - ${systemdRun} --user --scope --quiet --collect --same-dir \ - --slice="$slice_base" --unit="$scope_unit" "$@" || status=$? - - if ! _scope_running "$slice_base"; then - ${systemctl} stop "$user_svc" || true - ${systemctl} --user stop "$anchor_unit" || true - if ! _any_user_active "$slice_base-user@"; then - ${systemctl} stop "$backend_svc" || true - fi - fi - exit "$status" - } - - cmd="''${1:-status}" - shift || true - - case "$cmd" in - status) - if [ "''${1:-}" = "--tray" ]; then - _status_tray - else - echo "proxy-suite services:" - for svc in "''${ALL_SERVICES[@]}"; do - _svc_status "$svc" - done - fi - ;; - - proxy) - case "''${1:-on}" in - on) - systemctl start proxy-suite-socks - ;; - off) - systemctl stop proxy-suite-tproxy || true - systemctl stop proxy-suite-tun || true - systemctl stop proxy-suite-socks - ;; - *) - echo "Usage: proxy-ctl proxy on|off"; exit 1 ;; - esac - ;; - - tproxy) - case "''${1:-on}" in - on) systemctl start proxy-suite-tproxy ;; - off) systemctl stop proxy-suite-tproxy ;; - *) echo "Usage: proxy-ctl tproxy on|off"; exit 1 ;; - esac - ;; - - tun) - case "''${1:-on}" in - on) systemctl start proxy-suite-tun ;; - off) systemctl stop proxy-suite-tun ;; - *) echo "Usage: proxy-ctl tun on|off"; exit 1 ;; - esac - ;; - - zapret) - case "''${1:-on}" in - on) systemctl start zapret-discord-youtube ;; - off) systemctl stop zapret-discord-youtube ;; - *) echo "Usage: proxy-ctl zapret on|off"; exit 1 ;; - esac - ;; - - restart) - systemctl restart proxy-suite-socks - if systemctl is-active --quiet proxy-suite-tproxy; then - systemctl restart proxy-suite-tproxy - fi - if systemctl is-active --quiet proxy-suite-tun; then - systemctl restart proxy-suite-tun - fi - if systemctl is-active --quiet zapret-discord-youtube; then - systemctl restart zapret-discord-youtube - fi - ;; - - logs) - svc="''${1:-proxy-suite-socks}" - shift || true - exec journalctl -fu "$svc" "$@" - ;; - - outbounds) - data=$(curl -sf "$CLASH_API/proxies/proxy" 2>/dev/null) || { - echo "Clash API not available." - echo " - Make sure proxy-suite-socks is running" - echo " - selection must be 'selector' or 'urltest' (currently: $SELECTION)" - exit 1 - } - now=$(echo "$data" | jq -r '.now // "n/a"') - ob_type=$(echo "$data" | jq -r '.type // "n/a"') - echo "Type: $ob_type" - echo "Current: $now" - echo "" - echo "Available:" - echo "$data" | jq -r '.all[]? | " " + .' - ;; - - select) - tag="''${1:?Usage: proxy-ctl select }" - if [ "$SELECTION" != "selector" ]; then - echo "selection must be 'selector' (currently: $SELECTION)" - exit 1 - fi - payload=$(jq -cn --arg name "$tag" '{name:$name}') - if curl -sf -X PUT "$CLASH_API/proxies/proxy" \ - -H "Content-Type: application/json" \ - -d "$payload" >/dev/null; then - echo "Switched to: $tag" - else - echo "Failed – is proxy-suite-socks running with selector mode?" - exit 1 - fi - ;; - - apps) - _ensure_app_routing - if [ "$(${jq} 'length' "$PER_APP_ROUTING_PROFILES_FILE")" -eq 0 ]; then - echo "No perAppRouting profiles configured." - else - printf " %-24s %s\n" "PROFILE" "ROUTE" - ${jq} -r '.[] | " " + (.name | tostring) + "\t" + (.route | tostring)' \ - "$PER_APP_ROUTING_PROFILES_FILE" \ - | while IFS=$'\t' read -r profile route; do - printf "%-26s %s\n" "$profile" "$route" - done - fi - ;; - - wrap) - profile="''${1:?Usage: proxy-ctl wrap -- [args...]}" - shift || true - if [ "''${1:-}" = "--" ]; then - shift - fi - if [ "$#" -eq 0 ]; then - echo "Usage: proxy-ctl wrap -- [args...]" - exit 1 - fi - - _ensure_app_routing - route="$(_profile_route "$profile")" - if [ -z "$route" ] || [ "$route" = "null" ]; then - echo "Unknown perAppRouting profile: $profile" - exit 1 - fi - - case "$route" in - direct) - exec "$@" - ;; - proxychains) - if [ "$PER_APP_ROUTING_PROXYCHAINS_ENABLED" != "1" ]; then - echo "Profile '$profile' uses route=proxychains, but perAppRouting.proxychains.enable is false." - exit 1 - fi - exec ${proxychains4} ${proxychainsQuietArg}-f "$PROXYCHAINS_CONFIG" "$@" - ;; - tun) - _wrap_slice "$PER_APP_TUN_SLICE_BASE" "$PER_APP_ROUTING_TUN_ENABLED" \ - "Profile '$profile' uses route=tun, but singBox.tun.perApp.enable is false." \ - "proxy-suite-per-app-tun.service" "$@" - ;; - tproxy) - _check_no_global_proxy tproxy - _wrap_slice "$PER_APP_TPROXY_SLICE_BASE" "$PER_APP_ROUTING_TPROXY_ENABLED" \ - "Profile '$profile' uses route=tproxy, but singBox.tproxy.perApp.enable is false." \ - "proxy-suite-per-app-tproxy.service" "$@" - ;; - zapret) - _check_no_global_proxy zapret - _wrap_slice "$PER_APP_ZAPRET_SLICE_BASE" "$PER_APP_ROUTING_ZAPRET_ENABLED" \ - "Profile '$profile' uses route=zapret, but zapret.perApp.enable or zapret.enable is false." \ - "proxy-suite-per-app-zapret.service" "$@" - ;; - *) - echo "Route backend '$route' is not implemented in this build." - exit 1 - ;; - esac - ;; - - subscription) - subcmd="''${1:-list}" - shift || true - case "$subcmd" in - list) - CACHE_DIR="/var/lib/proxy-suite/subscriptions" - if [ ''${#SUB_TAGS[@]} -eq 0 ]; then - echo "No subscriptions configured." - else - printf " %-30s %-22s %s\n" "TAG" "LAST UPDATED" "PROXIES" - for t in "''${SUB_TAGS[@]}"; do - cache="$CACHE_DIR/$t.json" - if [ -f "$cache" ]; then - age=$(date -r "$cache" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || echo "unknown") - count=$(jq 'length' "$cache" 2>/dev/null || echo "?") - else - age="(no cache)" - count="-" - fi - printf " %-30s %-22s %s\n" "$t" "$age" "$count" - done - fi - ;; - update) - systemctl start proxy-suite-subscription-update - echo "Subscription update triggered. Follow with: proxy-ctl logs proxy-suite-subscription-update" - ;; - *) - echo "Usage: proxy-ctl subscription list|update"; exit 1 ;; - esac - ;; - - *) - _usage - ;; - esac - ''; - }; in { inherit @@ -653,6 +274,5 @@ in hasTunProfiles hasTproxyProfiles hasZapretProfiles - proxyCtl ; } diff --git a/nix/checks.nix b/nix/checks.nix index fa68361..2bde1a3 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -3,6 +3,7 @@ nixpkgs, proxySuiteModule, generatedOptionsDoc, + generatedReadmeDoc, zapret, }: @@ -117,6 +118,17 @@ let quotedValueByPrefix = text: prefix: pkgs.lib.removeSuffix "\"" (pkgs.lib.removePrefix prefix (lineByPrefix text prefix)); + shellValueByPrefix = + text: prefix: + let + value = pkgs.lib.removePrefix prefix (lineByPrefix text prefix); + in + if pkgs.lib.hasPrefix "'" value && pkgs.lib.hasSuffix "'" value then + pkgs.lib.removeSuffix "'" (pkgs.lib.removePrefix "'" value) + else if pkgs.lib.hasPrefix "\"" value && pkgs.lib.hasSuffix "\"" value then + pkgs.lib.removeSuffix "\"" (pkgs.lib.removePrefix "\"" value) + else + value; baseModule = { system.stateVersion = "26.05"; @@ -728,19 +740,22 @@ let }; } ]; + perAppRoutingProxychainsProxyCtl = + packageByPattern perAppRoutingProxychainsFixture.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"; + perAppRoutingProxychainsWrapper = + builtins.readFile "${perAppRoutingProxychainsProxyCtl}/bin/proxy-ctl"; perAppRoutingProxychainsScript = - builtins.readFile ( - "${packageByPattern perAppRoutingProxychainsFixture.config.environment.systemPackages - ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" - ); + perAppRoutingProxychainsWrapper + + "\n" + + builtins.readFile "${perAppRoutingProxychainsProxyCtl}/bin/.proxy-ctl-wrapped"; perAppRoutingProxychainsConfig = builtins.readFile ( - quotedValueByPrefix perAppRoutingProxychainsScript "PROXYCHAINS_CONFIG=\"" + shellValueByPrefix perAppRoutingProxychainsWrapper "export PROXYCHAINS_CONFIG=" ); perAppRoutingProxychainsProfiles = builtins.fromJSON ( builtins.readFile ( - quotedValueByPrefix perAppRoutingProxychainsScript "PER_APP_ROUTING_PROFILES_FILE=\"" + shellValueByPrefix perAppRoutingProxychainsWrapper "export PER_APP_ROUTING_PROFILES_FILE=" ) ); @@ -754,15 +769,18 @@ let }; } ]; + perAppRoutingDefaultProfilesProxyCtl = + packageByPattern perAppRoutingDefaultProfilesFixture.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"; + perAppRoutingDefaultProfilesWrapper = + builtins.readFile "${perAppRoutingDefaultProfilesProxyCtl}/bin/proxy-ctl"; perAppRoutingDefaultProfilesScript = - builtins.readFile ( - "${packageByPattern perAppRoutingDefaultProfilesFixture.config.environment.systemPackages - ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" - ); + perAppRoutingDefaultProfilesWrapper + + "\n" + + builtins.readFile "${perAppRoutingDefaultProfilesProxyCtl}/bin/.proxy-ctl-wrapped"; perAppRoutingDefaultProfiles = builtins.fromJSON ( builtins.readFile ( - quotedValueByPrefix perAppRoutingDefaultProfilesScript "PER_APP_ROUTING_PROFILES_FILE=\"" + shellValueByPrefix perAppRoutingDefaultProfilesWrapper "export PER_APP_ROUTING_PROFILES_FILE=" ) ); @@ -786,15 +804,18 @@ let }; } ]; + perAppRoutingNoDefaultProfilesProxyCtl = + packageByPattern perAppRoutingNoDefaultProfilesFixture.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"; + perAppRoutingNoDefaultProfilesWrapper = + builtins.readFile "${perAppRoutingNoDefaultProfilesProxyCtl}/bin/proxy-ctl"; perAppRoutingNoDefaultProfilesScript = - builtins.readFile ( - "${packageByPattern perAppRoutingNoDefaultProfilesFixture.config.environment.systemPackages - ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" - ); + perAppRoutingNoDefaultProfilesWrapper + + "\n" + + builtins.readFile "${perAppRoutingNoDefaultProfilesProxyCtl}/bin/.proxy-ctl-wrapped"; perAppRoutingNoDefaultProfiles = builtins.fromJSON ( builtins.readFile ( - quotedValueByPrefix perAppRoutingNoDefaultProfilesScript "PER_APP_ROUTING_PROFILES_FILE=\"" + shellValueByPrefix perAppRoutingNoDefaultProfilesWrapper "export PER_APP_ROUTING_PROFILES_FILE=" ) ); @@ -822,15 +843,18 @@ let }; } ]; + perAppRoutingTunProxyCtl = + packageByPattern perAppRoutingTunFixture.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"; + perAppRoutingTunWrapper = + builtins.readFile "${perAppRoutingTunProxyCtl}/bin/proxy-ctl"; perAppRoutingTunScript = - builtins.readFile ( - "${packageByPattern perAppRoutingTunFixture.config.environment.systemPackages - ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" - ); + perAppRoutingTunWrapper + + "\n" + + builtins.readFile "${perAppRoutingTunProxyCtl}/bin/.proxy-ctl-wrapped"; perAppRoutingTunProfiles = builtins.fromJSON ( builtins.readFile ( - quotedValueByPrefix perAppRoutingTunScript "PER_APP_ROUTING_PROFILES_FILE=\"" + shellValueByPrefix perAppRoutingTunWrapper "export PER_APP_ROUTING_PROFILES_FILE=" ) ); perAppRoutingTunStartScript = @@ -893,15 +917,18 @@ let }; } ]; + perAppRoutingTproxyProxyCtl = + packageByPattern perAppRoutingTproxyFixture.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"; + perAppRoutingTproxyWrapper = + builtins.readFile "${perAppRoutingTproxyProxyCtl}/bin/proxy-ctl"; perAppRoutingTproxyScript = - builtins.readFile ( - "${packageByPattern perAppRoutingTproxyFixture.config.environment.systemPackages - ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" - ); + perAppRoutingTproxyWrapper + + "\n" + + builtins.readFile "${perAppRoutingTproxyProxyCtl}/bin/.proxy-ctl-wrapped"; perAppRoutingTproxyProfiles = builtins.fromJSON ( builtins.readFile ( - quotedValueByPrefix perAppRoutingTproxyScript "PER_APP_ROUTING_PROFILES_FILE=\"" + shellValueByPrefix perAppRoutingTproxyWrapper "export PER_APP_ROUTING_PROFILES_FILE=" ) ); perAppRoutingTproxyStartScript = @@ -1029,15 +1056,18 @@ let }; } ]; + perAppRoutingZapretProxyCtl = + packageByPattern perAppRoutingZapretFixture.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"; + perAppRoutingZapretWrapper = + builtins.readFile "${perAppRoutingZapretProxyCtl}/bin/proxy-ctl"; perAppRoutingZapretScript = - builtins.readFile ( - "${packageByPattern perAppRoutingZapretFixture.config.environment.systemPackages - ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" - ); + perAppRoutingZapretWrapper + + "\n" + + builtins.readFile "${perAppRoutingZapretProxyCtl}/bin/.proxy-ctl-wrapped"; perAppRoutingZapretProfiles = builtins.fromJSON ( builtins.readFile ( - quotedValueByPrefix perAppRoutingZapretScript "PER_APP_ROUTING_PROFILES_FILE=\"" + shellValueByPrefix perAppRoutingZapretWrapper "export PER_APP_ROUTING_PROFILES_FILE=" ) ); perAppRoutingZapretStartScript = @@ -1803,6 +1833,8 @@ let # -- perAppRouting: proxy-ctl script embeds wrap/apps commands -- ( + assert pkgs.lib.hasInfix "help)" perAppRoutingProxychainsScript; + assert pkgs.lib.hasInfix "show this help message" perAppRoutingProxychainsScript; assert pkgs.lib.hasInfix "wrap -- " perAppRoutingProxychainsScript; assert pkgs.lib.hasInfix "apps" perAppRoutingProxychainsScript; true @@ -1818,7 +1850,8 @@ let # -- perAppRouting: generated proxy-ctl script dispatches through proxychains4 -- ( - assert pkgs.lib.hasInfix "proxychains4 -q -f \"$PROXYCHAINS_CONFIG\" \"$@\"" perAppRoutingProxychainsScript; + assert pkgs.lib.hasInfix "export PROXYCHAINS_QUIET_ARG='-q'" perAppRoutingProxychainsScript; + assert pkgs.lib.hasInfix "exec proxychains4 $PROXYCHAINS_QUIET_ARG -f \"$PROXYCHAINS_CONFIG\" \"$@\"" perAppRoutingProxychainsScript; true ) @@ -1826,7 +1859,7 @@ let ( assert pkgs.lib.hasInfix "PER_APP_ROUTING_TUN_ENABLED" perAppRoutingTunScript; assert pkgs.lib.hasInfix "systemd-run --user --scope --quiet --collect --same-dir" perAppRoutingTunScript; - assert pkgs.lib.hasInfix ''PER_APP_TUN_SLICE_BASE="proxy-suite-per-app-tun"'' perAppRoutingTunScript; + assert pkgs.lib.hasInfix ''_wrap_slice "proxy-suite-per-app-tun" "$PER_APP_ROUTING_TUN_ENABLED"'' perAppRoutingTunScript; assert pkgs.lib.hasInfix "$slice_base-user@$uid.service" perAppRoutingTunScript; true ) @@ -1834,7 +1867,7 @@ let # -- perAppRouting: generated proxy-ctl script dispatches tproxy profiles through systemd slices -- ( assert pkgs.lib.hasInfix "PER_APP_ROUTING_TPROXY_ENABLED" perAppRoutingTproxyScript; - assert pkgs.lib.hasInfix ''PER_APP_TPROXY_SLICE_BASE="proxy-suite-per-app-tproxy"'' perAppRoutingTproxyScript; + assert pkgs.lib.hasInfix ''_wrap_slice "proxy-suite-per-app-tproxy" "$PER_APP_ROUTING_TPROXY_ENABLED"'' perAppRoutingTproxyScript; assert pkgs.lib.hasInfix ''$slice_base-''${profile}-$$'' perAppRoutingTproxyScript; true ) @@ -1842,7 +1875,7 @@ let # -- perAppRouting: generated proxy-ctl script dispatches zapret profiles through systemd slices -- ( assert pkgs.lib.hasInfix "PER_APP_ROUTING_ZAPRET_ENABLED" perAppRoutingZapretScript; - assert pkgs.lib.hasInfix ''PER_APP_ZAPRET_SLICE_BASE="proxy-suite-per-app-zapret"'' perAppRoutingZapretScript; + assert pkgs.lib.hasInfix ''_wrap_slice "proxy-suite-per-app-zapret" "$PER_APP_ROUTING_ZAPRET_ENABLED"'' perAppRoutingZapretScript; assert pkgs.lib.hasInfix ''$slice_base-''${profile}-$$'' perAppRoutingZapretScript; true ) @@ -2212,4 +2245,9 @@ in diff -u ${../docs/options.md} ${generatedOptionsDoc} touch "$out" ''; + + readme-doc = pkgs.runCommand "proxy-suite-readme-doc-check" { nativeBuildInputs = [ pkgs.diffutils ]; } '' + diff -u ${../README.md} ${generatedReadmeDoc} + touch "$out" + ''; } diff --git a/nix/readme-doc.nix b/nix/readme-doc.nix new file mode 100644 index 0000000..ddda44f --- /dev/null +++ b/nix/readme-doc.nix @@ -0,0 +1,70 @@ +{ + nixpkgs, + pkgsFor, + proxySuiteModule, +}: + +system: + +let + pkgs = pkgsFor system; + lib = pkgs.lib; + eval = import "${nixpkgs}/nixos/lib/eval-config.nix" { + inherit system; + modules = [ + proxySuiteModule + { + system.stateVersion = lib.trivial.release; + services.proxy-suite = { + enable = true; + singBox.outbounds = [ + { + tag = "primary"; + url = "http://proxy.example.com:8080"; + } + ]; + }; + } + ]; + }; + packageByPattern = + packages: pattern: + builtins.head ( + builtins.filter ( + pkg: builtins.match pattern (builtins.unsafeDiscardStringContext (toString pkg)) != null + ) packages + ); + proxyCtl = + packageByPattern eval.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"; +in +pkgs.runCommand "proxy-suite-README.md" { nativeBuildInputs = [ pkgs.python3 ]; } '' + src=${../README.md} + help_file="$TMPDIR/proxy-ctl-help.txt" + export src help_file + + ${proxyCtl}/bin/proxy-ctl help > "$help_file" + + python <<'PY' + from pathlib import Path + import os + + src = Path(os.environ["src"]) + help_file = Path(os.environ["help_file"]) + out = Path(os.environ["out"]) + + start_marker = "" + end_marker = "" + + readme = src.read_text() + help_text = help_file.read_text().rstrip("\n") + replacement = f"{start_marker}\n```text\n{help_text}\n```\n{end_marker}" + + try: + start = readme.index(start_marker) + end = readme.index(end_marker) + len(end_marker) + except ValueError as exc: + raise SystemExit(f"README markers missing: {exc}") + + out.write_text(readme[:start] + replacement + readme[end:]) + PY +'' diff --git a/pkgs/proxy-ctl.nix b/pkgs/proxy-ctl.nix new file mode 100644 index 0000000..c5fc619 --- /dev/null +++ b/pkgs/proxy-ctl.nix @@ -0,0 +1,50 @@ +{ + lib, + pkgs, +}: + +{ + clashApi, + selection, + subscriptionTagsList, + perAppRoutingEnabled, + perAppRoutingProxychainsEnabled, + perAppRoutingTunEnabled, + perAppRoutingTproxyEnabled, + perAppRoutingZapretEnabled, + perAppRoutingProfilesFile, + proxychainsConfigFile, + proxychainsQuietArg, +}: + +let + unwrapped = pkgs.writeShellScriptBin "proxy-ctl" (builtins.readFile ./proxy-ctl.sh); +in +pkgs.symlinkJoin { + name = "proxy-ctl"; + paths = [ unwrapped ]; + nativeBuildInputs = [ pkgs.makeWrapper ]; + postBuild = '' + wrapProgram "$out/bin/proxy-ctl" \ + --prefix PATH : "${lib.makeBinPath [ + pkgs.coreutils + pkgs.curl + pkgs.gawk + pkgs.gnugrep + pkgs.jq + pkgs.proxychains-ng + pkgs.systemd + ]}" \ + --set CLASH_API ${lib.escapeShellArg clashApi} \ + --set SELECTION ${lib.escapeShellArg selection} \ + --set SUB_TAGS_RAW ${lib.escapeShellArg subscriptionTagsList} \ + --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} \ + --set PER_APP_ROUTING_TPROXY_ENABLED ${lib.escapeShellArg perAppRoutingTproxyEnabled} \ + --set PER_APP_ROUTING_ZAPRET_ENABLED ${lib.escapeShellArg perAppRoutingZapretEnabled} \ + --set PER_APP_ROUTING_PROFILES_FILE ${lib.escapeShellArg (toString perAppRoutingProfilesFile)} \ + --set PROXYCHAINS_CONFIG ${lib.escapeShellArg (toString proxychainsConfigFile)} \ + --set PROXYCHAINS_QUIET_ARG ${lib.escapeShellArg (lib.removeSuffix " " proxychainsQuietArg)} + ''; +} diff --git a/pkgs/proxy-ctl.sh b/pkgs/proxy-ctl.sh new file mode 100644 index 0000000..9904af8 --- /dev/null +++ b/pkgs/proxy-ctl.sh @@ -0,0 +1,354 @@ +set -euo pipefail + +# Wrapped binary provides runtime config through env vars. +SUB_TAGS_RAW="${SUB_TAGS_RAW:-}" +PROXYCHAINS_QUIET_ARG="${PROXYCHAINS_QUIET_ARG:-}" +# shellcheck disable=SC2206 +SUB_TAGS=(${SUB_TAGS_RAW}) + +ALL_SERVICES=( + proxy-suite-socks + proxy-suite-tproxy + proxy-suite-tun + proxy-suite-tg-ws-proxy + proxy-suite-zapret-vm-exempt + zapret-discord-youtube +) + +_usage() { + local status="${1:-1}" + echo "Usage: proxy-ctl [args]" + echo "" + 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 " 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" + echo " restart restart active global proxy-suite services" + echo " logs [service] follow service logs (default: proxy-suite-socks)" + echo " outbounds list outbounds and current selection" + echo " select switch to a specific outbound (selector mode)" + 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" + exit "$status" +} + +_svc_status() { + local svc="$1" + if ! systemctl cat "$svc" &>/dev/null; then + return + fi + local state + state=$(systemctl is-active "$svc" 2>/dev/null || true) + printf " %-44s %s\n" "$svc" "$state" +} + +_bool() { + if "$@"; then + printf true + else + printf false + fi +} + +_svc_exists() { + systemctl cat "$1" &>/dev/null +} + +_svc_active() { + systemctl is-active --quiet "$1" +} + +_status_tray() { + printf 'socks_available=%s\n' "$(_bool _svc_exists proxy-suite-socks)" + printf 'socks_active=%s\n' "$(_bool _svc_active proxy-suite-socks)" + printf 'tproxy_available=%s\n' "$(_bool _svc_exists proxy-suite-tproxy)" + printf 'tproxy_active=%s\n' "$(_bool _svc_active proxy-suite-tproxy)" + printf 'tun_available=%s\n' "$(_bool _svc_exists proxy-suite-tun)" + printf 'tun_active=%s\n' "$(_bool _svc_active proxy-suite-tun)" + printf 'zapret_available=%s\n' "$(_bool _svc_exists zapret-discord-youtube)" + printf 'zapret_active=%s\n' "$(_bool _svc_active zapret-discord-youtube)" + printf 'subscription_update_available=%s\n' "$(_bool _svc_exists proxy-suite-subscription-update)" +} + +_ensure_app_routing() { + if [ "$PER_APP_ROUTING_ENABLED" != "1" ]; then + echo "perAppRouting is not enabled in services.proxy-suite.perAppRouting." + exit 1 + fi +} + +_profile_route() { + local profile="$1" + jq -r --arg name "$profile" '.[] | select(.name == $name) | .route' "$PER_APP_ROUTING_PROFILES_FILE" +} + +_scope_running() { + systemctl --user list-units --type=scope --state=running --plain --no-legend "$1-*" \ + | grep -q . +} + +_any_user_active() { + systemctl list-units --type=service --state=active --plain --no-legend "$1*.service" \ + | grep -q . +} + +_check_no_global_proxy() { + if systemctl is-active --quiet proxy-suite-tun.service; then + echo "Global proxy-suite-tun.service is active. Stop it before using route=$1 profiles." + exit 1 + fi + if systemctl is-active --quiet proxy-suite-tproxy.service; then + echo "Global proxy-suite-tproxy.service is active. Stop it before using route=$1 profiles." + exit 1 + fi +} + +# _wrap_slice [cmd...] +_wrap_slice() { + local slice_base="$1" enabled="$2" disabled_msg="$3" backend_svc="$4" + shift 4 + if [ "$enabled" != "1" ]; then + echo "$disabled_msg" + exit 1 + fi + local uid; uid="$(id -u)" + local scope_unit="$slice_base-${profile}-$$" + local anchor_unit="$slice_base-anchor.service" + local user_svc="$slice_base-user@$uid.service" + + systemctl --user start "$anchor_unit" + systemctl start "$backend_svc" + systemctl start "$user_svc" + + local status=0 + systemd-run --user --scope --quiet --collect --same-dir \ + --slice="$slice_base" --unit="$scope_unit" "$@" || status=$? + + if ! _scope_running "$slice_base"; then + systemctl stop "$user_svc" || true + systemctl --user stop "$anchor_unit" || true + if ! _any_user_active "$slice_base-user@"; then + systemctl stop "$backend_svc" || true + fi + fi + exit "$status" +} + +cmd="${1:-status}" +shift || true + +case "$cmd" in + help) + _usage 0 + ;; + + status) + if [ "${1:-}" = "--tray" ]; then + _status_tray + else + echo "proxy-suite services:" + for svc in "${ALL_SERVICES[@]}"; do + _svc_status "$svc" + done + fi + ;; + + proxy) + case "${1:-on}" in + on) + systemctl start proxy-suite-socks + ;; + off) + systemctl stop proxy-suite-tproxy || true + systemctl stop proxy-suite-tun || true + systemctl stop proxy-suite-socks + ;; + *) + echo "Usage: proxy-ctl proxy on|off"; exit 1 ;; + esac + ;; + + tproxy) + case "${1:-on}" in + on) systemctl start proxy-suite-tproxy ;; + off) systemctl stop proxy-suite-tproxy ;; + *) echo "Usage: proxy-ctl tproxy on|off"; exit 1 ;; + esac + ;; + + tun) + case "${1:-on}" in + on) systemctl start proxy-suite-tun ;; + off) systemctl stop proxy-suite-tun ;; + *) echo "Usage: proxy-ctl tun on|off"; exit 1 ;; + esac + ;; + + zapret) + case "${1:-on}" in + on) systemctl start zapret-discord-youtube ;; + off) systemctl stop zapret-discord-youtube ;; + *) echo "Usage: proxy-ctl zapret on|off"; exit 1 ;; + esac + ;; + + restart) + systemctl restart proxy-suite-socks + if systemctl is-active --quiet proxy-suite-tproxy; then + systemctl restart proxy-suite-tproxy + fi + if systemctl is-active --quiet proxy-suite-tun; then + systemctl restart proxy-suite-tun + fi + if systemctl is-active --quiet zapret-discord-youtube; then + systemctl restart zapret-discord-youtube + fi + ;; + + logs) + svc="${1:-proxy-suite-socks}" + shift || true + exec journalctl -fu "$svc" "$@" + ;; + + outbounds) + data=$(curl -sf "$CLASH_API/proxies/proxy" 2>/dev/null) || { + echo "Clash API not available." + echo " - Make sure proxy-suite-socks is running" + echo " - selection must be 'selector' or 'urltest' (currently: $SELECTION)" + exit 1 + } + now=$(echo "$data" | jq -r '.now // "n/a"') + ob_type=$(echo "$data" | jq -r '.type // "n/a"') + echo "Type: $ob_type" + echo "Current: $now" + echo "" + echo "Available:" + echo "$data" | jq -r '.all[]? | " " + .' + ;; + + select) + tag="${1:?Usage: proxy-ctl select }" + if [ "$SELECTION" != "selector" ]; then + echo "selection must be 'selector' (currently: $SELECTION)" + exit 1 + fi + payload=$(jq -cn --arg name "$tag" '{name:$name}') + if curl -sf -X PUT "$CLASH_API/proxies/proxy" \ + -H "Content-Type: application/json" \ + -d "$payload" >/dev/null; then + echo "Switched to: $tag" + else + echo "Failed – is proxy-suite-socks running with selector mode?" + exit 1 + fi + ;; + + apps) + _ensure_app_routing + if [ "$(jq 'length' "$PER_APP_ROUTING_PROFILES_FILE")" -eq 0 ]; then + echo "No perAppRouting profiles configured." + else + printf " %-24s %s\n" "PROFILE" "ROUTE" + jq -r '.[] | " " + (.name | tostring) + "\t" + (.route | tostring)' \ + "$PER_APP_ROUTING_PROFILES_FILE" \ + | while IFS=$'\t' read -r profile route; do + printf "%-26s %s\n" "$profile" "$route" + done + fi + ;; + + wrap) + profile="${1:?Usage: proxy-ctl wrap -- [args...]}" + shift || true + if [ "${1:-}" = "--" ]; then + shift + fi + if [ "$#" -eq 0 ]; then + echo "Usage: proxy-ctl wrap -- [args...]" + exit 1 + fi + + _ensure_app_routing + route="$(_profile_route "$profile")" + if [ -z "$route" ] || [ "$route" = "null" ]; then + echo "Unknown perAppRouting profile: $profile" + exit 1 + fi + + case "$route" in + direct) + exec "$@" + ;; + proxychains) + if [ "$PER_APP_ROUTING_PROXYCHAINS_ENABLED" != "1" ]; then + echo "Profile '$profile' uses route=proxychains, but perAppRouting.proxychains.enable is false." + exit 1 + fi + exec proxychains4 $PROXYCHAINS_QUIET_ARG -f "$PROXYCHAINS_CONFIG" "$@" + ;; + tun) + _wrap_slice "proxy-suite-per-app-tun" "$PER_APP_ROUTING_TUN_ENABLED" \ + "Profile '$profile' uses route=tun, but singBox.tun.perApp.enable is false." \ + "proxy-suite-per-app-tun.service" "$@" + ;; + tproxy) + _check_no_global_proxy tproxy + _wrap_slice "proxy-suite-per-app-tproxy" "$PER_APP_ROUTING_TPROXY_ENABLED" \ + "Profile '$profile' uses route=tproxy, but singBox.tproxy.perApp.enable is false." \ + "proxy-suite-per-app-tproxy.service" "$@" + ;; + zapret) + _check_no_global_proxy zapret + _wrap_slice "proxy-suite-per-app-zapret" "$PER_APP_ROUTING_ZAPRET_ENABLED" \ + "Profile '$profile' uses route=zapret, but zapret.perApp.enable or zapret.enable is false." \ + "proxy-suite-per-app-zapret.service" "$@" + ;; + *) + echo "Route backend '$route' is not implemented in this build." + exit 1 + ;; + esac + ;; + + subscription) + subcmd="${1:-list}" + shift || true + case "$subcmd" in + list) + CACHE_DIR="/var/lib/proxy-suite/subscriptions" + if [ ${#SUB_TAGS[@]} -eq 0 ]; then + echo "No subscriptions configured." + else + printf " %-30s %-22s %s\n" "TAG" "LAST UPDATED" "PROXIES" + for t in "${SUB_TAGS[@]}"; do + cache="$CACHE_DIR/$t.json" + if [ -f "$cache" ]; then + age=$(date -r "$cache" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || echo "unknown") + count=$(jq 'length' "$cache" 2>/dev/null || echo "?") + else + age="(no cache)" + count="-" + fi + printf " %-30s %-22s %s\n" "$t" "$age" "$count" + done + fi + ;; + update) + systemctl start proxy-suite-subscription-update + echo "Subscription update triggered. Follow with: proxy-ctl logs proxy-suite-subscription-update" + ;; + *) + echo "Usage: proxy-ctl subscription list|update"; exit 1 ;; + esac + ;; + + *) + _usage 1 + ;; +esac