183 lines
5.9 KiB
Nix
183 lines
5.9 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,
|
|
proxyInboundsRouteOnion,
|
|
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);
|
|
|
|
# A blocked listener stays blocked: neither serverAddress nor the proxy exceptions open it.
|
|
exceptionInboundTags = map (ib: ib.tag) (builtins.filter (ib: ib.via != "block") proxyInbounds);
|
|
|
|
# Only Tor reaches .onion: to the local proxy, whose own rule sends it there. First, so no
|
|
# IP rule before it has XRay look the name up, which would leak it to a resolver.
|
|
onionRule = lib.optional proxyInboundsRouteOnion {
|
|
type = "field";
|
|
ruleTag = "inbound-tor-onion";
|
|
domain = [ "domain:onion" ];
|
|
inboundTag = map (ib: ib.tag) (
|
|
builtins.filter (ib: ib.via != "block" && ib.listener.type != "amneziawg") proxyInbounds
|
|
);
|
|
outboundTag = "proxy";
|
|
};
|
|
|
|
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. Its aliases
|
|
# too: routing.via usually cannot loop back to this host, so they would fail there.
|
|
# A "domain:" alias is a suffix, for a wildcard DNS record: the name and every name under it.
|
|
isSuffix = lib.hasPrefix "domain:";
|
|
isIp =
|
|
addr:
|
|
!isSuffix addr
|
|
&& (lib.hasInfix ":" addr || builtins.match "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+" addr != null);
|
|
serverAddresses =
|
|
lib.optional (proxyInboundsCfg.serverAddress != null) proxyInboundsCfg.serverAddress
|
|
++ proxyInboundsCfg.serverAliases;
|
|
serverIps = lib.unique (builtins.filter isIp serverAddresses);
|
|
serverNames = lib.unique (builtins.filter (addr: !isIp addr) serverAddresses);
|
|
|
|
# Only the ports clients dial there, and the ones listed as public: the rest of this host
|
|
# (wildcard services the firewall keeps from the internet) must not be reachable through it.
|
|
serverAddressPorts = lib.unique (
|
|
map (ib: if ib.listener.sharePort != null then ib.listener.sharePort else ib.listener.port) (
|
|
# AmneziaWG is UDP to the interface, never relayed through XRay.
|
|
builtins.filter (ib: ib.listener.type != "amneziawg") proxyInbounds
|
|
)
|
|
++ lib.optionals proxyInboundsCfg.subscriptions.enable [
|
|
80
|
|
443
|
|
]
|
|
++ proxyInboundsCfg.serverPorts
|
|
);
|
|
|
|
# Names and IPs in rules of their own: XRay ANDs the fields of one rule.
|
|
mkServerAddressRule =
|
|
ruleTag: field: items:
|
|
lib.optional (items != [ ] && exceptionInboundTags != [ ]) {
|
|
type = "field";
|
|
inherit ruleTag;
|
|
${field} = items;
|
|
inboundTag = exceptionInboundTags;
|
|
port = lib.concatMapStringsSep "," toString serverAddressPorts;
|
|
outboundTag = "direct";
|
|
};
|
|
|
|
serverAddressRule =
|
|
mkServerAddressRule "inbound-server-address-direct" "domain" (
|
|
map (name: if isSuffix name then name else "full:${name}") serverNames
|
|
)
|
|
++ mkServerAddressRule "inbound-server-address-direct-ip" "ip" serverIps;
|
|
|
|
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
|
|
(exceptionInboundTags != [ ] && (inboundProxy.domains != [ ] || inboundProxy.geosites != [ ]))
|
|
{
|
|
type = "field";
|
|
ruleTag = "inbound-proxy-domain";
|
|
domain =
|
|
map (domain: "domain:${domain}") inboundProxy.domains
|
|
++ map (name: "geosite:${name}") inboundProxy.geosites;
|
|
inboundTag = exceptionInboundTags;
|
|
outboundTag = "proxy";
|
|
};
|
|
|
|
proxyIpRule =
|
|
lib.optional
|
|
(exceptionInboundTags != [ ] && (inboundProxy.ips != [ ] || inboundProxy.geoips != [ ]))
|
|
{
|
|
type = "field";
|
|
ruleTag = "inbound-proxy-ip";
|
|
ip = inboundProxy.ips ++ map (name: "geoip:${name}") inboundProxy.geoips;
|
|
inboundTag = exceptionInboundTags;
|
|
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 =
|
|
onionRule
|
|
++ blockPrivateRule
|
|
++ serverAddressRule
|
|
++ proxyDomainRule
|
|
++ proxyIpRule
|
|
++ blockRuRules
|
|
++ zapretRules
|
|
++ overrideRules
|
|
++ [ finalRule ];
|
|
in
|
|
{
|
|
inherit xrayInboundRules;
|
|
}
|