Files
proxy-suite-flake/modules/proxy-suite/config-templates/proxy-inbounds.nix
T

118 lines
3.1 KiB
Nix

# XRay config template for the inbound service. Inbounds and pinned outbounds
# are injected at start, so no credential reaches the Nix store.
{
lib,
derived,
inboundRules,
mkDnsServer,
}:
let
inherit (derived)
proxyCfg
proxyInboundsNeedLocalProxy
proxyInboundsResolveInSingBox
;
localProxyAddress = derived.localProxy.host;
# The client stack's SOCKS listener; credentials are injected at start.
localProxyOutbound = lib.optional proxyInboundsNeedLocalProxy {
protocol = "socks";
tag = "proxy";
settings = {
servers = [
{
address = localProxyAddress;
port = proxyCfg.listener.port;
}
];
};
};
in
{
log.loglevel = "warning";
dns.servers = [
(mkDnsServer "remote" proxyCfg.dns.remote)
(mkDnsServer "local" proxyCfg.dns.local)
];
# Counters per user, inbound and outbound for proxy-suite-inbound-stats, and who is
# connected right now, from where. A socket rather than loopback, which any local
# user could ask: the start script's setgid directory gives it the group that may
# read the stats. The listeners are appended at start.
inbounds = [
{
tag = "api-in";
protocol = "dokodemo-door";
listen = "${derived.constants.inboundStatsApiSocket},0660";
settings = {
address = "127.0.0.1";
network = "unix";
};
}
];
stats = { };
api = {
tag = "api";
services = [ "StatsService" ];
};
policy = {
levels."0" = {
statsUserUplink = true;
statsUserDownlink = true;
statsUserOnline = true;
};
system = {
statsInboundUplink = true;
statsInboundDownlink = true;
statsOutboundUplink = true;
statsOutboundDownlink = true;
};
};
outbounds = localProxyOutbound ++ [
{
protocol = "freedom";
tag = "direct";
# Checked on the address actually dialed, so a name resolving private is caught too
# (routing's geoip:private sees only literal IPs under AsIs). Explicit either way:
# XRay's own default blocks private for vless/vmess/trojan/shadowsocks only, which
# left socks/http open and ignored blockPrivate = false.
settings.finalRules =
if derived.proxyInboundsCfg.routing.blockPrivate then
[
{
action = "block";
ip = [ "geoip:private" ];
}
]
else
[ { action = "allow"; } ];
}
{
protocol = "blackhole";
tag = "block";
settings = { };
}
];
routing = {
# AsIs where sing-box dials: resolving here made every new site wait on a
# lookup through the proxy, and sing-box enforces blockPrivate after its
# own. Where XRay dials itself it must resolve, and IPOnDemand: under
# IPIfNonMatch the network-only final rule matches names unresolved.
domainStrategy = if proxyInboundsResolveInSingBox then "AsIs" else "IPOnDemand";
domainMatcher = "hybrid";
rules = [
{
ruleTag = "inbound-stats-api";
inboundTag = [ "api-in" ];
outboundTag = "api";
}
]
++ inboundRules.xrayInboundRules;
};
}