From e6b92ecac790c8e2d8acabf8cf619601f08ed098 Mon Sep 17 00:00:00 2001 From: fufsob Date: Mon, 14 Sep 2026 09:55:24 +0000 Subject: [PATCH] chore: collapse packages --- modules/proxy-suite/amnezia-wg.nix | 21 +++++++++------ modules/proxy-suite/autoproxy-render.nix | 2 +- modules/proxy-suite/autoproxy.nix | 18 ++++++++----- modules/proxy-suite/config.nix | 12 ++++----- modules/proxy-suite/default.nix | 18 +++++++++++++ modules/proxy-suite/nftables.nix | 8 +++--- modules/proxy-suite/service/context.nix | 7 +++-- .../proxy-suite/service/control-scripts.nix | 8 +++--- .../per-app-routing/backend-scripts.nix | 10 +++---- .../service/per-app-routing/profiles.nix | 4 +-- .../service/per-app-routing/user-rules.nix | 4 +-- .../service/proxy-inbounds-scripts.nix | 15 +++++++---- .../proxy-suite/service/routing-scripts.nix | 8 +++--- .../service/script-blocks/outbounds.nix | 14 +++++----- .../service/script-blocks/subscriptions.nix | 4 +-- modules/proxy-suite/service/scripts.nix | 4 +-- modules/proxy-suite/service/start-scripts.nix | 6 +---- modules/proxy-suite/ssh-proxy.nix | 2 +- modules/proxy-suite/tg-ws-proxy.nix | 6 ++--- modules/proxy-suite/warp.nix | 12 ++++++--- modules/proxy-suite/zapret.nix | 8 +++--- .../proxy-suite/zapret/package-builder.nix | 6 ++--- modules/proxy-suite/zapret/packages.nix | 27 +++++++++++-------- modules/proxy-suite/zapret2.nix | 6 ++--- modules/proxy-suite/zapret2/cutoff.nix | 6 ++--- modules/proxy-suite/zapret2/runtime.nix | 10 +++---- nix/checks/proxy-inbounds-runtime.nix | 2 +- nix/checks/ssh-proxy.nix | 12 ++++----- nix/checks/zapret2.nix | 2 +- 29 files changed, 151 insertions(+), 111 deletions(-) diff --git a/modules/proxy-suite/amnezia-wg.nix b/modules/proxy-suite/amnezia-wg.nix index f06cd15..fdab7e8 100644 --- a/modules/proxy-suite/amnezia-wg.nix +++ b/modules/proxy-suite/amnezia-wg.nix @@ -25,7 +25,7 @@ let profile.settings ] ); - inlineVpnFile = name: profile: pkgs.writeText "proxy-suite-awg-${name}.vpn" profile.vpn; + inlineVpnFile = name: profile: pkgs.writeText "proxy-suite-awg" profile.vpn; sourceKind = profile: if profile.configFile != null then @@ -48,7 +48,7 @@ let null; manifestFor = name: profile: - pkgs.writeText "proxy-suite-awg-${name}-manifest.json" ( + pkgs.writeText "proxy-suite-awg" ( builtins.toJSON ( { kind = sourceKind profile; @@ -59,7 +59,12 @@ let // lib.optionalAttrs (profile.settings != null) { settings = profile.settings; } ) ); - configTool = ../../scripts/amneziawg_config.py; + configTool = "${ + builtins.path { + name = "proxy-suite-scripts"; + path = ../../scripts; + } + }/amneziawg_config.py"; runtimeDir = name: "/run/${serviceName name}"; runtimeConfig = name: profile: "${runtimeDir name}/${profile.interfaceName}.conf"; # Keep proxy backend sockets (proxyMark) out of AWG's default-route table. @@ -70,13 +75,13 @@ let let manifest = manifestFor name profile; configPath = runtimeConfig name profile; - prepare = pkgs.writeShellScript "proxy-suite-awg-${name}-prepare" '' + prepare = pkgs.writeShellScript "proxy-suite-awg" '' set -euo pipefail ${pkgs.python3}/bin/python3 ${configTool} \ --manifest ${lib.escapeShellArg (toString manifest)} \ --output ${lib.escapeShellArg configPath} ''; - proxyBypassUp = pkgs.writeShellScript "proxy-suite-awg-${name}-proxy-bypass-up" '' + proxyBypassUp = pkgs.writeShellScript "proxy-suite-awg" '' set -euo pipefail add_bypass_rule() { @@ -99,7 +104,7 @@ let add_bypass_rule -4 add_bypass_rule -6 ''; - proxyBypassDown = pkgs.writeShellScript "proxy-suite-awg-${name}-proxy-bypass-down" '' + proxyBypassDown = pkgs.writeShellScript "proxy-suite-awg" '' set +e while ${pkgs.iproute2}/bin/ip -4 rule del \ @@ -109,7 +114,7 @@ let pref ${toString proxyBypassRulePriority} \ fwmark ${toString cfg.proxy.tproxy.proxyMark} lookup main 2>/dev/null; do :; done ''; - start = pkgs.writeShellScript "proxy-suite-awg-${name}-start" '' + start = pkgs.writeShellScript "proxy-suite-awg" '' set -Eeuo pipefail cleanup() { @@ -170,7 +175,7 @@ let echo "proxy-suite: AmneziaWG profile '${name}' did not complete a handshake; rolling back routes" >&2 false ''; - stop = pkgs.writeShellScript "proxy-suite-awg-${name}-stop" '' + stop = pkgs.writeShellScript "proxy-suite-awg" '' set -euo pipefail exec ${awgCfg.toolsPackage}/bin/awg-quick down ${lib.escapeShellArg configPath} ''; diff --git a/modules/proxy-suite/autoproxy-render.nix b/modules/proxy-suite/autoproxy-render.nix index b5dba8f..61bf37d 100644 --- a/modules/proxy-suite/autoproxy-render.nix +++ b/modules/proxy-suite/autoproxy-render.nix @@ -6,7 +6,7 @@ # publish what it learns, which sing-box then picks up without a restart. { pkgs }: -pkgs.writeShellScript "proxy-suite-autoproxy-render" '' +pkgs.writeShellScript "proxy-suite-autoproxy" '' set -euo pipefail # $1 - probe-exits.json (index -> tag -> path), $2 - state.json export PATH=${pkgs.lib.makeBinPath [ diff --git a/modules/proxy-suite/autoproxy.nix b/modules/proxy-suite/autoproxy.nix index e31ab97..83838b8 100644 --- a/modules/proxy-suite/autoproxy.nix +++ b/modules/proxy-suite/autoproxy.nix @@ -12,6 +12,12 @@ let apCfg = cfg.proxy.autoProxy; render = import ./autoproxy-render.nix { inherit pkgs; }; + jqFile = + path: + builtins.path { + name = "proxy-suite-autoproxy"; + inherit path; + }; bin = lib.makeBinPath [ pkgs.coreutils @@ -36,7 +42,7 @@ let [ -z "$clash_secret" ] || clash_auth=(-H "Authorization: Bearer $clash_secret") ''; - sampler = pkgs.writeShellScript "proxy-suite-autoproxy-sample" '' + sampler = pkgs.writeShellScript "proxy-suite-autoproxy" '' set -euo pipefail export PATH=${bin} state_dir=''${AUTOPROXY_STATE_DIR:-${lib.escapeShellArg autoProxyStateDir}} @@ -55,12 +61,12 @@ let curl -sS --noproxy '*' --max-time 1 "''${clash_auth[@]}" "http://$clash_api/connections" 2>/dev/null || echo '{}' done | jq -s -r --argjson min ${toString (300 * 1024)} \ - --argjson below ${toString (apCfg.slowBelowKiBps * 1024)} -f ${./autoproxy-slow-sample.jq} || true + --argjson below ${toString (apCfg.slowBelowKiBps * 1024)} -f ${jqFile ./autoproxy-slow-sample.jq} || true ) [ -z "$lines" ] || printf '%s\n' "$lines" >> "$state_dir/samples" ''; - runner = pkgs.writeShellScript "proxy-suite-autoproxy-run" '' + runner = pkgs.writeShellScript "proxy-suite-autoproxy" '' set -euo pipefail export PATH=${bin} @@ -116,7 +122,7 @@ let # $1 JSON array of exit tags, $2 registrable domain, $3 wall:|refused|slow. strike() { update --argjson tags "$1" --arg d "$2" --arg why "$3" --argjson now "$now" --argjson ttl "$ttl" \ - -f ${./autoproxy-strike.jq} + -f ${jqFile ./autoproxy-strike.jq} } # $1 registrable domain, $2 host probed, $3 probe JSON. Routes apply to the @@ -198,7 +204,7 @@ let strike '[]' "" "" # See autoproxy-rounds.jq: one exit per AS, then the rest, bad exits last. - rounds=$(jq -c --slurpfile s "$state" -f ${./autoproxy-rounds.jq} "$index") + rounds=$(jq -c --slurpfile s "$state" -f ${jqFile ./autoproxy-rounds.jq} "$index") round1=$(jq -r .r1 <<<"$rounds") round2=$(jq -r .r2 <<<"$rounds") @@ -293,7 +299,7 @@ let rm -f "$state_dir/samples.taking" before=$(jq -c '.domains' "$state") update --argjson o "$obs" --argjson now "$now" --argjson ttl "$ttl" --argjson hits 3 \ - -f ${./autoproxy-slow-judge.jq} + -f ${jqFile ./autoproxy-slow-judge.jq} # Each crawling destination gets the next exit, in probe order, that # answers it as well as direct does (`probe --via`). None left: it diff --git a/modules/proxy-suite/config.nix b/modules/proxy-suite/config.nix index 357b71d..b6da946 100644 --- a/modules/proxy-suite/config.nix +++ b/modules/proxy-suite/config.nix @@ -63,22 +63,22 @@ let routeModeRules = if derived.pureXrayEnabled then rules.xrayRouteModeRules else rules.singBoxRouteModeRules; - tproxyFile = pkgs.writeText "proxy-suite-tproxy-template.json" ( + tproxyFile = pkgs.writeText "proxy-suite-core" ( builtins.toJSON selectedTemplates.tproxy ); - tunFile = pkgs.writeText "proxy-suite-tun-template.json" ( + tunFile = pkgs.writeText "proxy-suite-core" ( builtins.toJSON selectedTemplates.tun ); - perAppTunFile = pkgs.writeText "proxy-suite-per-app-tun-template.json" ( + perAppTunFile = pkgs.writeText "proxy-suite-core" ( builtins.toJSON selectedTemplates.perAppTun ); - routeModeRulesFile = pkgs.writeText "proxy-suite-route-mode-rules.json" ( + routeModeRulesFile = pkgs.writeText "proxy-suite-core" ( builtins.toJSON routeModeRules ); - proxyInboundsFile = pkgs.writeText "proxy-suite-inbounds-template.json" ( + proxyInboundsFile = pkgs.writeText "proxy-suite-inbounds" ( builtins.toJSON proxyInboundsTemplate ); - proxyInboundsSpecFile = pkgs.writeText "proxy-suite-inbounds-spec.json" ( + proxyInboundsSpecFile = pkgs.writeText "proxy-suite-inbounds" ( builtins.toJSON proxyInboundsSpec ); in diff --git a/modules/proxy-suite/default.nix b/modules/proxy-suite/default.nix index 11fa431..0e1d7e3 100644 --- a/modules/proxy-suite/default.nix +++ b/modules/proxy-suite/default.nix @@ -20,12 +20,30 @@ let packages = import ../../pkgs/default.nix { inherit pkgs; }; nftr = import ./nftables.nix { inherit lib pkgs cfg; }; derived = import ./derived.nix { inherit lib cfg; }; + + # Scripts share one store name per block, and journald names a unit's output + # after its ExecStart basename; tag each unit with its own name instead. + unitsWithOwnSyslogIdentifier = lib.mkOption { + type = lib.types.attrsOf ( + lib.types.submodule ( + { name, ... }: + { + config.serviceConfig.SyslogIdentifier = lib.mkIf ( + lib.hasPrefix "proxy-suite-" name || name == "zapret-discord-youtube" + ) (lib.mkDefault (lib.removeSuffix "@" name)); + } + ) + ); + }; in { imports = [ ./options ]; + options.systemd.services = unitsWithOwnSyslogIdentifier; + options.systemd.user.services = unitsWithOwnSyslogIdentifier; + config = lib.mkMerge [ { # xray and sing-box default to this flake's own nixpkgs (redirect it diff --git a/modules/proxy-suite/nftables.nix b/modules/proxy-suite/nftables.nix index 1c2918e..91234e1 100644 --- a/modules/proxy-suite/nftables.nix +++ b/modules/proxy-suite/nftables.nix @@ -42,7 +42,7 @@ let tproxyLocalSubnetLines = mkLocalSubnetLines globalTproxy.localSubnets; perAppTproxyLocalSubnetLines = mkLocalSubnetLines perAppTproxy.localSubnets; - nftablesRulesFile = pkgs.writeText "proxy-suite-tproxy.nft" '' + nftablesRulesFile = pkgs.writeText "proxy-suite-routing" '' ${reservedIpBlock} table ip singbox { chain prerouting { @@ -66,7 +66,7 @@ let } ''; - perAppTproxyRulesFile = pkgs.writeText "proxy-suite-per-app-tproxy.nft" '' + perAppTproxyRulesFile = pkgs.writeText "proxy-suite-routing" '' ${reservedIpBlock} table ip proxy_suite_per_app_tproxy { chain prerouting { @@ -90,7 +90,7 @@ let } ''; - perAppZapretRulesFile = pkgs.writeText "proxy-suite-per-app-zapret.nft" '' + perAppZapretRulesFile = pkgs.writeText "proxy-suite-routing" '' table inet proxy_suite_per_app_zapret_mark { chain prerouting { type filter hook prerouting priority -103; policy accept; @@ -105,7 +105,7 @@ let } ''; - perAppTunChainFile = pkgs.writeText "proxy-suite-per-app-tun-chain.nft" '' + perAppTunChainFile = pkgs.writeText "proxy-suite-routing" '' ${reservedIpBlock} table inet proxy_suite_per_app_tun { chain output { diff --git a/modules/proxy-suite/service/context.nix b/modules/proxy-suite/service/context.nix index 1bad942..21f69b0 100644 --- a/modules/proxy-suite/service/context.nix +++ b/modules/proxy-suite/service/context.nix @@ -69,11 +69,14 @@ let headBin = "${pkgs.coreutils}/bin/head"; seqBin = "${pkgs.coreutils}/bin/seq"; findBin = "${pkgs.findutils}/bin/find"; - amneziaWgProfileNamesFile = pkgs.writeText "proxy-suite-awg-profiles.json" ( + amneziaWgProfileNamesFile = pkgs.writeText "proxy-suite-core" ( builtins.toJSON (builtins.attrNames cfg.amneziaWg.profiles) ); - proxySuiteScriptsDir = ../../../scripts; + proxySuiteScriptsDir = builtins.path { + name = "proxy-suite-scripts"; + path = ../../../scripts; + }; parserScriptsPythonPath = proxySuiteScriptsDir; buildOutboundPy = "${proxySuiteScriptsDir}/build-outbound.py"; buildInboundPy = "${proxySuiteScriptsDir}/build-inbound.py"; diff --git a/modules/proxy-suite/service/control-scripts.nix b/modules/proxy-suite/service/control-scripts.nix index 8b50186..2207286 100644 --- a/modules/proxy-suite/service/control-scripts.nix +++ b/modules/proxy-suite/service/control-scripts.nix @@ -31,7 +31,7 @@ let "proxy-suite-per-app-tun" ]; - subscriptionUpdateScript = pkgs.writeShellScript "proxy-suite-subscription-update" '' + subscriptionUpdateScript = pkgs.writeShellScript "proxy-suite-core" '' set -euo pipefail CACHE_DIR="${subscriptionCacheDir}" mkdir -p "$CACHE_DIR" @@ -46,7 +46,7 @@ let exit "$FAILED" ''; - setRouteModeScript = pkgs.writeShellScript "proxy-suite-set-route-mode" '' + setRouteModeScript = pkgs.writeShellScript "proxy-suite-core" '' set -euo pipefail mode="''${1:-}" @@ -80,7 +80,7 @@ let # Pins the outbound the proxy prefers, or "auto" to hand the choice back to the # configured selection. The pin is persisted either way; a live Clash API switch # only saves the restart. - setPriorityOutboundScript = pkgs.writeShellScript "proxy-suite-set-priority-outbound" '' + setPriorityOutboundScript = pkgs.writeShellScript "proxy-suite-core" '' set -euo pipefail tag="''${1:-}" @@ -117,7 +117,7 @@ let # Applies whatever is now in the runtime spool directories. A removed runtime # subscription leaves a root-owned cache behind that the group cannot unlink, # so the cache dir is reconciled against the sources here. - reloadOutboundsScript = pkgs.writeShellScript "proxy-suite-reload-outbounds" '' + reloadOutboundsScript = pkgs.writeShellScript "proxy-suite-core" '' set -euo pipefail ${subscriptionCacheHelpersBlock} diff --git a/modules/proxy-suite/service/per-app-routing/backend-scripts.nix b/modules/proxy-suite/service/per-app-routing/backend-scripts.nix index aa3936d..b84edc7 100644 --- a/modules/proxy-suite/service/per-app-routing/backend-scripts.nix +++ b/modules/proxy-suite/service/per-app-routing/backend-scripts.nix @@ -27,7 +27,7 @@ let xrayPerAppTunIPv6RoutePrefix ; - perAppTunWaitForInterface = pkgs.writeShellScript "proxy-suite-per-app-tun-wait-for-interface" '' + perAppTunWaitForInterface = pkgs.writeShellScript "proxy-suite-per-app" '' set -euo pipefail for _ in $(${seqBin} 1 50); do if ${ip} link show dev ${lib.escapeShellArg perAppRoutingTun.interface} >/dev/null 2>&1; then @@ -39,7 +39,7 @@ let exit 1 ''; - perAppTunUpScript = pkgs.writeShellScript "proxy-suite-per-app-tun-up" '' + perAppTunUpScript = pkgs.writeShellScript "proxy-suite-per-app" '' set -euo pipefail tun_cidr=${lib.escapeShellArg perAppRoutingTun.address} @@ -83,7 +83,7 @@ let ''} ''; - perAppTunDownScript = pkgs.writeShellScript "proxy-suite-per-app-tun-down" '' + perAppTunDownScript = pkgs.writeShellScript "proxy-suite-per-app" '' set +e # Best-effort cleanup for graceful stops and for unclean previous exits. @@ -106,7 +106,7 @@ let ${builders.flushResolvedCaches} ''; - perAppTproxyUpScript = pkgs.writeShellScript "proxy-suite-per-app-tproxy-up" '' + perAppTproxyUpScript = pkgs.writeShellScript "proxy-suite-per-app" '' set -euo pipefail ${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "proxy_suite_per_app_tproxy"; }} @@ -122,7 +122,7 @@ let ${ip} rule add fwmark ${toString perAppRoutingTproxy.fwmark} table ${toString perAppRoutingTproxy.routeTable} ''; - perAppTproxyDownScript = pkgs.writeShellScript "proxy-suite-per-app-tproxy-down" '' + perAppTproxyDownScript = pkgs.writeShellScript "proxy-suite-per-app" '' set +e ${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "proxy_suite_per_app_tproxy"; }} diff --git a/modules/proxy-suite/service/per-app-routing/profiles.nix b/modules/proxy-suite/service/per-app-routing/profiles.nix index 5c2427e..b0a27da 100644 --- a/modules/proxy-suite/service/per-app-routing/profiles.nix +++ b/modules/proxy-suite/service/per-app-routing/profiles.nix @@ -49,7 +49,7 @@ let localProxyAuth.username != null && (localProxyAuth.password != null || localProxyAuth.passwordFile != null); - perAppRoutingProfilesFile = pkgs.writeText "proxy-suite-per-app-routing-profiles.json" ( + perAppRoutingProfilesFile = pkgs.writeText "proxy-suite-per-app" ( builtins.toJSON effectivePerAppRoutingProfiles ); @@ -57,7 +57,7 @@ let if localProxyAuthEnabled then "/run/proxy-suite-socks/proxychains.conf" else - pkgs.writeText "proxy-suite-proxychains.conf" '' + pkgs.writeText "proxy-suite-per-app" '' strict_chain ${lib.optionalString perAppRoutingCfg.proxychains.quiet "quiet_mode"} ${lib.optionalString perAppRoutingCfg.proxychains.proxyDns "proxy_dns"} diff --git a/modules/proxy-suite/service/per-app-routing/user-rules.nix b/modules/proxy-suite/service/per-app-routing/user-rules.nix index ac41ff8..0d0e475 100644 --- a/modules/proxy-suite/service/per-app-routing/user-rules.nix +++ b/modules/proxy-suite/service/per-app-routing/user-rules.nix @@ -26,7 +26,7 @@ let sliceLabel, markRule, }: - pkgs.writeShellScript "proxy-suite-${name}-user-start" '' + pkgs.writeShellScript "proxy-suite-per-app" '' set -euo pipefail uid="$1" rule_comment_prefix="proxy-suite-${name}-user-$uid" @@ -67,7 +67,7 @@ let nftTable, nftChain, }: - pkgs.writeShellScript "proxy-suite-${name}-user-stop" '' + pkgs.writeShellScript "proxy-suite-per-app" '' set -euo pipefail uid="$1" rule_comment_prefix="proxy-suite-${name}-user-$uid" diff --git a/modules/proxy-suite/service/proxy-inbounds-scripts.nix b/modules/proxy-suite/service/proxy-inbounds-scripts.nix index 2b66bc6..8686925 100644 --- a/modules/proxy-suite/service/proxy-inbounds-scripts.nix +++ b/modules/proxy-suite/service/proxy-inbounds-scripts.nix @@ -56,7 +56,7 @@ let if ob.urlFile != null then ob.urlFile else if ob.url != null then - pkgs.writeText "proxy-suite-inbound-via-url-${ob.tag}" ob.url + pkgs.writeText "proxy-suite-inbounds" ob.url else null; in @@ -64,7 +64,7 @@ let '' # via outbound: ${ob.tag} (static xray json) OB_JSON=$(cat ${ - pkgs.writeText "proxy-suite-inbound-via-${ob.tag}.json" ( + pkgs.writeText "proxy-suite-inbounds" ( builtins.toJSON (ob.xrayJson // { inherit (ob) tag; }) ) }) @@ -111,7 +111,7 @@ let chmod ${if userControlEnabled then "640" else "600"} "${subscriptionsFile}" ''; - startInbounds = pkgs.writeShellScript "proxy-suite-start-inbounds" '' + startInbounds = pkgs.writeShellScript "proxy-suite-inbounds" '' set -euo pipefail umask 077 RUNTIME_DIR="${runtimeDir}" @@ -156,7 +156,7 @@ let # Adds XRay's per-user counters to the daily totals, read and reset in one call. # ponytail: a restart of the inbounds loses what was counted since the last run, at # most one timer interval; collect from ExecStop if that matters. - collectInboundStats = pkgs.writeShellScript "proxy-suite-inbound-stats" '' + collectInboundStats = pkgs.writeShellScript "proxy-suite-inbounds" '' set -euo pipefail file=${lib.escapeShellArg constants.inboundStatsFile} if ! reading=$(${xray} api statsquery --server=127.0.0.1:${toString constants.inboundStatsApiPort} \ @@ -170,7 +170,12 @@ let ${jq} --argjson q "$reading" \ --arg day "$(${pkgs.coreutils}/bin/date +%F)" \ --argjson now "$(${pkgs.coreutils}/bin/date +%s)" \ - -f ${../inbound-stats-add.jq} "$file" > "$tmp" + -f ${ + builtins.path { + name = "proxy-suite-inbounds"; + path = ../inbound-stats-add.jq; + } + } "$file" > "$tmp" ${lib.optionalString userControlEnabled '' ${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "$tmp" || echo "proxy-suite: group ${userControlCfg.group} cannot read the stats; they stay root-only" >&2 diff --git a/modules/proxy-suite/service/routing-scripts.nix b/modules/proxy-suite/service/routing-scripts.nix index 0ac9471..56a0f48 100644 --- a/modules/proxy-suite/service/routing-scripts.nix +++ b/modules/proxy-suite/service/routing-scripts.nix @@ -29,7 +29,7 @@ let }; in { - xrayTunUpScript = pkgs.writeShellScript "proxy-suite-xray-tun-up" '' + xrayTunUpScript = pkgs.writeShellScript "proxy-suite-routing" '' set -euo pipefail tun_cidr=${lib.escapeShellArg globalTun.address} @@ -81,7 +81,7 @@ in ${ip} -6 rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex} ''; - tproxyUpScript = pkgs.writeShellScript "proxy-suite-tproxy-up" '' + tproxyUpScript = pkgs.writeShellScript "proxy-suite-routing" '' set -euo pipefail # Start from a clean policy-routing state. `ip rule add` permits duplicate @@ -100,7 +100,7 @@ in ${ip} rule add fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable} ''; - tproxyDownScript = pkgs.writeShellScript "proxy-suite-tproxy-down" '' + tproxyDownScript = pkgs.writeShellScript "proxy-suite-routing" '' set +e ${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "singbox"; }} @@ -112,7 +112,7 @@ in ${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = globalTproxy.routeTable; }} ''; - tunCleanupScript = pkgs.writeShellScript "proxy-suite-tun-cleanup" '' + tunCleanupScript = pkgs.writeShellScript "proxy-suite-routing" '' set +e # SingBox normally removes these on graceful shutdown, but stale diff --git a/modules/proxy-suite/service/script-blocks/outbounds.nix b/modules/proxy-suite/service/script-blocks/outbounds.nix index 1f775fb..8067c60 100644 --- a/modules/proxy-suite/service/script-blocks/outbounds.nix +++ b/modules/proxy-suite/service/script-blocks/outbounds.nix @@ -179,7 +179,7 @@ let if backendRaw != null then let outboundJson = builtins.toJSON (rawOutboundJson ob tag routingMark); - jsonFile = pkgs.writeText "proxy-suite-ob-${tag}.json" outboundJson; + jsonFile = pkgs.writeText "proxy-suite-core" outboundJson; in '' # outbound: ${tag} (static ${backend} json) @@ -189,8 +189,7 @@ let '' else let - urlSource = - if ob.urlFile != null then ob.urlFile else pkgs.writeText "proxy-suite-url-${ob.tag}" ob.url; + urlSource = if ob.urlFile != null then ob.urlFile else pkgs.writeText "proxy-suite-core" ob.url; in '' # outbound: ${tag} @@ -219,7 +218,7 @@ let if ob.xrayJson != null then let outboundJson = builtins.toJSON (xrayRawOutboundJson ob tag); - jsonFile = pkgs.writeText "proxy-suite-xray-sidecar-ob-${tag}.json" outboundJson; + jsonFile = pkgs.writeText "proxy-suite-core" outboundJson; in '' # outbound: ${tag} (hybrid XRay json sidecar) @@ -230,7 +229,7 @@ let else if ob.singBoxJson != null || ob.json != null then let outboundJson = builtins.toJSON (singBoxRawOutboundJson ob tag routingMark); - jsonFile = pkgs.writeText "proxy-suite-sing-box-ob-${tag}.json" outboundJson; + jsonFile = pkgs.writeText "proxy-suite-core" outboundJson; in '' # outbound: ${tag} (hybrid SingBox json) @@ -240,8 +239,7 @@ let '' else let - urlSource = - if ob.urlFile != null then ob.urlFile else pkgs.writeText "proxy-suite-url-${ob.tag}" ob.url; + urlSource = if ob.urlFile != null then ob.urlFile else pkgs.writeText "proxy-suite-core" ob.url; in '' # outbound: ${tag} (hybrid ${ob.backend}) @@ -286,7 +284,7 @@ let }; outbound = if pureXrayEnabled then xrayOutbound else singBoxOutbound; outboundJson = builtins.toJSON outbound; - jsonFile = pkgs.writeText "proxy-suite-ob-ssh-proxy-${backend}.json" outboundJson; + jsonFile = pkgs.writeText "proxy-suite-core" outboundJson; # Read at start so the known-hosts file stays out of the store. knownHostsTarget = diff --git a/modules/proxy-suite/service/script-blocks/subscriptions.nix b/modules/proxy-suite/service/script-blocks/subscriptions.nix index 850e9d7..887d460 100644 --- a/modules/proxy-suite/service/script-blocks/subscriptions.nix +++ b/modules/proxy-suite/service/script-blocks/subscriptions.nix @@ -28,7 +28,7 @@ let if sub.urlFile != null then sub.urlFile else - pkgs.writeText "proxy-suite-sub-url-${sub.tag}" sub.url; + pkgs.writeText "proxy-suite-core" sub.url; # Defined in every script that touches a cache: the start scripts and the # update unit. @@ -181,7 +181,7 @@ let done < <(_proxy_suite_runtime_subscriptions) ''; - subscriptionTagsFile = pkgs.writeText "proxy-suite-subscription-tags.json" ( + subscriptionTagsFile = pkgs.writeText "proxy-suite-core" ( builtins.toJSON (map (sub: sub.tag) proxyCfg.subscriptions) ); in diff --git a/modules/proxy-suite/service/scripts.nix b/modules/proxy-suite/service/scripts.nix index 19b1587..fcebe67 100644 --- a/modules/proxy-suite/service/scripts.nix +++ b/modules/proxy-suite/service/scripts.nix @@ -52,7 +52,7 @@ let if localProxyAuth.passwordFile != null then localProxyAuth.passwordFile else if localProxyAuth.password != null then - pkgs.writeText "proxy-suite-local-proxy-password" localProxyAuth.password + pkgs.writeText "proxy-suite-core" localProxyAuth.password else null; routeModeStateFile = "/run/proxy-suite/route-mode"; @@ -148,7 +148,7 @@ let proxyInboundsGuardPrivate ; }; - backendJqFilterFile = pkgs.writeText "proxy-suite-${backend}-backend-filter.jq" backendJqFilter; + backendJqFilterFile = pkgs.writeText "proxy-suite-core" backendJqFilter; startScripts = import ./start-scripts.nix { inherit diff --git a/modules/proxy-suite/service/start-scripts.nix b/modules/proxy-suite/service/start-scripts.nix index 6c0f9c2..0d2f5c9 100644 --- a/modules/proxy-suite/service/start-scripts.nix +++ b/modules/proxy-suite/service/start-scripts.nix @@ -135,7 +135,6 @@ let mkStartScript = { - name, runtimeDir, configFile, routingMark ? null, @@ -148,7 +147,7 @@ let enableOutboundTest ? false, enableZapretCutoff ? false, }: - pkgs.writeShellScript name '' + pkgs.writeShellScript "proxy-suite-core" '' set -euo pipefail RUNTIME_DIR="${runtimeDir}" ROUTE_MODE_STATE_FILE="${routeModeStateFile}" @@ -370,7 +369,6 @@ let ''; startSocks = mkStartScript { - name = "proxy-suite-start-socks"; runtimeDir = "/run/proxy-suite-socks"; configFile = tproxyFile; routingMark = globalTproxy.proxyMark; @@ -384,7 +382,6 @@ let }; startTun = mkStartScript { - name = "proxy-suite-start-tun"; runtimeDir = "/run/proxy-suite-tun"; configFile = tunFile; routingMark = if pureXrayEnabled then globalTproxy.proxyMark else null; @@ -395,7 +392,6 @@ let }; startPerAppTun = mkStartScript { - name = "proxy-suite-start-per-app-tun"; runtimeDir = "/run/proxy-suite-per-app-tun"; configFile = perAppTunFile; routingMark = if xrayEnabled || globalTproxy.enable then globalTproxy.proxyMark else null; diff --git a/modules/proxy-suite/ssh-proxy.nix b/modules/proxy-suite/ssh-proxy.nix index 3cd7d6c..0bbe746 100644 --- a/modules/proxy-suite/ssh-proxy.nix +++ b/modules/proxy-suite/ssh-proxy.nix @@ -9,7 +9,7 @@ let s = cfg.sshProxy; destination = if s.server.user == null then s.server.host else "${s.server.user}@${s.server.host}"; extraArgs = lib.concatMapStrings (arg: " args+=(${lib.escapeShellArg arg})\n") s.extraArgs; - startScript = pkgs.writeShellScript "proxy-suite-ssh-proxy-start" '' + startScript = pkgs.writeShellScript "proxy-suite-ssh-proxy" '' set -euo pipefail args=( diff --git a/modules/proxy-suite/tg-ws-proxy.nix b/modules/proxy-suite/tg-ws-proxy.nix index f906bfc..3676898 100644 --- a/modules/proxy-suite/tg-ws-proxy.nix +++ b/modules/proxy-suite/tg-ws-proxy.nix @@ -25,7 +25,7 @@ let dcArgs = lib.concatMapStrings (id: mkValueArg "--dc-ip" "${id}:${t.dcIps.${id}}") ( builtins.attrNames t.dcIps ); - startScript = pkgs.writeShellScript "proxy-suite-tg-ws-proxy-start" '' + startScript = pkgs.writeShellScript "proxy-suite-tg-ws-proxy" '' args=( --port=${toString t.listener.port} --host=${lib.escapeShellArg t.listener.address} @@ -48,7 +48,7 @@ let exec ${tgPkg}/bin/tg-ws-proxy "''${args[@]}" ''; - bypassUpScript = pkgs.writeShellScript "proxy-suite-tg-ws-proxy-bypass-up" '' + bypassUpScript = pkgs.writeShellScript "proxy-suite-tg-ws-proxy" '' set -euo pipefail add_bypass_rule() { @@ -61,7 +61,7 @@ let add_bypass_rule -6 ''; - bypassDownScript = pkgs.writeShellScript "proxy-suite-tg-ws-proxy-bypass-down" '' + bypassDownScript = pkgs.writeShellScript "proxy-suite-tg-ws-proxy" '' set +e while ${ip} -4 rule del pref ${toString bypassRulePriority} fwmark ${toString t.fwmark} lookup main 2>/dev/null; do :; done diff --git a/modules/proxy-suite/warp.nix b/modules/proxy-suite/warp.nix index 341809d..9268f65 100644 --- a/modules/proxy-suite/warp.nix +++ b/modules/proxy-suite/warp.nix @@ -11,6 +11,10 @@ let w = derived.warpCfg; listener = cfg.proxy.listener; auth = listener.auth; + scriptsDir = builtins.path { + name = "proxy-suite-scripts"; + path = ../../scripts; + }; host = if builtins.elem listener.address [ @@ -26,7 +30,7 @@ let if auth.passwordFile != null then auth.passwordFile else if auth.password != null then - pkgs.writeText "proxy-suite-warp-proxy-password" auth.password + pkgs.writeText "proxy-suite-warp" auth.password else null; userinfo = lib.optionalString (auth.username != null && passwordSource != null) '' @@ -34,7 +38,7 @@ let --rawfile p ${lib.escapeShellArg passwordSource} '"\($u | @uri):\($p | rtrimstr("\n") | @uri)@"') ''; - registerScript = pkgs.writeShellScript "proxy-suite-warp-register" '' + registerScript = pkgs.writeShellScript "proxy-suite-warp" '' set -euo pipefail [ -s wgcf-profile.conf ] && exit 0 @@ -73,7 +77,7 @@ let # Some lines drop a share of fresh WARP handshakes for good, and sing-box retries on the # same source port forever. A new process binds a new port, so the probe exits after # repeated failures and systemd starts the tunnel again. - tunnelScript = pkgs.writeShellScript "proxy-suite-warp-tunnel" '' + tunnelScript = pkgs.writeShellScript "proxy-suite-warp" '' set -euo pipefail profile=${lib.escapeShellArg w.profilePath} if [ ! -s "$profile" ]; then @@ -82,7 +86,7 @@ let fi # The profile is a secret, so it is converted here rather than baked into the store. - endpoint=$(${pkgs.python3}/bin/python3 ${../../scripts/warp_outbound.py} --tag warp --routing-mark ${toString cfg.proxy.tproxy.proxyMark} < "$profile") + endpoint=$(${pkgs.python3}/bin/python3 ${scriptsDir}/warp_outbound.py --tag warp --routing-mark ${toString cfg.proxy.tproxy.proxyMark} < "$profile") ${pkgs.jq}/bin/jq -n --argjson ep "$endpoint" '{ log: {level: "warn"}, dns: {servers: [${builtins.toJSON dnsServer}]}, diff --git a/modules/proxy-suite/zapret.nix b/modules/proxy-suite/zapret.nix index 18ca84e..7bf1edb 100644 --- a/modules/proxy-suite/zapret.nix +++ b/modules/proxy-suite/zapret.nix @@ -46,13 +46,13 @@ let fi ''; - perAppZapretMarkUpScript = pkgs.writeShellScript "proxy-suite-per-app-zapret-mark-up" '' + perAppZapretMarkUpScript = pkgs.writeShellScript "proxy-suite-zapret" '' set -euo pipefail ${nft} delete table inet proxy_suite_per_app_zapret_mark 2>/dev/null || true ${nft} -f ${perAppZapretRulesFile} ''; - perAppZapretMarkDownScript = pkgs.writeShellScript "proxy-suite-per-app-zapret-mark-down" '' + perAppZapretMarkDownScript = pkgs.writeShellScript "proxy-suite-zapret" '' set -euo pipefail ${nft} delete table inet proxy_suite_per_app_zapret_mark 2>/dev/null || true ''; @@ -125,7 +125,7 @@ in wants = [ "zapret-discord-youtube.service" ]; conflicts = awgServiceNames; wantedBy = [ "multi-user.target" ]; - execStart = pkgs.writeShellScript "proxy-suite-zapret-vm-exempt-start" exemptStart; - execStop = pkgs.writeShellScript "proxy-suite-zapret-vm-exempt-stop" exemptStop; + execStart = pkgs.writeShellScript "proxy-suite-zapret" exemptStart; + execStop = pkgs.writeShellScript "proxy-suite-zapret" exemptStop; }); } diff --git a/modules/proxy-suite/zapret/package-builder.nix b/modules/proxy-suite/zapret/package-builder.nix index 5f99210..e663a3e 100644 --- a/modules/proxy-suite/zapret/package-builder.nix +++ b/modules/proxy-suite/zapret/package-builder.nix @@ -20,7 +20,7 @@ let tunInterfaces ? [ ], scope ? "global", }: - pkgs.writeText "proxy-suite-zapret-bypass.sh" '' + pkgs.writeText "proxy-suite-zapret" '' # Upstream presets can select iptables explicitly, in which case the nft # hook below is never called. Use upstream helpers for idempotent start # and stop, and distinct comments so each service owns its exemptions. @@ -205,10 +205,10 @@ let ${lib.concatMapStrings ( rule: let - domainsFile = pkgs.writeText "proxy-suite-zapret-hostlist-${rule.name}.txt" ( + domainsFile = pkgs.writeText "proxy-suite-zapret" ( lib.concatStringsSep "\n" (lib.unique rule.domains) + "\n" ); - ipsetFile = pkgs.writeText "proxy-suite-zapret-ipset-${rule.name}.txt" ( + ipsetFile = pkgs.writeText "proxy-suite-zapret" ( lib.concatStringsSep "\n" (lib.unique rule.ips) + "\n" ); in diff --git a/modules/proxy-suite/zapret/packages.nix b/modules/proxy-suite/zapret/packages.nix index cee8acd..c48504c 100644 --- a/modules/proxy-suite/zapret/packages.nix +++ b/modules/proxy-suite/zapret/packages.nix @@ -54,15 +54,18 @@ let if upstreamPackages ? zapret then upstreamPackages.zapret else upstreamPackages.default; mkOptionalHostlistFile = - fileName: entries: - if entries != [ ] then pkgs.writeText fileName (lib.concatStringsSep "\n" entries + "\n") else null; + entries: + if entries != [ ] then + pkgs.writeText "proxy-suite-zapret" (lib.concatStringsSep "\n" entries + "\n") + else + null; - listGeneralFile = mkOptionalHostlistFile "proxy-suite-zapret-list-general-user.txt" zapretCfg.zapret-discord-youtube.domains; - listExcludeFile = mkOptionalHostlistFile "proxy-suite-zapret-list-exclude-user.txt" zapretCfg.zapret-discord-youtube.excludeDomains; - ipsetAllFile = mkOptionalHostlistFile "proxy-suite-zapret-ipset-all.txt" zapretCfg.zapret-discord-youtube.ips; - ipsetExcludeFile = mkOptionalHostlistFile "proxy-suite-zapret-ipset-exclude-user.txt" zapretCfg.zapret-discord-youtube.excludeIps; + listGeneralFile = mkOptionalHostlistFile zapretCfg.zapret-discord-youtube.domains; + listExcludeFile = mkOptionalHostlistFile zapretCfg.zapret-discord-youtube.excludeDomains; + ipsetAllFile = mkOptionalHostlistFile zapretCfg.zapret-discord-youtube.ips; + ipsetExcludeFile = mkOptionalHostlistFile zapretCfg.zapret-discord-youtube.excludeIps; - hostlistRuleSpec = pkgs.writeText "proxy-suite-zapret-hostlist-rules.json" ( + hostlistRuleSpec = pkgs.writeText "proxy-suite-zapret" ( builtins.toJSON { includeExtraUpstreamLists = zapretCfg.zapret-discord-youtube.includeExtraUpstreamLists; entries = map (rule: { @@ -81,10 +84,12 @@ let ); selectedConfigName = lib.strings.sanitizeDerivationName zapretCfg.zapret-discord-youtube.configName; - patchConfigScriptSrc = builtins.path { - path = ../../../scripts/patch-zapret-config.py; - name = "patch-zapret-config.py"; - }; + patchConfigScriptSrc = "${ + builtins.path { + name = "proxy-suite-scripts"; + path = ../../../scripts; + } + }/patch-zapret-config.py"; patchConfigScript = "${pkgs.python3}/bin/python3 ${patchConfigScriptSrc}"; packageBuilder = import ./package-builder.nix { diff --git a/modules/proxy-suite/zapret2.nix b/modules/proxy-suite/zapret2.nix index ca599cb..de56e2c 100644 --- a/modules/proxy-suite/zapret2.nix +++ b/modules/proxy-suite/zapret2.nix @@ -59,7 +59,7 @@ let # Wrapped apps opted in: MODE_FILTER=none applies every profile to all their traffic. perAppRuntime = runtime.mkRuntime { - name = "proxy-suite-per-app-zapret2"; + name = "proxy-suite-zapret2"; qnum = perAppZapretCfg.qnum; desyncMark = 134217728; # 0x8000000 desyncMarkPostnat = 67108864; # 0x4000000 @@ -87,13 +87,13 @@ let ; }; - perAppZapretMarkUpScript = pkgs.writeShellScript "proxy-suite-per-app-zapret2-mark-up" '' + perAppZapretMarkUpScript = pkgs.writeShellScript "proxy-suite-zapret2" '' set -euo pipefail ${nft} delete table inet proxy_suite_per_app_zapret_mark 2>/dev/null || true ${nft} -f ${perAppZapretRulesFile} ''; - perAppZapretMarkDownScript = pkgs.writeShellScript "proxy-suite-per-app-zapret2-mark-down" '' + perAppZapretMarkDownScript = pkgs.writeShellScript "proxy-suite-zapret2" '' set -euo pipefail ${nft} delete table inet proxy_suite_per_app_zapret_mark 2>/dev/null || true ''; diff --git a/modules/proxy-suite/zapret2/cutoff.nix b/modules/proxy-suite/zapret2/cutoff.nix index 69cdedf..5df5151 100644 --- a/modules/proxy-suite/zapret2/cutoff.nix +++ b/modules/proxy-suite/zapret2/cutoff.nix @@ -36,7 +36,7 @@ let # $1 asn.txt, $2 sni.txt, $3 AS-to-prefix map; prints a sing-box source rule-set # with the prefixes of every cut-off network that no whitelisted name fixes. - proxyRules = pkgs.writeShellScript "proxy-suite-zapret2-cutoff-rules" '' + proxyRules = pkgs.writeShellScript "proxy-suite-zapret2" '' set -euo pipefail ${pkgs.gawk}/bin/awk -F'\t' ' FILENAME == ARGV[1] { if ($1 ~ /^[0-9]+$/) cut[$1] = 1; next } @@ -48,7 +48,7 @@ let | {version: 1, rules: (if length > 0 then [{ip_cidr: .}] else [] end)}' ''; - probe = pkgs.writeShellScript unit '' + probe = pkgs.writeShellScript "proxy-suite-zapret2" '' set -euo pipefail export PATH=${ lib.makeBinPath [ @@ -130,7 +130,7 @@ let # The probe measures this line, so its traffic leaves directly: proxyMark keeps it # out of TUN/TProxy like the backend's own, and the conntrack bit keeps zapret2 from # touching or learning it. - exempt = pkgs.writeShellScript "${unit}-exempt" '' + exempt = pkgs.writeShellScript "proxy-suite-zapret2" '' set -euo pipefail ${nft} delete table inet ${table} 2>/dev/null || true ${nft} -f - <asn.txt printf '24940\t300.ya.ru\n' >sni.txt printf '# map\n24940\t5.9.0.0/16\n14061\t104.131.0.0/16\n14061\t2604:a880::/32\n7777\t1.2.0.0/16\n' >nets.txt