Files
proxy-suite-flake/modules/proxy-suite/rules/proxy-inbounds.nix
T
FUFSoB fdb2b08790 refactor: overhaul of options and features
Includes, but not limited to:
- zapret2 support
- declarative inbounds
- proxy-ctl revamp
- automatic proxy learning
2026-09-13 22:37:45 +05:00

136 lines
3.8 KiB
Nix

# XRay routing for the inbound service. Separate from the client ruleset:
# "proxy" here is the client stack's SOCKS listener, so its selection applies.
{
lib,
proxyInboundsCfg,
proxyInbounds,
zapretDirectRules,
}:
let
defaultVia = proxyInboundsCfg.routing.via;
# Listeners on the default egress are covered by the final rule.
overrideInbounds = builtins.filter (ib: ib.via != defaultVia) proxyInbounds;
defaultInboundTags = map (ib: ib.tag) (builtins.filter (ib: ib.via == defaultVia) proxyInbounds);
blockPrivateRule = lib.optional proxyInboundsCfg.routing.blockPrivate {
type = "field";
ruleTag = "inbound-block-private";
ip = [ "geoip:private" ];
outboundTag = "block";
};
# The address clients dial must stay reachable even when blockRu covers it
# (a .ru domain or IP). After blockPrivate, so it cannot open the LAN.
serverAddressIsIp =
let
addr = proxyInboundsCfg.serverAddress;
in
addr != null
&& (lib.hasInfix ":" addr || builtins.match "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+" addr != null);
serverAddressRule = lib.optional (proxyInboundsCfg.serverAddress != null) (
{
type = "field";
ruleTag = "inbound-server-address-direct";
outboundTag = "direct";
}
// (
if serverAddressIsIp then
{ ip = [ proxyInboundsCfg.serverAddress ]; }
else
{ domain = [ "full:${proxyInboundsCfg.serverAddress}" ]; }
)
);
blockRuRules = lib.optionals proxyInboundsCfg.routing.blockRu [
{
type = "field";
ruleTag = "inbound-block-ru-domain";
domain = [ "geosite:category-ru" ];
outboundTag = "block";
}
{
type = "field";
ruleTag = "inbound-block-ru-ip";
ip = [ "geoip:ru" ];
outboundTag = "block";
}
];
inboundProxy = proxyInboundsCfg.routing.proxy;
proxyDomainRule = lib.optional (inboundProxy.domains != [ ] || inboundProxy.geosites != [ ]) {
type = "field";
ruleTag = "inbound-proxy-domain";
domain =
map (domain: "domain:${domain}") inboundProxy.domains
++ map (name: "geosite:${name}") inboundProxy.geosites;
inboundTag = map (ib: ib.tag) proxyInbounds;
outboundTag = "proxy";
};
proxyIpRule = lib.optional (inboundProxy.ips != [ ] || inboundProxy.geoips != [ ]) {
type = "field";
ruleTag = "inbound-proxy-ip";
ip = inboundProxy.ips ++ map (name: "geoip:${name}") inboundProxy.geoips;
inboundTag = map (ib: ib.tag) proxyInbounds;
outboundTag = "proxy";
};
# Only when the default egress is a proxy, and only for its listeners.
zapretDirectEnabled =
proxyInboundsCfg.routing.zapretDirect
&& defaultInboundTags != [ ]
&& !builtins.elem defaultVia [
"direct"
"block"
];
mkZapretRule =
ruleTag: field: items:
lib.optional (zapretDirectEnabled && items != [ ]) {
type = "field";
inherit ruleTag;
${field} = items;
inboundTag = defaultInboundTags;
outboundTag = "direct";
};
zapretRules =
mkZapretRule "inbound-zapret-direct-domain" "domain" (
map (domain: "domain:${domain}") zapretDirectRules.domains
)
++ mkZapretRule "inbound-zapret-direct-ip" "ip" zapretDirectRules.ips;
overrideRules = map (ib: {
type = "field";
ruleTag = "inbound-via-${ib.tag}";
inboundTag = [ ib.tag ];
outboundTag = ib.via;
}) overrideInbounds;
finalRule = {
type = "field";
ruleTag = "inbound-final";
network = "tcp,udp";
outboundTag = defaultVia;
};
# Explicit proxy exceptions beat blockRu: RU-geolocated ranges (Telegram's,
# at times) would otherwise swallow them.
xrayInboundRules =
blockPrivateRule
++ serverAddressRule
++ proxyDomainRule
++ proxyIpRule
++ blockRuRules
++ zapretRules
++ overrideRules
++ [ finalRule ];
in
{
inherit xrayInboundRules;
}