chore: refactoring (split)
This commit is contained in:
84 files changed
+9149
-7244
No files matched your search
@@ -0,0 +1,98 @@
|
||||
{
|
||||
pureXrayEnabled,
|
||||
selectionMode,
|
||||
}:
|
||||
|
||||
if pureXrayEnabled then
|
||||
''
|
||||
def xray_bind_outbound($interface):
|
||||
if $interface == "" or .protocol == "blackhole" or .protocol == "dns" then
|
||||
.
|
||||
else
|
||||
.streamSettings = (.streamSettings // {})
|
||||
| .streamSettings.sockopt = (.streamSettings.sockopt // {})
|
||||
| if (.streamSettings.sockopt.interface? // "") == "" then
|
||||
.streamSettings.sockopt.interface = $interface
|
||||
else
|
||||
.
|
||||
end
|
||||
end;
|
||||
def xray_proxy_rule_target($single_tag):
|
||||
if $single_tag != "" then
|
||||
{outboundTag:$single_tag}
|
||||
else
|
||||
{balancerTag:"proxy"}
|
||||
end;
|
||||
def xray_rewrite_proxy_rule($single_tag):
|
||||
if $single_tag != "" and (.balancerTag? // "") == "proxy" then
|
||||
.outboundTag = $single_tag | del(.balancerTag)
|
||||
else
|
||||
.
|
||||
end;
|
||||
def xray_preserved_rules:
|
||||
[.routing.rules[]
|
||||
| select(((.ruleTag? // "") == "dns-hijack") or ((.ruleTag? // "") == "dns-upstream-direct"))];
|
||||
def xray_final_rule($tag; $single_tag):
|
||||
if $tag == "proxy" and "${selectionMode}" == "urltest" then
|
||||
{type:"field",network:"tcp,udp",ruleTag:"final-default"} + xray_proxy_rule_target($single_tag)
|
||||
else
|
||||
{type:"field",network:"tcp,udp",ruleTag:"final-default",outboundTag:$tag}
|
||||
end;
|
||||
def xray_dns_server_order($dns_final):
|
||||
if $dns_final == "" then
|
||||
.dns.servers
|
||||
else
|
||||
([.dns.servers[] | select((.tag? // "") == "fakedns")]
|
||||
+ [.dns.servers[] | select((.tag? // "") == $dns_final)]
|
||||
+ [.dns.servers[] | select((.tag? // "") != "fakedns" and (.tag? // "") != $dns_final)])
|
||||
end;
|
||||
def xray_tun_dns_addresses($dns_final; $local_addr; $remote_addr):
|
||||
if $dns_final == "local" then
|
||||
[$local_addr, $remote_addr]
|
||||
elif $dns_final == "remote" then
|
||||
[$remote_addr, $local_addr]
|
||||
else
|
||||
[$remote_addr, $local_addr]
|
||||
end;
|
||||
.outbounds = (($obs + .outbounds) | map(xray_bind_outbound($xray_bind_interface)))
|
||||
| if $xray_loglevel == "" then . else .log.loglevel = $xray_loglevel end
|
||||
| if $auth_enabled then
|
||||
(.inbounds[] | select(.protocol == "socks" and .tag == "mixed-in") | .settings.auth) = "password"
|
||||
| (.inbounds[] | select(.protocol == "socks" and .tag == "mixed-in") | .settings.accounts) = [{user:$user,pass:$password}]
|
||||
else . end
|
||||
| if $xray_tun_dns_runtime then
|
||||
.dns.servers = xray_dns_server_order($dns_final)
|
||||
| if $dns_final == "" then
|
||||
.
|
||||
else
|
||||
(.inbounds[] | select(.protocol == "tun" and .tag == "tun-in") | .settings.dns) = xray_tun_dns_addresses($dns_final; $xray_dns_local_client; $xray_dns_remote_client)
|
||||
end
|
||||
else . end
|
||||
| if $route_enabled then
|
||||
.routing.rules = (xray_preserved_rules + $route_rules + [xray_final_rule($route_final; $xray_single_proxy_tag)])
|
||||
else . end
|
||||
| if $xray_single_proxy_tag == "" then
|
||||
.
|
||||
else
|
||||
.routing.rules |= map(xray_rewrite_proxy_rule($xray_single_proxy_tag))
|
||||
| del(.routing.balancers)
|
||||
| del(.observatory)
|
||||
end
|
||||
''
|
||||
else
|
||||
''
|
||||
def sing_box_preserved_rules:
|
||||
[.route.rules[]
|
||||
| select(((.action? // "") == "hijack-dns")
|
||||
and (((.inbound? // []) | index("xray-dns-in")) != null))];
|
||||
.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 = (sing_box_preserved_rules + $route_rules)
|
||||
| .route.final = $route_final
|
||||
| .dns.final = $dns_final
|
||||
| if $clear_dns_rules then .dns.rules = [] else . end
|
||||
else . end
|
||||
''
|
||||
@@ -0,0 +1,111 @@
|
||||
{
|
||||
lib,
|
||||
jq,
|
||||
hybridEnabled,
|
||||
xraySidecarRoutingMark,
|
||||
}:
|
||||
|
||||
routingMark: xraySidecarBasePort: xrayDnsBridgePort:
|
||||
|
||||
lib.optionalString hybridEnabled ''
|
||||
XRAY_OUTBOUNDS_JSON='[]'
|
||||
XRAY_INBOUNDS_JSON='[]'
|
||||
XRAY_ROUTE_RULES_JSON='[]'
|
||||
XRAY_SIDECAR_NEXT_PORT=${toString xraySidecarBasePort}
|
||||
XRAY_SIDECAR_DNS_PORT=${toString xrayDnsBridgePort}
|
||||
|
||||
_proxy_suite_add_sing_box_ob() {
|
||||
local ob="$1"
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson ob "$ob" '. + [$ob]' <<< "$OUTBOUNDS_JSON")
|
||||
}
|
||||
|
||||
_proxy_suite_next_xray_sidecar_port() {
|
||||
local port="$XRAY_SIDECAR_NEXT_PORT"
|
||||
XRAY_SIDECAR_NEXT_PORT=$((XRAY_SIDECAR_NEXT_PORT + 1))
|
||||
printf '%s\n' "$port"
|
||||
}
|
||||
|
||||
_proxy_suite_add_xray_sidecar_ob() {
|
||||
local ob="$1" tag="$2" port auth inbound_tag sing_ob inbound route_rule
|
||||
port="$(_proxy_suite_next_xray_sidecar_port)"
|
||||
auth="proxy-suite-xray-$port"
|
||||
inbound_tag="$tag-inbound"
|
||||
ob=$(${jq} \
|
||||
--arg tag "$tag" \
|
||||
--argjson mark ${toString xraySidecarRoutingMark} \
|
||||
'.tag = $tag
|
||||
| .streamSettings = (.streamSettings // {})
|
||||
| .streamSettings.sockopt = (.streamSettings.sockopt // {})
|
||||
| .streamSettings.sockopt.mark = $mark
|
||||
| .streamSettings.sockopt.domainStrategy = (.streamSettings.sockopt.domainStrategy // "UseIP")' \
|
||||
<<< "$ob")
|
||||
XRAY_OUTBOUNDS_JSON=$(${jq} --argjson ob "$ob" '. + [$ob]' <<< "$XRAY_OUTBOUNDS_JSON")
|
||||
inbound=$(${jq} -n \
|
||||
--arg tag "$inbound_tag" \
|
||||
--arg auth "$auth" \
|
||||
--argjson port "$port" \
|
||||
'{tag:$tag,listen:"127.0.0.1",port:$port,protocol:"socks",settings:{auth:"password",udp:true,accounts:[{user:$auth,pass:$auth}]}}')
|
||||
XRAY_INBOUNDS_JSON=$(${jq} --argjson inbound "$inbound" '. + [$inbound]' <<< "$XRAY_INBOUNDS_JSON")
|
||||
route_rule=$(${jq} -n \
|
||||
--arg inbound "$inbound_tag" \
|
||||
--arg outbound "$tag" \
|
||||
'{type:"field",inboundTag:[$inbound],outboundTag:$outbound}')
|
||||
XRAY_ROUTE_RULES_JSON=$(${jq} --argjson rule "$route_rule" '. + [$rule]' <<< "$XRAY_ROUTE_RULES_JSON")
|
||||
sing_ob=$(${jq} -n \
|
||||
--arg tag "$tag" \
|
||||
--arg auth "$auth" \
|
||||
--argjson port "$port" \
|
||||
'{type:"socks",tag:$tag,server:"127.0.0.1",server_port:$port,version:"5",username:$auth,password:$auth${
|
||||
lib.optionalString (routingMark != null) ",routing_mark:${toString routingMark}"
|
||||
}}')
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson ob "$sing_ob" '. + [$ob]' <<< "$OUTBOUNDS_JSON")
|
||||
}
|
||||
|
||||
_proxy_suite_write_xray_sidecar_config() {
|
||||
if [ "$(${jq} 'length' <<< "$XRAY_OUTBOUNDS_JSON")" -eq 0 ]; then
|
||||
return 0
|
||||
fi
|
||||
${jq} -n \
|
||||
--arg loglevel "$XRAY_LOGLEVEL" \
|
||||
--arg bind_interface "$XRAY_TUN_BIND_INTERFACE" \
|
||||
--argjson dns_port "$XRAY_SIDECAR_DNS_PORT" \
|
||||
--argjson inbounds "$XRAY_INBOUNDS_JSON" \
|
||||
--argjson outbounds "$XRAY_OUTBOUNDS_JSON" \
|
||||
--argjson route_rules "$XRAY_ROUTE_RULES_JSON" \
|
||||
'
|
||||
def bind_xray_sidecar($interface):
|
||||
if $interface == "" then
|
||||
.
|
||||
else
|
||||
.streamSettings = (.streamSettings // {})
|
||||
| .streamSettings.sockopt = (.streamSettings.sockopt // {})
|
||||
| if (.streamSettings.sockopt.interface? // "") == "" then
|
||||
.streamSettings.sockopt.interface = $interface
|
||||
else
|
||||
.
|
||||
end
|
||||
end;
|
||||
{
|
||||
log: {
|
||||
loglevel: (if $loglevel == "" then "warning" else $loglevel end),
|
||||
access: "none"
|
||||
},
|
||||
dns: {
|
||||
servers: [
|
||||
{
|
||||
address: "127.0.0.1",
|
||||
port: $dns_port,
|
||||
queryStrategy: "UseIP",
|
||||
skipFallBack: true
|
||||
}
|
||||
]
|
||||
},
|
||||
inbounds: $inbounds,
|
||||
outbounds: (($outbounds | map(bind_xray_sidecar($bind_interface))) + [{protocol:"freedom",tag:"direct"}]),
|
||||
routing: {
|
||||
domainStrategy: "AsIs",
|
||||
rules: ($route_rules + [{type:"field",ip:["127.0.0.1"],port:$dns_port,outboundTag:"direct"}])
|
||||
}
|
||||
}' > "$RUNTIME_DIR/xray-sidecar.json"
|
||||
}
|
||||
''
|
||||
@@ -0,0 +1,230 @@
|
||||
# Outbound construction script fragments for backend startup scripts.
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
singBoxCfg,
|
||||
proxyCfg,
|
||||
pureXrayEnabled,
|
||||
hybridEnabled,
|
||||
collapseNamedOutbounds,
|
||||
selectionMode,
|
||||
backend,
|
||||
backendArg,
|
||||
xraySidecarRoutingMark,
|
||||
jq,
|
||||
python3,
|
||||
parserScriptsPythonPath,
|
||||
buildOutboundPy,
|
||||
mkSubscriptionBlock,
|
||||
}:
|
||||
|
||||
let
|
||||
rawOutboundJson =
|
||||
ob: tag: routingMark:
|
||||
let
|
||||
backendRaw =
|
||||
if pureXrayEnabled then
|
||||
ob.xrayJson
|
||||
else if ob.singBoxJson != null then
|
||||
ob.singBoxJson
|
||||
else
|
||||
ob.json;
|
||||
markAttrs =
|
||||
if routingMark == null then
|
||||
{ }
|
||||
else if pureXrayEnabled then
|
||||
{ streamSettings.sockopt.mark = routingMark; }
|
||||
else
|
||||
{ routing_mark = routingMark; };
|
||||
taggedRaw = backendRaw // {
|
||||
inherit tag;
|
||||
};
|
||||
in
|
||||
if pureXrayEnabled then lib.recursiveUpdate taggedRaw markAttrs else taggedRaw // markAttrs;
|
||||
|
||||
mkOutboundBlock =
|
||||
ob: routingMark: tag:
|
||||
let
|
||||
backendRaw =
|
||||
if pureXrayEnabled then
|
||||
ob.xrayJson
|
||||
else if ob.singBoxJson != null then
|
||||
ob.singBoxJson
|
||||
else
|
||||
ob.json;
|
||||
in
|
||||
if backendRaw != null then
|
||||
let
|
||||
outboundJson = builtins.toJSON (rawOutboundJson ob tag routingMark);
|
||||
jsonFile = pkgs.writeText "proxy-suite-ob-${tag}.json" outboundJson;
|
||||
in
|
||||
''
|
||||
# outbound: ${tag} (static ${backend} json)
|
||||
OB_JSON=$(cat "${jsonFile}")
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson ob "$OB_JSON" '. + [$ob]' <<< "$OUTBOUNDS_JSON")
|
||||
''
|
||||
else
|
||||
let
|
||||
urlSource =
|
||||
if ob.urlFile != null then ob.urlFile else pkgs.writeText "proxy-suite-url-${ob.tag}" ob.url;
|
||||
in
|
||||
''
|
||||
# outbound: ${tag}
|
||||
URL=$(cat "${urlSource}")
|
||||
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")
|
||||
'';
|
||||
|
||||
singBoxRawOutboundJson =
|
||||
ob: tag: routingMark:
|
||||
let
|
||||
backendRaw = if ob.singBoxJson != null then ob.singBoxJson else ob.json;
|
||||
markAttrs = lib.optionalAttrs (routingMark != null) { routing_mark = routingMark; };
|
||||
in
|
||||
backendRaw // { inherit tag; } // markAttrs;
|
||||
|
||||
xrayRawOutboundJson =
|
||||
ob: tag:
|
||||
lib.recursiveUpdate (ob.xrayJson // { inherit tag; }) {
|
||||
streamSettings.sockopt = {
|
||||
mark = xraySidecarRoutingMark;
|
||||
domainStrategy = "UseIP";
|
||||
};
|
||||
};
|
||||
|
||||
mkHybridOutboundBlock =
|
||||
ob: routingMark: tag:
|
||||
if ob.xrayJson != null then
|
||||
let
|
||||
outboundJson = builtins.toJSON (xrayRawOutboundJson ob tag);
|
||||
jsonFile = pkgs.writeText "proxy-suite-xray-sidecar-ob-${tag}.json" outboundJson;
|
||||
in
|
||||
''
|
||||
# outbound: ${tag} (hybrid XRay json sidecar)
|
||||
OB_JSON=$(cat "${jsonFile}")
|
||||
_proxy_suite_add_xray_sidecar_ob "$OB_JSON" ${lib.escapeShellArg tag}
|
||||
''
|
||||
else if ob.singBoxJson != null || ob.json != null then
|
||||
let
|
||||
outboundJson = builtins.toJSON (singBoxRawOutboundJson ob tag routingMark);
|
||||
jsonFile = pkgs.writeText "proxy-suite-sing-box-ob-${tag}.json" outboundJson;
|
||||
in
|
||||
''
|
||||
# outbound: ${tag} (hybrid SingBox json)
|
||||
OB_JSON=$(cat "${jsonFile}")
|
||||
_proxy_suite_add_sing_box_ob "$OB_JSON"
|
||||
''
|
||||
else
|
||||
let
|
||||
urlSource =
|
||||
if ob.urlFile != null then ob.urlFile else pkgs.writeText "proxy-suite-url-${ob.tag}" ob.url;
|
||||
singBoxCommand = ''
|
||||
printf '%s' "$URL" | PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${buildOutboundPy} --backend sing-box --tag ${lib.escapeShellArg tag}${
|
||||
lib.optionalString (routingMark != null) " --routing-mark ${toString routingMark}"
|
||||
}
|
||||
'';
|
||||
xrayCommand = ''
|
||||
printf '%s' "$URL" | PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${buildOutboundPy} --backend xray --tag ${lib.escapeShellArg tag} --routing-mark ${toString xraySidecarRoutingMark}
|
||||
'';
|
||||
in
|
||||
if ob.backend == "xray" then
|
||||
''
|
||||
# outbound: ${tag} (hybrid forced XRay sidecar)
|
||||
URL=$(cat "${urlSource}")
|
||||
OB_JSON=$(${xrayCommand})
|
||||
_proxy_suite_add_xray_sidecar_ob "$OB_JSON" ${lib.escapeShellArg tag}
|
||||
''
|
||||
else if ob.backend == "sing-box" then
|
||||
''
|
||||
# outbound: ${tag} (hybrid forced SingBox)
|
||||
URL=$(cat "${urlSource}")
|
||||
OB_JSON=$(${singBoxCommand})
|
||||
_proxy_suite_add_sing_box_ob "$OB_JSON"
|
||||
''
|
||||
else
|
||||
''
|
||||
# outbound: ${tag} (hybrid auto)
|
||||
URL=$(cat "${urlSource}")
|
||||
if OB_JSON=$(${singBoxCommand} 2>"$RUNTIME_DIR/sing-box-parser.err"); then
|
||||
_proxy_suite_add_sing_box_ob "$OB_JSON"
|
||||
else
|
||||
SING_BOX_PARSE_ERROR="$(cat "$RUNTIME_DIR/sing-box-parser.err" 2>/dev/null || true)"
|
||||
if OB_JSON=$(${xrayCommand}); then
|
||||
_proxy_suite_add_xray_sidecar_ob "$OB_JSON" ${lib.escapeShellArg tag}
|
||||
else
|
||||
echo "proxy-suite: outbound '${tag}' cannot be parsed by SingBox or XRay" >&2
|
||||
if [ -n "$SING_BOX_PARSE_ERROR" ]; then
|
||||
printf '%s\n' "$SING_BOX_PARSE_ERROR" >&2
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
'';
|
||||
|
||||
mkBackendOutboundBlock = if hybridEnabled then mkHybridOutboundBlock else mkOutboundBlock;
|
||||
|
||||
requireOutboundsBlock = ''
|
||||
if [ "$(${jq} 'length' <<< "$OUTBOUNDS_JSON")" -eq 0 ]; then
|
||||
echo "proxy-suite: no proxy outbounds are available; check static outbound definitions and subscription caches" >&2
|
||||
exit 1
|
||||
fi
|
||||
'';
|
||||
|
||||
mkOutboundScript =
|
||||
routingMark:
|
||||
let
|
||||
outboundBlocks =
|
||||
if collapseNamedOutbounds && proxyCfg.outbounds != [ ] then
|
||||
mkBackendOutboundBlock (builtins.head proxyCfg.outbounds) routingMark "proxy"
|
||||
else
|
||||
lib.concatMapStrings (ob: mkBackendOutboundBlock ob routingMark ob.tag) proxyCfg.outbounds;
|
||||
|
||||
subscriptionBlocks = lib.concatMapStrings (
|
||||
sub: mkSubscriptionBlock sub routingMark
|
||||
) proxyCfg.subscriptions;
|
||||
|
||||
wrapperBlock =
|
||||
if pureXrayEnabled && selectionMode == "urltest" then
|
||||
''
|
||||
OUTBOUNDS_JSON=$(${jq} 'map(.tag = ("proxy-suite-ob-" + .tag))' <<< "$OUTBOUNDS_JSON")
|
||||
if [ "$(${jq} 'length' <<< "$OUTBOUNDS_JSON")" -eq 1 ]; then
|
||||
XRAY_SINGLE_PROXY_TAG="$(${jq} -r '.[0].tag' <<< "$OUTBOUNDS_JSON")"
|
||||
fi
|
||||
''
|
||||
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" \
|
||||
'map(if .tag == $t then .tag = "proxy" else . end)' <<< "$OUTBOUNDS_JSON")
|
||||
fi
|
||||
''
|
||||
else if selectionMode == "selector" then
|
||||
''
|
||||
TAGS=$(${jq} '[.[].tag]' <<< "$OUTBOUNDS_JSON")
|
||||
FIRST=$(${jq} -r '.[0].tag' <<< "$OUTBOUNDS_JSON")
|
||||
WRAPPER=$(${jq} -n \
|
||||
--argjson tags "$TAGS" \
|
||||
--arg default "$FIRST" \
|
||||
'{type:"selector",tag:"proxy",outbounds:$tags,default:$default}')
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson w "$WRAPPER" '[$w] + .' <<< "$OUTBOUNDS_JSON")
|
||||
''
|
||||
else
|
||||
''
|
||||
TAGS=$(${jq} '[.[].tag]' <<< "$OUTBOUNDS_JSON")
|
||||
WRAPPER=$(${jq} -n \
|
||||
--argjson tags "$TAGS" \
|
||||
--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")
|
||||
'';
|
||||
in
|
||||
outboundBlocks + subscriptionBlocks + requireOutboundsBlock + wrapperBlock;
|
||||
in
|
||||
{
|
||||
inherit mkOutboundScript rawOutboundJson;
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
# Subscription cache and outbound-loading script fragments.
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
proxyCfg,
|
||||
hasSubscriptions,
|
||||
hybridEnabled,
|
||||
mainBackend,
|
||||
backend,
|
||||
jq,
|
||||
python3,
|
||||
parserScriptsPythonPath,
|
||||
fetchSubscriptionPy,
|
||||
routingMarkJq,
|
||||
}:
|
||||
|
||||
let
|
||||
subscriptionBackend = if hybridEnabled then "hybrid" else mainBackend;
|
||||
subscriptionBackendArg = "--backend ${subscriptionBackend}";
|
||||
subscriptionCacheDir = "/var/lib/proxy-suite/subscriptions/${backend}";
|
||||
|
||||
mkSubscriptionCacheFile = sub: "${subscriptionCacheDir}/${sub.tag}.json";
|
||||
|
||||
mkSubscriptionUrlSource =
|
||||
sub:
|
||||
if sub.urlFile != null then
|
||||
sub.urlFile
|
||||
else
|
||||
pkgs.writeText "proxy-suite-sub-url-${sub.tag}" sub.url;
|
||||
|
||||
mkSubscriptionFetchCommand =
|
||||
sub:
|
||||
let
|
||||
urlSource = mkSubscriptionUrlSource sub;
|
||||
in
|
||||
''
|
||||
printf '%s' "$(cat "${urlSource}")" \
|
||||
| PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${fetchSubscriptionPy} ${subscriptionBackendArg} --tag-prefix ${lib.escapeShellArg sub.tag}
|
||||
'';
|
||||
|
||||
subscriptionCacheHelpersBlock = lib.optionalString hasSubscriptions ''
|
||||
_proxy_suite_valid_subscription_cache() {
|
||||
[ -s "$1" ] && ${jq} -e ${
|
||||
lib.escapeShellArg (
|
||||
if hybridEnabled then
|
||||
''type == "object" and (.singBox | type == "array") and (.xray | type == "array")''
|
||||
else
|
||||
''type == "array"''
|
||||
)
|
||||
} "$1" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
_proxy_suite_commit_subscription_cache() {
|
||||
local tmp="$1" target="$2" tag="$3"
|
||||
if _proxy_suite_valid_subscription_cache "$tmp"; then
|
||||
mv "$tmp" "$target"
|
||||
return 0
|
||||
fi
|
||||
rm -f "$tmp"
|
||||
echo "proxy-suite: warning: subscription '$tag' produced an invalid cache; ignoring it" >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
_proxy_suite_drop_invalid_subscription_cache() {
|
||||
local target="$1" tag="$2"
|
||||
if [ -f "$target" ] && ! _proxy_suite_valid_subscription_cache "$target"; then
|
||||
rm -f "$target"
|
||||
echo "proxy-suite: warning: removed invalid subscription cache for '$tag'" >&2
|
||||
fi
|
||||
}
|
||||
'';
|
||||
|
||||
mkSubscriptionBlock =
|
||||
sub: routingMark:
|
||||
let
|
||||
cacheFile = mkSubscriptionCacheFile sub;
|
||||
markFilter = routingMarkJq routingMark;
|
||||
in
|
||||
''
|
||||
# subscription: ${sub.tag}
|
||||
CACHE_DIR="${subscriptionCacheDir}"
|
||||
CACHE_FILE="${cacheFile}"
|
||||
_proxy_suite_drop_invalid_subscription_cache "$CACHE_FILE" ${lib.escapeShellArg sub.tag}
|
||||
if [ ! -f "$CACHE_FILE" ]; then
|
||||
mkdir -p "$CACHE_DIR"
|
||||
if ${mkSubscriptionFetchCommand sub} > "$CACHE_FILE.tmp"; then
|
||||
_proxy_suite_commit_subscription_cache "$CACHE_FILE.tmp" "$CACHE_FILE" ${lib.escapeShellArg sub.tag} || true
|
||||
else
|
||||
rm -f "$CACHE_FILE.tmp"
|
||||
echo "proxy-suite: warning: could not fetch subscription '${sub.tag}'" >&2
|
||||
fi
|
||||
fi
|
||||
${
|
||||
if hybridEnabled then
|
||||
''
|
||||
if [ -f "$CACHE_FILE" ]; then
|
||||
SUB_SING_BOX_JSON=$(${jq} -c '.singBox' "$CACHE_FILE")
|
||||
${
|
||||
lib.optionalString (routingMark != null) ''
|
||||
SUB_SING_BOX_JSON=$(${jq} 'map(.${markFilter})' <<< "$SUB_SING_BOX_JSON")
|
||||
''
|
||||
}OUTBOUNDS_JSON=$(${jq} --argjson sub "$SUB_SING_BOX_JSON" '. + $sub' <<< "$OUTBOUNDS_JSON")
|
||||
while IFS= read -r SUB_XRAY_OB; do
|
||||
SUB_XRAY_TAG="$(${jq} -r '.tag' <<< "$SUB_XRAY_OB")"
|
||||
_proxy_suite_add_xray_sidecar_ob "$SUB_XRAY_OB" "$SUB_XRAY_TAG"
|
||||
done < <(${jq} -c '.xray[]' "$CACHE_FILE")
|
||||
fi
|
||||
''
|
||||
else
|
||||
''
|
||||
if [ -f "$CACHE_FILE" ]; then
|
||||
SUB_JSON=$(cat "$CACHE_FILE")
|
||||
${
|
||||
lib.optionalString (routingMark != null) ''
|
||||
SUB_JSON=$(${jq} 'map(.${markFilter})' <<< "$SUB_JSON")
|
||||
''
|
||||
}OUTBOUNDS_JSON=$(${jq} --argjson sub "$SUB_JSON" '. + $sub' <<< "$OUTBOUNDS_JSON")
|
||||
fi
|
||||
''
|
||||
}
|
||||
'';
|
||||
|
||||
mkSubscriptionFetchBlock =
|
||||
sub:
|
||||
let
|
||||
cacheFile = mkSubscriptionCacheFile sub;
|
||||
in
|
||||
''
|
||||
if ${mkSubscriptionFetchCommand sub} > "${cacheFile}.tmp"; then
|
||||
if _proxy_suite_commit_subscription_cache "${cacheFile}.tmp" "${cacheFile}" ${lib.escapeShellArg sub.tag}; then
|
||||
echo "Updated subscription: ${sub.tag}"
|
||||
else
|
||||
FAILED=1
|
||||
fi
|
||||
else
|
||||
rm -f "${cacheFile}.tmp"
|
||||
echo "proxy-suite: failed to update subscription '${sub.tag}'" >&2
|
||||
FAILED=1
|
||||
fi
|
||||
'';
|
||||
|
||||
subscriptionTagsFile = pkgs.writeText "proxy-suite-subscription-tags.json" (
|
||||
builtins.toJSON (map (sub: sub.tag) proxyCfg.subscriptions)
|
||||
);
|
||||
in
|
||||
{
|
||||
inherit
|
||||
subscriptionCacheDir
|
||||
subscriptionCacheHelpersBlock
|
||||
mkSubscriptionBlock
|
||||
mkSubscriptionFetchBlock
|
||||
subscriptionTagsFile
|
||||
;
|
||||
}
|
||||
Reference in new issue
Block a user