another round of refactoring
This commit is contained in:
23 files changed
+1362
-1090
No files matched your search
@@ -0,0 +1,23 @@
|
||||
{ lib }:
|
||||
|
||||
rec {
|
||||
mkAssertion = assertion: message: { inherit assertion message; };
|
||||
|
||||
requireEnabled = featureEnabled: dependencyEnabled: message:
|
||||
mkAssertion (!featureEnabled || dependencyEnabled) message;
|
||||
|
||||
requireAvailable = featureUsed: dependencyEnabled: message:
|
||||
mkAssertion (!featureUsed || dependencyEnabled) message;
|
||||
|
||||
uniqueValues = condition: values: message:
|
||||
mkAssertion (!condition || builtins.length values == builtins.length (lib.unique values)) message;
|
||||
|
||||
notEqualWhen = condition: left: right: message:
|
||||
mkAssertion (!(condition && left == right)) message;
|
||||
|
||||
forbiddenValues = condition: value: disallowed: message:
|
||||
mkAssertion (!(condition && builtins.elem value disallowed)) message;
|
||||
|
||||
exactlyOneOf = condition: values: message:
|
||||
mkAssertion (!condition || builtins.length (builtins.filter (value: value != null) values) == 1) message;
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
}:
|
||||
|
||||
let
|
||||
mkOptionalTopLevel =
|
||||
{
|
||||
description,
|
||||
after ? [ ],
|
||||
wantedBy ? [ ],
|
||||
wants ? [ ],
|
||||
requires ? [ ],
|
||||
conflicts ? [ ],
|
||||
preStart ? null,
|
||||
}:
|
||||
{
|
||||
inherit description after wantedBy wants requires conflicts;
|
||||
}
|
||||
// lib.optionalAttrs (preStart != null) { inherit preStart; };
|
||||
in
|
||||
rec {
|
||||
mkNamedUnits =
|
||||
entries:
|
||||
lib.listToAttrs (
|
||||
map (entry: lib.nameValuePair entry.name entry.value) (builtins.filter (entry: entry.enable) entries)
|
||||
);
|
||||
|
||||
mkRestartingService =
|
||||
{
|
||||
description,
|
||||
execStart,
|
||||
runtimeDirectory,
|
||||
stateDirectory ? null,
|
||||
after ? [ ],
|
||||
wantedBy ? [ ],
|
||||
wants ? [ ],
|
||||
requires ? [ ],
|
||||
conflicts ? [ ],
|
||||
execStartPost ? null,
|
||||
execStopPost ? null,
|
||||
extraServiceConfig ? { },
|
||||
preStart ? null,
|
||||
}:
|
||||
(mkOptionalTopLevel {
|
||||
inherit
|
||||
description
|
||||
after
|
||||
wantedBy
|
||||
wants
|
||||
requires
|
||||
conflicts
|
||||
preStart
|
||||
;
|
||||
})
|
||||
// {
|
||||
serviceConfig =
|
||||
{
|
||||
ExecStart = execStart;
|
||||
Restart = "on-failure";
|
||||
RestartSec = 5;
|
||||
RuntimeDirectory = runtimeDirectory;
|
||||
}
|
||||
// lib.optionalAttrs (stateDirectory != null) { StateDirectory = stateDirectory; }
|
||||
// lib.optionalAttrs (execStartPost != null) { ExecStartPost = execStartPost; }
|
||||
// lib.optionalAttrs (execStopPost != null) { ExecStopPost = execStopPost; }
|
||||
// extraServiceConfig;
|
||||
};
|
||||
|
||||
mkOneshotService =
|
||||
{
|
||||
description,
|
||||
execStart,
|
||||
execStop ? null,
|
||||
execStartPre ? null,
|
||||
execStartPost ? null,
|
||||
execStopPost ? null,
|
||||
runtimeDirectory ? null,
|
||||
stateDirectory ? null,
|
||||
after ? [ ],
|
||||
wantedBy ? [ ],
|
||||
wants ? [ ],
|
||||
requires ? [ ],
|
||||
conflicts ? [ ],
|
||||
extraServiceConfig ? { },
|
||||
preStart ? null,
|
||||
}:
|
||||
(mkOptionalTopLevel {
|
||||
inherit
|
||||
description
|
||||
after
|
||||
wantedBy
|
||||
wants
|
||||
requires
|
||||
conflicts
|
||||
preStart
|
||||
;
|
||||
})
|
||||
// {
|
||||
serviceConfig =
|
||||
{
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
ExecStart = execStart;
|
||||
}
|
||||
// lib.optionalAttrs (execStop != null) { ExecStop = execStop; }
|
||||
// lib.optionalAttrs (execStartPre != null) { ExecStartPre = execStartPre; }
|
||||
// lib.optionalAttrs (execStartPost != null) { ExecStartPost = execStartPost; }
|
||||
// lib.optionalAttrs (execStopPost != null) { ExecStopPost = execStopPost; }
|
||||
// lib.optionalAttrs (runtimeDirectory != null) { RuntimeDirectory = runtimeDirectory; }
|
||||
// lib.optionalAttrs (stateDirectory != null) { StateDirectory = stateDirectory; }
|
||||
// extraServiceConfig;
|
||||
};
|
||||
|
||||
mkUserRuleService =
|
||||
{
|
||||
description,
|
||||
backendService,
|
||||
execStart,
|
||||
execStop,
|
||||
}:
|
||||
mkOneshotService {
|
||||
inherit description execStart execStop;
|
||||
requires = [ "${backendService}.service" ];
|
||||
after = [ "${backendService}.service" ];
|
||||
};
|
||||
|
||||
mkAnchorService =
|
||||
sliceName: description:
|
||||
mkOneshotService {
|
||||
inherit description;
|
||||
execStart = "${pkgs.coreutils}/bin/true";
|
||||
execStop = "${pkgs.coreutils}/bin/true";
|
||||
extraServiceConfig.Slice = sliceName;
|
||||
};
|
||||
}
|
||||
@@ -15,23 +15,24 @@
|
||||
}:
|
||||
|
||||
let
|
||||
singBoxCfg = cfg.singBox;
|
||||
perAppRoutingCfg = cfg.perAppRouting;
|
||||
globalTun = singBoxCfg.tun;
|
||||
globalTproxy = singBoxCfg.tproxy;
|
||||
perAppRoutingTun = singBoxCfg.tun.perApp;
|
||||
perAppRoutingTproxy = singBoxCfg.tproxy.perApp;
|
||||
perAppZapretCfg = cfg.zapret.perApp;
|
||||
userControlCfg = cfg.userControl;
|
||||
|
||||
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
|
||||
)
|
||||
);
|
||||
derived = import ../derived.nix { inherit lib cfg; };
|
||||
inherit (derived)
|
||||
singBoxCfg
|
||||
perAppRoutingCfg
|
||||
globalTun
|
||||
globalTproxy
|
||||
perAppRoutingTun
|
||||
perAppRoutingTproxy
|
||||
perAppZapretCfg
|
||||
userControlCfg
|
||||
selectionMode
|
||||
builtinTags
|
||||
outboundTags
|
||||
subscriptionTags
|
||||
invalidRoutingTargets
|
||||
collapseNamedOutbounds
|
||||
hasSubscriptions
|
||||
;
|
||||
|
||||
# Tool paths – defined once here and passed into sub-modules as needed.
|
||||
jq = "${pkgs.jq}/bin/jq";
|
||||
@@ -54,7 +55,7 @@ let
|
||||
};
|
||||
|
||||
scripts = import ./scripts.nix {
|
||||
inherit lib pkgs singBoxCfg perAppRoutingTun;
|
||||
inherit lib pkgs singBoxCfg perAppRoutingTun selectionMode collapseNamedOutbounds hasSubscriptions;
|
||||
inherit jq python3 singBox parserScriptsPythonPath buildOutboundPy fetchSubscriptionPy;
|
||||
inherit tproxyFile tunFile perAppTunFile;
|
||||
};
|
||||
@@ -68,7 +69,15 @@ let
|
||||
};
|
||||
|
||||
control = import ./control.nix {
|
||||
inherit packages singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy perAppZapretCfg;
|
||||
inherit
|
||||
packages
|
||||
singBoxCfg
|
||||
perAppRoutingCfg
|
||||
perAppRoutingTun
|
||||
perAppRoutingTproxy
|
||||
perAppZapretCfg
|
||||
selectionMode
|
||||
;
|
||||
zapretEnabled = cfg.zapret.enable;
|
||||
inherit (scripts) subscriptionTagsFile;
|
||||
inherit (perAppRouting)
|
||||
@@ -80,6 +89,7 @@ let
|
||||
in
|
||||
{
|
||||
inherit
|
||||
derived
|
||||
singBoxCfg
|
||||
perAppRoutingCfg
|
||||
globalTun
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
perAppRoutingTun,
|
||||
perAppRoutingTproxy,
|
||||
perAppZapretCfg,
|
||||
selectionMode,
|
||||
zapretEnabled,
|
||||
subscriptionTagsFile,
|
||||
perAppRoutingProfilesFile,
|
||||
@@ -15,7 +16,7 @@
|
||||
{
|
||||
proxyCtl = packages.mkProxyCtl {
|
||||
clashApi = "http://127.0.0.1:${toString singBoxCfg.clashApiPort}";
|
||||
selection = singBoxCfg.selection;
|
||||
selection = selectionMode;
|
||||
inherit
|
||||
subscriptionTagsFile
|
||||
perAppRoutingProfilesFile
|
||||
|
||||
@@ -34,7 +34,24 @@ let
|
||||
;
|
||||
};
|
||||
|
||||
builders = import ./builders.nix { inherit lib pkgs; };
|
||||
inherit (builders)
|
||||
mkNamedUnits
|
||||
mkRestartingService
|
||||
mkOneshotService
|
||||
mkUserRuleService
|
||||
mkAnchorService
|
||||
;
|
||||
|
||||
inherit (context)
|
||||
derived
|
||||
polkit
|
||||
scripts
|
||||
perAppRouting
|
||||
control
|
||||
;
|
||||
|
||||
inherit (derived)
|
||||
singBoxCfg
|
||||
perAppRoutingCfg
|
||||
globalTun
|
||||
@@ -43,30 +60,209 @@ let
|
||||
perAppRoutingTproxy
|
||||
perAppZapretCfg
|
||||
userControlCfg
|
||||
builtinTags
|
||||
userControlEnabled
|
||||
perAppZapretEnabled
|
||||
hasSubscriptions
|
||||
outboundTags
|
||||
subscriptionTags
|
||||
invalidRoutingTargets
|
||||
polkit
|
||||
scripts
|
||||
perAppRouting
|
||||
control
|
||||
builtinTags
|
||||
;
|
||||
|
||||
serviceNames = {
|
||||
socks = "proxy-suite-socks";
|
||||
tproxy = "proxy-suite-tproxy";
|
||||
tun = "proxy-suite-tun";
|
||||
perAppTun = "proxy-suite-per-app-tun";
|
||||
perAppTproxy = "proxy-suite-per-app-tproxy";
|
||||
perAppZapret = "proxy-suite-per-app-zapret";
|
||||
subscriptionUpdate = "proxy-suite-subscription-update";
|
||||
};
|
||||
|
||||
tproxyUpScript = pkgs.writeShellScript "proxy-suite-tproxy-up" ''
|
||||
${nft} delete table ip singbox 2>/dev/null || true
|
||||
${nft} -f ${nftablesRulesFile}
|
||||
${ip} route add local default dev lo table ${toString globalTproxy.routeTable}
|
||||
${ip} rule add fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable}
|
||||
'';
|
||||
|
||||
tproxyDownScript = pkgs.writeShellScript "proxy-suite-tproxy-down" ''
|
||||
${nft} delete table ip singbox 2>/dev/null || true
|
||||
${ip} route del local default dev lo table ${toString globalTproxy.routeTable} 2>/dev/null || true
|
||||
${ip} rule del fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
systemServiceEntries = [
|
||||
{
|
||||
enable = singBoxCfg.enable;
|
||||
name = serviceNames.socks;
|
||||
value = mkRestartingService {
|
||||
description = "sing-box proxy client (SOCKS + TProxy-ready)";
|
||||
after = [ "network.target" ];
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
execStart = scripts.startSocks;
|
||||
runtimeDirectory = serviceNames.socks;
|
||||
stateDirectory = "proxy-suite";
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && globalTproxy.enable;
|
||||
name = serviceNames.tproxy;
|
||||
value = mkOneshotService {
|
||||
description = "sing-box TProxy – nftables rules and policy routing";
|
||||
after = [
|
||||
"network.target"
|
||||
"${serviceNames.socks}.service"
|
||||
];
|
||||
wantedBy = lib.optionals globalTproxy.autostart [ "multi-user.target" ];
|
||||
requires = [ "${serviceNames.socks}.service" ];
|
||||
conflicts = [
|
||||
"${serviceNames.tun}.service"
|
||||
"${serviceNames.perAppTproxy}.service"
|
||||
];
|
||||
execStart = tproxyUpScript;
|
||||
execStop = tproxyDownScript;
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && globalTun.enable;
|
||||
name = serviceNames.tun;
|
||||
value = mkRestartingService {
|
||||
description = "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" ];
|
||||
execStart = scripts.startTun;
|
||||
runtimeDirectory = serviceNames.tun;
|
||||
stateDirectory = "proxy-suite";
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && perAppRoutingTun.enable;
|
||||
name = serviceNames.perAppTun;
|
||||
value = mkRestartingService {
|
||||
description = "sing-box per-app-routing TUN backend";
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
execStart = scripts.startPerAppTun;
|
||||
execStartPost = perAppRouting.perAppTunUpScript;
|
||||
execStopPost = perAppRouting.perAppTunDownScript;
|
||||
runtimeDirectory = serviceNames.perAppTun;
|
||||
stateDirectory = "proxy-suite";
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && perAppRoutingTun.enable;
|
||||
name = "${serviceNames.perAppTun}-user@";
|
||||
value = mkUserRuleService {
|
||||
description = "Enable proxy-suite app TUN marking for user %i";
|
||||
backendService = serviceNames.perAppTun;
|
||||
execStart = "${perAppRouting.perAppTunUserRuleStart} %i";
|
||||
execStop = "${perAppRouting.perAppTunUserRuleStop} %i";
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && perAppRoutingTproxy.enable;
|
||||
name = serviceNames.perAppTproxy;
|
||||
value = mkOneshotService {
|
||||
description = "proxy-suite per-app-routing TProxy backend";
|
||||
after = [
|
||||
"network.target"
|
||||
"${serviceNames.socks}.service"
|
||||
];
|
||||
requires = [ "${serviceNames.socks}.service" ];
|
||||
conflicts = [
|
||||
"${serviceNames.tproxy}.service"
|
||||
"${serviceNames.tun}.service"
|
||||
];
|
||||
execStart = perAppRouting.perAppTproxyUpScript;
|
||||
execStop = perAppRouting.perAppTproxyDownScript;
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && perAppRoutingTproxy.enable;
|
||||
name = "${serviceNames.perAppTproxy}-user@";
|
||||
value = mkUserRuleService {
|
||||
description = "Enable proxy-suite app TProxy marking for user %i";
|
||||
backendService = serviceNames.perAppTproxy;
|
||||
execStart = "${perAppRouting.perAppTproxyUserRuleStart} %i";
|
||||
execStop = "${perAppRouting.perAppTproxyUserRuleStop} %i";
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = perAppZapretEnabled;
|
||||
name = "${serviceNames.perAppZapret}-user@";
|
||||
value = mkUserRuleService {
|
||||
description = "Enable proxy-suite app zapret marking for user %i";
|
||||
backendService = serviceNames.perAppZapret;
|
||||
execStart = "${perAppRouting.perAppZapretUserRuleStart} %i";
|
||||
execStop = "${perAppRouting.perAppZapretUserRuleStop} %i";
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = singBoxCfg.enable && hasSubscriptions;
|
||||
name = serviceNames.subscriptionUpdate;
|
||||
value = mkOneshotService {
|
||||
description = "Refresh proxy-suite subscription caches";
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
execStart = scripts.subscriptionUpdateScript;
|
||||
stateDirectory = "proxy-suite";
|
||||
};
|
||||
}
|
||||
];
|
||||
|
||||
userServiceEntries = [
|
||||
{
|
||||
enable = perAppRoutingTun.enable;
|
||||
name = "${serviceNames.perAppTun}-anchor";
|
||||
value =
|
||||
mkAnchorService perAppRouting.perAppTunSliceName "Anchor service for proxy-suite app TUN slice";
|
||||
}
|
||||
{
|
||||
enable = perAppRoutingTproxy.enable;
|
||||
name = "${serviceNames.perAppTproxy}-anchor";
|
||||
value =
|
||||
mkAnchorService perAppRouting.perAppTproxySliceName "Anchor service for proxy-suite app TProxy slice";
|
||||
}
|
||||
{
|
||||
enable = perAppZapretEnabled;
|
||||
name = "${serviceNames.perAppZapret}-anchor";
|
||||
value =
|
||||
mkAnchorService perAppRouting.perAppZapretSliceName "Anchor service for proxy-suite app zapret slice";
|
||||
}
|
||||
];
|
||||
|
||||
timerEntries = [
|
||||
{
|
||||
enable = singBoxCfg.enable && hasSubscriptions;
|
||||
name = serviceNames.subscriptionUpdate;
|
||||
value = {
|
||||
description = "Periodic proxy-suite subscription refresh";
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = {
|
||||
OnBootSec = "5m";
|
||||
OnUnitActiveSec = singBoxCfg.subscriptionUpdateInterval;
|
||||
};
|
||||
};
|
||||
}
|
||||
];
|
||||
in
|
||||
{
|
||||
environment.systemPackages = [ control.proxyCtl ];
|
||||
|
||||
# nftables must be on for TProxy to work.
|
||||
networking.nftables.enable = lib.mkIf (
|
||||
globalTproxy.enable || perAppRoutingTun.enable || perAppRoutingTproxy.enable || perAppZapretCfg.enable
|
||||
globalTproxy.enable || perAppRoutingTun.enable || perAppRoutingTproxy.enable || perAppZapretEnabled
|
||||
) (lib.mkDefault true);
|
||||
|
||||
users.groups = lib.mkIf (cfg.enable && polkit.userControlEnabled) {
|
||||
users.groups = lib.mkIf (cfg.enable && userControlEnabled) {
|
||||
"${userControlCfg.group}" = { };
|
||||
};
|
||||
|
||||
security.polkit.enable = lib.mkIf (cfg.enable && polkit.userControlEnabled) true;
|
||||
security.polkit.extraConfig = lib.mkIf (cfg.enable && polkit.userControlEnabled) (lib.mkAfter ''
|
||||
security.polkit.enable = lib.mkIf (cfg.enable && userControlEnabled) true;
|
||||
security.polkit.extraConfig = lib.mkIf (cfg.enable && userControlEnabled) (lib.mkAfter ''
|
||||
polkit.addRule(function(action, subject) {
|
||||
if (!subject.isInGroup("${userControlCfg.group}")) {
|
||||
return null;
|
||||
@@ -83,24 +279,10 @@ in
|
||||
});
|
||||
'');
|
||||
|
||||
systemd.user.services =
|
||||
lib.optionalAttrs perAppRoutingTun.enable {
|
||||
proxy-suite-per-app-tun-anchor =
|
||||
perAppRouting.mkAnchorService perAppRouting.perAppTunSliceName "Anchor service for proxy-suite app TUN slice";
|
||||
}
|
||||
// lib.optionalAttrs perAppRoutingTproxy.enable {
|
||||
proxy-suite-per-app-tproxy-anchor =
|
||||
perAppRouting.mkAnchorService perAppRouting.perAppTproxySliceName "Anchor service for proxy-suite app TProxy slice";
|
||||
}
|
||||
// lib.optionalAttrs (perAppZapretCfg.enable && cfg.zapret.enable) {
|
||||
proxy-suite-per-app-zapret-anchor =
|
||||
perAppRouting.mkAnchorService perAppRouting.perAppZapretSliceName "Anchor service for proxy-suite app zapret slice";
|
||||
};
|
||||
systemd.user.services = mkNamedUnits userServiceEntries;
|
||||
|
||||
assertions = import ../service-assertions.nix {
|
||||
inherit lib cfg;
|
||||
inherit singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy;
|
||||
perAppZapretCfg = perAppZapretCfg;
|
||||
inherit lib cfg derived;
|
||||
tgWsProxyCfg = cfg.tgWsProxy;
|
||||
inherit builtinTags outboundTags subscriptionTags invalidRoutingTargets;
|
||||
inherit (perAppRouting)
|
||||
@@ -112,162 +294,7 @@ in
|
||||
;
|
||||
};
|
||||
|
||||
systemd.services = {
|
||||
}
|
||||
// lib.optionalAttrs singBoxCfg.enable {
|
||||
proxy-suite-socks = {
|
||||
description = "sing-box proxy client (SOCKS + TProxy-ready)";
|
||||
after = [ "network.target" ];
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
serviceConfig = {
|
||||
ExecStart = "${scripts.startSocks}";
|
||||
Restart = "on-failure";
|
||||
RestartSec = 5;
|
||||
RuntimeDirectory = "proxy-suite-socks";
|
||||
StateDirectory = "proxy-suite";
|
||||
};
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs (singBoxCfg.enable && globalTproxy.enable) {
|
||||
proxy-suite-tproxy = {
|
||||
description = "sing-box TProxy – nftables rules and policy routing";
|
||||
after = [
|
||||
"network.target"
|
||||
"proxy-suite-socks.service"
|
||||
];
|
||||
wantedBy = lib.optionals globalTproxy.autostart [ "multi-user.target" ];
|
||||
requires = [ "proxy-suite-socks.service" ];
|
||||
conflicts = [
|
||||
"proxy-suite-tun.service"
|
||||
"proxy-suite-per-app-tproxy.service"
|
||||
];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
ExecStart = pkgs.writeShellScript "proxy-suite-tproxy-up" ''
|
||||
${nft} delete table ip singbox 2>/dev/null || true
|
||||
${nft} -f ${nftablesRulesFile}
|
||||
${ip} route add local default dev lo table ${toString globalTproxy.routeTable}
|
||||
${ip} rule add fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable}
|
||||
'';
|
||||
ExecStop = pkgs.writeShellScript "proxy-suite-tproxy-down" ''
|
||||
${nft} delete table ip singbox 2>/dev/null || true
|
||||
${ip} route del local default dev lo table ${toString globalTproxy.routeTable} 2>/dev/null || true
|
||||
${ip} rule del fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable} 2>/dev/null || true
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs (singBoxCfg.enable && globalTun.enable) {
|
||||
proxy-suite-tun = {
|
||||
description = "sing-box TUN proxy client";
|
||||
after = [ "network-online.target" ];
|
||||
wantedBy = lib.optionals globalTun.autostart [ "multi-user.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
conflicts = [ "proxy-suite-tproxy.service" ];
|
||||
serviceConfig = {
|
||||
ExecStart = "${scripts.startTun}";
|
||||
Restart = "on-failure";
|
||||
RestartSec = 5;
|
||||
RuntimeDirectory = "proxy-suite-tun";
|
||||
StateDirectory = "proxy-suite";
|
||||
};
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs (singBoxCfg.enable && perAppRoutingTun.enable) {
|
||||
proxy-suite-per-app-tun = {
|
||||
description = "sing-box per-app-routing TUN backend";
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
serviceConfig = {
|
||||
ExecStart = "${scripts.startPerAppTun}";
|
||||
ExecStartPost = "${perAppRouting.perAppTunUpScript}";
|
||||
ExecStopPost = "${perAppRouting.perAppTunDownScript}";
|
||||
Restart = "on-failure";
|
||||
RestartSec = 5;
|
||||
RuntimeDirectory = "proxy-suite-per-app-tun";
|
||||
StateDirectory = "proxy-suite";
|
||||
};
|
||||
};
|
||||
systemd.services = mkNamedUnits systemServiceEntries;
|
||||
|
||||
"proxy-suite-per-app-tun-user@" = {
|
||||
description = "Enable proxy-suite app TUN marking for user %i";
|
||||
requires = [ "proxy-suite-per-app-tun.service" ];
|
||||
after = [ "proxy-suite-per-app-tun.service" ];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
ExecStart = "${perAppRouting.perAppTunUserRuleStart} %i";
|
||||
ExecStop = "${perAppRouting.perAppTunUserRuleStop} %i";
|
||||
};
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs (singBoxCfg.enable && perAppRoutingTproxy.enable) {
|
||||
proxy-suite-per-app-tproxy = {
|
||||
description = "proxy-suite per-app-routing TProxy backend";
|
||||
after = [
|
||||
"network.target"
|
||||
"proxy-suite-socks.service"
|
||||
];
|
||||
requires = [ "proxy-suite-socks.service" ];
|
||||
conflicts = [
|
||||
"proxy-suite-tproxy.service"
|
||||
"proxy-suite-tun.service"
|
||||
];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
ExecStart = "${perAppRouting.perAppTproxyUpScript}";
|
||||
ExecStop = "${perAppRouting.perAppTproxyDownScript}";
|
||||
};
|
||||
};
|
||||
|
||||
"proxy-suite-per-app-tproxy-user@" = {
|
||||
description = "Enable proxy-suite app TProxy marking for user %i";
|
||||
requires = [ "proxy-suite-per-app-tproxy.service" ];
|
||||
after = [ "proxy-suite-per-app-tproxy.service" ];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
ExecStart = "${perAppRouting.perAppTproxyUserRuleStart} %i";
|
||||
ExecStop = "${perAppRouting.perAppTproxyUserRuleStop} %i";
|
||||
};
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs (perAppZapretCfg.enable && cfg.zapret.enable) {
|
||||
"proxy-suite-per-app-zapret-user@" = {
|
||||
description = "Enable proxy-suite app zapret marking for user %i";
|
||||
requires = [ "proxy-suite-per-app-zapret.service" ];
|
||||
after = [ "proxy-suite-per-app-zapret.service" ];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
ExecStart = "${perAppRouting.perAppZapretUserRuleStart} %i";
|
||||
ExecStop = "${perAppRouting.perAppZapretUserRuleStop} %i";
|
||||
};
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs (singBoxCfg.enable && scripts.hasSubscriptions) {
|
||||
proxy-suite-subscription-update = {
|
||||
description = "Refresh proxy-suite subscription caches";
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
StateDirectory = "proxy-suite";
|
||||
ExecStart = "${scripts.subscriptionUpdateScript}";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
systemd.timers = lib.optionalAttrs (singBoxCfg.enable && scripts.hasSubscriptions) {
|
||||
proxy-suite-subscription-update = {
|
||||
description = "Periodic proxy-suite subscription refresh";
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = {
|
||||
OnBootSec = "5m";
|
||||
OnUnitActiveSec = singBoxCfg.subscriptionUpdateInterval;
|
||||
};
|
||||
};
|
||||
};
|
||||
systemd.timers = mkNamedUnits timerEntries;
|
||||
}
|
||||
@@ -22,6 +22,9 @@
|
||||
}:
|
||||
|
||||
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";
|
||||
@@ -238,16 +241,6 @@ let
|
||||
nftChain = "output";
|
||||
};
|
||||
|
||||
mkAnchorService = sliceName: desc: {
|
||||
description = desc;
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
Slice = sliceName;
|
||||
ExecStart = "${pkgs.coreutils}/bin/true";
|
||||
ExecStop = "${pkgs.coreutils}/bin/true";
|
||||
};
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
pkgs,
|
||||
singBoxCfg,
|
||||
perAppRoutingTun,
|
||||
selectionMode,
|
||||
collapseNamedOutbounds,
|
||||
hasSubscriptions,
|
||||
jq,
|
||||
python3,
|
||||
singBox,
|
||||
@@ -108,7 +111,7 @@ let
|
||||
routingMark:
|
||||
let
|
||||
outboundBlocks =
|
||||
if singBoxCfg.selection == "first" && singBoxCfg.outbounds != [ ] then
|
||||
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"
|
||||
else
|
||||
@@ -118,7 +121,7 @@ let
|
||||
lib.concatMapStrings (sub: mkSubscriptionBlock sub routingMark) singBoxCfg.subscriptions;
|
||||
|
||||
wrapperBlock =
|
||||
if singBoxCfg.selection == "first" then
|
||||
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 != [ ]) ''
|
||||
@@ -128,7 +131,7 @@ let
|
||||
'map(if .tag == $t then .tag = "proxy" else . end)' <<< "$OUTBOUNDS_JSON")
|
||||
fi
|
||||
''
|
||||
else if singBoxCfg.selection == "selector" then
|
||||
else if selectionMode == "selector" then
|
||||
''
|
||||
TAGS=$(${jq} '[.[].tag]' <<< "$OUTBOUNDS_JSON")
|
||||
FIRST=$(${jq} -r '.[0].tag' <<< "$OUTBOUNDS_JSON")
|
||||
@@ -244,7 +247,6 @@ let
|
||||
exit "$FAILED"
|
||||
'';
|
||||
|
||||
hasSubscriptions = singBoxCfg.subscriptions != [ ];
|
||||
subscriptionTagsFile = pkgs.writeText "proxy-suite-subscription-tags.json" (
|
||||
builtins.toJSON (map (sub: sub.tag) singBoxCfg.subscriptions)
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user