feat(wip): xray proxy backend
This commit is contained in:
36 files changed
+2265
-996
No files matched your search
@@ -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