refactor: overhaul of options and features

Includes, but not limited to:
- zapret2 support
- declarative inbounds
- proxy-ctl revamp
- automatic proxy learning
This commit is contained in:
FUFSoB committed 2026-09-13 22:37:45 +05:00
1 parent b892384527
commit fdb2b08790
144 files changed
+13198 -14066

No files matched your search

@@ -1,6 +1,5 @@
# Build-time XRay configuration template for the server-side inbound service.
# Inbounds and proxy outbounds are both injected at service start time, so no
# credential reaches the Nix store.
# XRay config template for the inbound service. Inbounds and pinned outbounds
# are injected at start, so no credential reaches the Nix store.
{
lib,
derived,
@@ -9,23 +8,25 @@
}:
let
inherit (derived) proxyCfg proxyInboundsCfg proxyInboundsNeedLocalProxy;
inherit (derived)
proxyCfg
proxyInboundsNeedLocalProxy
proxyInboundsResolveInSingBox
;
# Relaying goes through the client stack's SOCKS listener rather than a second
# copy of the outbound machinery, so the inbound service always follows the
# currently selected outbound. Connect over loopback when the listener is on a
# wildcard address.
localProxyAddress =
if builtins.elem proxyCfg.listenAddress [
"0.0.0.0"
"::"
""
] then
if
builtins.elem proxyCfg.listener.address [
"0.0.0.0"
"::"
""
]
then
"127.0.0.1"
else
proxyCfg.listenAddress;
proxyCfg.listener.address;
# Credentials, when the local proxy has them, are injected at start time.
# The client stack's SOCKS listener; credentials are injected at start.
localProxyOutbound = lib.optional proxyInboundsNeedLocalProxy {
protocol = "socks";
tag = "proxy";
@@ -33,7 +34,7 @@ let
servers = [
{
address = localProxyAddress;
port = proxyCfg.port;
port = proxyCfg.listener.port;
}
];
};
@@ -47,9 +48,20 @@ in
(mkDnsServer "local" proxyCfg.dns.local)
];
# Filled in at start time from the listener spec.
inbounds = [ ];
# Per-user counters for proxy-suite-inbound-stats, on loopback only.
stats = { };
api = {
tag = "api";
listen = "127.0.0.1:${toString derived.constants.inboundStatsApiPort}";
services = [ "StatsService" ];
};
policy.levels."0" = {
statsUserUplink = true;
statsUserDownlink = true;
};
outbounds = localProxyOutbound ++ [
{
protocol = "freedom";
@@ -64,7 +76,11 @@ in
];
routing = {
domainStrategy = "IPIfNonMatch";
# 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 = inboundRules.xrayInboundRules;
};
@@ -52,7 +52,7 @@ let
rule_set = map (s: "geosite-${s}") direct.geosites;
server = "local";
};
final = if proxyCfg.proxyByDefault then "remote" else "local";
final = if (proxyCfg.routing.default == "proxy") then "remote" else "local";
};
xrayDnsBridgeHijackRule = {
@@ -105,8 +105,8 @@ let
++ lib.optional enableMixed {
type = "mixed";
tag = "mixed-in";
listen = proxyCfg.listenAddress;
listen_port = proxyCfg.port;
listen = proxyCfg.listener.address;
listen_port = proxyCfg.listener.port;
}
++ lib.optional enableTProxy {
type = "tproxy";
@@ -150,7 +150,7 @@ let
default_domain_resolver = "local";
rule_set = rules.geositeRuleSets ++ rules.geoIPRuleSets;
rules = lib.optionals enableXrayDnsBridge [ xrayDnsBridgeHijackRule ] ++ rules.singBoxRoutingRules;
final = if proxyCfg.proxyByDefault then "proxy" else "direct";
final = if (proxyCfg.routing.default == "proxy") then "proxy" else "direct";
}
// lib.optionalAttrs (enableTun && tunAutoRoute) {
auto_detect_interface = true;
@@ -42,7 +42,7 @@ let
mkTunDnsConfig =
{
preferRemote ? proxyCfg.proxyByDefault,
preferRemote ? (proxyCfg.routing.default == "proxy"),
}:
let
primaryTag = if preferRemote then "remote" else "local";
@@ -143,8 +143,8 @@ let
}:
let
tunDnsServers = [
(if proxyCfg.proxyByDefault then proxyCfg.dns.remote.address else proxyCfg.dns.local.address)
(if proxyCfg.proxyByDefault then proxyCfg.dns.local.address else proxyCfg.dns.remote.address)
(if (proxyCfg.routing.default == "proxy") then proxyCfg.dns.remote.address else proxyCfg.dns.local.address)
(if (proxyCfg.routing.default == "proxy") then proxyCfg.dns.local.address else proxyCfg.dns.remote.address)
];
tunSniffing = mkSniffing { fakeDnsOnly = enableTunFakeDns; };
dnsConfig =
@@ -178,7 +178,7 @@ let
tunDnsHijackRule
]
++ rules.xrayRoutingRules
++ [ (finalRule (if proxyCfg.proxyByDefault then "proxy" else "direct")) ];
++ [ (finalRule (if (proxyCfg.routing.default == "proxy") then "proxy" else "direct")) ];
in
{
log.loglevel = "warning";
@@ -187,12 +187,12 @@ let
lib.optional enableMixed {
tag = "mixed-in";
protocol = "socks";
listen = proxyCfg.listenAddress;
port = proxyCfg.port;
listen = proxyCfg.listener.address;
port = proxyCfg.listener.port;
settings = {
auth = "noauth";
udp = true;
ip = proxyCfg.listenAddress;
ip = proxyCfg.listener.address;
};
sniffing = standardSniffing;
}