feat(wip): xray proxy backend
This commit is contained in:
36 files changed
+2265
-996
No files matched your search
@@ -19,6 +19,12 @@ let
|
||||
derived = import ../derived.nix { inherit lib cfg; };
|
||||
inherit (derived)
|
||||
singBoxCfg
|
||||
proxyCfg
|
||||
xrayCfg
|
||||
proxyEnabled
|
||||
singBoxEnabled
|
||||
xrayEnabled
|
||||
activeBackend
|
||||
perAppRoutingCfg
|
||||
globalTun
|
||||
globalTproxy
|
||||
@@ -39,6 +45,7 @@ let
|
||||
jq = "${pkgs.jq}/bin/jq";
|
||||
python3 = "${pkgs.python3}/bin/python3";
|
||||
singBox = "${singBoxCfg.package}/bin/sing-box";
|
||||
xray = "${xrayCfg.package}/bin/xray";
|
||||
grepBin = "${pkgs.gnugrep}/bin/grep";
|
||||
awk = "${pkgs.gawk}/bin/awk";
|
||||
sleepBin = "${pkgs.coreutils}/bin/sleep";
|
||||
@@ -60,6 +67,11 @@ let
|
||||
lib
|
||||
pkgs
|
||||
singBoxCfg
|
||||
proxyCfg
|
||||
proxyEnabled
|
||||
singBoxEnabled
|
||||
xrayEnabled
|
||||
activeBackend
|
||||
perAppRoutingCfg
|
||||
userControlCfg
|
||||
perAppRoutingTun
|
||||
@@ -71,6 +83,7 @@ let
|
||||
jq
|
||||
python3
|
||||
singBox
|
||||
xray
|
||||
parserScriptsPythonPath
|
||||
buildOutboundPy
|
||||
fetchSubscriptionPy
|
||||
@@ -84,6 +97,7 @@ let
|
||||
pkgs
|
||||
cfg
|
||||
singBoxCfg
|
||||
proxyCfg
|
||||
perAppRoutingCfg
|
||||
perAppRoutingTun
|
||||
perAppRoutingTproxy
|
||||
@@ -105,13 +119,14 @@ let
|
||||
inherit
|
||||
packages
|
||||
singBoxCfg
|
||||
proxyCfg
|
||||
perAppRoutingCfg
|
||||
perAppRoutingTun
|
||||
perAppRoutingTproxy
|
||||
perAppZapretCfg
|
||||
selectionMode
|
||||
;
|
||||
inherit (scripts) subscriptionTagsFile;
|
||||
inherit (scripts) subscriptionTagsFile subscriptionCacheDir;
|
||||
inherit (scripts) routeModeStateFile;
|
||||
inherit (perAppRouting)
|
||||
perAppRoutingProfilesFile
|
||||
@@ -124,6 +139,12 @@ in
|
||||
inherit
|
||||
derived
|
||||
singBoxCfg
|
||||
proxyCfg
|
||||
xrayCfg
|
||||
proxyEnabled
|
||||
singBoxEnabled
|
||||
xrayEnabled
|
||||
activeBackend
|
||||
perAppRoutingCfg
|
||||
globalTun
|
||||
globalTproxy
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
{
|
||||
packages,
|
||||
singBoxCfg,
|
||||
proxyCfg,
|
||||
perAppRoutingCfg,
|
||||
perAppRoutingTun,
|
||||
perAppRoutingTproxy,
|
||||
perAppZapretCfg,
|
||||
selectionMode,
|
||||
subscriptionTagsFile,
|
||||
subscriptionCacheDir,
|
||||
perAppRoutingProfilesFile,
|
||||
proxychainsConfigFile,
|
||||
proxychainsQuietArg,
|
||||
@@ -19,12 +21,13 @@
|
||||
selection = selectionMode;
|
||||
inherit
|
||||
subscriptionTagsFile
|
||||
subscriptionCacheDir
|
||||
perAppRoutingProfilesFile
|
||||
proxychainsConfigFile
|
||||
proxychainsQuietArg
|
||||
routeModeStateFile
|
||||
;
|
||||
defaultRouteMode = if singBoxCfg.proxyByDefault then "blacklist" else "whitelist";
|
||||
defaultRouteMode = if proxyCfg.proxyByDefault then "blacklist" else "whitelist";
|
||||
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";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Assembles all sing-box systemd services from the sub-modules.
|
||||
# Assembles proxy-suite systemd services from the sub-modules.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
@@ -55,6 +55,10 @@ let
|
||||
|
||||
inherit (derived)
|
||||
singBoxCfg
|
||||
proxyCfg
|
||||
proxyEnabled
|
||||
xrayEnabled
|
||||
activeBackend
|
||||
perAppRoutingCfg
|
||||
globalTun
|
||||
globalTproxy
|
||||
@@ -82,12 +86,34 @@ let
|
||||
};
|
||||
|
||||
localProxyAuthEnabled =
|
||||
singBoxCfg.auth.username != null
|
||||
&& (singBoxCfg.auth.password != null || singBoxCfg.auth.passwordFile != null);
|
||||
proxyCfg.auth.username != null
|
||||
&& (proxyCfg.auth.password != null || proxyCfg.auth.passwordFile != null);
|
||||
|
||||
# Must match the explicit iproute2_* indexes written to the global TUN
|
||||
# sing-box template in config.nix.
|
||||
# Must match the explicit iproute2_* indexes written to the global SingBox TUN
|
||||
# template in config.nix. XRay uses the same table for manual Linux
|
||||
# policy routing because XRay's autoSystemRoutingTable is not Linux-ready.
|
||||
tunAutoRouteTableIndex = 2022;
|
||||
tunAutoRouteRulePriority = 9000;
|
||||
|
||||
xrayTunUpScript = pkgs.writeShellScript "proxy-suite-xray-tun-up" ''
|
||||
set -euo pipefail
|
||||
|
||||
for _ in $(${pkgs.coreutils}/bin/seq 1 50); do
|
||||
if ${ip} link show dev ${lib.escapeShellArg globalTun.interface} >/dev/null 2>&1; then
|
||||
break
|
||||
fi
|
||||
${pkgs.coreutils}/bin/sleep 0.1
|
||||
done
|
||||
|
||||
if ! ${ip} link show dev ${lib.escapeShellArg globalTun.interface} >/dev/null 2>&1; then
|
||||
echo "proxy-suite: XRay TUN interface ${globalTun.interface} did not appear in time" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
while ${ip} -4 rule del pref ${toString tunAutoRouteRulePriority} 2>/dev/null; do :; done
|
||||
${ip} -4 route replace default dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
|
||||
${ip} -4 rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex}
|
||||
'';
|
||||
|
||||
tproxyUpScript = pkgs.writeShellScript "proxy-suite-tproxy-up" ''
|
||||
set -euo pipefail
|
||||
@@ -115,7 +141,7 @@ let
|
||||
tunCleanupScript = pkgs.writeShellScript "proxy-suite-tun-cleanup" ''
|
||||
set +e
|
||||
|
||||
# sing-box normally removes these on graceful shutdown, but stale
|
||||
# SingBox normally removes these on graceful shutdown, but stale
|
||||
# auto_route/auto_redirect state leaves the host routing through a dead TUN
|
||||
# interface after `proxy-ctl tun off` or an unclean service stop.
|
||||
${nft} delete table inet sing-box 2>/dev/null || true
|
||||
@@ -130,10 +156,10 @@ let
|
||||
|
||||
systemServiceEntries = [
|
||||
{
|
||||
enable = singBoxCfg.enable;
|
||||
enable = proxyEnabled;
|
||||
name = serviceNames.socks;
|
||||
value = mkRestartingService {
|
||||
description = "sing-box proxy client (SOCKS + TProxy-ready)";
|
||||
description = "${if xrayEnabled then "XRay" else "sing-box"} proxy client (SOCKS + TProxy-ready)";
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
@@ -143,10 +169,10 @@ let
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && globalTproxy.enable;
|
||||
enable = proxyEnabled && globalTproxy.enable;
|
||||
name = serviceNames.tproxy;
|
||||
value = mkOneshotService {
|
||||
description = "sing-box TProxy – nftables rules and policy routing";
|
||||
description = "proxy-suite TProxy - nftables rules and policy routing";
|
||||
after = [
|
||||
"network.target"
|
||||
"${serviceNames.socks}.service"
|
||||
@@ -162,26 +188,27 @@ let
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && globalTun.enable;
|
||||
enable = proxyEnabled && globalTun.enable;
|
||||
name = serviceNames.tun;
|
||||
value = mkRestartingService {
|
||||
description = "sing-box TUN proxy client";
|
||||
description = "${if xrayEnabled then "XRay" else "sing-box"} TUN proxy client";
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
wantedBy = lib.optionals globalTun.autostart [ "multi-user.target" ];
|
||||
conflicts = [ "${serviceNames.tproxy}.service" ];
|
||||
execStartPre = tunCleanupScript;
|
||||
execStart = scripts.startTun;
|
||||
execStartPost = if xrayEnabled then xrayTunUpScript else null;
|
||||
execStopPost = tunCleanupScript;
|
||||
runtimeDirectory = serviceNames.tun;
|
||||
stateDirectory = "proxy-suite";
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && perAppRoutingTun.enable;
|
||||
enable = proxyEnabled && perAppRoutingTun.enable;
|
||||
name = serviceNames.perAppTun;
|
||||
value = mkRestartingService {
|
||||
description = "sing-box per-app-routing TUN backend";
|
||||
description = "proxy-suite per-app-routing TUN backend";
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
execStartPre = perAppRouting.perAppTunDownScript;
|
||||
@@ -193,7 +220,7 @@ let
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && perAppRoutingTun.enable;
|
||||
enable = proxyEnabled && perAppRoutingTun.enable;
|
||||
name = "${serviceNames.perAppTun}-user@";
|
||||
value = mkUserRuleService {
|
||||
description = "Enable proxy-suite app TUN marking for user %i";
|
||||
@@ -203,7 +230,7 @@ let
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && perAppRoutingTproxy.enable;
|
||||
enable = proxyEnabled && perAppRoutingTproxy.enable;
|
||||
name = serviceNames.perAppTproxy;
|
||||
value = mkOneshotService {
|
||||
description = "proxy-suite per-app-routing TProxy backend";
|
||||
@@ -221,7 +248,7 @@ let
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && perAppRoutingTproxy.enable;
|
||||
enable = proxyEnabled && perAppRoutingTproxy.enable;
|
||||
name = "${serviceNames.perAppTproxy}-user@";
|
||||
value = mkUserRuleService {
|
||||
description = "Enable proxy-suite app TProxy marking for user %i";
|
||||
@@ -241,7 +268,7 @@ let
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && hasSubscriptions;
|
||||
enable = proxyEnabled && hasSubscriptions;
|
||||
name = serviceNames.subscriptionUpdate;
|
||||
value = mkOneshotService {
|
||||
description = "Refresh proxy-suite subscription caches";
|
||||
@@ -252,7 +279,7 @@ let
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable;
|
||||
enable = proxyEnabled;
|
||||
name = "proxy-suite-route-mode@";
|
||||
value = mkOneshotService {
|
||||
description = "Set proxy-suite route mode to %i";
|
||||
@@ -282,14 +309,14 @@ let
|
||||
|
||||
timerEntries = [
|
||||
{
|
||||
enable = singBoxCfg.enable && hasSubscriptions;
|
||||
enable = proxyEnabled && hasSubscriptions;
|
||||
name = serviceNames.subscriptionUpdate;
|
||||
value = {
|
||||
description = "Periodic proxy-suite subscription refresh";
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = {
|
||||
OnBootSec = "5m";
|
||||
OnUnitActiveSec = singBoxCfg.subscriptionUpdateInterval;
|
||||
OnUnitActiveSec = proxyCfg.subscriptionUpdateInterval;
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -299,7 +326,7 @@ in
|
||||
environment.systemPackages = [ control.proxyCtl ];
|
||||
|
||||
# nftables must be on for transparent routing backends. Global TUN uses
|
||||
# sing-box auto_redirect, which programs an `inet sing-box` nftables table.
|
||||
# SingBox auto_redirect programs an `inet sing-box` nftables table.
|
||||
networking.nftables.enable = lib.mkIf (
|
||||
globalTun.enable
|
||||
|| globalTproxy.enable
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
pkgs,
|
||||
cfg,
|
||||
singBoxCfg,
|
||||
proxyCfg,
|
||||
perAppRoutingCfg,
|
||||
perAppRoutingTun,
|
||||
perAppRoutingTproxy,
|
||||
@@ -62,7 +63,7 @@ let
|
||||
profile: !(builtins.elem profile.name perAppRoutingProfileNames)
|
||||
) defaultPerAppRoutingProfiles;
|
||||
effectivePerAppRoutingProfileNames = map (profile: profile.name) effectivePerAppRoutingProfiles;
|
||||
localProxyAuth = singBoxCfg.auth;
|
||||
localProxyAuth = proxyCfg.auth;
|
||||
localProxyAuthEnabled =
|
||||
localProxyAuth.username != null
|
||||
&& (localProxyAuth.password != null || localProxyAuth.passwordFile != null);
|
||||
@@ -83,7 +84,7 @@ let
|
||||
tcp_connect_time_out 8000
|
||||
|
||||
[ProxyList]
|
||||
socks5 ${singBoxCfg.listenAddress} ${toString singBoxCfg.port}
|
||||
socks5 ${proxyCfg.listenAddress} ${toString proxyCfg.port}
|
||||
'';
|
||||
proxychainsQuietArg = lib.optionalString perAppRoutingCfg.proxychains.quiet "-q ";
|
||||
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
# Generates the sing-box startup scripts and subscription management scripts.
|
||||
# Generates proxy backend startup scripts and subscription management scripts.
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
singBoxCfg,
|
||||
proxyCfg,
|
||||
proxyEnabled,
|
||||
singBoxEnabled,
|
||||
xrayEnabled,
|
||||
activeBackend,
|
||||
perAppRoutingCfg,
|
||||
userControlCfg,
|
||||
perAppRoutingTun,
|
||||
@@ -12,6 +17,7 @@
|
||||
jq,
|
||||
python3,
|
||||
singBox,
|
||||
xray,
|
||||
parserScriptsPythonPath,
|
||||
buildOutboundPy,
|
||||
fetchSubscriptionPy,
|
||||
@@ -22,9 +28,12 @@
|
||||
}:
|
||||
|
||||
let
|
||||
globalTproxy = singBoxCfg.tproxy;
|
||||
globalTproxy = proxyCfg.tproxy;
|
||||
systemctl = "${pkgs.systemd}/bin/systemctl";
|
||||
localProxyAuth = singBoxCfg.auth;
|
||||
backend = if activeBackend == null then "sing-box" else activeBackend;
|
||||
backendArg = "--backend ${backend}";
|
||||
backendBin = if xrayEnabled then xray else singBox;
|
||||
localProxyAuth = proxyCfg.auth;
|
||||
localProxyAuthEnabled =
|
||||
localProxyAuth.username != null
|
||||
&& (localProxyAuth.password != null || localProxyAuth.passwordFile != null);
|
||||
@@ -35,7 +44,7 @@ let
|
||||
pkgs.writeText "proxy-suite-local-proxy-password" localProxyAuth.password
|
||||
else
|
||||
null;
|
||||
subscriptionCacheDir = "/var/lib/proxy-suite/subscriptions";
|
||||
subscriptionCacheDir = "/var/lib/proxy-suite/subscriptions/${backend}";
|
||||
routeModeStateFile = "/run/proxy-suite/route-mode";
|
||||
runtimeProxychainsConfig = "/run/proxy-suite-socks/proxychains.conf";
|
||||
|
||||
@@ -55,7 +64,7 @@ let
|
||||
in
|
||||
''
|
||||
printf '%s' "$(cat "${urlSource}")" \
|
||||
| PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${fetchSubscriptionPy} --tag-prefix ${lib.escapeShellArg sub.tag}
|
||||
| PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${fetchSubscriptionPy} ${backendArg} --tag-prefix ${lib.escapeShellArg sub.tag}
|
||||
'';
|
||||
|
||||
subscriptionCacheHelpersBlock = lib.optionalString hasSubscriptions ''
|
||||
@@ -83,23 +92,53 @@ let
|
||||
}
|
||||
'';
|
||||
|
||||
# Build the shell code block for a single outbound entry.
|
||||
# tag overrides ob.tag (used in "first" mode to force tag = "proxy").
|
||||
# routingMark is null or an int; added to outbound JSON if set.
|
||||
routingMarkJq =
|
||||
routingMark:
|
||||
if routingMark == null then
|
||||
""
|
||||
else if xrayEnabled then
|
||||
" | .streamSettings.sockopt.mark = ${toString routingMark}"
|
||||
else
|
||||
" | .routing_mark = ${toString routingMark}";
|
||||
|
||||
rawOutboundJson =
|
||||
ob: tag: routingMark:
|
||||
let
|
||||
backendRaw =
|
||||
if xrayEnabled then
|
||||
ob.xrayJson
|
||||
else if ob.singBoxJson != null then
|
||||
ob.singBoxJson
|
||||
else
|
||||
ob.json;
|
||||
markAttrs =
|
||||
if routingMark == null then
|
||||
{ }
|
||||
else if xrayEnabled then
|
||||
{ streamSettings.sockopt.mark = routingMark; }
|
||||
else
|
||||
{ routing_mark = routingMark; };
|
||||
in
|
||||
backendRaw // { inherit tag; } // markAttrs;
|
||||
|
||||
mkOutboundBlock =
|
||||
ob: routingMark: tag:
|
||||
let
|
||||
markArg = lib.optionalString (routingMark != null) " --routing-mark ${toString routingMark}";
|
||||
backendRaw =
|
||||
if xrayEnabled then
|
||||
ob.xrayJson
|
||||
else if ob.singBoxJson != null then
|
||||
ob.singBoxJson
|
||||
else
|
||||
ob.json;
|
||||
in
|
||||
if ob.json != null then
|
||||
if backendRaw != null then
|
||||
let
|
||||
outboundJson = builtins.toJSON (
|
||||
ob.json // { tag = tag; } // lib.optionalAttrs (routingMark != null) { routing_mark = routingMark; }
|
||||
);
|
||||
outboundJson = builtins.toJSON (rawOutboundJson ob tag routingMark);
|
||||
jsonFile = pkgs.writeText "proxy-suite-ob-${tag}.json" outboundJson;
|
||||
in
|
||||
''
|
||||
# outbound: ${tag} (static json)
|
||||
# outbound: ${tag} (static ${backend} json)
|
||||
OB_JSON=$(cat "${jsonFile}")
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson ob "$OB_JSON" '. + [$ob]' <<< "$OUTBOUNDS_JSON")
|
||||
''
|
||||
@@ -109,23 +148,20 @@ let
|
||||
if ob.urlFile != null then
|
||||
ob.urlFile
|
||||
else
|
||||
# Literal URL – write to nix store so the script can cat it.
|
||||
# Less secret than urlFile, but convenient for non-sensitive configs.
|
||||
pkgs.writeText "proxy-suite-url-${ob.tag}" ob.url;
|
||||
in
|
||||
''
|
||||
# outbound: ${tag}
|
||||
URL=$(cat "${urlSource}")
|
||||
OB_JSON=$(printf '%s' "$URL" | PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${buildOutboundPy} --tag ${lib.escapeShellArg tag}${markArg})
|
||||
OB_JSON=$(printf '%s' "$URL" | PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${buildOutboundPy} ${backendArg} --tag ${lib.escapeShellArg tag}${lib.optionalString (routingMark != null) " --routing-mark ${toString routingMark}"})
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson ob "$OB_JSON" '. + [$ob]' <<< "$OUTBOUNDS_JSON")
|
||||
'';
|
||||
|
||||
# Build the shell code block for a single subscription entry.
|
||||
# Fetches subscription into the cache on first run; subsequent starts use the cache.
|
||||
mkSubscriptionBlock =
|
||||
sub: routingMark:
|
||||
let
|
||||
cacheFile = mkSubscriptionCacheFile sub;
|
||||
markFilter = routingMarkJq routingMark;
|
||||
in
|
||||
''
|
||||
# subscription: ${sub.tag}
|
||||
@@ -143,28 +179,28 @@ let
|
||||
fi
|
||||
if [ -f "$CACHE_FILE" ]; then
|
||||
SUB_JSON=$(cat "$CACHE_FILE")
|
||||
${
|
||||
lib.optionalString (routingMark != null) ''
|
||||
SUB_JSON=$(${jq} --argjson m ${toString routingMark} 'map(. + {routing_mark: $m})' <<< "$SUB_JSON")
|
||||
''
|
||||
}OUTBOUNDS_JSON=$(${jq} --argjson sub "$SUB_JSON" '. + $sub' <<< "$OUTBOUNDS_JSON")
|
||||
${lib.optionalString (routingMark != null) ''
|
||||
SUB_JSON=$(${jq} 'map(.${markFilter})' <<< "$SUB_JSON")
|
||||
''}OUTBOUNDS_JSON=$(${jq} --argjson sub "$SUB_JSON" '. + $sub' <<< "$OUTBOUNDS_JSON")
|
||||
fi
|
||||
'';
|
||||
|
||||
# Shell code that builds all outbounds, then optionally adds a selector/urltest wrapper.
|
||||
xrayUrltestTagFilter = ''
|
||||
OUTBOUNDS_JSON=$(${jq} 'map(.tag = ("proxy-suite-ob-" + .tag))' <<< "$OUTBOUNDS_JSON")
|
||||
'';
|
||||
|
||||
mkOutboundScript =
|
||||
routingMark:
|
||||
let
|
||||
outboundBlocks =
|
||||
if collapseNamedOutbounds && singBoxCfg.outbounds != [ ] then
|
||||
# Only the first static outbound, tagged "proxy" so sing-box routes to it.
|
||||
mkOutboundBlock (builtins.head singBoxCfg.outbounds) routingMark "proxy"
|
||||
if collapseNamedOutbounds && proxyCfg.outbounds != [ ] then
|
||||
mkOutboundBlock (builtins.head proxyCfg.outbounds) routingMark "proxy"
|
||||
else
|
||||
lib.concatMapStrings (ob: mkOutboundBlock ob routingMark ob.tag) singBoxCfg.outbounds;
|
||||
lib.concatMapStrings (ob: mkOutboundBlock ob routingMark ob.tag) proxyCfg.outbounds;
|
||||
|
||||
subscriptionBlocks = lib.concatMapStrings (
|
||||
sub: mkSubscriptionBlock sub routingMark
|
||||
) singBoxCfg.subscriptions;
|
||||
) proxyCfg.subscriptions;
|
||||
|
||||
requireOutboundsBlock = ''
|
||||
if [ "$(${jq} 'length' <<< "$OUTBOUNDS_JSON")" -eq 0 ]; then
|
||||
@@ -174,10 +210,10 @@ let
|
||||
'';
|
||||
|
||||
wrapperBlock =
|
||||
if collapseNamedOutbounds then
|
||||
# When there are no static outbounds, subscription outbounds keep their
|
||||
# real tags. Rename the first one to "proxy" so routing rules resolve.
|
||||
lib.optionalString (singBoxCfg.outbounds == [ ] && singBoxCfg.subscriptions != [ ]) ''
|
||||
if xrayEnabled && selectionMode == "urltest" then
|
||||
xrayUrltestTagFilter
|
||||
else if collapseNamedOutbounds then
|
||||
lib.optionalString (proxyCfg.outbounds == [ ] && proxyCfg.subscriptions != [ ]) ''
|
||||
FIRST_TAG=$(${jq} -r 'if length > 0 then .[0].tag else "" end' <<< "$OUTBOUNDS_JSON")
|
||||
if [ -n "$FIRST_TAG" ]; then
|
||||
OUTBOUNDS_JSON=$(${jq} --arg t "$FIRST_TAG" \
|
||||
@@ -195,13 +231,12 @@ let
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson w "$WRAPPER" '[$w] + .' <<< "$OUTBOUNDS_JSON")
|
||||
''
|
||||
else
|
||||
# urltest
|
||||
''
|
||||
TAGS=$(${jq} '[.[].tag]' <<< "$OUTBOUNDS_JSON")
|
||||
WRAPPER=$(${jq} -n \
|
||||
--argjson tags "$TAGS" \
|
||||
--arg url ${lib.escapeShellArg singBoxCfg.urlTest.url} \
|
||||
--arg interval ${lib.escapeShellArg singBoxCfg.urlTest.interval} \
|
||||
--arg url ${lib.escapeShellArg proxyCfg.urlTest.url} \
|
||||
--arg interval ${lib.escapeShellArg proxyCfg.urlTest.interval} \
|
||||
--argjson tolerance ${toString singBoxCfg.urlTest.tolerance} \
|
||||
'{type:"urltest",tag:"proxy",outbounds:$tags,url:$url,interval:$interval,tolerance:$tolerance}')
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson w "$WRAPPER" '[$w] + .' <<< "$OUTBOUNDS_JSON")
|
||||
@@ -218,8 +253,8 @@ let
|
||||
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 proxyCfg.listenAddress} \
|
||||
${lib.escapeShellArg (toString proxyCfg.port)} \
|
||||
${lib.escapeShellArg localProxyAuth.username} \
|
||||
"$LOCAL_PROXY_PASSWORD"
|
||||
} > "${runtimeProxychainsConfig}"
|
||||
@@ -227,8 +262,6 @@ let
|
||||
chmod 640 "${runtimeProxychainsConfig}"
|
||||
'';
|
||||
|
||||
modeBaseline = if singBoxCfg.proxyByDefault then "blacklist" else "whitelist";
|
||||
|
||||
restartActiveConfigConsumersBlock = lib.concatMapStrings (svc: ''
|
||||
if ${systemctl} is-active --quiet ${svc}; then
|
||||
${systemctl} restart ${svc}
|
||||
@@ -239,6 +272,103 @@ let
|
||||
"proxy-suite-per-app-tun"
|
||||
];
|
||||
|
||||
routeModeCaseBlock = ''
|
||||
if [ -r "$ROUTE_MODE_STATE_FILE" ]; then
|
||||
ROUTE_MODE="$(tr -d '\r\n[:space:]' < "$ROUTE_MODE_STATE_FILE" 2>/dev/null || true)"
|
||||
fi
|
||||
case "$ROUTE_MODE" in
|
||||
blacklist)
|
||||
ROUTE_FINAL="proxy"
|
||||
DNS_FINAL="remote"
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(.entries) | add // [])
|
||||
+ .proxyPrimary
|
||||
+ .direct
|
||||
+ .safetyDirect
|
||||
+ .block
|
||||
+ .proxyGeo
|
||||
' "${routeModeRulesFile}")
|
||||
;;
|
||||
whitelist)
|
||||
ROUTE_FINAL="direct"
|
||||
DNS_FINAL="local"
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(.entries) | add // [])
|
||||
+ .proxyPrimary
|
||||
+ .direct
|
||||
+ .safetyDirect
|
||||
+ .block
|
||||
+ .proxyGeo
|
||||
' "${routeModeRulesFile}")
|
||||
;;
|
||||
all-proxy)
|
||||
ROUTE_FINAL="proxy"
|
||||
DNS_FINAL="remote"
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
CLEAR_DNS_RULES=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(select(.category == "proxy" or .category == "block") | .entries) | add // [])
|
||||
+ .proxyPrimary
|
||||
+ .safetyDirect
|
||||
+ .block
|
||||
+ .proxyGeo
|
||||
' "${routeModeRulesFile}")
|
||||
;;
|
||||
all-bypass)
|
||||
ROUTE_FINAL="direct"
|
||||
DNS_FINAL="local"
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
CLEAR_DNS_RULES=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(select(.category == "block") | .entries) | add // [])
|
||||
+ .safetyDirect
|
||||
+ .block
|
||||
' "${routeModeRulesFile}")
|
||||
;;
|
||||
*)
|
||||
ROUTE_MODE=""
|
||||
;;
|
||||
esac
|
||||
'';
|
||||
|
||||
mkBackendJqFilter =
|
||||
if xrayEnabled then
|
||||
''
|
||||
def xray_final_rule($tag):
|
||||
if $tag == "proxy" and "${selectionMode}" == "urltest" then
|
||||
{type:"field",network:"tcp,udp",balancerTag:"proxy"}
|
||||
else
|
||||
{type:"field",network:"tcp,udp",outboundTag:$tag}
|
||||
end;
|
||||
.outbounds = $obs + .outbounds
|
||||
| if $auth_enabled then
|
||||
(.inbounds[] | select(.protocol == "socks" and .tag == "mixed-in") | .settings.auth) = "password"
|
||||
| (.inbounds[] | select(.protocol == "socks" and .tag == "mixed-in") | .settings.users) = [{user:$user,pass:$password}]
|
||||
else . end
|
||||
| if $route_enabled then
|
||||
.routing.rules = ($route_rules + [xray_final_rule($route_final)])
|
||||
else . end
|
||||
''
|
||||
else
|
||||
''
|
||||
.outbounds = $obs + .outbounds
|
||||
| if $auth_enabled then
|
||||
(.inbounds[] | select(.type == "mixed" and .tag == "mixed-in") | .users) = [{username:$user,password:$password}]
|
||||
else . end
|
||||
| if $route_enabled then
|
||||
.route.rules = $route_rules
|
||||
| .route.final = $route_final
|
||||
| .dns.final = $dns_final
|
||||
| if $clear_dns_rules then .dns.rules = [] else . end
|
||||
else . end
|
||||
'';
|
||||
|
||||
mkStartScript =
|
||||
{
|
||||
name,
|
||||
@@ -266,68 +396,7 @@ let
|
||||
${writeProxychainsConfigBlock}
|
||||
''}
|
||||
|
||||
if [ -r "$ROUTE_MODE_STATE_FILE" ]; then
|
||||
ROUTE_MODE="$(tr -d '\r\n[:space:]' < "$ROUTE_MODE_STATE_FILE" 2>/dev/null || true)"
|
||||
fi
|
||||
case "$ROUTE_MODE" in
|
||||
blacklist)
|
||||
ROUTE_FINAL="proxy"
|
||||
DNS_FINAL="remote"
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(.entries) | add // [])
|
||||
+ .proxyPrimary
|
||||
+ .direct
|
||||
+ .safetyDirect
|
||||
+ .block
|
||||
+ .proxyGeo
|
||||
' "${routeModeRulesFile}")
|
||||
;;
|
||||
whitelist)
|
||||
ROUTE_FINAL="direct"
|
||||
DNS_FINAL="local"
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(.entries) | add // [])
|
||||
+ .proxyPrimary
|
||||
+ .direct
|
||||
+ .safetyDirect
|
||||
+ .block
|
||||
+ .proxyGeo
|
||||
' "${routeModeRulesFile}")
|
||||
;;
|
||||
all-proxy)
|
||||
ROUTE_FINAL="proxy"
|
||||
DNS_FINAL="remote"
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
CLEAR_DNS_RULES=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(select(.category == "proxy" or .category == "block") | .entries) | add // [])
|
||||
+ .proxyPrimary
|
||||
+ .safetyDirect
|
||||
+ .block
|
||||
+ .proxyGeo
|
||||
' "${routeModeRulesFile}")
|
||||
;;
|
||||
all-bypass)
|
||||
ROUTE_FINAL="direct"
|
||||
DNS_FINAL="local"
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
CLEAR_DNS_RULES=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(select(.category == "block") | .entries) | add // [])
|
||||
+ .safetyDirect
|
||||
+ .block
|
||||
' "${routeModeRulesFile}")
|
||||
;;
|
||||
*)
|
||||
ROUTE_MODE=""
|
||||
;;
|
||||
esac
|
||||
${routeModeCaseBlock}
|
||||
|
||||
${mkOutboundScript routingMark}
|
||||
|
||||
@@ -341,22 +410,13 @@ let
|
||||
--arg route_final "$ROUTE_FINAL" \
|
||||
--arg dns_final "$DNS_FINAL" \
|
||||
--argjson clear_dns_rules "$CLEAR_DNS_RULES" \
|
||||
'.outbounds = $obs + .outbounds
|
||||
| if $auth_enabled then
|
||||
(.inbounds[] | select(.type == "mixed" and .tag == "mixed-in") | .users) = [{username:$user,password:$password}]
|
||||
else . end
|
||||
| if $route_enabled then
|
||||
.route.rules = $route_rules
|
||||
| .route.final = $route_final
|
||||
| .dns.final = $dns_final
|
||||
| if $clear_dns_rules then .dns.rules = [] else . end
|
||||
else . end' \
|
||||
'${mkBackendJqFilter}' \
|
||||
"${configFile}" > "$RUNTIME_DIR/config.json"
|
||||
${lib.optionalString enableLocalProxyAuth ''
|
||||
chmod 600 "$RUNTIME_DIR/config.json"
|
||||
''}
|
||||
|
||||
exec ${singBox} run -c "$RUNTIME_DIR/config.json"
|
||||
exec ${backendBin} run -c "$RUNTIME_DIR/config.json"
|
||||
'';
|
||||
|
||||
startSocks = mkStartScript {
|
||||
@@ -371,19 +431,16 @@ let
|
||||
name = "proxy-suite-start-tun";
|
||||
runtimeDir = "/run/proxy-suite-tun";
|
||||
configFile = tunFile;
|
||||
routingMark = null;
|
||||
routingMark = if xrayEnabled then globalTproxy.proxyMark else null;
|
||||
};
|
||||
|
||||
startPerAppTun = mkStartScript {
|
||||
name = "proxy-suite-start-per-app-tun";
|
||||
runtimeDir = "/run/proxy-suite-per-app-tun";
|
||||
configFile = perAppTunFile;
|
||||
# Only needed when TProxy mode is enabled; otherwise avoid tagging app TUN
|
||||
# proxy outbounds with a host-global mark.
|
||||
routingMark = if globalTproxy.enable then globalTproxy.proxyMark else null;
|
||||
};
|
||||
|
||||
# Shell code to fetch a single subscription into the cache (used in update service).
|
||||
mkSubscriptionFetchBlock =
|
||||
sub:
|
||||
let
|
||||
@@ -409,7 +466,7 @@ let
|
||||
mkdir -p "$CACHE_DIR"
|
||||
FAILED=0
|
||||
${subscriptionCacheHelpersBlock}
|
||||
${lib.concatMapStrings mkSubscriptionFetchBlock singBoxCfg.subscriptions}
|
||||
${lib.concatMapStrings mkSubscriptionFetchBlock proxyCfg.subscriptions}
|
||||
|
||||
if [ "$FAILED" -eq 0 ]; then
|
||||
${restartActiveConfigConsumersBlock}
|
||||
@@ -449,7 +506,7 @@ let
|
||||
'';
|
||||
|
||||
subscriptionTagsFile = pkgs.writeText "proxy-suite-subscription-tags.json" (
|
||||
builtins.toJSON (map (sub: sub.tag) singBoxCfg.subscriptions)
|
||||
builtins.toJSON (map (sub: sub.tag) proxyCfg.subscriptions)
|
||||
);
|
||||
in
|
||||
{
|
||||
@@ -460,6 +517,7 @@ in
|
||||
subscriptionUpdateScript
|
||||
hasSubscriptions
|
||||
subscriptionTagsFile
|
||||
subscriptionCacheDir
|
||||
runtimeProxychainsConfig
|
||||
;
|
||||
}
|
||||
Reference in new issue
Block a user