add optional user/password for sing-box

This commit is contained in:
FUFSoB committed 2026-04-22 17:29:12 +05:00
1 parent b696821d8c
commit 43935daeb1
9 files changed
+362 -5

No files matched your search

+129 -2
View File
@@ -23,6 +23,10 @@ Update module option docs there instead of editing this file by hand.
- [quiet](#services-proxy-suite-perapprouting-proxychains-quiet) - [quiet](#services-proxy-suite-perapprouting-proxychains-quiet)
- singBox - singBox
- [enable](#services-proxy-suite-singbox-enable) - [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) - [clashApiPort](#services-proxy-suite-singbox-clashapiport)
- dns - dns
- [local](#services-proxy-suite-singbox-dns-local) - [local](#services-proxy-suite-singbox-dns-local)
@@ -170,6 +174,11 @@ services.proxy-suite = {
}; };
}; };
singBox = { singBox = {
auth = {
password = null;
passwordFile = null;
username = null;
};
clashApiPort = 9090; clashApiPort = 9090;
dns = { dns = {
local = { local = {
@@ -332,6 +341,11 @@ services.proxy-suite = {
}; };
}; };
singBox = { singBox = {
auth = {
password = "change-me";
passwordFile = "/run/secrets/proxy-suite-local-proxy-password";
username = "proxy-user";
};
clashApiPort = 9090; clashApiPort = 9090;
dns = { dns = {
local = { local = {
@@ -952,6 +966,119 @@ true
<a id="services-proxy-suite-singbox-auth-password"></a>
## 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)
<a id="services-proxy-suite-singbox-auth-passwordfile"></a>
## 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)
<a id="services-proxy-suite-singbox-auth-username"></a>
## 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)
<a id="services-proxy-suite-singbox-clashapiport"></a> <a id="services-proxy-suite-singbox-clashapiport"></a>
## services\.proxy-suite\.singBox\.clashApiPort ## services\.proxy-suite\.singBox\.clashApiPort
@@ -3997,8 +4124,6 @@ true
<a id="services-proxy-suite-zapret-enable"></a> <a id="services-proxy-suite-zapret-enable"></a>
## services\.proxy-suite\.zapret\.enable ## services\.proxy-suite\.zapret\.enable
Whether to enable zapret DPI bypass\. Whether to enable zapret DPI bypass\.
@@ -4251,6 +4376,8 @@ true
<a id="services-proxy-suite-zapret-hostlistrules-domains"></a> <a id="services-proxy-suite-zapret-hostlistrules-domains"></a>
## services\.proxy-suite\.zapret\.hostlistRules\.\*\.domains ## services\.proxy-suite\.zapret\.hostlistRules\.\*\.domains
Domains written into the generated custom zapret hostlist file\. Domains written into the generated custom zapret hostlist file\.
Must be non-empty for every hostlistRules entry\. Must be non-empty for every hostlistRules entry\.
+43
View File
@@ -39,6 +39,49 @@ in
example = 1080; 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 { proxyByDefault = mkOption {
type = types.bool; type = types.bool;
default = true; default = true;
@@ -202,6 +202,21 @@ let
134217728 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 = forbiddenValueAssertions =
map (item: forbiddenValues item.condition item.value item.disallowed item.message) map (item: forbiddenValues item.condition item.value item.disallowed item.message)
[ [
@@ -271,6 +286,7 @@ in
featureAssertions featureAssertions
++ perAppRoutingAssertions ++ perAppRoutingAssertions
++ collisionAssertions ++ collisionAssertions
++ localProxyAuthAssertions
++ forbiddenValueAssertions ++ forbiddenValueAssertions
++ secretAssertions ++ secretAssertions
++ outboundAssertions ++ outboundAssertions
+2
View File
@@ -59,6 +59,8 @@ let
lib lib
pkgs pkgs
singBoxCfg singBoxCfg
perAppRoutingCfg
userControlCfg
perAppRoutingTun perAppRoutingTun
selectionMode selectionMode
collapseNamedOutbounds collapseNamedOutbounds
+5 -1
View File
@@ -79,6 +79,10 @@ let
subscriptionUpdate = "proxy-suite-subscription-update"; 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" '' tproxyUpScript = pkgs.writeShellScript "proxy-suite-tproxy-up" ''
${nft} delete table ip singbox 2>/dev/null || true ${nft} delete table ip singbox 2>/dev/null || true
${nft} -f ${nftablesRulesFile} ${nft} -f ${nftablesRulesFile}
@@ -254,7 +258,7 @@ in
globalTproxy.enable || perAppRoutingTun.enable || perAppRoutingTproxy.enable || perAppZapretEnabled globalTproxy.enable || perAppRoutingTun.enable || perAppRoutingTproxy.enable || perAppZapretEnabled
) (lib.mkDefault true); ) (lib.mkDefault true);
users.groups = lib.mkIf (cfg.enable && userControlEnabled) { users.groups = lib.mkIf (cfg.enable && (userControlEnabled || localProxyAuthEnabled)) {
"${userControlCfg.group}" = { }; "${userControlCfg.group}" = { };
}; };
@@ -62,12 +62,20 @@ let
profile: !(builtins.elem profile.name perAppRoutingProfileNames) profile: !(builtins.elem profile.name perAppRoutingProfileNames)
) defaultPerAppRoutingProfiles; ) defaultPerAppRoutingProfiles;
effectivePerAppRoutingProfileNames = map (profile: profile.name) effectivePerAppRoutingProfiles; 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" ( perAppRoutingProfilesFile = pkgs.writeText "proxy-suite-per-app-routing-profiles.json" (
builtins.toJSON effectivePerAppRoutingProfiles builtins.toJSON effectivePerAppRoutingProfiles
); );
proxychainsConfigFile = pkgs.writeText "proxy-suite-proxychains.conf" '' proxychainsConfigFile =
if localProxyAuthEnabled then
"/run/proxy-suite-socks/proxychains.conf"
else
pkgs.writeText "proxy-suite-proxychains.conf" ''
strict_chain strict_chain
${lib.optionalString perAppRoutingCfg.proxychains.quiet "quiet_mode"} ${lib.optionalString perAppRoutingCfg.proxychains.quiet "quiet_mode"}
${lib.optionalString perAppRoutingCfg.proxychains.proxyDns "proxy_dns"} ${lib.optionalString perAppRoutingCfg.proxychains.proxyDns "proxy_dns"}
+61 -1
View File
@@ -3,6 +3,8 @@
lib, lib,
pkgs, pkgs,
singBoxCfg, singBoxCfg,
perAppRoutingCfg,
userControlCfg,
perAppRoutingTun, perAppRoutingTun,
selectionMode, selectionMode,
collapseNamedOutbounds, collapseNamedOutbounds,
@@ -20,7 +22,19 @@
let let
globalTproxy = singBoxCfg.tproxy; 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"; subscriptionCacheDir = "/var/lib/proxy-suite/subscriptions";
runtimeProxychainsConfig = "/run/proxy-suite-socks/proxychains.conf";
mkSubscriptionCacheFile = sub: "${subscriptionCacheDir}/${sub.tag}.json"; mkSubscriptionCacheFile = sub: "${subscriptionCacheDir}/${sub.tag}.json";
@@ -159,24 +173,64 @@ let
in in
outboundBlocks + subscriptionBlocks + wrapperBlock; 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 = mkStartScript =
{ {
name, name,
runtimeDir, runtimeDir,
configFile, configFile,
routingMark ? null, routingMark ? null,
enableLocalProxyAuth ? false,
}: }:
pkgs.writeShellScript name '' pkgs.writeShellScript name ''
set -euo pipefail set -euo pipefail
RUNTIME_DIR="${runtimeDir}" RUNTIME_DIR="${runtimeDir}"
mkdir -p "$RUNTIME_DIR" mkdir -p "$RUNTIME_DIR"
OUTBOUNDS_JSON='[]' OUTBOUNDS_JSON='[]'
${lib.optionalString enableLocalProxyAuth ''
umask 077
LOCAL_PROXY_PASSWORD="$(cat "${localProxyAuthPasswordSource}")"
${writeProxychainsConfigBlock}
''}
${mkOutboundScript routingMark} ${mkOutboundScript routingMark}
${
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" \ ${jq} --argjson obs "$OUTBOUNDS_JSON" \
'.outbounds = $obs + .outbounds' \ '.outbounds = $obs + .outbounds' \
"${configFile}" > "$RUNTIME_DIR/config.json" "${configFile}" > "$RUNTIME_DIR/config.json"
''
}
exec ${singBox} run -c "$RUNTIME_DIR/config.json" exec ${singBox} run -c "$RUNTIME_DIR/config.json"
''; '';
@@ -186,6 +240,7 @@ let
runtimeDir = "/run/proxy-suite-socks"; runtimeDir = "/run/proxy-suite-socks";
configFile = tproxyFile; configFile = tproxyFile;
routingMark = globalTproxy.proxyMark; routingMark = globalTproxy.proxyMark;
enableLocalProxyAuth = localProxyAuthEnabled;
}; };
startTun = mkStartScript { startTun = mkStartScript {
@@ -256,5 +311,10 @@ let
in in
{ {
inherit startSocks startTun startPerAppTun; inherit startSocks startTun startPerAppTun;
inherit subscriptionUpdateScript hasSubscriptions subscriptionTagsFile; inherit
subscriptionUpdateScript
hasSubscriptions
subscriptionTagsFile
runtimeProxychainsConfig
;
} }
+92
View File
@@ -772,6 +772,42 @@ let
perAppRoutingProxychainsConfig = perAppRoutingProxychainsConfig =
builtins.readFile (shellValueByPrefix perAppRoutingProxychainsWrapper "export PROXYCHAINS_CONFIG="); 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 [ perAppRoutingDefaultProfilesFixture = evalProxySuite [
baseModule 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 [ noOutboundsNoSubscriptions = mkBadFixtureRaw [
{ system.stateVersion = "26.05"; services.proxy-suite.enable = true; } { 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"; assert minimal.config.services.proxy-suite.singBox.listenAddress == "127.0.0.1";
true 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"; assert tgSecretFile.config.services.proxy-suite.tgWsProxy.host == "127.0.0.1";
true true
+5
View File
@@ -277,6 +277,11 @@ cmd_wrap() {
echo "Profile '$profile' uses route=proxychains, but perAppRouting.proxychains.enable is false." echo "Profile '$profile' uses route=proxychains, but perAppRouting.proxychains.enable is false."
exit 1 exit 1
fi 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" "$@" exec proxychains4 $PROXYCHAINS_QUIET_ARG -f "$PROXYCHAINS_CONFIG" "$@"
;; ;;
tun) tun)