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
+373 -16

No files matched your search

+43
View File
@@ -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;
@@ -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
+2
View File
@@ -59,6 +59,8 @@ let
lib
pkgs
singBoxCfg
perAppRoutingCfg
userControlCfg
perAppRoutingTun
selectionMode
collapseNamedOutbounds
+5 -1
View File
@@ -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}" = { };
};
@@ -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 (
+64 -4
View File
@@ -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
;
}