feat(wip): xray proxy backend

This commit is contained in:
FUFSoB committed 2026-06-07 18:15:37 +05:00
1 parent 377d974a18
commit 39ce4d6be2
36 files changed
+2265 -996

No files matched your search

+96 -72
View File
@@ -27,7 +27,11 @@ let
;
inherit (derived)
proxyCfg
singBoxCfg
proxyEnabled
singBoxEnabled
xrayEnabled
perAppRoutingCfg
globalTun
globalTproxy
@@ -39,34 +43,46 @@ let
globalTunAutoRouteTable = 2022;
featureAssertions = [
(requireAvailable singBoxCfg.enable (
singBoxCfg.outbounds != [ ] || singBoxCfg.subscriptions != [ ]
) "proxy-suite: at least one outbound or subscription is required when singBox.enable = true")
(uniqueValues singBoxCfg.enable outboundTags "proxy-suite: outbound tags must be unique")
(uniqueValues singBoxCfg.enable subscriptionTags
(requireAvailable proxyCfg.singBox.enable proxyEnabled
"proxy-suite: proxy.singBox.enable requires proxy.enable = true"
)
(requireAvailable proxyCfg.xray.enable proxyEnabled
"proxy-suite: proxy.xray.enable requires proxy.enable = true"
)
(mkAssertion (
!proxyEnabled || (proxyCfg.singBox.enable != proxyCfg.xray.enable)
) "proxy-suite: exactly one of proxy.singBox.enable or proxy.xray.enable must be true when proxy.enable = true")
(requireAvailable proxyEnabled (
proxyCfg.outbounds != [ ] || proxyCfg.subscriptions != [ ]
) "proxy-suite: at least one outbound or subscription is required when proxy.enable = true")
(uniqueValues proxyEnabled outboundTags "proxy-suite: outbound tags must be unique")
(uniqueValues proxyEnabled subscriptionTags
"proxy-suite: subscription tags must be unique because they are used as cache keys and outbound tag prefixes"
)
(mkAssertion (
!singBoxCfg.enable || builtins.all (tag: !builtins.elem tag builtinTags) outboundTags
!proxyEnabled || builtins.all (tag: !builtins.elem tag builtinTags) outboundTags
) "proxy-suite: outbound tags must not use reserved names: proxy, direct, block")
(mkAssertion (!singBoxCfg.enable || invalidRoutingTargets == [ ])
(mkAssertion (!proxyEnabled || invalidRoutingTargets == [ ])
"proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}"
)
(requireEnabled globalTun.enable singBoxCfg.enable
"proxy-suite: singBox.tun.enable requires singBox.enable = true"
(mkAssertion (
!(xrayEnabled && proxyCfg.selection == "selector")
) "proxy-suite: proxy.selection = \"selector\" is only available with proxy.singBox.enable = true")
(requireEnabled globalTun.enable proxyEnabled
"proxy-suite: proxy.tun.enable requires proxy.enable = true"
)
(requireEnabled globalTproxy.enable singBoxCfg.enable
"proxy-suite: singBox.tproxy.enable requires singBox.enable = true"
(requireEnabled globalTproxy.enable proxyEnabled
"proxy-suite: proxy.tproxy.enable requires proxy.enable = true"
)
(requireEnabled globalTun.autostart globalTun.enable
"proxy-suite: singBox.tun.autostart requires singBox.tun.enable = true"
"proxy-suite: proxy.tun.autostart requires proxy.tun.enable = true"
)
(requireEnabled globalTproxy.autostart globalTproxy.enable
"proxy-suite: singBox.tproxy.autostart requires singBox.tproxy.enable = true"
"proxy-suite: proxy.tproxy.autostart requires proxy.tproxy.enable = true"
)
(mkAssertion
(!(globalTproxy.enable && globalTproxy.autostart && globalTun.enable && globalTun.autostart))
"proxy-suite: singBox.tproxy.autostart and singBox.tun.autostart cannot both be enabled at the same time"
"proxy-suite: proxy.tproxy.autostart and proxy.tun.autostart cannot both be enabled at the same time"
)
];
@@ -77,8 +93,8 @@ let
(requireEnabled perAppRoutingCfg.proxychains.enable perAppRoutingCfg.enable
"proxy-suite: perAppRouting.proxychains.enable requires perAppRouting.enable = true"
)
(requireEnabled perAppRoutingCfg.proxychains.enable singBoxCfg.enable
"proxy-suite: perAppRouting.proxychains.enable requires singBox.enable = true"
(requireEnabled perAppRoutingCfg.proxychains.enable proxyEnabled
"proxy-suite: perAppRouting.proxychains.enable requires proxy.enable = true"
)
(uniqueValues true effectivePerAppRoutingProfileNames
"proxy-suite: perAppRouting profile names must be unique"
@@ -86,32 +102,32 @@ let
(requireAvailable hasProxychainsProfiles perAppRoutingCfg.proxychains.enable
"proxy-suite: route=proxychains in perAppRouting.profiles requires perAppRouting.proxychains.enable = true"
)
(requireAvailable hasProxychainsProfiles singBoxCfg.enable
"proxy-suite: route=proxychains in perAppRouting.profiles requires singBox.enable = true"
(requireAvailable hasProxychainsProfiles proxyEnabled
"proxy-suite: route=proxychains in perAppRouting.profiles requires proxy.enable = true"
)
(requireEnabled perAppRoutingTun.enable perAppRoutingCfg.enable
"proxy-suite: singBox.tun.perApp.enable requires perAppRouting.enable = true"
"proxy-suite: proxy.tun.perApp.enable requires perAppRouting.enable = true"
)
(requireEnabled perAppRoutingTun.enable singBoxCfg.enable
"proxy-suite: singBox.tun.perApp.enable requires singBox.enable = true"
(requireEnabled perAppRoutingTun.enable proxyEnabled
"proxy-suite: proxy.tun.perApp.enable requires proxy.enable = true"
)
(requireAvailable hasTunProfiles perAppRoutingTun.enable
"proxy-suite: route=tun in perAppRouting.profiles requires singBox.tun.perApp.enable = true"
"proxy-suite: route=tun in perAppRouting.profiles requires proxy.tun.perApp.enable = true"
)
(requireAvailable hasTunProfiles singBoxCfg.enable
"proxy-suite: route=tun in perAppRouting.profiles requires singBox.enable = true"
(requireAvailable hasTunProfiles proxyEnabled
"proxy-suite: route=tun in perAppRouting.profiles requires proxy.enable = true"
)
(requireEnabled perAppRoutingTproxy.enable perAppRoutingCfg.enable
"proxy-suite: singBox.tproxy.perApp.enable requires perAppRouting.enable = true"
"proxy-suite: proxy.tproxy.perApp.enable requires perAppRouting.enable = true"
)
(requireEnabled perAppRoutingTproxy.enable singBoxCfg.enable
"proxy-suite: singBox.tproxy.perApp.enable requires singBox.enable = true"
(requireEnabled perAppRoutingTproxy.enable proxyEnabled
"proxy-suite: proxy.tproxy.perApp.enable requires proxy.enable = true"
)
(requireAvailable hasTproxyProfiles perAppRoutingTproxy.enable
"proxy-suite: route=tproxy in perAppRouting.profiles requires singBox.tproxy.perApp.enable = true"
"proxy-suite: route=tproxy in perAppRouting.profiles requires proxy.tproxy.perApp.enable = true"
)
(requireAvailable hasTproxyProfiles singBoxCfg.enable
"proxy-suite: route=tproxy in perAppRouting.profiles requires singBox.enable = true"
(requireAvailable hasTproxyProfiles proxyEnabled
"proxy-suite: route=tproxy in perAppRouting.profiles requires proxy.enable = true"
)
(requireEnabled perAppZapretCfg.enable perAppRoutingCfg.enable
"proxy-suite: zapret.perApp.enable requires perAppRouting.enable = true"
@@ -126,115 +142,115 @@ let
condition = globalTun.enable && perAppRoutingTun.enable;
left = globalTun.interface;
right = perAppRoutingTun.interface;
message = "proxy-suite: singBox.tun.interface and singBox.tun.perApp.interface must differ";
message = "proxy-suite: proxy.tun.interface and proxy.tun.perApp.interface must differ";
}
{
condition = globalTun.enable && perAppRoutingTun.enable;
left = globalTun.address;
right = perAppRoutingTun.address;
message = "proxy-suite: singBox.tun.address and singBox.tun.perApp.address must differ";
message = "proxy-suite: proxy.tun.address and proxy.tun.perApp.address must differ";
}
{
condition = globalTun.enable && perAppRoutingTun.enable;
left = perAppRoutingTun.routeTable;
right = globalTunAutoRouteTable;
message = "proxy-suite: singBox.tun.perApp.routeTable must differ from the global TUN auto-route table 2022";
message = "proxy-suite: proxy.tun.perApp.routeTable must differ from the global TUN auto-route table 2022";
}
{
condition = perAppRoutingTun.enable && globalTproxy.enable;
left = perAppRoutingTun.fwmark;
right = globalTproxy.fwmark;
message = "proxy-suite: singBox.tun.perApp.fwmark must differ from singBox.tproxy.fwmark when global TProxy is enabled";
message = "proxy-suite: proxy.tun.perApp.fwmark must differ from proxy.tproxy.fwmark when global TProxy is enabled";
}
{
condition = perAppRoutingTun.enable && globalTproxy.enable;
left = perAppRoutingTun.fwmark;
right = globalTproxy.proxyMark;
message = "proxy-suite: singBox.tun.perApp.fwmark must differ from singBox.tproxy.proxyMark when global TProxy is enabled";
message = "proxy-suite: proxy.tun.perApp.fwmark must differ from proxy.tproxy.proxyMark when global TProxy is enabled";
}
{
condition = perAppRoutingTun.enable && globalTproxy.enable;
left = perAppRoutingTun.routeTable;
right = globalTproxy.routeTable;
message = "proxy-suite: singBox.tun.perApp.routeTable must differ from singBox.tproxy.routeTable when global TProxy is enabled";
message = "proxy-suite: proxy.tun.perApp.routeTable must differ from proxy.tproxy.routeTable when global TProxy is enabled";
}
{
condition = perAppRoutingTproxy.enable;
left = perAppRoutingTproxy.fwmark;
right = globalTproxy.fwmark;
message = "proxy-suite: singBox.tproxy.perApp.fwmark must differ from singBox.tproxy.fwmark";
message = "proxy-suite: proxy.tproxy.perApp.fwmark must differ from proxy.tproxy.fwmark";
}
{
condition = perAppRoutingTproxy.enable;
left = perAppRoutingTproxy.fwmark;
right = globalTproxy.proxyMark;
message = "proxy-suite: singBox.tproxy.perApp.fwmark must differ from singBox.tproxy.proxyMark";
message = "proxy-suite: proxy.tproxy.perApp.fwmark must differ from proxy.tproxy.proxyMark";
}
{
condition = perAppRoutingTproxy.enable;
left = perAppRoutingTproxy.routeTable;
right = globalTproxy.routeTable;
message = "proxy-suite: singBox.tproxy.perApp.routeTable must differ from singBox.tproxy.routeTable";
message = "proxy-suite: proxy.tproxy.perApp.routeTable must differ from proxy.tproxy.routeTable";
}
{
condition = perAppRoutingTun.enable && perAppRoutingTproxy.enable;
left = perAppRoutingTun.fwmark;
right = perAppRoutingTproxy.fwmark;
message = "proxy-suite: singBox.tun.perApp.fwmark and singBox.tproxy.perApp.fwmark must differ";
message = "proxy-suite: proxy.tun.perApp.fwmark and proxy.tproxy.perApp.fwmark must differ";
}
{
condition = perAppRoutingTun.enable && perAppRoutingTproxy.enable;
left = perAppRoutingTun.routeTable;
right = perAppRoutingTproxy.routeTable;
message = "proxy-suite: singBox.tun.perApp.routeTable and singBox.tproxy.perApp.routeTable must differ";
message = "proxy-suite: proxy.tun.perApp.routeTable and proxy.tproxy.perApp.routeTable must differ";
}
{
condition = perAppZapretCfg.enable;
left = perAppZapretCfg.filterMark;
right = globalTproxy.fwmark;
message = "proxy-suite: zapret.perApp.filterMark must differ from singBox.tproxy.fwmark";
message = "proxy-suite: zapret.perApp.filterMark must differ from proxy.tproxy.fwmark";
}
{
condition = perAppZapretCfg.enable;
left = perAppZapretCfg.filterMark;
right = globalTproxy.proxyMark;
message = "proxy-suite: zapret.perApp.filterMark must differ from singBox.tproxy.proxyMark";
message = "proxy-suite: zapret.perApp.filterMark must differ from proxy.tproxy.proxyMark";
}
{
condition = perAppRoutingTun.enable && perAppZapretCfg.enable;
left = perAppRoutingTun.fwmark;
right = perAppZapretCfg.filterMark;
message = "proxy-suite: singBox.tun.perApp.fwmark and zapret.perApp.filterMark must differ";
message = "proxy-suite: proxy.tun.perApp.fwmark and zapret.perApp.filterMark must differ";
}
{
condition = perAppRoutingTproxy.enable && perAppZapretCfg.enable;
left = perAppRoutingTproxy.fwmark;
right = perAppZapretCfg.filterMark;
message = "proxy-suite: singBox.tproxy.perApp.fwmark and zapret.perApp.filterMark must differ";
message = "proxy-suite: proxy.tproxy.perApp.fwmark and zapret.perApp.filterMark must differ";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && globalTproxy.enable;
left = tgWsProxyCfg.routingMark;
right = globalTproxy.fwmark;
message = "proxy-suite: tgWsProxy.routingMark must differ from singBox.tproxy.fwmark";
message = "proxy-suite: tgWsProxy.routingMark must differ from proxy.tproxy.fwmark";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && globalTproxy.enable;
left = tgWsProxyCfg.routingMark;
right = globalTproxy.proxyMark;
message = "proxy-suite: tgWsProxy.routingMark must differ from singBox.tproxy.proxyMark";
message = "proxy-suite: tgWsProxy.routingMark must differ from proxy.tproxy.proxyMark";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && perAppRoutingTun.enable;
left = tgWsProxyCfg.routingMark;
right = perAppRoutingTun.fwmark;
message = "proxy-suite: tgWsProxy.routingMark must differ from singBox.tun.perApp.fwmark";
message = "proxy-suite: tgWsProxy.routingMark must differ from proxy.tun.perApp.fwmark";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && perAppRoutingTproxy.enable;
left = tgWsProxyCfg.routingMark;
right = perAppRoutingTproxy.fwmark;
message = "proxy-suite: tgWsProxy.routingMark must differ from singBox.tproxy.perApp.fwmark";
message = "proxy-suite: tgWsProxy.routingMark must differ from proxy.tproxy.perApp.fwmark";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && perAppZapretCfg.enable;
@@ -253,47 +269,47 @@ let
{
condition = globalTun.enable;
value = globalTun.mtu;
message = "proxy-suite: singBox.tun.mtu must be greater than zero";
message = "proxy-suite: proxy.tun.mtu must be greater than zero";
}
{
condition = perAppRoutingTun.enable;
value = perAppRoutingTun.mtu;
message = "proxy-suite: singBox.tun.perApp.mtu must be greater than zero";
message = "proxy-suite: proxy.tun.perApp.mtu must be greater than zero";
}
{
condition = globalTproxy.enable;
value = globalTproxy.fwmark;
message = "proxy-suite: singBox.tproxy.fwmark must be greater than zero";
message = "proxy-suite: proxy.tproxy.fwmark must be greater than zero";
}
{
condition = globalTproxy.enable;
value = globalTproxy.proxyMark;
message = "proxy-suite: singBox.tproxy.proxyMark must be greater than zero";
message = "proxy-suite: proxy.tproxy.proxyMark must be greater than zero";
}
{
condition = globalTproxy.enable;
value = globalTproxy.routeTable;
message = "proxy-suite: singBox.tproxy.routeTable must be greater than zero";
message = "proxy-suite: proxy.tproxy.routeTable must be greater than zero";
}
{
condition = perAppRoutingTun.enable;
value = perAppRoutingTun.fwmark;
message = "proxy-suite: singBox.tun.perApp.fwmark must be greater than zero";
message = "proxy-suite: proxy.tun.perApp.fwmark must be greater than zero";
}
{
condition = perAppRoutingTun.enable;
value = perAppRoutingTun.routeTable;
message = "proxy-suite: singBox.tun.perApp.routeTable must be greater than zero";
message = "proxy-suite: proxy.tun.perApp.routeTable must be greater than zero";
}
{
condition = perAppRoutingTproxy.enable;
value = perAppRoutingTproxy.fwmark;
message = "proxy-suite: singBox.tproxy.perApp.fwmark must be greater than zero";
message = "proxy-suite: proxy.tproxy.perApp.fwmark must be greater than zero";
}
{
condition = perAppRoutingTproxy.enable;
value = perAppRoutingTproxy.routeTable;
message = "proxy-suite: singBox.tproxy.perApp.routeTable must be greater than zero";
message = "proxy-suite: proxy.tproxy.perApp.routeTable must be greater than zero";
}
{
condition = perAppZapretCfg.enable;
@@ -312,19 +328,19 @@ let
}
];
localProxyAuthCfg = singBoxCfg.auth;
localProxyAuthCfg = proxyCfg.auth;
localProxyAuthUsed =
localProxyAuthCfg.username != null
|| localProxyAuthCfg.password != null
|| localProxyAuthCfg.passwordFile != null;
localProxyAuthAssertions = [
(mkAssertion (
!singBoxCfg.enable || !localProxyAuthUsed || localProxyAuthCfg.username != null
) "proxy-suite: singBox.auth requires username when password or passwordFile is set")
(exactlyOneOf (singBoxCfg.enable && localProxyAuthUsed) [
!proxyEnabled || !localProxyAuthUsed || localProxyAuthCfg.username != null
) "proxy-suite: proxy.auth requires username when password or passwordFile is set")
(exactlyOneOf (proxyEnabled && localProxyAuthUsed) [
localProxyAuthCfg.password
localProxyAuthCfg.passwordFile
] "proxy-suite: singBox.auth requires exactly one of password or passwordFile")
] "proxy-suite: proxy.auth requires exactly one of password or passwordFile")
];
forbiddenValueAssertions =
@@ -343,25 +359,25 @@ let
condition = perAppZapretCfg.enable;
value = globalTproxy.fwmark;
disallowed = perAppZapretDesyncMarks;
message = "proxy-suite: singBox.tproxy.fwmark must not use per-app-zapret internal desync mark bits";
message = "proxy-suite: proxy.tproxy.fwmark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppZapretCfg.enable;
value = globalTproxy.proxyMark;
disallowed = perAppZapretDesyncMarks;
message = "proxy-suite: singBox.tproxy.proxyMark must not use per-app-zapret internal desync mark bits";
message = "proxy-suite: proxy.tproxy.proxyMark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppRoutingTun.enable && perAppZapretCfg.enable;
value = perAppRoutingTun.fwmark;
disallowed = perAppZapretDesyncMarks;
message = "proxy-suite: singBox.tun.perApp.fwmark must not use per-app-zapret internal desync mark bits";
message = "proxy-suite: proxy.tun.perApp.fwmark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppRoutingTproxy.enable && perAppZapretCfg.enable;
value = perAppRoutingTproxy.fwmark;
disallowed = perAppZapretDesyncMarks;
message = "proxy-suite: singBox.tproxy.perApp.fwmark must not use per-app-zapret internal desync mark bits";
message = "proxy-suite: proxy.tproxy.perApp.fwmark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppZapretCfg.enable;
@@ -385,19 +401,27 @@ let
];
outboundAssertions = lib.concatMap (ob: [
(exactlyOneOf singBoxCfg.enable [
(exactlyOneOf proxyEnabled [
ob.urlFile
ob.url
ob.singBoxJson
ob.xrayJson
ob.json
] "proxy-suite: outbound '${ob.tag}': set exactly one of urlFile, url, or json")
]) singBoxCfg.outbounds;
] "proxy-suite: outbound '${ob.tag}': set exactly one of urlFile, url, singBoxJson, xrayJson, or json")
(mkAssertion (!singBoxEnabled || ob.xrayJson == null)
"proxy-suite: outbound '${ob.tag}': xrayJson is only available with proxy.xray.enable = true"
)
(mkAssertion (!xrayEnabled || (ob.singBoxJson == null && ob.json == null))
"proxy-suite: outbound '${ob.tag}': singBoxJson/json are only available with proxy.singBox.enable = true"
)
]) proxyCfg.outbounds;
subscriptionAssertions = lib.concatMap (sub: [
(exactlyOneOf singBoxCfg.enable [
(exactlyOneOf proxyEnabled [
sub.urlFile
sub.url
] "proxy-suite: subscription '${sub.tag}': set exactly one of urlFile or url")
]) singBoxCfg.subscriptions;
]) proxyCfg.subscriptions;
in
featureAssertions
++ perAppRoutingAssertions