other improvements
This commit is contained in:
11 files changed
+192
-26
No files matched your search
@@ -76,11 +76,14 @@ let
|
||||
subscriptionType = types.submodule {
|
||||
options = {
|
||||
tag = mkOption {
|
||||
type = types.str;
|
||||
type = types.strMatching "^[A-Za-z0-9][A-Za-z0-9._-]*$";
|
||||
description = ''
|
||||
Unique identifier for this subscription.
|
||||
Used as a prefix for all outbound tags generated from its proxy list,
|
||||
e.g. "my-sub" -> tags like "my-sub-Server-DE".
|
||||
|
||||
This value is also used as the subscription cache filename stem under
|
||||
/var/lib/proxy-suite/subscriptions/, so it must be a safe identifier.
|
||||
'';
|
||||
example = "community-list";
|
||||
};
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
perAppZapretCfg,
|
||||
builtinTags,
|
||||
outboundTags,
|
||||
subscriptionTags,
|
||||
invalidRoutingTargets,
|
||||
effectivePerAppRoutingProfileNames,
|
||||
hasProxychainsProfiles,
|
||||
@@ -30,6 +31,10 @@ in
|
||||
assertion = !singBoxCfg.enable || builtins.length outboundTags == builtins.length (lib.unique outboundTags);
|
||||
message = "proxy-suite: outbound tags must be unique";
|
||||
}
|
||||
{
|
||||
assertion = !singBoxCfg.enable || builtins.length subscriptionTags == builtins.length (lib.unique subscriptionTags);
|
||||
message = "proxy-suite: subscription tags must be unique because they are used as cache keys and outbound tag prefixes";
|
||||
}
|
||||
{
|
||||
assertion = !singBoxCfg.enable || builtins.all (tag: !builtins.elem tag builtinTags) outboundTags;
|
||||
message = "proxy-suite: outbound tags must not use reserved names: proxy, direct, block";
|
||||
|
||||
@@ -26,6 +26,7 @@ let
|
||||
|
||||
builtinTags = [ "proxy" "direct" "block" ];
|
||||
outboundTags = map (ob: ob.tag) singBoxCfg.outbounds;
|
||||
subscriptionTags = map (sub: sub.tag) singBoxCfg.subscriptions;
|
||||
invalidRoutingTargets = lib.unique (
|
||||
map (rule: rule.outbound) (
|
||||
builtins.filter (rule: !builtins.elem rule.outbound (builtinTags ++ outboundTags)) singBoxCfg.routing.rules
|
||||
@@ -69,7 +70,7 @@ let
|
||||
control = import ./control.nix {
|
||||
inherit packages singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy perAppZapretCfg;
|
||||
zapretEnabled = cfg.zapret.enable;
|
||||
inherit (scripts) subscriptionTagsList;
|
||||
inherit (scripts) subscriptionTagsFile;
|
||||
inherit (perAppRouting)
|
||||
perAppRoutingProfilesFile
|
||||
proxychainsConfigFile
|
||||
@@ -89,6 +90,7 @@ in
|
||||
userControlCfg
|
||||
builtinTags
|
||||
outboundTags
|
||||
subscriptionTags
|
||||
invalidRoutingTargets
|
||||
polkit
|
||||
scripts
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
perAppRoutingTproxy,
|
||||
perAppZapretCfg,
|
||||
zapretEnabled,
|
||||
subscriptionTagsList,
|
||||
subscriptionTagsFile,
|
||||
perAppRoutingProfilesFile,
|
||||
proxychainsConfigFile,
|
||||
proxychainsQuietArg,
|
||||
@@ -17,7 +17,7 @@
|
||||
clashApi = "http://127.0.0.1:${toString singBoxCfg.clashApiPort}";
|
||||
selection = singBoxCfg.selection;
|
||||
inherit
|
||||
subscriptionTagsList
|
||||
subscriptionTagsFile
|
||||
perAppRoutingProfilesFile
|
||||
proxychainsConfigFile
|
||||
proxychainsQuietArg
|
||||
|
||||
@@ -45,6 +45,7 @@ let
|
||||
userControlCfg
|
||||
builtinTags
|
||||
outboundTags
|
||||
subscriptionTags
|
||||
invalidRoutingTargets
|
||||
polkit
|
||||
scripts
|
||||
@@ -101,7 +102,7 @@ in
|
||||
inherit singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy;
|
||||
perAppZapretCfg = perAppZapretCfg;
|
||||
tgWsProxyCfg = cfg.tgWsProxy;
|
||||
inherit builtinTags outboundTags invalidRoutingTargets;
|
||||
inherit builtinTags outboundTags subscriptionTags invalidRoutingTargets;
|
||||
inherit (perAppRouting)
|
||||
effectivePerAppRoutingProfileNames
|
||||
hasProxychainsProfiles
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
|
||||
let
|
||||
globalTproxy = singBoxCfg.tproxy;
|
||||
subscriptionCacheDir = "/var/lib/proxy-suite/subscriptions";
|
||||
|
||||
mkSubscriptionCacheFile = sub: "${subscriptionCacheDir}/${sub.tag}.json";
|
||||
|
||||
mkSubscriptionUrlSource =
|
||||
sub:
|
||||
@@ -76,10 +79,13 @@ let
|
||||
# Fetches subscription into the cache on first run; subsequent starts use the cache.
|
||||
mkSubscriptionBlock =
|
||||
sub: routingMark:
|
||||
let
|
||||
cacheFile = mkSubscriptionCacheFile sub;
|
||||
in
|
||||
''
|
||||
# subscription: ${sub.tag}
|
||||
CACHE_DIR="/var/lib/proxy-suite/subscriptions"
|
||||
CACHE_FILE="$CACHE_DIR/${lib.escapeShellArg sub.tag}.json"
|
||||
CACHE_DIR="${subscriptionCacheDir}"
|
||||
CACHE_FILE="${cacheFile}"
|
||||
if [ ! -f "$CACHE_FILE" ]; then
|
||||
mkdir -p "$CACHE_DIR"
|
||||
if ${mkSubscriptionFetchCommand sub} > "$CACHE_FILE.tmp"; then
|
||||
@@ -195,13 +201,15 @@ let
|
||||
# Shell code to fetch a single subscription into the cache (used in update service).
|
||||
mkSubscriptionFetchBlock =
|
||||
sub:
|
||||
let
|
||||
cacheFile = mkSubscriptionCacheFile sub;
|
||||
in
|
||||
''
|
||||
if ${mkSubscriptionFetchCommand sub} > "$CACHE_DIR/${lib.escapeShellArg sub.tag}.json.tmp"; then
|
||||
mv "$CACHE_DIR/${lib.escapeShellArg sub.tag}.json.tmp" \
|
||||
"$CACHE_DIR/${lib.escapeShellArg sub.tag}.json"
|
||||
if ${mkSubscriptionFetchCommand sub} > "${cacheFile}.tmp"; then
|
||||
mv "${cacheFile}.tmp" "${cacheFile}"
|
||||
echo "Updated subscription: ${sub.tag}"
|
||||
else
|
||||
rm -f "$CACHE_DIR/${lib.escapeShellArg sub.tag}.json.tmp"
|
||||
rm -f "${cacheFile}.tmp"
|
||||
echo "proxy-suite: failed to update subscription '${sub.tag}'" >&2
|
||||
FAILED=1
|
||||
fi
|
||||
@@ -209,23 +217,39 @@ let
|
||||
|
||||
subscriptionUpdateScript = pkgs.writeShellScript "proxy-suite-subscription-update" ''
|
||||
set -euo pipefail
|
||||
CACHE_DIR="/var/lib/proxy-suite/subscriptions"
|
||||
CACHE_DIR="${subscriptionCacheDir}"
|
||||
mkdir -p "$CACHE_DIR"
|
||||
FAILED=0
|
||||
SOCKS_WAS_ACTIVE=0
|
||||
TUN_WAS_ACTIVE=0
|
||||
PER_APP_TUN_WAS_ACTIVE=0
|
||||
|
||||
systemctl is-active --quiet proxy-suite-socks && SOCKS_WAS_ACTIVE=1 || true
|
||||
systemctl is-active --quiet proxy-suite-tun && TUN_WAS_ACTIVE=1 || true
|
||||
systemctl is-active --quiet proxy-suite-per-app-tun && PER_APP_TUN_WAS_ACTIVE=1 || true
|
||||
|
||||
${lib.concatMapStrings mkSubscriptionFetchBlock singBoxCfg.subscriptions}
|
||||
|
||||
if [ "$FAILED" -eq 0 ]; then
|
||||
systemctl restart proxy-suite-socks
|
||||
systemctl is-active --quiet proxy-suite-tun && systemctl restart proxy-suite-tun || true
|
||||
if [ "$SOCKS_WAS_ACTIVE" -eq 1 ]; then
|
||||
systemctl restart proxy-suite-socks
|
||||
fi
|
||||
if [ "$TUN_WAS_ACTIVE" -eq 1 ]; then
|
||||
systemctl restart proxy-suite-tun
|
||||
fi
|
||||
if [ "$PER_APP_TUN_WAS_ACTIVE" -eq 1 ]; then
|
||||
systemctl restart proxy-suite-per-app-tun
|
||||
fi
|
||||
fi
|
||||
exit "$FAILED"
|
||||
'';
|
||||
|
||||
hasSubscriptions = singBoxCfg.subscriptions != [ ];
|
||||
subscriptionTagsList = lib.concatStringsSep " " (map (sub: lib.escapeShellArg sub.tag) singBoxCfg.subscriptions);
|
||||
subscriptionTagsFile = pkgs.writeText "proxy-suite-subscription-tags.json" (
|
||||
builtins.toJSON (map (sub: sub.tag) singBoxCfg.subscriptions)
|
||||
);
|
||||
in
|
||||
{
|
||||
inherit startSocks startTun startPerAppTun;
|
||||
inherit subscriptionUpdateScript hasSubscriptions subscriptionTagsList;
|
||||
inherit subscriptionUpdateScript hasSubscriptions subscriptionTagsFile;
|
||||
}
|
||||
Reference in new issue
Block a user