diff --git a/docs/options.md b/docs/options.md index f5e7051..919ab48 100644 --- a/docs/options.md +++ b/docs/options.md @@ -23,6 +23,10 @@ Update module option docs there instead of editing this file by hand. - [quiet](#services-proxy-suite-perapprouting-proxychains-quiet) - singBox - [enable](#services-proxy-suite-singbox-enable) + - auth + - [password](#services-proxy-suite-singbox-auth-password) + - [passwordFile](#services-proxy-suite-singbox-auth-passwordfile) + - [username](#services-proxy-suite-singbox-auth-username) - [clashApiPort](#services-proxy-suite-singbox-clashapiport) - dns - [local](#services-proxy-suite-singbox-dns-local) @@ -170,6 +174,11 @@ services.proxy-suite = { }; }; singBox = { + auth = { + password = null; + passwordFile = null; + username = null; + }; clashApiPort = 9090; dns = { local = { @@ -332,6 +341,11 @@ services.proxy-suite = { }; }; singBox = { + auth = { + password = "change-me"; + passwordFile = "/run/secrets/proxy-suite-local-proxy-password"; + username = "proxy-user"; + }; clashApiPort = 9090; dns = { local = { @@ -952,6 +966,119 @@ true + +## services\.proxy-suite\.singBox\.auth\.password + + + +Optional inline password for the local SOCKS5/HTTP mixed inbound\. +Convenient for testing, but the password ends up in the Nix store\. + +Prefer auth\.passwordFile for real deployments\. + + + +*Type:* +null or string matching the pattern \[^\[:space:]]+ + + + +*Default:* + +```nix +null +``` + + + +*Example:* + +```nix +"change-me" +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.auth\.passwordFile + + + +Runtime path to a file containing the local proxy password\. +Intended for use with secret managers so the password stays out of +the Nix store\. The file is read when proxy-suite-socks starts\. + +If perAppRouting\.proxychains\.enable is also used, keep this password +as a single non-whitespace token so it can be written to the +proxychains-ng config format\. The generated proxychains config is +readable by members of userControl\.group\. + + + +*Type:* +null or string + + + +*Default:* + +```nix +null +``` + + + +*Example:* + +```nix +"/run/secrets/proxy-suite-local-proxy-password" +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.auth\.username + + + +Optional username for the local SOCKS5/HTTP mixed inbound\. + +Set this together with exactly one of auth\.password or +auth\.passwordFile to require clients to authenticate before using the +local proxy\. Leave unset to keep the local proxy unauthenticated\. + + + +*Type:* +null or string matching the pattern \[^\[:space:]]+ + + + +*Default:* + +```nix +null +``` + + + +*Example:* + +```nix +"proxy-user" +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + ## services\.proxy-suite\.singBox\.clashApiPort @@ -3997,8 +4124,6 @@ true ## services\.proxy-suite\.zapret\.enable - - Whether to enable zapret DPI bypass\. @@ -4251,6 +4376,8 @@ true ## services\.proxy-suite\.zapret\.hostlistRules\.\*\.domains + + Domains written into the generated custom zapret hostlist file\. Must be non-empty for every hostlistRules entry\. diff --git a/modules/proxy-suite/options/sing-box.nix b/modules/proxy-suite/options/sing-box.nix index eee5d8a..b676cef 100644 --- a/modules/proxy-suite/options/sing-box.nix +++ b/modules/proxy-suite/options/sing-box.nix @@ -39,6 +39,49 @@ in example = 1080; }; + auth = { + username = mkOption { + type = types.nullOr (types.strMatching "[^[:space:]]+"); + default = null; + description = '' + Optional username for the local SOCKS5/HTTP mixed inbound. + + Set this together with exactly one of auth.password or + auth.passwordFile to require clients to authenticate before using the + local proxy. Leave unset to keep the local proxy unauthenticated. + ''; + example = "proxy-user"; + }; + + password = mkOption { + type = types.nullOr (types.strMatching "[^[:space:]]+"); + default = null; + description = '' + Optional inline password for the local SOCKS5/HTTP mixed inbound. + Convenient for testing, but the password ends up in the Nix store. + + Prefer auth.passwordFile for real deployments. + ''; + example = "change-me"; + }; + + passwordFile = mkOption { + type = types.nullOr types.str; + default = null; + description = '' + Runtime path to a file containing the local proxy password. + Intended for use with secret managers so the password stays out of + the Nix store. The file is read when proxy-suite-socks starts. + + If perAppRouting.proxychains.enable is also used, keep this password + as a single non-whitespace token so it can be written to the + proxychains-ng config format. The generated proxychains config is + readable by members of userControl.group. + ''; + example = "/run/secrets/proxy-suite-local-proxy-password"; + }; + }; + proxyByDefault = mkOption { type = types.bool; default = true; diff --git a/modules/proxy-suite/service-assertions.nix b/modules/proxy-suite/service-assertions.nix index a0e5563..a08b5c9 100644 --- a/modules/proxy-suite/service-assertions.nix +++ b/modules/proxy-suite/service-assertions.nix @@ -202,6 +202,21 @@ let 134217728 ]; + localProxyAuthCfg = singBoxCfg.auth; + localProxyAuthUsed = + localProxyAuthCfg.username != null + || localProxyAuthCfg.password != null + || localProxyAuthCfg.passwordFile != null; + localProxyAuthAssertions = [ + (mkAssertion ( + !singBoxCfg.enable || !localProxyAuthUsed || localProxyAuthCfg.username != null + ) "proxy-suite: singBox.auth requires username when password or passwordFile is set") + (exactlyOneOf (singBoxCfg.enable && localProxyAuthUsed) [ + localProxyAuthCfg.password + localProxyAuthCfg.passwordFile + ] "proxy-suite: singBox.auth requires exactly one of password or passwordFile") + ]; + forbiddenValueAssertions = map (item: forbiddenValues item.condition item.value item.disallowed item.message) [ @@ -271,6 +286,7 @@ in featureAssertions ++ perAppRoutingAssertions ++ collisionAssertions +++ localProxyAuthAssertions ++ forbiddenValueAssertions ++ secretAssertions ++ outboundAssertions diff --git a/modules/proxy-suite/service/context.nix b/modules/proxy-suite/service/context.nix index f799ff8..83694ca 100644 --- a/modules/proxy-suite/service/context.nix +++ b/modules/proxy-suite/service/context.nix @@ -59,6 +59,8 @@ let lib pkgs singBoxCfg + perAppRoutingCfg + userControlCfg perAppRoutingTun selectionMode collapseNamedOutbounds diff --git a/modules/proxy-suite/service/default.nix b/modules/proxy-suite/service/default.nix index f98019b..8512fa1 100644 --- a/modules/proxy-suite/service/default.nix +++ b/modules/proxy-suite/service/default.nix @@ -79,6 +79,10 @@ let subscriptionUpdate = "proxy-suite-subscription-update"; }; + localProxyAuthEnabled = + singBoxCfg.auth.username != null + && (singBoxCfg.auth.password != null || singBoxCfg.auth.passwordFile != null); + tproxyUpScript = pkgs.writeShellScript "proxy-suite-tproxy-up" '' ${nft} delete table ip singbox 2>/dev/null || true ${nft} -f ${nftablesRulesFile} @@ -254,7 +258,7 @@ in globalTproxy.enable || perAppRoutingTun.enable || perAppRoutingTproxy.enable || perAppZapretEnabled ) (lib.mkDefault true); - users.groups = lib.mkIf (cfg.enable && userControlEnabled) { + users.groups = lib.mkIf (cfg.enable && (userControlEnabled || localProxyAuthEnabled)) { "${userControlCfg.group}" = { }; }; diff --git a/modules/proxy-suite/service/per-app-routing.nix b/modules/proxy-suite/service/per-app-routing.nix index 43adbfa..317de7c 100644 --- a/modules/proxy-suite/service/per-app-routing.nix +++ b/modules/proxy-suite/service/per-app-routing.nix @@ -62,21 +62,29 @@ let profile: !(builtins.elem profile.name perAppRoutingProfileNames) ) defaultPerAppRoutingProfiles; effectivePerAppRoutingProfileNames = map (profile: profile.name) effectivePerAppRoutingProfiles; + localProxyAuth = singBoxCfg.auth; + localProxyAuthEnabled = + localProxyAuth.username != null + && (localProxyAuth.password != null || localProxyAuth.passwordFile != null); perAppRoutingProfilesFile = pkgs.writeText "proxy-suite-per-app-routing-profiles.json" ( builtins.toJSON effectivePerAppRoutingProfiles ); - proxychainsConfigFile = pkgs.writeText "proxy-suite-proxychains.conf" '' - strict_chain - ${lib.optionalString perAppRoutingCfg.proxychains.quiet "quiet_mode"} - ${lib.optionalString perAppRoutingCfg.proxychains.proxyDns "proxy_dns"} - tcp_read_time_out 15000 - tcp_connect_time_out 8000 + proxychainsConfigFile = + if localProxyAuthEnabled then + "/run/proxy-suite-socks/proxychains.conf" + else + pkgs.writeText "proxy-suite-proxychains.conf" '' + strict_chain + ${lib.optionalString perAppRoutingCfg.proxychains.quiet "quiet_mode"} + ${lib.optionalString perAppRoutingCfg.proxychains.proxyDns "proxy_dns"} + tcp_read_time_out 15000 + tcp_connect_time_out 8000 - [ProxyList] - socks5 ${singBoxCfg.listenAddress} ${toString singBoxCfg.port} - ''; + [ProxyList] + socks5 ${singBoxCfg.listenAddress} ${toString singBoxCfg.port} + ''; proxychainsQuietArg = lib.optionalString perAppRoutingCfg.proxychains.quiet "-q "; hasProxychainsProfiles = builtins.any ( diff --git a/modules/proxy-suite/service/scripts.nix b/modules/proxy-suite/service/scripts.nix index d8be69f..26a1f3d 100644 --- a/modules/proxy-suite/service/scripts.nix +++ b/modules/proxy-suite/service/scripts.nix @@ -3,6 +3,8 @@ lib, pkgs, singBoxCfg, + perAppRoutingCfg, + userControlCfg, perAppRoutingTun, selectionMode, collapseNamedOutbounds, @@ -20,7 +22,19 @@ let globalTproxy = singBoxCfg.tproxy; + localProxyAuth = singBoxCfg.auth; + localProxyAuthEnabled = + localProxyAuth.username != null + && (localProxyAuth.password != null || localProxyAuth.passwordFile != null); + localProxyAuthPasswordSource = + if localProxyAuth.passwordFile != null then + localProxyAuth.passwordFile + else if localProxyAuth.password != null then + pkgs.writeText "proxy-suite-local-proxy-password" localProxyAuth.password + else + null; subscriptionCacheDir = "/var/lib/proxy-suite/subscriptions"; + runtimeProxychainsConfig = "/run/proxy-suite-socks/proxychains.conf"; mkSubscriptionCacheFile = sub: "${subscriptionCacheDir}/${sub.tag}.json"; @@ -159,24 +173,64 @@ let in outboundBlocks + subscriptionBlocks + wrapperBlock; + writeProxychainsConfigBlock = '' + { + printf '%s\n' 'strict_chain' + ${lib.optionalString perAppRoutingCfg.proxychains.quiet "printf '%s\\n' 'quiet_mode'"} + ${lib.optionalString perAppRoutingCfg.proxychains.proxyDns "printf '%s\\n' 'proxy_dns'"} + printf '%s\n' 'tcp_read_time_out 15000' + printf '%s\n' 'tcp_connect_time_out 8000' + printf '\n%s\n' '[ProxyList]' + printf 'socks5 %s %s %s %s\n' \ + ${lib.escapeShellArg singBoxCfg.listenAddress} \ + ${lib.escapeShellArg (toString singBoxCfg.port)} \ + ${lib.escapeShellArg localProxyAuth.username} \ + "$LOCAL_PROXY_PASSWORD" + } > "${runtimeProxychainsConfig}" + ${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "${runtimeProxychainsConfig}" + chmod 640 "${runtimeProxychainsConfig}" + ''; + mkStartScript = { name, runtimeDir, configFile, routingMark ? null, + enableLocalProxyAuth ? false, }: pkgs.writeShellScript name '' set -euo pipefail RUNTIME_DIR="${runtimeDir}" mkdir -p "$RUNTIME_DIR" OUTBOUNDS_JSON='[]' + ${lib.optionalString enableLocalProxyAuth '' + umask 077 + LOCAL_PROXY_PASSWORD="$(cat "${localProxyAuthPasswordSource}")" + ${writeProxychainsConfigBlock} + ''} ${mkOutboundScript routingMark} - ${jq} --argjson obs "$OUTBOUNDS_JSON" \ - '.outbounds = $obs + .outbounds' \ - "${configFile}" > "$RUNTIME_DIR/config.json" + ${ + if enableLocalProxyAuth then + '' + ${jq} \ + --argjson obs "$OUTBOUNDS_JSON" \ + --arg user ${lib.escapeShellArg localProxyAuth.username} \ + --arg password "$LOCAL_PROXY_PASSWORD" \ + '.outbounds = $obs + .outbounds + | (.inbounds[] | select(.type == "mixed" and .tag == "mixed-in") | .users) = [{username:$user,password:$password}]' \ + "${configFile}" > "$RUNTIME_DIR/config.json" + chmod 600 "$RUNTIME_DIR/config.json" + '' + else + '' + ${jq} --argjson obs "$OUTBOUNDS_JSON" \ + '.outbounds = $obs + .outbounds' \ + "${configFile}" > "$RUNTIME_DIR/config.json" + '' + } exec ${singBox} run -c "$RUNTIME_DIR/config.json" ''; @@ -186,6 +240,7 @@ let runtimeDir = "/run/proxy-suite-socks"; configFile = tproxyFile; routingMark = globalTproxy.proxyMark; + enableLocalProxyAuth = localProxyAuthEnabled; }; startTun = mkStartScript { @@ -256,5 +311,10 @@ let in { inherit startSocks startTun startPerAppTun; - inherit subscriptionUpdateScript hasSubscriptions subscriptionTagsFile; + inherit + subscriptionUpdateScript + hasSubscriptions + subscriptionTagsFile + runtimeProxychainsConfig + ; } diff --git a/nix/checks.nix b/nix/checks.nix index a55a1ba..5339782 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -772,6 +772,42 @@ let perAppRoutingProxychainsConfig = builtins.readFile (shellValueByPrefix perAppRoutingProxychainsWrapper "export PROXYCHAINS_CONFIG="); + localProxyAuthFixture = evalProxySuite [ + baseModule + { + services.proxy-suite.singBox.auth = { + username = "local-user"; + password = "local-pass"; + }; + } + ]; + localProxyAuthStartScript = builtins.readFile ( + localProxyAuthFixture.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart + ); + + localProxyAuthPasswordFileFixture = evalProxySuite [ + baseModule + { + services.proxy-suite = { + singBox.auth = { + username = "local-user"; + passwordFile = "/run/secrets/local-proxy-password"; + }; + perAppRouting = { + enable = true; + proxychains.enable = true; + profiles = [ { name = "steam-browser"; route = "proxychains"; } ]; + }; + }; + } + ]; + _localProxyAuthPasswordFile = mkProxyCtlDerived localProxyAuthPasswordFileFixture; + localProxyAuthPasswordFileWrapper = _localProxyAuthPasswordFile.wrapper; + localProxyAuthPasswordFileScript = _localProxyAuthPasswordFile.script; + localProxyAuthPasswordFileStartScript = builtins.readFile ( + localProxyAuthPasswordFileFixture.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart + ); + perAppRoutingDefaultProfilesFixture = evalProxySuite [ baseModule { @@ -1152,6 +1188,24 @@ let } ]; + localProxyAuthNoPassword = mkBadFixture [ + { services.proxy-suite.singBox.auth.username = "local-user"; } + ]; + + localProxyAuthNoUsername = mkBadFixture [ + { services.proxy-suite.singBox.auth.password = "local-pass"; } + ]; + + localProxyAuthBothPasswordSources = mkBadFixture [ + { + services.proxy-suite.singBox.auth = { + username = "local-user"; + password = "local-pass"; + passwordFile = "/run/secrets/local-proxy-password"; + }; + } + ]; + noOutboundsNoSubscriptions = mkBadFixtureRaw [ { system.stateVersion = "26.05"; services.proxy-suite.enable = true; } ]; @@ -1182,6 +1236,44 @@ let assert minimal.config.services.proxy-suite.singBox.listenAddress == "127.0.0.1"; true ) + # -- singBox.auth: default mixed inbound stays unauthenticated -- + ( + let + mixedInbound = builtins.head ( + builtins.filter (inbound: inbound.tag == "mixed-in") ruDefaultConfig.inbounds + ); + in + assert !(mixedInbound ? users); + true + ) + # -- singBox.auth: authenticated mixed inbound is injected at runtime -- + ( + assert pkgs.lib.hasInfix ''LOCAL_PROXY_PASSWORD="$(cat "'' localProxyAuthStartScript; + assert pkgs.lib.hasInfix ''--arg user local-user'' localProxyAuthStartScript; + assert pkgs.lib.hasInfix ''--arg password "$LOCAL_PROXY_PASSWORD"'' localProxyAuthStartScript; + assert pkgs.lib.hasInfix ''select(.type == "mixed" and .tag == "mixed-in") | .users'' localProxyAuthStartScript; + assert pkgs.lib.hasInfix ''chmod 600 "$RUNTIME_DIR/config.json"'' localProxyAuthStartScript; + true + ) + # -- singBox.auth: passwordFile is read from runtime path and proxychains uses runtime config -- + ( + assert pkgs.lib.hasInfix ''/run/secrets/local-proxy-password'' localProxyAuthPasswordFileStartScript; + assert pkgs.lib.hasInfix ''/run/proxy-suite-socks/proxychains.conf'' localProxyAuthPasswordFileStartScript; + assert pkgs.lib.hasInfix "printf 'socks5 %s %s %s %s\\n'" localProxyAuthPasswordFileStartScript; + assert pkgs.lib.hasInfix ''chgrp proxy-suite "/run/proxy-suite-socks/proxychains.conf"'' localProxyAuthPasswordFileStartScript; + assert pkgs.lib.hasInfix ''chmod 640 "/run/proxy-suite-socks/proxychains.conf"'' localProxyAuthPasswordFileStartScript; + assert builtins.hasAttr "proxy-suite" localProxyAuthPasswordFileFixture.config.users.groups; + assert shellValueByPrefix localProxyAuthPasswordFileWrapper "export PROXYCHAINS_CONFIG=" == "/run/proxy-suite-socks/proxychains.conf"; + assert pkgs.lib.hasInfix "Proxychains config is not readable" localProxyAuthPasswordFileScript; + true + ) + # -- singBox.auth: incomplete or conflicting auth config fails -- + ( + assert localProxyAuthNoPassword.success == false; + assert localProxyAuthNoUsername.success == false; + assert localProxyAuthBothPasswordSources.success == false; + true + ) ( assert tgSecretFile.config.services.proxy-suite.tgWsProxy.host == "127.0.0.1"; true diff --git a/pkgs/proxy-ctl-lib.sh b/pkgs/proxy-ctl-lib.sh index 11b8c12..8d08b40 100644 --- a/pkgs/proxy-ctl-lib.sh +++ b/pkgs/proxy-ctl-lib.sh @@ -277,6 +277,11 @@ cmd_wrap() { echo "Profile '$profile' uses route=proxychains, but perAppRouting.proxychains.enable is false." exit 1 fi + if [ ! -r "$PROXYCHAINS_CONFIG" ]; then + echo "Proxychains config is not readable: $PROXYCHAINS_CONFIG" + echo "Make sure proxy-suite-socks.service is running." + exit 1 + fi exec proxychains4 $PROXYCHAINS_QUIET_ARG -f "$PROXYCHAINS_CONFIG" "$@" ;; tun)