Files
proxy-suite-flake/modules/proxy-suite/service-assertions.nix
T

1481 lines
66 KiB
Nix

{
lib,
cfg,
derived,
tgWsProxyCfg,
builtinTags,
outboundTags,
effectiveOutboundTags ? outboundTags,
subscriptionTags,
invalidRoutingTargets,
effectivePerAppRoutingProfileNames,
hasProxychainsProfiles,
hasTunProfiles,
hasTproxyProfiles,
hasZapretProfiles,
}:
let
inherit (derived)
constants
proxyCfg
proxyEnabled
singBoxEnabled
xrayEnabled
hybridEnabled
pureXrayEnabled
perAppRoutingCfg
globalTun
globalTproxy
perAppRoutingTun
perAppRoutingTproxy
perAppZapretCfg
zapretCfg
zapretEngine
;
mkAssertion = assertion: message: { inherit assertion message; };
requireEnabled =
featureEnabled: dependencyEnabled: message:
mkAssertion (!featureEnabled || dependencyEnabled) message;
# The shape of nearly every dependency assertion: "<used> requires <dependency> = true".
requires =
used: dependency: usedLabel: dependencyLabel:
mkAssertion (!used || dependency) "proxy-suite: ${usedLabel} requires ${dependencyLabel} = true";
uniqueValues =
condition: values: message:
mkAssertion (!condition || builtins.length values == builtins.length (lib.unique values)) message;
exactlyOneOf =
condition: values: message:
mkAssertion (
!condition || builtins.length (builtins.filter (value: value != null) values) == 1
) message;
notEqualWhen =
condition: left: right: message:
mkAssertion (!(condition && left == right)) message;
forbiddenValues =
condition: value: disallowed: message:
mkAssertion (!(condition && builtins.elem value disallowed)) message;
# Two values that must not coincide; the message names the config that conflicts them.
distinct =
condition: left: right: message:
notEqualWhen condition left right "proxy-suite: ${message}";
positive =
condition: value: label:
mkAssertion (!condition || value > 0) "proxy-suite: ${label} must be greater than zero";
# Every loopback listener proxy-suite opens with a fixed port of its own, which
# the autoProxy prober's range must stay clear of.
reservedLoopbackPorts =
builtins.attrValues constants.xrayDnsBridgePorts
++ [ constants.outboundTestPort ]
++ lib.optionals hybridEnabled (builtins.attrValues constants.xraySidecarPorts)
++ lib.optionals derived.warpOutboundEnabled (
lib.concatMap (d: [
d.tunnelPort
d.directPort
]) derived.warpCfg.devices
)
++ lib.concatMap (ob: [
ob.tunnelPort
ob.directPort
]) derived.awgTunnelOutbounds
++ lib.optional derived.torOutboundEnabled derived.torCfg.socksPort
++ map (j: j.port) derived.whitelistBypassJoiners
++ lib.optionals derived.proxyInboundsEnabled (
map (listener: listener.internalPort) derived.proxyInboundsAwg
)
++ awgRuntimeTunnelPorts;
# The loopback listeners of AmneziaWG outbounds added at runtime, one per slot.
awgRuntimeTunnelPorts = lib.optionals derived.awgRuntimeOutbounds (
lib.genList (i: constants.awgRuntimeTunnelBasePort + i) constants.awgRuntimeTunnelSlots
);
# A rootless host (home-manager, nix-on-droid) runs everything as the user: nothing
# that programs routing, firewalls or interfaces, and no privileged ports.
rootless = !constants.privileged;
hostKind = cfg.host.kind;
rootlessForbids =
used: feature:
mkAssertion (
!(rootless && used)
) "proxy-suite: ${feature} needs root, which services on a ${hostKind} host do not have";
rootlessPorts =
lib.optional proxyEnabled {
name = "proxy.listener.port";
port = proxyCfg.listener.port;
}
++ lib.optional tgWsProxyCfg.enable {
name = "tgWsProxy.listener.port";
port = tgWsProxyCfg.listener.port;
}
++ lib.optional cfg.sshProxy.enable {
name = "sshProxy.listener.port";
port = cfg.sshProxy.listener.port;
}
++ lib.optionals cfg.inbounds.enable (
lib.mapAttrsToList (name: listener: {
name = "inbounds.listeners.${name}.port";
inherit (listener) port;
}) cfg.inbounds.listeners
);
rootlessAssertions = [
(rootlessForbids (proxyEnabled && globalTun.enable) "proxy.tun")
(rootlessForbids (proxyEnabled && globalTproxy.enable) "proxy.tproxy")
(rootlessForbids cfg.killSwitch.enable "killSwitch")
(rootlessForbids (perAppRoutingCfg.enable && perAppRoutingTun.enable) "perAppRouting.tun")
(rootlessForbids (perAppRoutingCfg.enable && perAppRoutingTproxy.enable) "perAppRouting.tproxy")
(rootlessForbids perAppZapretCfg.enable "perAppRouting.zapret")
(rootlessForbids zapretCfg.enable "zapret")
(rootlessForbids (
derived.awgGlobalProfiles != { } || derived.awgInterfaceOutbounds != [ ]
) ''an AmneziaWG interface (a global profile, or asOutbound = "interface"; use "userspace")'')
(mkAssertion (!(rootless && cfg.userControl.enable))
"proxy-suite: userControl has nothing to grant on a ${hostKind} host, whose services already belong to the user"
)
(mkAssertion
(
!(
rootless
&& cfg.sshProxy.enable
&& cfg.sshProxy.serviceUser != null
&& cfg.sshProxy.serviceUser != constants.serviceUser
)
)
"proxy-suite: sshProxy.serviceUser cannot switch users on a ${hostKind} host; leave it at its default"
)
]
++ map (
item:
mkAssertion (!(rootless && item.port < 1024))
"proxy-suite: ${item.name} = ${toString item.port} is a privileged port, which services on a ${hostKind} host cannot bind; pick one from 1024 up"
) rootlessPorts;
unknownRuleSets = lib.unique (
builtins.filter (name: !proxyCfg.routing.ruleSets ? ${name}) (
lib.concatMap (category: proxyCfg.routing.${category}.ruleSets) [
"proxy"
"direct"
"block"
]
++ lib.concatMap (rule: rule.ruleSets) proxyCfg.routing.rules
++ lib.concatMap (ob: ob.routing.ruleSets) proxyCfg.outbounds
)
);
featureAssertions = [
# Declaring no outbound at all is legal: they can be added at runtime with
# `proxy-ctl proxy outbounds add`. Only a warning here; the backend still
# refuses to start with nothing to dial.
(uniqueValues proxyEnabled effectiveOutboundTags "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 (
!proxyEnabled || builtins.all (tag: !builtins.elem tag builtinTags) effectiveOutboundTags
) "proxy-suite: outbound tags must not use reserved names: proxy, direct, block")
(requireEnabled cfg.sshProxy.asOutbound cfg.sshProxy.enable
"proxy-suite: sshProxy.asOutbound requires sshProxy.enable = true"
)
(requireEnabled cfg.sshProxy.asOutbound proxyEnabled
"proxy-suite: sshProxy.asOutbound requires proxy.enable = true"
)
(mkAssertion (
!cfg.warp.enable || cfg.warp.asOutbound != null || cfg.warp.asAmneziaWg
) "proxy-suite: warp.enable = true needs warp.asOutbound or warp.asAmneziaWg")
# One key, one session: the second peer on a WARP key takes the session over.
(mkAssertion (!(cfg.warp.enable && cfg.warp.asOutbound != null && cfg.warp.asAmneziaWg))
"proxy-suite: warp.asOutbound and warp.asAmneziaWg share one WARP key and would knock each other off; enable one"
)
(mkAssertion (
!(cfg.warp.instances != null && cfg.warp.devices != { })
) "proxy-suite: warp.instances is a shorthand for warp.devices; set one of them")
(mkAssertion (!(derived.warpCfg.grouped && builtins.elem "warp" derived.warpDeviceNames))
"proxy-suite: with several WARP devices, \"warp\" names their group; call the device something else"
)
(mkAssertion (
!(cfg.warp.enable && derived.warpCfg.grouped && cfg.warp.asAmneziaWg)
) "proxy-suite: warp.asAmneziaWg is one global profile, so it takes a single WARP device")
# inbounds.routing.serverSource: a dummy interface, and an address per user in each range.
(
let
src = cfg.inbounds.routing.serverSource;
highest = lib.foldl lib.max 0 (map (s: s.number) derived.proxyInboundsSelfSources);
v4 = builtins.match "([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)/([0-9]+)" src.ipv4;
v4Prefix = lib.toInt (lib.last v4);
v4Int = lib.foldl (acc: o: acc * 256 + lib.toInt o) 0 (lib.take 4 v4);
v4Size = lib.foldl (acc: _: acc * 2) 1 (lib.range 1 (32 - v4Prefix));
in
mkAssertion
(
src.ipv4 == null
|| (v4Prefix >= 8 && v4Prefix <= 30 && lib.mod v4Int v4Size == 0 && highest + 2 <= v4Size)
)
"proxy-suite: inbounds.routing.serverSource.ipv4 must be a network address (host bits zero), /8 to /30, with room for user number ${toString highest}"
)
# A user's order is their serverSource number: two would share an address.
(uniqueValues true (builtins.filter (order: order != null) (
lib.mapAttrsToList (_: user: user.order) cfg.inbounds.users
)) "proxy-suite: inbounds.users must each have a distinct order")
(mkAssertion (
cfg.inbounds.routing.serverSource.ipv6 == null
|| lib.toInt (lib.last (lib.splitString "/" cfg.inbounds.routing.serverSource.ipv6)) <= 112
) "proxy-suite: inbounds.routing.serverSource.ipv6 must be a prefix of /112 or shorter")
# A user's number is the last group of their IPv6 address.
(mkAssertion
(
cfg.inbounds.routing.serverSource.ipv6 == null
|| lib.all (s: s.number <= 65535) derived.proxyInboundsSelfSources
)
"proxy-suite: with inbounds.routing.serverSource.ipv6, inbounds.users orders must be 65535 or less"
)
(mkAssertion
(
(
derived.proxyInboundsSelfSources == [ ]
&& !(
derived.proxyInboundsRuntimeEnabled
&& (
cfg.inbounds.routing.serverSource.ipv4 != null || cfg.inbounds.routing.serverSource.ipv6 != null
)
)
)
|| cfg.host.privileged
)
"proxy-suite: inbounds.routing.serverSource needs a system install: it puts addresses on a dummy interface"
)
# Daemon configs are readable by the service group; users must not share it.
(mkAssertion (cfg.userControl.group != derived.constants.serviceUser)
"proxy-suite: userControl.group must not be ${derived.constants.serviceUser}, the group that can read backend configs"
)
(mkAssertion (cfg.userControl.scopes == [ ] || cfg.userControl.enable)
"proxy-suite: userControl.scopes is set but userControl.enable is false, so the group gets nothing"
)
(mkAssertion (cfg.userControl.groups == { } || cfg.userControl.enable)
"proxy-suite: userControl.groups is set but userControl.enable is false, so those groups get nothing"
)
(mkAssertion
(lib.all (
g:
builtins.match "[a-z_][a-z0-9_-]*" g != null
&& g != cfg.userControl.group
&& g != derived.constants.serviceUser
) (builtins.attrNames cfg.userControl.groups))
"proxy-suite: userControl.groups names must be group names (lowercase letters, digits, - and _), and neither userControl.group nor ${derived.constants.serviceUser}"
)
(requireEnabled (
cfg.warp.enable && cfg.warp.asOutbound != null
) proxyEnabled "proxy-suite: warp.asOutbound requires proxy.enable = true")
(requireEnabled
(
cfg.warp.enable
&& builtins.elem cfg.warp.asOutbound [
"userspace"
"interface"
]
)
cfg.amneziaWg.enable
''proxy-suite: warp.asOutbound = "${toString cfg.warp.asOutbound}" requires amneziaWg.enable = true''
)
(requireEnabled (
cfg.warp.enable && cfg.warp.asAmneziaWg
) cfg.amneziaWg.enable "proxy-suite: warp.asAmneziaWg requires amneziaWg.enable = true")
(requireEnabled (cfg.warp.enable && cfg.warp.autostart) cfg.warp.asAmneziaWg
"proxy-suite: warp.autostart starts the global AmneziaWG profile and requires warp.asAmneziaWg = true"
)
(requireEnabled proxyCfg.autoProxy.enable proxyEnabled
"proxy-suite: proxy.autoProxy.enable requires proxy.enable = true"
)
(mkAssertion (!(proxyCfg.autoProxy.enable && pureXrayEnabled))
"proxy-suite: proxy.autoProxy needs the sing-box backend: learned routes are local rule-sets that sing-box reloads from disk, and pure XRay has no equivalent"
)
(mkAssertion (!cfg.debug.connectionLog.enable || singBoxEnabled)
"proxy-suite: debug.connectionLog reads sing-box's Clash API: it needs proxy.enable = true and proxy.backend = \"sing-box\" or \"hybrid\""
)
(mkAssertion
(!cfg.sshProxy.enable || (cfg.sshProxy.server.user != null && cfg.sshProxy.server.host != null))
"proxy-suite: sshProxy.server.user and sshProxy.server.host are required when sshProxy.enable = true"
)
# SingBox dials SSH itself and verifies host keys by value, with no
# knownHostsFile and no trust-on-first-use fallback. An empty list accepts
# any host key, so refuse rather than silently downgrade the tunnel.
(mkAssertion
(!derived.sshProxyNativeOutbound || cfg.sshProxy.hostKey != [ ] || cfg.sshProxy.hostKeyFile != null)
"proxy-suite: sshProxy.hostKey or sshProxy.hostKeyFile is required when sshProxy.asOutbound = true with a SingBox or hybrid backend, because SingBox verifies host keys by value and an empty list accepts any key. Point hostKeyFile at a known-hosts file, or list every key from `ssh-keyscan -p <server.port> <server.host>` in hostKey -- the host key algorithm is negotiated, so a single pinned key fails the handshake when the server picks another algorithm."
)
# OpenSSH's own: with "yes" and no key known, it would refuse every connection.
(mkAssertion
(
!(cfg.sshProxy.enable && derived.sshProxyUnitEnabled)
|| cfg.sshProxy.strictHostKeyChecking != "yes"
|| cfg.sshProxy.knownHostsFile != null
|| cfg.sshProxy.hostKeyFile != null
|| cfg.sshProxy.hostKey != [ ]
)
"proxy-suite: sshProxy needs the server's host keys (knownHostsFile, hostKeyFile or hostKey, from `ssh-keyscan -p <server.port> <server.host>`), or strictHostKeyChecking = \"accept-new\" to trust whatever key the first connection meets"
)
(mkAssertion (!proxyEnabled || invalidRoutingTargets == [ ])
"proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}"
)
(mkAssertion (!proxyEnabled || unknownRuleSets == [ ])
"proxy-suite: routing refers to rule set(s) proxy.routing.ruleSets does not declare: ${lib.concatStringsSep ", " unknownRuleSets}"
)
(mkAssertion (!(pureXrayEnabled && proxyCfg.routing.ruleSets != { }))
"proxy-suite: proxy.routing.ruleSets are sing-box rule sets, which proxy.backend = \"xray\" cannot read; use \"sing-box\" or \"hybrid\""
)
(mkAssertion (
!(pureXrayEnabled && proxyCfg.selection == "selector")
) "proxy-suite: proxy.selection = \"selector\" requires proxy.backend = \"sing-box\" or \"hybrid\"")
(mkAssertion (
!(pureXrayEnabled && proxyCfg.selection == "failover")
) "proxy-suite: proxy.selection = \"failover\" requires proxy.backend = \"sing-box\" or \"hybrid\"")
(mkAssertion (
!(pureXrayEnabled && proxyCfg.groups != { })
) "proxy-suite: proxy.groups requires proxy.backend = \"sing-box\" or \"hybrid\"")
(
let
clashing = builtins.filter (
g: builtins.elem g (builtinTags ++ effectiveOutboundTags ++ subscriptionTags)
) derived.groupTags;
in
mkAssertion (!proxyEnabled || clashing == [ ])
"proxy-suite: proxy.groups names must differ from outbound and subscription tags and from proxy, direct, block: ${lib.concatStringsSep ", " clashing}"
)
(
let
bad = builtins.filter (g: builtins.match "[A-Za-z0-9][A-Za-z0-9._-]*" g == null) derived.groupTags;
in
mkAssertion (bad == [ ])
"proxy-suite: proxy.groups names may hold only letters, digits, '.', '_' and '-', starting with a letter or digit: ${lib.concatStringsSep ", " bad}"
)
(
let
empty = builtins.filter (
g:
let
grp = proxyCfg.groups.${g};
in
grp.outbounds == [ ] && grp.subscriptions == [ ] && grp.match == [ ]
) derived.groupTags;
in
mkAssertion (empty == [ ])
"proxy-suite: proxy.groups need members in outbounds, subscriptions or match: ${lib.concatStringsSep ", " empty}"
)
(mkAssertion
(!(pureXrayEnabled && (proxyCfg.dns.local.type == "tls" || proxyCfg.dns.remote.type == "tls")))
"proxy-suite: proxy.dns.*.type = \"tls\" is not supported with proxy.backend = \"xray\"; use udp/tcp DNS for XRay"
)
(mkAssertion
(
!pureXrayEnabled
|| (
proxyCfg.dns.strategy == null
&& proxyCfg.dns.clientSubnet == null
&& !proxyCfg.dns.fakeIp.enable
&& proxyCfg.dns.singBox.servers == [ ]
&& proxyCfg.dns.singBox.rules == [ ]
)
)
"proxy-suite: proxy.dns.strategy, clientSubnet, fakeIp and singBox require proxy.backend = \"sing-box\" or \"hybrid\""
)
(requireEnabled globalTun.enable proxyEnabled
"proxy-suite: proxy.tun.enable requires proxy.enable = true"
)
(requireEnabled globalTproxy.enable proxyEnabled
"proxy-suite: proxy.tproxy.enable requires proxy.enable = true"
)
(requireEnabled (
proxyCfg.autostart == "tun"
) globalTun.enable ''proxy-suite: proxy.autostart = "tun" requires proxy.tun.enable = true'')
(requireEnabled (proxyCfg.autostart == "tproxy") globalTproxy.enable
''proxy-suite: proxy.autostart = "tproxy" requires proxy.tproxy.enable = true''
)
(requireEnabled (
globalTproxy.lanInterfaces != [ ]
) globalTproxy.enable "proxy-suite: proxy.tproxy.lanInterfaces requires proxy.tproxy.enable = true")
(requireEnabled cfg.killSwitch.enable derived.killSwitchEnabled
"proxy-suite: killSwitch.enable needs a global tunnel to guard: proxy.tun, proxy.tproxy or a global amneziaWg profile"
)
(mkAssertion
(
!derived.killSwitchEnabled
|| !derived.awgGlobalAvailable
|| !builtins.elem constants.awgGlobalFwmark (
[
globalTproxy.fwmark
globalTproxy.proxyMark
globalTproxy.routeTable
constants.tunAutoRouteTableIndex
]
++ lib.optionals perAppRoutingTun.enable [
perAppRoutingTun.fwmark
perAppRoutingTun.routeTable
]
++ lib.optionals perAppRoutingTproxy.enable [
perAppRoutingTproxy.fwmark
perAppRoutingTproxy.routeTable
]
++ lib.optional tgWsProxyCfg.enable tgWsProxyCfg.fwmark
)
)
"proxy-suite: global AmneziaWG profiles mark and route with ${toString constants.awgGlobalFwmark}, which a proxy.tproxy, perAppRouting or tgWsProxy mark or table also uses"
)
];
# Each case reads "<used> requires <dependency> = true", with option paths as labels.
perAppRoutingAssertions = [
(requires (
perAppRoutingCfg.profiles != [ ]
) perAppRoutingCfg.enable "perAppRouting.profiles" "perAppRouting.enable")
(requires perAppRoutingCfg.proxychains.enable perAppRoutingCfg.enable
"perAppRouting.proxychains.enable"
"perAppRouting.enable"
)
(requires perAppRoutingCfg.proxychains.enable proxyEnabled "perAppRouting.proxychains.enable"
"proxy.enable"
)
(uniqueValues true effectivePerAppRoutingProfileNames
"proxy-suite: perAppRouting profile names must be unique"
)
(requires hasProxychainsProfiles perAppRoutingCfg.proxychains.enable
"route=proxychains in perAppRouting.profiles"
"perAppRouting.proxychains.enable"
)
(requires hasProxychainsProfiles proxyEnabled "route=proxychains in perAppRouting.profiles"
"proxy.enable"
)
(requires perAppRoutingTun.enable perAppRoutingCfg.enable "perAppRouting.tun.enable"
"perAppRouting.enable"
)
(requires perAppRoutingTun.enable proxyEnabled "perAppRouting.tun.enable" "proxy.enable")
(requires hasTunProfiles perAppRoutingTun.enable "route=tun in perAppRouting.profiles"
"perAppRouting.tun.enable"
)
(requires hasTunProfiles proxyEnabled "route=tun in perAppRouting.profiles" "proxy.enable")
(requires perAppRoutingTproxy.enable perAppRoutingCfg.enable "perAppRouting.tproxy.enable"
"perAppRouting.enable"
)
(requires perAppRoutingTproxy.enable proxyEnabled "perAppRouting.tproxy.enable" "proxy.enable")
(requires hasTproxyProfiles perAppRoutingTproxy.enable "route=tproxy in perAppRouting.profiles"
"perAppRouting.tproxy.enable"
)
(requires hasTproxyProfiles proxyEnabled "route=tproxy in perAppRouting.profiles" "proxy.enable")
(requires perAppZapretCfg.enable perAppRoutingCfg.enable "perAppRouting.zapret.enable"
"perAppRouting.enable"
)
(mkAssertion (!perAppZapretCfg.enable || zapretCfg.enable) ''
proxy-suite: perAppRouting.zapret.enable requires zapret.enable = true. For per-app zapret
alone, also set zapret.global.enable = false.'')
(requires hasZapretProfiles perAppZapretCfg.enable "route=zapret in perAppRouting.profiles"
"perAppRouting.zapret.enable"
)
(mkAssertion
(
!lib.any (
mark:
builtins.elem mark (
[
globalTproxy.fwmark
globalTproxy.proxyMark
perAppRoutingTun.fwmark
perAppRoutingTproxy.fwmark
constants.awgGlobalFwmark
constants.awgInboundFwmark
]
++ lib.optional tgWsProxyCfg.enable tgWsProxyCfg.fwmark
)
) derived.perAppViaMarks
)
"proxy-suite: apps run --via an AmneziaWG interface are marked from ${toString constants.awgPerAppFwmarkBase} up, which a proxy.tproxy, perAppRouting or tgWsProxy fwmark also uses"
)
(mkAssertion (
perAppRoutingCfg.via.pinSlots <= 16
) "proxy-suite: perAppRouting.via.pinSlots is at most 16")
]
# An "interface" AmneziaWG outbound or a global profile takes the app directly; any other
# outbound, a pin slot of the profile's route. A name declared nowhere may be one added at
# runtime; one that is both a global profile and an outbound has to say which.
++ lib.concatMap (
profile:
let
target = derived.perAppViaTarget profile.outbound;
direct = derived.perAppViaDirect profile.outbound;
pinned =
target.kind != "awg"
&& (
(profile.route == "tproxy" && derived.perAppPinTproxy)
|| (profile.route == "tun" && derived.perAppPinTun)
);
in
lib.optionals (profile.outbound != null) [
(mkAssertion (direct || pinned)
"proxy-suite: perAppRouting profile '${profile.name}': outbound '${profile.outbound}' needs route \"tproxy\" or \"tun\" with that method enabled, on a privileged host with the sing-box or hybrid backend; or an \"interface\" AmneziaWG outbound (amneziaWg.profiles.<name>.asOutbound), or a global AmneziaWG profile on a privileged host"
)
(mkAssertion (!derived.perAppViaAmbiguous profile.outbound)
"proxy-suite: perAppRouting profile '${profile.name}': '${profile.outbound}' is both a global AmneziaWG profile and an outbound; say \"awg:${target.name}\" or \"outbound:${target.name}\""
)
]
++ [
(mkAssertion (!profile.keepRunning || derived.perAppProfileUnits profile != null)
"proxy-suite: perAppRouting profile '${profile.name}': keepRunning has nothing to keep for route \"${profile.route}\" with no outbound"
)
]
) perAppRoutingCfg.profiles
# Kept pins never give their slot back: they have to leave none short.
++
map
(
route:
let
kept = builtins.filter (lib.hasPrefix "${route}-") derived.perAppKeptViaKeys;
in
mkAssertion (builtins.length kept <= derived.perAppPinSlots)
"proxy-suite: perAppRouting profiles keep ${toString (builtins.length kept)} outbounds running through per-app ${route}, more than perAppRouting.via.pinSlots (${toString derived.perAppPinSlots})"
)
[
"tun"
"tproxy"
];
localProxyAuthCfg = proxyCfg.listener.auth;
localProxyAuthUsed =
localProxyAuthCfg.username != null
|| localProxyAuthCfg.password != null
|| localProxyAuthCfg.passwordFile != null;
localProxyAuthAssertions = [
(mkAssertion (
!proxyEnabled || !localProxyAuthUsed || localProxyAuthCfg.username != null
) "proxy-suite: proxy.listener.auth requires username when password or passwordFile is set")
(exactlyOneOf (proxyEnabled && localProxyAuthUsed) [
localProxyAuthCfg.password
localProxyAuthCfg.passwordFile
] "proxy-suite: proxy.listener.auth requires exactly one of password or passwordFile")
];
secretAssertions = [
(exactlyOneOf tgWsProxyCfg.enable [
tgWsProxyCfg.secret
tgWsProxyCfg.secretFile
] "proxy-suite: tgWsProxy requires exactly one of secret or secretFile")
];
outboundAssertions =
lib.concatMap (ob: [
(exactlyOneOf proxyEnabled [
ob.urlFile
ob.url
ob.singBoxJson
ob.xrayJson
] "proxy-suite: outbound '${ob.tag}': set exactly one of urlFile, url, singBoxJson, or xrayJson")
(mkAssertion (
!singBoxEnabled || hybridEnabled || ob.xrayJson == null
) "proxy-suite: outbound '${ob.tag}': xrayJson requires proxy.backend = xray or hybrid")
(mkAssertion (
!xrayEnabled || hybridEnabled || ob.singBoxJson == null
) "proxy-suite: outbound '${ob.tag}': singBoxJson requires proxy.backend = sing-box or hybrid")
(mkAssertion (
!(ob.backend == "xray") || xrayEnabled
) "proxy-suite: outbound '${ob.tag}': backend = \"xray\" requires proxy.backend = xray or hybrid")
(mkAssertion (!(ob.backend == "sing-box") || singBoxEnabled)
"proxy-suite: outbound '${ob.tag}': backend = \"sing-box\" requires proxy.backend = sing-box or hybrid"
)
(mkAssertion (
ob.detour != ob.tag
) "proxy-suite: outbound '${ob.tag}': detour cannot name the outbound itself")
]) proxyCfg.outbounds
++ map (
e:
mkAssertion (
!builtins.elem e.detour [
"proxy"
"direct"
"block"
]
) "proxy-suite: '${e.tag}': detour must name a real outbound, not proxy, direct or block"
) (builtins.filter (e: e.detour != null) (proxyCfg.outbounds ++ proxyCfg.subscriptions));
proxyInboundsCfg = derived.proxyInboundsCfg;
proxyInboundsEnabled = derived.proxyInboundsEnabled;
proxyInbounds = derived.proxyInbounds;
proxyInboundsNeedLocalProxy = proxyInboundsEnabled && derived.proxyInboundsNeedLocalProxy;
# A pinned outbound is rendered by the XRay renderer inside the inbound
# service, so a sing-box-only definition cannot serve as one.
singBoxOnlyPinnedTags = map (ob: ob.tag) (
builtins.filter (ob: ob.singBoxJson != null) derived.proxyInboundViaOutbounds
);
inRuntimePorts =
port:
builtins.elem port derived.proxyInboundRuntimeSinglePorts
|| lib.any (range: range.from <= port && port <= range.to) derived.proxyInboundRuntimePortRanges;
proxyInboundAssertions = [
(mkAssertion (!proxyInboundsEnabled || derived.invalidInboundViaTargets == [ ])
"proxy-suite: inbounds via targets, or the hops they chain through, are not defined in proxy.outbounds: ${lib.concatStringsSep ", " derived.invalidInboundViaTargets}. Subscription proxies, warp, tor, ssh-proxy and AmneziaWG outbounds cannot be named here because the inbound service does not run them; use via = \"proxy\" to reach those."
)
(mkAssertion (!proxyInboundsEnabled || singBoxOnlyPinnedTags == [ ])
"proxy-suite: inbounds via targets a sing-box-only outbound: ${lib.concatStringsSep ", " singBoxOnlyPinnedTags}. The inbound service runs XRay, so a pinned outbound needs url, urlFile, or xrayJson."
)
(requireEnabled (proxyInboundsEnabled && derived.proxyInboundViaTags != [ ]) proxyEnabled
"proxy-suite: inbounds pins a listener to a proxy.outbounds tag, which requires proxy.enable = true"
)
(requireEnabled proxyInboundsEnabled (proxyInbounds != [ ] || derived.proxyInboundsRuntimeEnabled)
"proxy-suite: inbounds.enable requires at least one entry in inbounds.listeners, or inbounds.runtime.enable"
)
# Each range is sized for any sane config, not for every one: a second listener on a
# port fails its unit, and whatever routes to it goes nowhere.
(uniqueValues true reservedLoopbackPorts
"proxy-suite: two of proxy-suite's own loopback listeners share a port (${
lib.concatMapStringsSep ", " toString (
lib.unique (
builtins.filter (port: lib.count (p: p == port) reservedLoopbackPorts > 1) reservedLoopbackPorts
)
)
}): too many AmneziaWG outbounds, AmneziaWG listeners or whitelistBypass joiners for their ranges"
)
# Runtime listeners bind only there: clear of what is bound here already.
(mkAssertion
(
!derived.proxyInboundsRuntimeEnabled
|| !lib.any inRuntimePorts (
derived.proxyInboundPorts
++ reservedLoopbackPorts
++ lib.optional proxyEnabled proxyCfg.listener.port
++ lib.optional globalTproxy.enable globalTproxy.port
)
)
"proxy-suite: inbounds.runtime.ports cover a port a declared listener or proxy-suite itself uses (${
lib.concatMapStringsSep ", " toString (
builtins.filter inRuntimePorts (
derived.proxyInboundPorts
++ reservedLoopbackPorts
++ lib.optional proxyEnabled proxyCfg.listener.port
++ lib.optional globalTproxy.enable globalTproxy.port
)
)
})"
)
(mkAssertion (
!derived.proxyInboundsRuntimeEnabled
|| lib.all (
range: range.from >= 1 && range.from <= range.to && range.to <= 65535
) derived.proxyInboundRuntimePortRanges
) "proxy-suite: inbounds.runtime.ports ranges must be \"from-to\" within 1-65535, from <= to")
(mkAssertion (!derived.proxyInboundsRuntimeEnabled || derived.proxyInboundRuntimePorts != [ ])
"proxy-suite: inbounds.runtime.enable needs inbounds.runtime.ports, the ports runtime listeners may use"
)
]
# inbounds.manager and the users' policies.
++ (
let
mgr = cfg.inbounds.manager;
defaultPolicy = {
enabled = true;
expiresAt = null;
expiresAfter = null;
trafficLimit = null;
trafficPeriod = null;
ipLimit = null;
speedLimit = null;
};
withPolicy = lib.attrNames (
lib.filterAttrs (_: user: user.policy != defaultPolicy) cfg.inbounds.users
);
withSpeed = lib.attrNames (
lib.filterAttrs (_: user: user.policy.speedLimit != null) cfg.inbounds.users
);
withIpLimit = lib.attrNames (
lib.filterAttrs (_: user: user.policy.ipLimit != null) cfg.inbounds.users
);
speedOn = mgr.enable && mgr.speedLimits.enable;
in
[
(mkAssertion (withPolicy == [ ] || derived.proxyInboundsManagerEnabled)
"proxy-suite: inbounds.users.<name>.policy needs inbounds.manager.enable, which keeps it (set on: ${lib.concatStringsSep ", " withPolicy})"
)
(mkAssertion (withSpeed == [ ] || speedOn)
"proxy-suite: inbounds.users.<name>.policy.speedLimit needs inbounds.manager.speedLimits.enable (set on: ${lib.concatStringsSep ", " withSpeed})"
)
(mkAssertion (
!speedOn || cfg.host.privileged
) "proxy-suite: inbounds.manager.speedLimits needs root: nftables polices the users' traffic")
# Without root the manager has no nftables, and an ipLimit would hold nobody back.
(mkAssertion (withIpLimit == [ ] || cfg.host.privileged)
"proxy-suite: inbounds.users.<name>.policy.ipLimit needs root: nftables bans the addresses past it (set on: ${lib.concatStringsSep ", " withIpLimit})"
)
(mkAssertion (!speedOn || (cfg.inbounds.package.passthru.userLevelMark or false))
"proxy-suite: inbounds.manager.speedLimits needs proxy-suite's XRay (inbounds.package), whose patch marks a limited user's sockets"
)
(mkAssertion (mgr.api.listen == null || mgr.api.tokensFile != null)
"proxy-suite: inbounds.manager.api.listen needs inbounds.manager.api.tokensFile: the TCP API takes bearer tokens only"
)
(mkAssertion (
!mgr.subscriptions.enable || !mgr.enable || cfg.inbounds.shareLinks
) "proxy-suite: inbounds.manager.subscriptions.enable needs inbounds.shareLinks")
]
)
++ [
(requireEnabled proxyInboundsNeedLocalProxy proxyEnabled
"proxy-suite: inbounds.routing.via = \"proxy\" relays through the local proxy stack, which requires proxy.enable = true. Use via = \"direct\" for a plain exit node."
)
(requireEnabled proxyInboundsNeedLocalProxy derived.hasAvailableOutbounds
"proxy-suite: inbounds.routing.via = \"proxy\" relays through the local proxy stack, so at least one proxy.outbounds or proxy.subscriptions entry is required"
)
(uniqueValues proxyInboundsEnabled (map (
ib: ib.listener.port
) proxyInbounds) "proxy-suite: inbounds.listeners must each use a distinct port")
(requireEnabled (proxyInboundsEnabled && proxyInboundsCfg.subscriptions.enable)
proxyInboundsCfg.shareLinks
"proxy-suite: inbounds.subscriptions are made of the share links, so they need inbounds.shareLinks = true"
)
(mkAssertion
(
!proxyInboundsEnabled
|| !proxyEnabled
|| !builtins.elem proxyCfg.listener.port derived.proxyInboundPorts
)
"proxy-suite: a proxyInbounds listener port collides with proxy.listener.port (${toString proxyCfg.listener.port})"
)
(mkAssertion
(
!proxyInboundsEnabled
|| !globalTproxy.enable
|| !builtins.elem globalTproxy.port derived.proxyInboundPorts
)
"proxy-suite: a proxyInbounds listener port collides with proxy.tproxy.port (${toString globalTproxy.port})"
)
(mkAssertion
(
!proxyInboundsEnabled
|| !lib.any (port: builtins.elem port derived.proxyInboundPorts) (
map (listener: listener.internalPort) derived.proxyInboundsAwg
++ awgRuntimeTunnelPorts
++ builtins.attrValues derived.constants.xrayDnsBridgePorts
++ lib.optionals hybridEnabled (builtins.attrValues derived.constants.xraySidecarPorts)
)
)
"proxy-suite: a proxyInbounds listener port collides with a port proxy-suite uses internally (the AmneziaWG listeners' loopback inbounds from 18700, the runtime AmneziaWG outbounds' 18800-18831, 18533-18535, or the hybrid XRay sidecar's 33080, 33180 and 33280)"
)
# The prober opens one loopback listener per exit from probeBasePort; the WARP
# and AmneziaWG tunnels open two each from their own bases. They all bind
# 127.0.0.1, so an overlap leaves whichever unit starts second dead.
(mkAssertion
(
!(proxyEnabled && proxyCfg.autoProxy.enable)
|| !lib.any (
port:
port >= proxyCfg.autoProxy.probeBasePort
&& port < proxyCfg.autoProxy.probeBasePort + proxyCfg.autoProxy.maxExits
) reservedLoopbackPorts
)
"proxy-suite: proxy.autoProxy.probeBasePort ${toString proxyCfg.autoProxy.probeBasePort} and the next ${toString proxyCfg.autoProxy.maxExits} ports cover one proxy-suite already listens on (${
lib.concatMapStringsSep ", " toString reservedLoopbackPorts
}); move probeBasePort"
)
]
++ lib.concatMap (
ib:
let
l = ib.listener;
prefix = "proxy-suite: inbounds listener '${ib.tag}'";
needsUuid = builtins.elem l.type [
"vless"
"vmess"
];
needsPassword = builtins.elem l.type [
"trojan"
"hysteria2"
"shadowsocks"
"socks"
"http"
];
tlsTerminated = l.tls.enable || l.type == "trojan" || l.type == "hysteria2";
multiUserShadowsocks = l.type == "shadowsocks" && builtins.length l.users > 1;
eachUser =
condition: fields: message:
mkAssertion (
!condition
|| lib.all (user: builtins.length (builtins.filter (f: user.${f} != null) fields) == 1) l.users
) message;
in
[
(exactlyOneOf proxyInboundsEnabled [
l.type
l.xrayJson
l.jsonFile
] "${prefix}: set exactly one of type, xrayJson, or jsonFile")
(mkAssertion (
!proxyInboundsEnabled || !(l.tls.enable && l.reality.enable)
) "${prefix}: tls.enable and reality.enable are mutually exclusive")
(mkAssertion (
!proxyInboundsEnabled || l.type == null || l.users != [ ] || derived.proxyInboundsRuntimeEnabled
) "${prefix}: at least one entry in users is required (or inbounds.runtime, to add them there)")
# XRay refuses to start on a repeated email, which is the user's name.
(uniqueValues (proxyInboundsEnabled && l.type != null) (builtins.filter (n: n != "") (
map (user: user.name) l.users
)) "${prefix}: users must each have a distinct name")
(eachUser (proxyInboundsEnabled && needsUuid) [
"uuid"
"uuidFile"
] "${prefix}: ${toString l.type} users each need exactly one of uuid or uuidFile")
(eachUser (proxyInboundsEnabled && needsPassword) [
"password"
"passwordFile"
] "${prefix}: ${toString l.type} users each need exactly one of password or passwordFile")
(mkAssertion (
!proxyInboundsEnabled || !multiUserShadowsocks || lib.hasPrefix "2022-blake3-aes-" l.method
) "${prefix}: shadowsocks with more than one user needs a 2022-blake3-aes-* method")
(exactlyOneOf (proxyInboundsEnabled && multiUserShadowsocks)
[
l.serverPassword
l.serverPasswordFile
]
"${prefix}: shadowsocks with more than one user needs exactly one of serverPassword or serverPasswordFile"
)
(mkAssertion (
!proxyInboundsEnabled
|| !l.reality.enable
|| builtins.elem l.transport.type [
"raw"
"xhttp"
"grpc"
]
) "${prefix}: reality only runs over the raw, xhttp and grpc transports")
(mkAssertion (
!proxyInboundsEnabled || l.xrayJson == null || (l.xrayJson.port or l.port) == l.port
) "${prefix}: xrayJson.port and port differ; the firewall and the port checks go by port")
(mkAssertion
(
!proxyInboundsEnabled
|| !proxyInboundsCfg.shareLinks
|| !l.reality.enable
|| builtins.elem l.type [
"vless"
"trojan"
]
)
"${prefix}: share links carry reality only for vless and trojan; set inbounds.shareLinks = false to serve it anyway"
)
(mkAssertion
(
!proxyInboundsEnabled
|| !proxyInboundsCfg.shareLinks
|| !l.tls.enable
|| !builtins.elem l.type [
"shadowsocks"
"socks"
]
)
"${prefix}: share links cannot carry tls for ${toString l.type}; set inbounds.shareLinks = false to serve it anyway"
)
(exactlyOneOf (proxyInboundsEnabled && l.reality.enable) [
l.reality.privateKey
l.reality.privateKeyFile
] "${prefix}: reality needs exactly one of privateKey or privateKeyFile")
(mkAssertion (
!proxyInboundsEnabled || !l.reality.enable || l.reality.serverNames != [ ]
) "${prefix}: reality.serverNames must not be empty")
(requireEnabled (proxyInboundsEnabled && l.reality.enable && proxyInboundsCfg.shareLinks)
(l.reality.publicKey != null)
"${prefix}: reality.publicKey is required to generate share links. Run `xray x25519` to print it alongside the private key, or set inbounds.shareLinks = false."
)
(mkAssertion (
!proxyInboundsEnabled
|| !tlsTerminated
|| l.reality.enable
|| (l.tls.certificateFile != null && l.tls.keyFile != null)
) "${prefix}: TLS termination needs both tls.certificateFile and tls.keyFile")
(mkAssertion (
!proxyInboundsEnabled || l.flow == null || (l.type == "vless" && l.transport.type == "raw")
) "${prefix}: flow is only valid on a vless listener with transport.type = \"raw\"")
(mkAssertion
(
!proxyInboundsEnabled
|| l.tls.alpn == null
|| !builtins.elem "h3" l.tls.alpn
|| l.type == "hysteria2"
|| (l.transport.type == "xhttp" && l.tls.enable && !l.reality.enable)
)
"${prefix}: tls.alpn \"h3\" is served only by hysteria2, and by the xhttp transport with tls.enable (not REALITY)"
)
# hysteria2 is its own QUIC transport, under a certificate of its own.
(mkAssertion
(!proxyInboundsEnabled || l.type != "hysteria2" || (!l.reality.enable && l.transport.type == "raw"))
"${prefix}: hysteria2 takes no reality or transport; it needs tls.certificateFile and tls.keyFile"
)
(mkAssertion (
!proxyInboundsEnabled || l.hysteria.masquerade == null || l.type == "hysteria2"
) "${prefix}: hysteria.masquerade is for hysteria2 listeners only")
(mkAssertion (
!proxyInboundsEnabled || l.hysteria.rewriteHost || l.hysteria.masquerade != null
) "${prefix}: hysteria.rewriteHost = false needs hysteria.masquerade")
(mkAssertion (
!proxyInboundsEnabled || !l.hysteria.xForwarded || l.hysteria.masquerade != null
) "${prefix}: hysteria.xForwarded needs hysteria.masquerade")
(mkAssertion (
!proxyInboundsEnabled
|| (!l.hysteria.salamander.enable && l.hysteria.portHopping == null)
|| l.type == "hysteria2"
) "${prefix}: hysteria.salamander and hysteria.portHopping are for hysteria2 listeners only")
(mkAssertion (
!proxyInboundsEnabled
|| l.hysteria.salamander.password == null
|| l.hysteria.salamander.passwordFile == null
) "${prefix}: set at most one of hysteria.salamander.password and passwordFile")
(mkAssertion (
!proxyInboundsEnabled
|| l.hysteria.portHopping == null
|| (
let
bounds = map lib.toInt (lib.splitString "-" l.hysteria.portHopping);
in
builtins.elemAt bounds 0 >= 1
&& builtins.elemAt bounds 0 <= builtins.elemAt bounds 1
&& builtins.elemAt bounds 1 <= 65535
)
) "${prefix}: hysteria.portHopping must be a range \"from-to\" within 1-65535, from <= to")
# The redirect is an nftables table, which only the NixOS adapter installs.
(mkAssertion (
!proxyInboundsEnabled || l.hysteria.portHopping == null || hostKind == "nixos"
) "${prefix}: hysteria.portHopping needs a NixOS host")
# Share variants rewrite a link's query, which vmess (a JSON blob) and the others lack.
(mkAssertion (
!proxyInboundsEnabled
|| l.shareVariants == [ ]
|| builtins.elem l.type [
"vless"
"trojan"
"hysteria2"
]
) "${prefix}: shareVariants are for vless, trojan and hysteria2 listeners")
(mkAssertion (
!proxyInboundsEnabled
|| (
let
names = map (v: v.name) l.shareVariants;
in
lib.length (lib.unique names) == lib.length names && !builtins.elem "onion" names
)
) "${prefix}: shareVariants need names of their own, and not \"onion\"")
(mkAssertion (
!proxyInboundsEnabled || lib.all (v: v.mode == null || l.transport.type == "xhttp") l.shareVariants
) "${prefix}: a share variant's mode is for xhttp listeners only")
(mkAssertion (
!proxyInboundsEnabled || lib.all (v: v.portHopping == null || l.type == "hysteria2") l.shareVariants
) "${prefix}: a share variant's portHopping is for hysteria2 listeners only")
(mkAssertion (
!proxyInboundsEnabled
|| lib.all (
v:
(v.fingerprint == null && v.alpn == null && v.serverName == null)
|| tlsTerminated
|| l.reality.enable
) l.shareVariants
) "${prefix}: a share variant's fingerprint, alpn and serverName need tls or reality")
# A PROXY header opens a TCP stream; raw JSON listeners set their own sockopt.
(mkAssertion
(
!proxyInboundsEnabled
|| !l.acceptProxyProtocol
|| (l.type != null && l.type != "amneziawg" && !derived.proxyInboundIsUdpOnly l)
)
"${prefix}: acceptProxyProtocol is for TCP listeners with a type (not hysteria2, amneziawg, h3-only xhttp, xrayJson or jsonFile)"
)
(mkAssertion (
!proxyInboundsEnabled
|| l.fallbacks == [ ]
|| (
builtins.elem l.type [
"vless"
"trojan"
]
&& l.transport.type == "raw"
)
) "${prefix}: fallbacks run only on a vless or trojan listener with transport.type = \"raw\"")
]
++ map (
fb:
exactlyOneOf proxyInboundsEnabled [
fb.dest
fb.listener
] "${prefix}: fallbacks each need exactly one of dest or listener"
) l.fallbacks
# The target gets the connection decrypted and in PROXY protocol, which only
# the front may send it, and its share links take the front's security.
++ lib.concatMap (
fb:
let
target = lib.findFirst (other: other.tag == fb.listener) null proxyInbounds;
t = target.listener;
in
[
(mkAssertion
(
!proxyInboundsEnabled
|| (
target != null
&& target.tag != ib.tag
&& t.type == "vless"
&& !t.tls.enable
&& !t.reality.enable
&& (lib.hasPrefix "127." t.address || t.address == "::1")
)
)
"${prefix}: fallback listener '${fb.listener}' must be another vless listener, without tls or reality, on a loopback address"
)
# XRay matches path on HTTP/1.1 only; xhttp and grpc clients speak h2 under TLS.
(mkAssertion
(
!proxyInboundsEnabled
|| fb.path == null
|| target == null
|| (
builtins.elem t.transport.type [
"ws"
"httpupgrade"
]
&& fb.path == t.transport.path
)
)
"${prefix}: fallback path matches only a ws or httpupgrade listener '${fb.listener}', and must be its transport.path"
)
# REALITY clients run raw, xhttp and grpc only, and raw would be the front's own protocol.
(mkAssertion
(
!proxyInboundsEnabled
|| !l.reality.enable
|| target == null
|| builtins.elem t.transport.type [
"xhttp"
"grpc"
]
)
"${prefix}: a REALITY listener's fallback listener '${fb.listener}' must use the xhttp or grpc transport"
)
]
) (builtins.filter (fb: fb.listener != null) l.fallbacks)
) proxyInbounds
# Its share links can carry only one front's port and security.
++ [
(uniqueValues proxyInboundsEnabled (lib.concatMap (
ib: builtins.filter (tag: tag != null) (map (fb: fb.listener) ib.listener.fallbacks)
) proxyInbounds) "proxy-suite: a listener can be the fallback listener of only one other")
];
# AmneziaWG listeners run an interface each and give their clients addresses in a subnet.
awgListeners = builtins.filter (ib: ib.listener.type == "amneziawg") proxyInbounds;
awgListenersEnabled = proxyInboundsEnabled && awgListeners != [ ];
# "a.b.c.d/n" as { base; size; } (base is the address itself, not yet masked), or null.
parseIPv4 =
value:
let
match = builtins.match "([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})(/([0-9]{1,2}))?" value;
octets = map lib.toInt (lib.take 4 match);
prefix = if builtins.elemAt match 5 == null then 32 else lib.toInt (builtins.elemAt match 5);
in
if match == null || lib.any (octet: octet > 255) octets || prefix > 32 then
null
else
{
base = lib.foldl (acc: octet: acc * 256 + octet) 0 octets;
size = lib.foldl (acc: _: acc * 2) 1 (lib.range 1 (32 - prefix));
};
# Same network once both are cut down to the larger of the two.
ipv4Overlap =
a: b:
let
size = lib.max a.size b.size;
in
a.base / size == b.base / size;
awgInterfaces =
map (ib: ib.listener.amneziaWg.interfaceName) awgListeners
++ lib.optionals cfg.amneziaWg.enable (
lib.mapAttrsToList (_: profile: profile.interfaceName) cfg.amneziaWg.profiles
)
++ lib.optional derived.awgRuntimeGlobal cfg.amneziaWg.runtime.interfaceName;
otherInterfaces =
lib.optional (proxyEnabled && globalTun.enable) globalTun.interface
++ lib.optional (perAppRoutingCfg.enable && perAppRoutingTun.enable) perAppRoutingTun.interface;
awgInboundAssertions = [
(rootlessForbids awgListenersEnabled "an AmneziaWG listener in inbounds.listeners")
(uniqueValues awgListenersEnabled awgInterfaces
"proxy-suite: AmneziaWG inbounds listeners and amneziaWg.profiles must each use a distinct interfaceName"
)
(mkAssertion
(!awgListenersEnabled || !lib.any (name: builtins.elem name otherInterfaces) awgInterfaces)
"proxy-suite: an AmneziaWG inbounds listener's interfaceName is taken by proxy.tun or perAppRouting.tun"
)
(mkAssertion (
!awgListenersEnabled
|| lib.all (
ib: lib.all (other: ib.tag == other.tag || !ipv4Overlap ib.subnet other.subnet) awgSubnets
) awgSubnets
) "proxy-suite: AmneziaWG inbounds listeners must each use a subnet of their own")
(mkAssertion
(
!awgListenersEnabled
|| !builtins.elem derived.constants.awgInboundFwmark (
[
globalTproxy.fwmark
globalTproxy.proxyMark
]
++ lib.optional perAppRoutingTun.enable perAppRoutingTun.fwmark
++ lib.optional perAppRoutingTproxy.enable perAppRoutingTproxy.fwmark
++ lib.optional tgWsProxyCfg.enable tgWsProxyCfg.fwmark
)
)
"proxy-suite: AmneziaWG inbounds listeners mark their diverted packets with ${toString derived.constants.awgInboundFwmark}, which a proxy.tproxy, perAppRouting or tgWsProxy fwmark also uses"
)
(mkAssertion
(
!awgListenersEnabled
|| !builtins.elem derived.constants.awgInboundRouteTable (
[
globalTproxy.routeTable
derived.constants.tunAutoRouteTableIndex
]
++ lib.optional perAppRoutingTun.enable perAppRoutingTun.routeTable
++ lib.optional perAppRoutingTproxy.enable perAppRoutingTproxy.routeTable
)
)
"proxy-suite: AmneziaWG inbounds listeners route with table ${toString derived.constants.awgInboundRouteTable}, which proxy.tproxy.routeTable or a perAppRouting routeTable also uses"
)
# The tables proxy-suite numbers itself, which an option must not name too: each start
# flushes and refills its own.
(
let
c = derived.constants;
slots = base: n: lib.genList (i: base + i) n;
internal =
map (ob: ob.routeTable) derived.awgInterfaceOutbounds
++ lib.optionals derived.awgRuntimeIfaceOutbounds (
slots c.awgRuntimeIfaceTableBase c.awgRuntimeIfaceSlots
)
++ lib.optionals derived.perAppPinTproxy (slots c.perAppPinTproxyTableBase derived.perAppPinSlots)
++ lib.optionals derived.perAppPinTun (slots c.perAppPinTunTableBase derived.perAppPinSlots)
++ lib.optionals derived.perAppViaProfiles (slots c.awgAppTableBase c.awgAppSlots);
named =
lib.optional globalTproxy.enable globalTproxy.routeTable
++ lib.optional perAppRoutingTun.enable perAppRoutingTun.routeTable
++ lib.optional perAppRoutingTproxy.enable perAppRoutingTproxy.routeTable;
taken = builtins.filter (t: builtins.elem t internal) named;
in
mkAssertion (taken == [ ])
"proxy-suite: route table ${
lib.concatMapStringsSep ", " toString taken
} (proxy.tproxy.routeTable or a perAppRouting routeTable) is one proxy-suite uses itself: AmneziaWG interface outbounds take ${toString c.awgOutboundRouteTableBase} up, runtime ones ${toString c.awgRuntimeIfaceTableBase} up, pin slots ${toString c.perAppPinTproxyTableBase} and ${toString c.perAppPinTunTableBase} up, apps through AmneziaWG profiles ${toString c.awgAppTableBase} up"
)
]
++ lib.concatMap (
ib:
let
l = ib.listener;
awg = l.amneziaWg;
prefix = "proxy-suite: inbounds listener '${ib.tag}'";
subnet = parseIPv4 awg.subnet;
addresses = builtins.filter (address: address != null) (map (user: user.address) l.users);
inSubnet =
address:
let
parsed = parseIPv4 address;
offset = parsed.base - parsed.base / subnet.size * subnet.size;
in
parsed != null
&& parsed.size == 1
&& ipv4Overlap parsed subnet
&& !builtins.elem offset [
0
1
(subnet.size - 1)
];
in
lib.optionals (proxyInboundsEnabled && l.type == "amneziawg") [
(mkAssertion (builtins.stringLength awg.interfaceName <= 15)
"${prefix}: amneziaWg.interfaceName '${awg.interfaceName}' is longer than the kernel's 15 characters"
)
(mkAssertion (
!l.tls.enable && !l.reality.enable && l.flow == null && l.transport.type == "raw"
) "${prefix}: AmneziaWG takes no tls, reality, flow or transport")
(mkAssertion (
subnet != null && subnet.size >= 4
) "${prefix}: amneziaWg.subnet '${awg.subnet}' must be an IPv4 CIDR of /30 or wider")
(mkAssertion (subnet == null || lib.all inSubnet addresses)
"${prefix}: each user address must be a host address inside amneziaWg.subnet, other than its first (this host's)"
)
(uniqueValues true addresses "${prefix}: users must each have a distinct address")
(mkAssertion (lib.all (
user: user.publicKey == null || user.privateKeyFile == null
) l.users) "${prefix}: a user sets publicKey or privateKeyFile, not both")
]
) awgListeners;
awgSubnets = builtins.filter (entry: entry.subnet != null) (
map (ib: {
inherit (ib) tag;
subnet = parseIPv4 ib.listener.amneziaWg.subnet;
}) awgListeners
);
subscriptionAssertions = lib.concatMap (sub: [
(exactlyOneOf proxyEnabled [
sub.urlFile
sub.url
] "proxy-suite: subscription '${sub.tag}': set exactly one of urlFile or url")
]) proxyCfg.subscriptions;
globalTunAutoRouteTable = constants.tunAutoRouteTableIndex;
globalZapretQnum = constants.zapretGlobalQnum.${zapretEngine};
collisionAssertions =
let
bothTun = globalTun.enable && perAppRoutingTun.enable;
perAppTunWithTproxy = perAppRoutingTun.enable && globalTproxy.enable;
bothPerApp = perAppRoutingTun.enable && perAppRoutingTproxy.enable;
tgWsBypass = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy;
in
[
(distinct bothTun globalTun.interface perAppRoutingTun.interface
"proxy.tun.interface and perAppRouting.tun.interface must differ"
)
(distinct bothTun globalTun.address perAppRoutingTun.address
"proxy.tun.address and perAppRouting.tun.address must differ"
)
(distinct bothTun perAppRoutingTun.routeTable globalTunAutoRouteTable
"perAppRouting.tun.routeTable must differ from the global TUN auto-route table ${toString globalTunAutoRouteTable}"
)
(distinct perAppTunWithTproxy perAppRoutingTun.fwmark globalTproxy.fwmark
"perAppRouting.tun.fwmark must differ from proxy.tproxy.fwmark when global TProxy is enabled"
)
(distinct perAppTunWithTproxy perAppRoutingTun.fwmark globalTproxy.proxyMark
"perAppRouting.tun.fwmark must differ from proxy.tproxy.proxyMark when global TProxy is enabled"
)
(distinct perAppTunWithTproxy perAppRoutingTun.routeTable globalTproxy.routeTable
"perAppRouting.tun.routeTable must differ from proxy.tproxy.routeTable when global TProxy is enabled"
)
(distinct (perAppZapretCfg.enable && derived.zapretGlobalEnabled) perAppZapretCfg.qnum
globalZapretQnum
"perAppRouting.zapret.qnum must differ from the global zapret instance's NFQUEUE ${toString globalZapretQnum}"
)
(distinct perAppRoutingTproxy.enable perAppRoutingTproxy.fwmark globalTproxy.fwmark
"perAppRouting.tproxy.fwmark must differ from proxy.tproxy.fwmark"
)
(distinct perAppRoutingTproxy.enable perAppRoutingTproxy.fwmark globalTproxy.proxyMark
"perAppRouting.tproxy.fwmark must differ from proxy.tproxy.proxyMark"
)
(distinct perAppRoutingTproxy.enable perAppRoutingTproxy.routeTable globalTproxy.routeTable
"perAppRouting.tproxy.routeTable must differ from proxy.tproxy.routeTable"
)
(distinct bothPerApp perAppRoutingTun.fwmark perAppRoutingTproxy.fwmark
"perAppRouting.tun.fwmark and perAppRouting.tproxy.fwmark must differ"
)
(distinct bothPerApp perAppRoutingTun.routeTable perAppRoutingTproxy.routeTable
"perAppRouting.tun.routeTable and perAppRouting.tproxy.routeTable must differ"
)
(distinct perAppZapretCfg.enable perAppZapretCfg.filterMark globalTproxy.fwmark
"perAppRouting.zapret.filterMark must differ from proxy.tproxy.fwmark"
)
(distinct perAppZapretCfg.enable perAppZapretCfg.filterMark globalTproxy.proxyMark
"perAppRouting.zapret.filterMark must differ from proxy.tproxy.proxyMark"
)
(distinct (perAppRoutingTun.enable && perAppZapretCfg.enable) perAppRoutingTun.fwmark
perAppZapretCfg.filterMark
"perAppRouting.tun.fwmark and perAppRouting.zapret.filterMark must differ"
)
(distinct (perAppRoutingTproxy.enable && perAppZapretCfg.enable) perAppRoutingTproxy.fwmark
perAppZapretCfg.filterMark
"perAppRouting.tproxy.fwmark and perAppRouting.zapret.filterMark must differ"
)
(distinct (
tgWsBypass && globalTproxy.enable
) tgWsProxyCfg.fwmark globalTproxy.fwmark "tgWsProxy.fwmark must differ from proxy.tproxy.fwmark")
(distinct (tgWsBypass && globalTproxy.enable) tgWsProxyCfg.fwmark globalTproxy.proxyMark
"tgWsProxy.fwmark must differ from proxy.tproxy.proxyMark"
)
(distinct (tgWsBypass && perAppRoutingTun.enable) tgWsProxyCfg.fwmark perAppRoutingTun.fwmark
"tgWsProxy.fwmark must differ from perAppRouting.tun.fwmark"
)
(distinct (tgWsBypass && perAppRoutingTproxy.enable) tgWsProxyCfg.fwmark perAppRoutingTproxy.fwmark
"tgWsProxy.fwmark must differ from perAppRouting.tproxy.fwmark"
)
(distinct (tgWsBypass && perAppZapretCfg.enable) tgWsProxyCfg.fwmark perAppZapretCfg.filterMark
"tgWsProxy.fwmark must differ from perAppRouting.zapret.filterMark"
)
];
perAppZapretDesyncMarks = [
67108864
134217728
];
positiveNumberAssertions = [
(positive globalTun.enable globalTun.mtu "proxy.tun.mtu")
(positive perAppRoutingTun.enable perAppRoutingTun.mtu "perAppRouting.tun.mtu")
(positive globalTproxy.enable globalTproxy.fwmark "proxy.tproxy.fwmark")
(positive globalTproxy.enable globalTproxy.proxyMark "proxy.tproxy.proxyMark")
(positive globalTproxy.enable globalTproxy.routeTable "proxy.tproxy.routeTable")
(positive perAppRoutingTun.enable perAppRoutingTun.fwmark "perAppRouting.tun.fwmark")
(positive perAppRoutingTun.enable perAppRoutingTun.routeTable "perAppRouting.tun.routeTable")
(positive perAppRoutingTproxy.enable perAppRoutingTproxy.fwmark "perAppRouting.tproxy.fwmark")
(positive perAppRoutingTproxy.enable perAppRoutingTproxy.routeTable
"perAppRouting.tproxy.routeTable"
)
(positive perAppZapretCfg.enable perAppZapretCfg.filterMark "perAppRouting.zapret.filterMark")
(positive perAppZapretCfg.enable perAppZapretCfg.qnum "perAppRouting.zapret.qnum")
(positive (
tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy
) tgWsProxyCfg.fwmark "tgWsProxy.fwmark")
];
forbiddenValueAssertions =
let
# zapret's own desync marks: v1's pair, and the bits nfqws2 sets for per-app zapret.
zapretInternalMarks = [
536870912
1073741824
];
noDesyncBits =
condition: value: label:
forbiddenValues condition value perAppZapretDesyncMarks
"proxy-suite: ${label} must not use per-app-zapret internal desync mark bits";
tgWsBypass = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy;
in
[
(forbiddenValues perAppZapretCfg.enable perAppZapretCfg.filterMark zapretInternalMarks
"proxy-suite: perAppRouting.zapret.filterMark must not use zapret internal desync mark bits"
)
(noDesyncBits perAppZapretCfg.enable globalTproxy.fwmark "proxy.tproxy.fwmark")
(noDesyncBits perAppZapretCfg.enable globalTproxy.proxyMark "proxy.tproxy.proxyMark")
(noDesyncBits (
perAppRoutingTun.enable && perAppZapretCfg.enable
) perAppRoutingTun.fwmark "perAppRouting.tun.fwmark")
(noDesyncBits (
perAppRoutingTproxy.enable && perAppZapretCfg.enable
) perAppRoutingTproxy.fwmark "perAppRouting.tproxy.fwmark")
(noDesyncBits perAppZapretCfg.enable perAppZapretCfg.filterMark "perAppRouting.zapret.filterMark")
(noDesyncBits (tgWsBypass && perAppZapretCfg.enable) tgWsProxyCfg.fwmark "tgWsProxy.fwmark")
];
torCfg = cfg.tor;
torOnionListeners =
if torCfg.onionService.listeners == null then [ ] else torCfg.onionService.listeners;
torOnionUnfit = map (ib: ib.tag) (
builtins.filter (ib: !derived.proxyInboundOnionCapable ib) derived.torOnionInbounds
);
torOnionVirtualPorts = map (
ib: if ib.listener.sharePort != null then ib.listener.sharePort else ib.listener.port
) derived.torOnionInbounds;
whitelistBypassCfg = cfg.whitelistBypass;
whitelistBypassAssertions = [
# They become unit names, outbound tags and file names, spliced into the units' scripts.
(mkAssertion
(
!whitelistBypassCfg.enable
|| lib.all (name: builtins.match "[A-Za-z0-9][A-Za-z0-9._-]*" name != null) (
builtins.attrNames whitelistBypassCfg.joiners ++ builtins.attrNames whitelistBypassCfg.creators
)
)
"proxy-suite: whitelistBypass.joiners and .creators names must be letters, digits, '.', '_' and '-', starting with a letter or digit"
)
(requireEnabled (
derived.whitelistBypassJoiners != [ ]
) proxyEnabled "proxy-suite: whitelistBypass.joiners requires proxy.enable = true")
(requireEnabled
(
whitelistBypassCfg.enable
&& lib.any (c: c.upstream == "proxy") (builtins.attrValues whitelistBypassCfg.creators)
)
proxyEnabled
''proxy-suite: whitelistBypass.creators.<name>.upstream = "proxy" requires proxy.enable = true''
)
];
torSnowflake = lib.any (line: lib.hasPrefix "snowflake " line) torCfg.bridges.lines;
torAssertions = [
(mkAssertion (
!torCfg.enable || torCfg.asOutbound || torCfg.onionService.enable
) "proxy-suite: tor.enable = true needs tor.asOutbound or tor.onionService.enable")
(requireEnabled (
torCfg.enable && torCfg.asOutbound
) proxyEnabled "proxy-suite: tor.asOutbound requires proxy.enable = true")
(requireEnabled (
torCfg.enable && torCfg.upstream == "proxy"
) proxyEnabled ''proxy-suite: tor.upstream = "proxy" requires proxy.enable = true'')
# Tor as the one outbound would be what the local proxy dials Tor's own relays through.
(mkAssertion
(
!(
derived.torOutboundEnabled
&& torCfg.upstream == "proxy"
&& lib.subtractLists [ derived.torOutboundTag ] effectiveOutboundTags == [ ]
&& !derived.hasSubscriptions
)
)
''proxy-suite: tor.upstream = "proxy" needs an outbound besides "tor" to reach Tor through; with tor the only one, Tor would dial its relays through itself''
)
(mkAssertion (!(torCfg.enable && torCfg.upstream == "proxy" && torSnowflake))
''proxy-suite: tor.bridges.lines has a snowflake bridge, which cannot follow tor.upstream = "proxy"; use obfs4, webtunnel or meek_lite bridges, or upstream = "direct"''
)
(mkAssertion (!(derived.torOutboundEnabled && builtins.elem derived.torOutboundTag outboundTags))
"proxy-suite: the outbound tag \"tor\" belongs to tor.asOutbound; rename the proxy.outbounds entry"
)
(requireEnabled (
torCfg.enable && torCfg.onionService.enable
) cfg.inbounds.enable "proxy-suite: tor.onionService.enable requires inbounds.enable = true")
(mkAssertion
(
!derived.torOnionEnabled
|| builtins.all (tag: builtins.hasAttr tag cfg.inbounds.listeners) torOnionListeners
)
"proxy-suite: tor.onionService.listeners names listeners not in inbounds.listeners: ${
lib.concatStringsSep ", " (
builtins.filter (tag: !builtins.hasAttr tag cfg.inbounds.listeners) torOnionListeners
)
}"
)
(mkAssertion (!derived.torOnionEnabled || derived.torOnionInbounds != [ ])
"proxy-suite: tor.onionService has no listener to serve: amneziawg, h3-only xhttp and raw JSON listeners cannot go over an onion"
)
(mkAssertion (!derived.torOnionEnabled || torOnionUnfit == [ ])
"proxy-suite: tor.onionService.listeners cannot serve ${lib.concatStringsSep ", " torOnionUnfit}: an onion carries TCP only, so not amneziawg or h3-only xhttp, and raw JSON listeners have no known protocol"
)
(uniqueValues derived.torOnionEnabled torOnionVirtualPorts
"proxy-suite: tor.onionService listeners must each share a distinct port (sharePort, or port)"
)
(mkAssertion
(
!derived.torOutboundEnabled
|| !(
(proxyEnabled && proxyCfg.listener.port == torCfg.socksPort)
|| (proxyInboundsEnabled && builtins.elem torCfg.socksPort derived.proxyInboundPorts)
|| builtins.elem torCfg.socksPort (
builtins.attrValues constants.xrayDnsBridgePorts ++ [ constants.outboundTestPort ]
)
)
)
"proxy-suite: tor.socksPort ${toString torCfg.socksPort} collides with another proxy-suite listener"
)
];
# zapret2.blobs' names go into nfqws2's argv as --blob=<name>:@<file>.
blobNameAssertions = map (
name:
mkAssertion (builtins.match "[A-Za-z0-9._-]+" name != null)
"proxy-suite: zapret.zapret2.blobs name '${name}' must be letters, digits, dot, dash and underscore"
) (builtins.attrNames cfg.zapret.zapret2.blobs);
in
rootlessAssertions
++ blobNameAssertions
++ featureAssertions
++ torAssertions
++ whitelistBypassAssertions
++ perAppRoutingAssertions
++ localProxyAuthAssertions
++ secretAssertions
++ outboundAssertions
++ proxyInboundAssertions
++ awgInboundAssertions
++ subscriptionAssertions
++ collisionAssertions
++ positiveNumberAssertions
++ forbiddenValueAssertions