refactor: remove duplication

This commit is contained in:
FUFSoB committed 2026-09-18 21:39:18 +05:00
1 parent f7036f6075
commit 8a4ba2db6a
109 files changed
+2206 -3078

No files matched your search

+35 -149
View File
@@ -13,54 +13,15 @@
perAppTunChainFile,
perAppTproxyRulesFile,
perAppZapretRulesFile,
nftablesRulesFile,
ip,
nft,
}:
let
derived = import ../derived.nix { inherit lib cfg; };
constants = derived.constants;
inherit (derived)
singBoxCfg
proxyCfg
xrayCfg
proxyEnabled
singBoxEnabled
xrayEnabled
hybridEnabled
pureXrayEnabled
activeBackend
perAppRoutingCfg
globalTun
globalTproxy
perAppRoutingTun
perAppRoutingTproxy
perAppZapretCfg
zapretEngine
zapretCutoffEnabled
zapretCutoffProxyFallback
userControlCfg
selectionMode
builtinTags
outboundTags
subscriptionTags
invalidRoutingTargets
collapseNamedOutbounds
sshProxyCfg
sshProxyOutboundEnabled
sshProxyUnitEnabled
warpCfg
torCfg
awgOutbounds
proxyInboundsCfg
proxyInboundsEnabled
proxyInboundsAwg
proxyInboundsNeedLocalProxy
torOnionEnabled
proxyInboundsGuardPrivate
proxyInboundViaOutbounds
userControlAllows
;
# Only what this file itself needs; the rest reaches the sub-modules through ctx.
inherit (derived) singBoxCfg xrayCfg userControlCfg;
# Tool paths – defined once here and passed into sub-modules as needed.
jq = "${pkgs.jq}/bin/jq";
@@ -81,10 +42,7 @@ let
)
);
proxySuiteScriptsDir = builtins.path {
name = "proxy-suite-scripts";
path = ../../../scripts;
};
proxySuiteScriptsDir = import ../lib/scripts-dir.nix { inherit lib; };
parserScriptsPythonPath = proxySuiteScriptsDir;
buildOutboundPy = "${proxySuiteScriptsDir}/build-outbound.py";
buildInboundPy = "${proxySuiteScriptsDir}/build-inbound.py";
@@ -94,36 +52,38 @@ let
polkit = import ./polkit.nix { inherit userControlCfg; };
scripts = import ./scripts.nix {
# One context for the service layer: everything derived.nix computes, the tool paths and
# generated files above, and the shell-snippet builders. Sub-modules take it whole and
# `inherit` the names they use, so adding a value needs no plumbing in between.
ctx = derived // {
inherit
lib
pkgs
singBoxCfg
proxyCfg
sshProxyCfg
warpCfg
torCfg
awgOutbounds
xrayEnabled
hybridEnabled
pureXrayEnabled
activeBackend
perAppRoutingCfg
userControlCfg
selectionMode
collapseNamedOutbounds
constants
zapretCutoffProxyFallback
packages
cfg
builders
polkit
;
inherit
jq
python3
singBox
xray
grepBin
awk
sleepBin
headBin
seqBin
findBin
awgBin
;
inherit
proxySuiteScriptsDir
parserScriptsPythonPath
buildOutboundPy
buildInboundPy
fetchSubscriptionPy
amneziaWgProfileNamesFile
;
inherit
tproxyFile
@@ -132,102 +92,28 @@ let
routeModeRulesFile
proxyInboundsFile
proxyInboundsSpecFile
perAppTunChainFile
perAppTproxyRulesFile
perAppZapretRulesFile
nftablesRulesFile
ip
nft
;
inherit
proxyInboundsCfg
proxyInboundsAwg
awgBin
proxyInboundsNeedLocalProxy
torOnionEnabled
proxyInboundsGuardPrivate
proxyInboundViaOutbounds
userControlAllows
builders
;
# Filled in below; lazily, so the sub-modules can read each other's results.
inherit scripts perAppRouting;
};
perAppRouting = import ./per-app-routing.nix {
inherit
lib
pkgs
cfg
singBoxCfg
proxyCfg
perAppRoutingCfg
perAppRoutingTun
perAppRoutingTproxy
constants
;
perAppZapretCfg = perAppZapretCfg;
inherit perAppTunChainFile perAppTproxyRulesFile perAppZapretRulesFile;
inherit ip nft;
inherit
awk
grepBin
findBin
headBin
seqBin
sleepBin
;
};
control = import ./control.nix {
inherit
packages
singBoxCfg
proxyCfg
perAppRoutingCfg
perAppRoutingTun
perAppRoutingTproxy
perAppZapretCfg
zapretEngine
zapretCutoffEnabled
constants
selectionMode
userControlCfg
;
inherit (scripts) subscriptionTagsFile subscriptionCacheDir;
inherit (scripts) routeModeStateFile;
inherit proxyInboundsEnabled;
inherit (scripts) proxyInboundsLinksFile proxyInboundsSubscriptionsFile;
proxyInboundsSubscriptionsBaseUrl = proxyInboundsCfg.subscriptions.baseUrl;
proxyInboundsXray = "${proxyInboundsCfg.package}/bin/xray";
inherit amneziaWgProfileNamesFile;
guiRefreshInterval = cfg.gui.refreshInterval;
inherit (perAppRouting)
perAppRoutingProfilesFile
proxychainsConfigFile
proxychainsQuietArg
;
};
scripts = import ./scripts.nix { inherit ctx; };
perAppRouting = import ./per-app-routing.nix { inherit ctx; };
control = import ./control.nix { inherit ctx; };
in
{
inherit
ctx
derived
singBoxCfg
proxyCfg
xrayCfg
proxyEnabled
singBoxEnabled
xrayEnabled
hybridEnabled
pureXrayEnabled
activeBackend
perAppRoutingCfg
globalTun
globalTproxy
perAppRoutingTun
perAppRoutingTproxy
perAppZapretCfg
userControlCfg
builtinTags
outboundTags
subscriptionTags
invalidRoutingTargets
polkit
scripts
perAppRouting
control
amneziaWgProfileNamesFile
;
}
@@ -52,15 +52,7 @@ let
mode="''${1:-}"
case "$mode" in
default)
;;
whitelist)
;;
blacklist)
;;
all-proxy)
;;
all-bypass)
default | whitelist | blacklist | all-proxy | all-bypass)
;;
*)
echo "proxy-suite: route mode must be default, whitelist, blacklist, all-proxy, or all-bypass" >&2
+86 -64
View File
@@ -1,75 +1,97 @@
# Builds proxy-ctl from global proxy-suite state and per-app routing metadata.
{
packages,
singBoxCfg,
proxyCfg,
perAppRoutingCfg,
perAppRoutingTun,
perAppRoutingTproxy,
perAppZapretCfg,
zapretEngine,
zapretCutoffEnabled,
constants,
selectionMode,
userControlCfg,
subscriptionTagsFile,
subscriptionCacheDir,
perAppRoutingProfilesFile,
proxychainsConfigFile,
proxychainsQuietArg,
routeModeStateFile,
amneziaWgProfileNamesFile,
proxyInboundsEnabled,
proxyInboundsLinksFile,
proxyInboundsSubscriptionsFile,
proxyInboundsSubscriptionsBaseUrl,
proxyInboundsXray,
guiRefreshInterval,
}:
{ ctx }:
let
inherit (ctx)
lib
localProxy
packages
cfg
singBoxCfg
proxyCfg
perAppRoutingCfg
perAppRoutingTun
perAppRoutingTproxy
perAppZapretCfg
zapretEngine
zapretCutoffEnabled
constants
selectionMode
userControlCfg
proxyInboundsCfg
proxyInboundsEnabled
amneziaWgProfileNamesFile
;
inherit (ctx.scripts)
subscriptionTagsFile
subscriptionCacheDir
routeModeStateFile
proxyInboundsLinksFile
proxyInboundsSubscriptionsFile
;
inherit (ctx.perAppRouting)
perAppRoutingProfilesFile
proxychainsConfigFile
proxychainsQuietArg
;
proxyInboundsSubscriptionsBaseUrl = proxyInboundsCfg.subscriptions.baseUrl;
proxyInboundsXray = "${proxyInboundsCfg.package}/bin/xray";
guiRefreshInterval = cfg.gui.refreshInterval;
# proxy_ctl.py reads these as "1"/"0" strings.
flag = enabled: if enabled then "1" else "0";
in
{
proxyCtl = packages.mkProxyCtl {
clashApi = "http://127.0.0.1:${toString singBoxCfg.clashApiPort}";
selection = selectionMode;
inherit
guiRefreshInterval
subscriptionTagsFile
subscriptionCacheDir
perAppRoutingProfilesFile
proxychainsConfigFile
proxychainsQuietArg
routeModeStateFile
amneziaWgProfileNamesFile
guiRefreshInterval
;
defaultRouteMode = if (proxyCfg.routing.default == "proxy") 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";
perAppRoutingTproxyEnabled = if perAppRoutingTproxy.enable then "1" else "0";
perAppRoutingZapretEnabled = if perAppZapretCfg.enable then "1" else "0";
zapretAutoEnabled = if zapretEngine == "zapret2" then "1" else "0";
zapretStateDir = constants.zapret2StateDir;
zapretCutoffEnabled = if zapretCutoffEnabled then "1" else "0";
outboundInventoryFile = constants.outboundInventoryFile;
runtimeOutboundsDir = constants.runtimeOutboundsDir;
runtimeSubscriptionsDir = constants.runtimeSubscriptionsDir;
userControlGroup = userControlCfg.group;
inboundsEnabled = if proxyInboundsEnabled then "1" else "0";
inboundsLinksFile = proxyInboundsLinksFile;
inboundsStatsFile = constants.inboundStatsFile;
inboundsXray = proxyInboundsXray;
inboundsApi = "127.0.0.1:${toString constants.inboundStatsApiPort}";
inboundsSubscriptionsFile = proxyInboundsSubscriptionsFile;
inboundsSubscriptionsBaseUrl =
if proxyInboundsSubscriptionsBaseUrl == null then "" else proxyInboundsSubscriptionsBaseUrl;
autoProxyEnabled = if proxyCfg.autoProxy.enable then "1" else "0";
autoProxyStateDir = constants.autoProxyStateDir;
torControlSocket = constants.torControlSocket;
inherit (constants) stateDir runtimeDir serviceManager;
privileged = if constants.privileged then "1" else "0";
supervisorCtl = if constants.serviceManager == "supervisor" then constants.systemctl else "";
singBox = "${singBoxCfg.package}/bin/sing-box";
localProxyUrl = "http://${
if proxyCfg.listener.address == "0.0.0.0" then "127.0.0.1" else proxyCfg.listener.address
}:${toString proxyCfg.listener.port}";
env = {
CLASH_API = "http://127.0.0.1:${toString singBoxCfg.clashApiPort}";
SELECTION = selectionMode;
SUB_TAGS_FILE = toString subscriptionTagsFile;
SUB_CACHE_DIR = subscriptionCacheDir;
PER_APP_ROUTING_ENABLED = flag perAppRoutingCfg.enable;
PER_APP_ROUTING_PROXYCHAINS_ENABLED = flag perAppRoutingCfg.proxychains.enable;
PER_APP_ROUTING_TUN_ENABLED = flag perAppRoutingTun.enable;
PER_APP_ROUTING_TPROXY_ENABLED = flag perAppRoutingTproxy.enable;
PER_APP_ROUTING_ZAPRET_ENABLED = flag perAppZapretCfg.enable;
PER_APP_ROUTING_PROFILES_FILE = toString perAppRoutingProfilesFile;
PROXYCHAINS_CONFIG = toString proxychainsConfigFile;
PROXYCHAINS_QUIET_ARG = lib.removeSuffix " " proxychainsQuietArg;
ROUTE_MODE_STATE_FILE = routeModeStateFile;
DEFAULT_ROUTE_MODE = if proxyCfg.routing.default == "proxy" then "blacklist" else "whitelist";
AWG_PROFILES_FILE = toString amneziaWgProfileNamesFile;
INBOUNDS_ENABLED = flag proxyInboundsEnabled;
INBOUNDS_LINKS_FILE = proxyInboundsLinksFile;
INBOUNDS_STATS_FILE = constants.inboundStatsFile;
INBOUNDS_XRAY = proxyInboundsXray;
INBOUNDS_API = "127.0.0.1:${toString constants.inboundStatsApiPort}";
INBOUNDS_SUBS_FILE = proxyInboundsSubscriptionsFile;
INBOUNDS_SUB_BASE_URL =
if proxyInboundsSubscriptionsBaseUrl == null then "" else proxyInboundsSubscriptionsBaseUrl;
ZAPRET_AUTO_ENABLED = flag (zapretEngine == "zapret2");
ZAPRET_STATE_DIR = constants.zapret2StateDir;
ZAPRET_CUTOFF_ENABLED = flag zapretCutoffEnabled;
OUTBOUND_INVENTORY_FILE = constants.outboundInventoryFile;
RUNTIME_OUTBOUNDS_DIR = constants.runtimeOutboundsDir;
RUNTIME_SUBS_DIR = constants.runtimeSubscriptionsDir;
USER_CONTROL_GROUP = userControlCfg.group;
LOCAL_PROXY_URL = "http://${localProxy.hostPart}:${toString proxyCfg.listener.port}";
AUTOPROXY_ENABLED = flag proxyCfg.autoProxy.enable;
AUTOPROXY_STATE_DIR = constants.autoProxyStateDir;
TOR_CONTROL_SOCKET = constants.torControlSocket;
SING_BOX = "${singBoxCfg.package}/bin/sing-box";
STATE_DIR = constants.stateDir;
RUNTIME_DIR = constants.runtimeDir;
SERVICE_MANAGER = constants.serviceManager;
PRIVILEGED = flag constants.privileged;
}
// lib.optionalAttrs (constants.serviceManager == "supervisor") {
SUPERVISOR_CTL = constants.systemctl;
};
};
}
+5 -71
View File
@@ -3,43 +3,11 @@
{
lib,
pkgs,
packages,
cfg,
tproxyFile,
tunFile,
perAppTunFile,
routeModeRulesFile,
proxyInboundsFile,
proxyInboundsSpecFile,
nftablesRulesFile,
perAppTproxyRulesFile,
perAppZapretRulesFile,
perAppTunChainFile,
ip,
nft,
context,
}:
let
context = import ./context.nix {
inherit
lib
pkgs
packages
cfg
tproxyFile
tunFile
perAppTunFile
routeModeRulesFile
proxyInboundsFile
proxyInboundsSpecFile
perAppTunChainFile
perAppTproxyRulesFile
perAppZapretRulesFile
ip
nft
;
};
builders = import ./builders.nix { inherit lib pkgs; };
inherit (builders) mkNamedUnits;
@@ -79,46 +47,12 @@ let
builtinTags
;
constants = derived.constants;
routingScripts = import ./routing-scripts.nix {
inherit
lib
pkgs
builders
ip
nft
nftablesRulesFile
constants
globalTun
globalTproxy
perAppRoutingTun
;
inherit (proxyCfg) ipv6;
};
routingScripts = import ./routing-scripts.nix { inherit (context) ctx; };
serviceUnits = import ./units.nix {
inherit
lib
builders
proxyCfg
proxyEnabled
hybridEnabled
pureXrayEnabled
globalTun
globalTproxy
perAppRoutingTun
perAppRoutingTproxy
perAppZapretEnabled
sshProxyOutboundEnabled
sshProxyUnitEnabled
torOutboundEnabled
torOnionEnabled
proxyInboundsEnabled
proxyInboundsNeedLocalProxy
scripts
perAppRouting
routingScripts
;
inherit (cfg) geodata;
ctx = context.ctx // {
inherit routingScripts;
};
};
inherit (serviceUnits)
localProxyAuthEnabled
+38 -126
View File
@@ -1,135 +1,47 @@
# App routing backend infrastructure (TUN, TProxy, zapret).
{
lib,
pkgs,
cfg,
singBoxCfg,
proxyCfg,
perAppRoutingCfg,
perAppRoutingTun,
perAppRoutingTproxy,
constants,
perAppZapretCfg,
perAppTunChainFile,
perAppTproxyRulesFile,
perAppZapretRulesFile,
ip,
nft,
awk,
grepBin,
findBin,
headBin,
seqBin,
sleepBin,
}:
{ ctx }:
let
builders = import ./builders.nix { inherit lib pkgs; };
inherit (builders) mkAnchorService;
perAppTunSliceName = "proxy-suite-per-app-tun.slice";
perAppTproxySliceName = "proxy-suite-per-app-tproxy.slice";
perAppZapretSliceName = "proxy-suite-per-app-zapret.slice";
profiles = import ./per-app-routing/profiles.nix {
inherit (constants) runtimeDir;
inherit
lib
pkgs
proxyCfg
perAppRoutingCfg
perAppRoutingTun
perAppRoutingTproxy
perAppZapretCfg
;
};
inherit (profiles)
effectivePerAppRoutingProfiles
effectivePerAppRoutingProfileNames
perAppRoutingProfilesFile
proxychainsConfigFile
proxychainsQuietArg
hasProxychainsProfiles
hasTunProfiles
hasTproxyProfiles
hasZapretProfiles
inherit (ctx)
builders
nft
awk
grepBin
findBin
headBin
perAppRoutingTun
perAppRoutingTproxy
perAppZapretCfg
;
backendScripts = import ./per-app-routing/backend-scripts.nix {
inherit
lib
pkgs
builders
perAppRoutingTun
perAppRoutingTproxy
constants
perAppTunChainFile
perAppTproxyRulesFile
ip
nft
seqBin
sleepBin
;
inherit (proxyCfg) ipv6;
slices = {
perAppTunSliceName = "proxy-suite-per-app-tun.slice";
perAppTproxySliceName = "proxy-suite-per-app-tproxy.slice";
perAppZapretSliceName = "proxy-suite-per-app-zapret.slice";
};
inherit (backendScripts)
perAppTunUpScript
perAppTunDownScript
perAppTproxyUpScript
perAppTproxyDownScript
;
userRules = import ./per-app-routing/user-rules.nix {
inherit
lib
pkgs
perAppRoutingTun
perAppRoutingTproxy
perAppZapretCfg
perAppTunSliceName
perAppTproxySliceName
perAppZapretSliceName
nft
awk
grepBin
findBin
headBin
;
};
inherit (userRules)
perAppTunUserRuleStart
perAppTunUserRuleStop
perAppTproxyUserRuleStart
perAppTproxyUserRuleStop
perAppZapretUserRuleStart
perAppZapretUserRuleStop
;
# user-rules.nix is also imported by the checks, so it keeps an explicit argument list.
userRules = import ./per-app-routing/user-rules.nix (
{
inherit (ctx) lib pkgs;
inherit
perAppRoutingTun
perAppRoutingTproxy
perAppZapretCfg
nft
awk
grepBin
findBin
headBin
;
}
// slices
);
in
{
inherit
perAppTunSliceName
perAppTproxySliceName
perAppZapretSliceName
perAppTunUpScript
perAppTunDownScript
perAppTproxyUpScript
perAppTproxyDownScript
perAppTunUserRuleStart
perAppTunUserRuleStop
perAppTproxyUserRuleStart
perAppTproxyUserRuleStop
perAppZapretUserRuleStart
perAppZapretUserRuleStop
mkAnchorService
effectivePerAppRoutingProfiles
effectivePerAppRoutingProfileNames
perAppRoutingProfilesFile
proxychainsConfigFile
proxychainsQuietArg
hasProxychainsProfiles
hasTunProfiles
hasTproxyProfiles
hasZapretProfiles
;
slices
// import ./per-app-routing/profiles.nix { inherit ctx; }
// import ./per-app-routing/backend-scripts.nix { inherit ctx; }
// userRules
// {
inherit (builders) mkAnchorService;
}
@@ -1,21 +1,23 @@
# Per-app TUN and TProxy backend lifecycle scripts.
{
lib,
pkgs,
builders,
perAppRoutingTun,
perAppRoutingTproxy,
ipv6,
constants,
perAppTunChainFile,
perAppTproxyRulesFile,
ip,
nft,
seqBin,
sleepBin,
}:
{ ctx }:
let
inherit (ctx)
lib
pkgs
builders
perAppRoutingTun
perAppRoutingTproxy
proxyCfg
constants
perAppTunChainFile
perAppTproxyRulesFile
ip
nft
seqBin
sleepBin
;
inherit (proxyCfg) ipv6;
inherit (constants)
perAppTunIPv6Address
perAppTunIPv6RoutePrefix
@@ -1,16 +1,18 @@
# Per-app routing profile expansion and proxychains config.
{
lib,
pkgs,
proxyCfg,
perAppRoutingCfg,
perAppRoutingTun,
perAppRoutingTproxy,
perAppZapretCfg,
runtimeDir,
}:
{ ctx }:
let
inherit (ctx)
lib
pkgs
proxyCfg
perAppRoutingCfg
perAppRoutingTun
perAppRoutingTproxy
perAppZapretCfg
constants
;
inherit (constants) runtimeDir;
perAppRoutingProfileNames = map (profile: profile.name) perAppRoutingCfg.profiles;
defaultPerAppRoutingProfiles = lib.optionals perAppRoutingCfg.createDefaultProfiles (
[
@@ -46,9 +48,7 @@ let
effectivePerAppRoutingProfileNames = map (profile: profile.name) effectivePerAppRoutingProfiles;
localProxyAuth = proxyCfg.listener.auth;
localProxyAuthEnabled =
localProxyAuth.username != null
&& (localProxyAuth.password != null || localProxyAuth.passwordFile != null);
localProxyAuthEnabled = ctx.localProxy.authEnabled;
perAppRoutingProfilesFile = pkgs.writeText "proxy-suite-per-app" (
builtins.toJSON effectivePerAppRoutingProfiles
@@ -1,32 +1,33 @@
# Start script of the inbound service: renders the listeners with their secrets into the
# template and runs XRay.
{
lib,
pkgs,
proxyCfg,
proxyInboundsCfg,
proxyInboundsAwg,
awgBin,
proxyInboundsNeedLocalProxy,
torOnionEnabled,
proxyInboundViaOutbounds,
userControlCfg,
userControlAllows,
localProxyAuth,
localProxyAuthEnabled,
localProxyAuthPasswordSource,
jq,
python3,
parserScriptsPythonPath,
buildInboundPy,
buildOutboundPy,
proxyInboundsFile,
proxyInboundsSpecFile,
builders,
constants,
}:
{ ctx }:
let
inherit (ctx)
lib
pkgs
proxyCfg
proxyInboundsCfg
proxyInboundsAwg
awgBin
proxyInboundsNeedLocalProxy
torOnionEnabled
proxyInboundViaOutbounds
userControlCfg
userControlAllows
localProxyAuth
localProxyAuthEnabled
localProxyAuthPasswordSource
jq
python3
parserScriptsPythonPath
buildInboundPy
buildOutboundPy
proxyInboundsFile
proxyInboundsSpecFile
builders
constants
;
runtimeDir = "${constants.runtimeDir}/proxy-suite-inbounds";
linksFile = "${runtimeDir}/links.json";
subscriptionsFile = "${runtimeDir}/subscriptions.json";
+15 -13
View File
@@ -1,18 +1,20 @@
{
lib,
pkgs,
builders,
ip,
nft,
nftablesRulesFile,
constants,
globalTun,
globalTproxy,
perAppRoutingTun,
ipv6,
}:
{ ctx }:
let
inherit (ctx)
lib
pkgs
builders
ip
nft
nftablesRulesFile
constants
globalTun
globalTproxy
perAppRoutingTun
proxyCfg
;
inherit (proxyCfg) ipv6;
inherit (constants)
tunAutoRouteTableIndex
tunAutoRouteRulePriority
@@ -1,9 +1,13 @@
{
lib,
jq,
hybridEnabled,
xraySidecarRoutingMark,
}:
{ ctx }:
let
inherit (ctx)
lib
jq
hybridEnabled
xraySidecarRoutingMark
;
in
routingMark: xraySidecarBasePort: xrayDnsBridgePort:
@@ -3,23 +3,24 @@
# Everything is emitted as shell functions taking a tag and a file holding the
# subscription URL, so a subscription declared in Nix and one added at runtime
# under `subscriptions.d/` go through exactly the same code.
{
lib,
pkgs,
proxyCfg,
hybridEnabled,
mainBackend,
backend,
stateDir,
runtimeSubscriptionsDir,
jq,
python3,
parserScriptsPythonPath,
fetchSubscriptionPy,
routingMarkJq,
}:
{ ctx }:
let
inherit (ctx)
lib
pkgs
proxyCfg
hybridEnabled
mainBackend
backend
constants
jq
python3
parserScriptsPythonPath
fetchSubscriptionPy
routingMarkJq
;
inherit (constants) stateDir runtimeSubscriptionsDir;
subscriptionBackend = if hybridEnabled then "hybrid" else mainBackend;
subscriptionBackendArg = "--backend ${subscriptionBackend}";
subscriptionCacheDir = "${stateDir}/subscriptions/${backend}";
+66 -131
View File
@@ -1,49 +1,36 @@
# Assembles proxy backend startup and control scripts.
{
lib,
pkgs,
singBoxCfg,
proxyCfg,
sshProxyCfg,
warpCfg,
torCfg,
awgOutbounds,
xrayEnabled,
hybridEnabled,
pureXrayEnabled,
activeBackend,
perAppRoutingCfg,
userControlCfg,
selectionMode,
collapseNamedOutbounds,
constants,
zapretCutoffProxyFallback,
jq,
python3,
singBox,
xray,
parserScriptsPythonPath,
buildOutboundPy,
fetchSubscriptionPy,
tproxyFile,
tunFile,
perAppTunFile,
routeModeRulesFile,
proxyInboundsCfg,
proxyInboundsAwg,
awgBin,
proxyInboundsNeedLocalProxy,
torOnionEnabled,
proxyInboundsGuardPrivate,
proxyInboundViaOutbounds,
userControlAllows,
buildInboundPy,
proxyInboundsFile,
proxyInboundsSpecFile,
builders,
}:
{ ctx }:
let
inherit (ctx)
lib
pkgs
singBoxCfg
proxyCfg
activeBackend
xrayEnabled
hybridEnabled
pureXrayEnabled
perAppRoutingCfg
userControlCfg
userControlAllows
selectionMode
constants
zapretCutoffProxyFallback
jq
python3
singBox
xray
proxyInboundsCfg
proxyInboundsGuardPrivate
routeModeRulesFile
tproxyFile
tunFile
perAppTunFile
proxyInboundsFile
proxyInboundsSpecFile
builders
;
globalTproxy = proxyCfg.tproxy;
backend = if activeBackend == null then "sing-box" else activeBackend;
mainBackend = if pureXrayEnabled then "xray" else "sing-box";
@@ -81,23 +68,43 @@ let
else
" | .routing_mark = ${toString routingMark}";
subscriptionScripts = import ./script-blocks/subscriptions.nix {
inherit (constants) stateDir;
# ctx plus this layer's own values; the script blocks read what they need off it. Lazy, so
# a block may use a sibling's output (start-scripts uses the subscription helpers).
sctx = ctx // {
inherit
lib
pkgs
proxyCfg
hybridEnabled
mainBackend
backend
runtimeSubscriptionsDir
jq
python3
parserScriptsPythonPath
fetchSubscriptionPy
mainBackend
backendArg
backendBin
globalTproxy
localProxyAuth
localProxyAuthEnabled
localProxyAuthPasswordSource
routeModeStateFile
xrayLoglevelFile
runtimeProxychainsConfig
xraySidecarRoutingMark
routingMarkJq
pinnedOutboundFile
runtimeOutboundsDir
runtimeSubscriptionsDir
outboundInventoryFile
clashApi
;
inherit (subscriptionScripts)
subscriptionCacheDir
subscriptionCacheHelpersBlock
mkSubscriptionLoadHelperBlock
mkSubscriptionBlock
runtimeSubscriptionsBlock
mkSubscriptionFetchBlock
runtimeSubscriptionsFetchBlock
;
inherit (outboundScripts) mkOutboundScript;
inherit hybridRuntimeHelpersBlock backendJqFilterFile;
};
subscriptionScripts = import ./script-blocks/subscriptions.nix { ctx = sctx; };
inherit (subscriptionScripts)
subscriptionCacheDir
subscriptionCacheHelpersBlock
@@ -110,7 +117,7 @@ let
;
outboundScripts = import ./script-blocks/outbounds.nix {
inherit
inherit (sctx)
lib
pkgs
singBoxCfg
@@ -140,14 +147,7 @@ let
};
inherit (outboundScripts) mkOutboundScript;
hybridRuntimeHelpersBlock = import ./script-blocks/hybrid-runtime-helpers.nix {
inherit
lib
jq
hybridEnabled
xraySidecarRoutingMark
;
};
hybridRuntimeHelpersBlock = import ./script-blocks/hybrid-runtime-helpers.nix { ctx = sctx; };
backendJqFilter = import ./script-blocks/backend-jq-filter.nix {
inherit
@@ -160,69 +160,10 @@ let
};
backendJqFilterFile = pkgs.writeText "proxy-suite-core" backendJqFilter;
startScripts = import ./start-scripts.nix {
inherit
lib
pkgs
proxyCfg
perAppRoutingCfg
userControlCfg
userControlAllows
globalTproxy
xrayEnabled
hybridEnabled
pureXrayEnabled
constants
zapretCutoffProxyFallback
jq
singBox
xray
backendBin
routeModeStateFile
routeModeRulesFile
xrayLoglevelFile
runtimeProxychainsConfig
localProxyAuth
localProxyAuthEnabled
localProxyAuthPasswordSource
backendJqFilterFile
hybridRuntimeHelpersBlock
subscriptionCacheHelpersBlock
mkOutboundScript
tproxyFile
tunFile
perAppTunFile
;
};
startScripts = import ./start-scripts.nix { ctx = sctx; };
inherit (startScripts) startSocks startTun startPerAppTun;
proxyInboundsScripts = import ./proxy-inbounds-scripts.nix {
inherit
lib
pkgs
proxyCfg
proxyInboundsCfg
proxyInboundsAwg
awgBin
proxyInboundsNeedLocalProxy
torOnionEnabled
proxyInboundViaOutbounds
userControlCfg
userControlAllows
localProxyAuth
localProxyAuthEnabled
localProxyAuthPasswordSource
jq
python3
parserScriptsPythonPath
buildInboundPy
buildOutboundPy
proxyInboundsFile
proxyInboundsSpecFile
builders
constants
;
};
proxyInboundsScripts = import ./proxy-inbounds-scripts.nix { ctx = sctx; };
inherit (proxyInboundsScripts) startInbounds collectInboundStats;
proxyInboundsLinksFile = proxyInboundsScripts.linksFile;
proxyInboundsSubscriptionsFile = proxyInboundsScripts.subscriptionsFile;
@@ -271,11 +212,5 @@ in
subscriptionUpdateScript
subscriptionTagsFile
subscriptionCacheDir
runtimeProxychainsConfig
pinnedOutboundFile
runtimeOutboundsDir
runtimeSubscriptionsDir
outboundInventoryFile
clashApi
;
}
+112 -84
View File
@@ -1,38 +1,39 @@
# Backend startup script derivations.
{
lib,
pkgs,
proxyCfg,
perAppRoutingCfg,
userControlCfg,
userControlAllows,
globalTproxy,
xrayEnabled,
hybridEnabled,
pureXrayEnabled,
constants,
zapretCutoffProxyFallback,
jq,
singBox,
xray,
backendBin,
routeModeStateFile,
routeModeRulesFile,
xrayLoglevelFile,
runtimeProxychainsConfig,
localProxyAuth,
localProxyAuthEnabled,
localProxyAuthPasswordSource,
backendJqFilterFile,
hybridRuntimeHelpersBlock,
subscriptionCacheHelpersBlock,
mkOutboundScript,
tproxyFile,
tunFile,
perAppTunFile,
}:
{ ctx }:
let
inherit (ctx)
lib
pkgs
proxyCfg
perAppRoutingCfg
userControlCfg
userControlAllows
globalTproxy
xrayEnabled
hybridEnabled
pureXrayEnabled
constants
zapretCutoffProxyFallback
jq
singBox
xray
backendBin
routeModeStateFile
routeModeRulesFile
xrayLoglevelFile
runtimeProxychainsConfig
localProxyAuth
localProxyAuthEnabled
localProxyAuthPasswordSource
backendJqFilterFile
hybridRuntimeHelpersBlock
subscriptionCacheHelpersBlock
mkOutboundScript
tproxyFile
tunFile
perAppTunFile
;
inherit (constants)
autoProxyStateDir
outboundTestPort
@@ -60,62 +61,89 @@ let
+ .proxyGeo
'';
# Every route mode is the same four assignments over one jq program on the rule buckets.
routeModeArms = [
{
mode = "blacklist";
final = "proxy";
dns = "remote";
rules = ''
.common
+ (.custom | map(.entries) | add // [])
+ .proxyPrimary
+ .direct
+ .safetyDirect
${routeModeBlacklistTail}
'';
}
{
mode = "whitelist";
final = "direct";
dns = "local";
rules = ''
.common
+ (.custom | map(.entries) | add // [])
+ .proxyPrimary
+ .direct
+ .safetyDirect
${routeModeBlacklistTail}
'';
}
{
# Only the proxy and block lists are kept: everything else follows the final action.
mode = "all-proxy";
final = "proxy";
dns = "remote";
clearDns = true;
rules = ''
.common
+ (.custom | map(select(.category == "proxy" or .category == "block") | .entries) | add // [])
+ .proxyPrimary
+ .safetyDirect
${routeModeBlacklistTail}
'';
}
{
mode = "all-bypass";
final = "direct";
dns = "local";
clearDns = true;
rules = ''
.common
+ (.custom | map(select(.category == "block") | .entries) | add // [])
+ .safetyDirect
+ .block
'';
}
];
# Written out line by line: the block lands inside a `case`, where the indentation of an
# interpolated multi-line value would otherwise be lost.
mkRouteModeArm =
arm:
lib.concatStringsSep "\n" (
[
" ${arm.mode})"
" ROUTE_FINAL=\"${arm.final}\""
" DNS_FINAL=\"${arm.dns}\""
" ROUTE_MODE_ACTIVE=true"
]
++ lib.optional (arm.clearDns or false) " CLEAR_DNS_RULES=true"
++ [ " ROUTE_RULES_JSON=$(${jq} -c '" ]
++ map (line: if line == "" then "" else " ${line}") (
lib.splitString "\n" (lib.removeSuffix "\n" arm.rules)
)
++ [
" ' \"${routeModeRulesFile}\")"
" ;;"
]
);
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
${routeModeBlacklistTail}
' "${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
${routeModeBlacklistTail}
' "${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
${routeModeBlacklistTail}
' "${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}")
;;
case "$ROUTE_MODE" in${lib.concatMapStrings (arm: "\n" + mkRouteModeArm arm) routeModeArms}
*)
ROUTE_MODE=""
;;
+26 -27
View File
@@ -1,29 +1,31 @@
# Systemd unit definitions for proxy-suite services and timers.
{
lib,
builders,
proxyCfg,
proxyEnabled,
hybridEnabled,
pureXrayEnabled,
globalTun,
globalTproxy,
perAppRoutingTun,
perAppRoutingTproxy,
perAppZapretEnabled,
sshProxyOutboundEnabled,
sshProxyUnitEnabled,
torOutboundEnabled,
torOnionEnabled,
proxyInboundsEnabled,
proxyInboundsNeedLocalProxy,
scripts,
perAppRouting,
routingScripts,
geodata,
}:
{ ctx }:
let
inherit (ctx)
lib
builders
proxyCfg
proxyEnabled
hybridEnabled
pureXrayEnabled
globalTun
globalTproxy
perAppRoutingTun
perAppRoutingTproxy
perAppZapretEnabled
sshProxyOutboundEnabled
sshProxyUnitEnabled
torOutboundEnabled
torOnionEnabled
proxyInboundsEnabled
proxyInboundsNeedLocalProxy
scripts
perAppRouting
routingScripts
cfg
;
inherit (cfg) geodata;
inherit (builders)
mkAnchorService
mkRestartingService
@@ -58,9 +60,7 @@ let
else
"sing-box";
localProxyAuthEnabled =
proxyCfg.listener.auth.username != null
&& (proxyCfg.listener.auth.password != null || proxyCfg.listener.auth.passwordFile != null);
localProxyAuthEnabled = ctx.localProxy.authEnabled;
# XRay finds geoip.dat/geosite.dat through this; sing-box ignores it.
xrayAssetEnv = lib.optionalAttrs (geodata.xray.assets != null) {
@@ -336,7 +336,6 @@ let
in
{
inherit
serviceNames
localProxyAuthEnabled
systemServiceEntries
userServiceEntries