fix: various fixes

This commit is contained in:
fufsob committed 2026-09-15 17:52:49 +00:00
1 parent 303821f43a
commit 2ce8da3f43
18 files changed
+471 -88

No files matched your search

@@ -76,7 +76,20 @@ in
{
protocol = "freedom";
tag = "direct";
settings = { };
# 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";
+2
View File
@@ -49,6 +49,8 @@ let
users
flow
method
serverPassword
serverPasswordFile
transport
tls
reality
+13 -5
View File
@@ -94,11 +94,13 @@ let
# Names pass unresolved unless XRay dials itself (a direct listener, or pure XRay).
proxyInboundsResolveInSingBox = !pureXrayEnabled && !lib.any (ib: ib.via == "direct") proxyInbounds;
# Even when XRay resolves (a direct listener), it hands sing-box the name, which sing-box
# looks up again: guard there too.
proxyInboundsGuardPrivate =
proxyInboundsEnabled
&& proxyInboundsCfg.routing.blockPrivate
&& proxyInboundsNeedLocalProxy
&& proxyInboundsResolveInSingBox;
&& !pureXrayEnabled;
# Other vias name static outbounds, rendered into the inbound service's own config.
proxyInboundViaTags = lib.unique (
@@ -129,10 +131,13 @@ let
# Loopback listeners are fronted locally; their ports stay closed.
proxyInboundsPublic = builtins.filter (
ib:
!builtins.elem ib.listener.address [
"127.0.0.1"
"::1"
]
!(
lib.hasPrefix "127." ib.listener.address
|| builtins.elem ib.listener.address [
"::1"
"localhost"
]
)
) proxyInbounds;
# h3-only xhttp listeners are UDP-only, leaving the TCP port to a web server.
proxyInboundIsH3Only =
@@ -266,6 +271,9 @@ let
tunAutoRouteTableIndex = 2022;
tunAutoRouteRulePriority = 9000;
# Keeps proxy-suite's own daemons (the inbound XRay, replies to its clients included)
# out of the pure-XRay global TUN.
xrayTunServiceUserRulePriority = 8995;
xrayTunPerAppTproxyRulePriority = 8996;
xrayTunPerAppTunRulePriority = 8997;
+8
View File
@@ -6,6 +6,7 @@
}:
let
inherit ((import ./derived.nix { inherit lib cfg; }).constants) serviceUser;
proxyCfg = cfg.proxy;
globalTproxy = proxyCfg.tproxy;
perAppTun = cfg.perAppRouting.tun;
@@ -48,6 +49,9 @@ let
chain prerouting {
type filter hook prerouting priority mangle; policy accept;
ip daddr $RESERVED_IP return
# Connections to this host itself (inbounds, sshd on a public address)
# are served here, not taken by the tproxy socket.
fib daddr type local return
# Packets re-entering via loopback after output marking should not
# be skipped just because the host has an RFC1918 source address.
iifname != "lo" ip saddr $RESERVED_IP return
@@ -60,6 +64,10 @@ let
ip daddr $RESERVED_IP return
${tproxyLocalSubnetLines}
meta mark ${toString globalTproxy.proxyMark} return
# Replies to connections from outside, and proxy-suite's own daemons (the
# inbound XRay dials unmarked, lacking CAP_NET_ADMIN), leave as they are.
ct direction reply return
meta skuid "${serviceUser}" return
${tgWsProxyBypassMarkLine}${lib.optionalString perAppTun.enable " meta mark ${toString perAppTun.fwmark} return\n"}${lib.optionalString perAppTproxy.enable " meta mark ${toString perAppTproxy.fwmark} return\n"} ip protocol tcp meta mark set ${toString globalTproxy.fwmark}
ip protocol udp meta mark set ${toString globalTproxy.fwmark}
}
@@ -158,7 +158,7 @@ let
port = mkOption {
type = types.port;
default = 443;
description = "Bound port.";
description = "Bound port. A raw-JSON listener's port must match it: the firewall and the port checks go by this one.";
};
sharePort = mkOption {
@@ -194,10 +194,13 @@ let
method = mkOption {
type = types.str;
default = "2022-blake3-aes-128-gcm";
description = "Shadowsocks cipher. 2022 ciphers take a base64 key of matching length as password.";
description = "Shadowsocks cipher. 2022 ciphers take a base64 key of matching length as password; more than one user needs a 2022-blake3-aes cipher and serverPassword.";
example = "aes-128-gcm";
};
serverPassword = nullStr "Server key of a multi-user shadowsocks 2022 listener, shared by its users. Ends up in the Nix store; prefer serverPasswordFile." "c2VydmVyLWtleS0xNmJ5dGU=";
serverPasswordFile = nullStr "Runtime path to the server key." "/run/secrets/proxy-inbound-ss-server-key";
transport = mkOption {
type = transportType;
default = { };
@@ -219,14 +222,14 @@ let
xrayJson = mkOption {
type = types.nullOr types.attrs;
default = null;
description = "Raw XRay inbound, built into the store; tag is overridden.";
description = "Raw XRay inbound, built into the store; tag is overridden and port, if left out, filled in.";
example = {
protocol = "dokodemo-door";
settings.port = 8080;
};
};
jsonFile = nullStr "Runtime path to a raw XRay inbound (no share link); tag is overridden." "/run/secrets/proxy-inbound-vless.json";
jsonFile = nullStr "Runtime path to a raw XRay inbound (no share link); tag is overridden and port, if left out, filled in." "/run/secrets/proxy-inbound-vless.json";
};
};
in
+53 -28
View File
@@ -14,6 +14,9 @@ let
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);
blockPrivateRule = lib.optional proxyInboundsCfg.routing.blockPrivate {
type = "field";
ruleTag = "inbound-block-private";
@@ -30,20 +33,36 @@ let
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}" ]; }
)
# Only the ports clients dial there: 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
) proxyInbounds
++ lib.optionals proxyInboundsCfg.subscriptions.enable [
80
443
]
);
serverAddressRule =
lib.optional (proxyInboundsCfg.serverAddress != null && exceptionInboundTags != [ ])
(
{
type = "field";
ruleTag = "inbound-server-address-direct";
inboundTag = exceptionInboundTags;
port = lib.concatMapStringsSep "," toString serverAddressPorts;
outboundTag = "direct";
}
// (
if serverAddressIsIp then
{ ip = [ proxyInboundsCfg.serverAddress ]; }
else
{ domain = [ "full:${proxyInboundsCfg.serverAddress}" ]; }
)
);
blockRuRules = lib.optionals proxyInboundsCfg.routing.blockRu [
{
type = "field";
@@ -61,23 +80,29 @@ let
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";
};
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 (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";
};
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 =
+69 -9
View File
@@ -309,13 +309,22 @@ let
)
"proxy-suite: a proxyInbounds listener port collides with proxy.tproxy.port (${toString globalTproxy.port})"
)
(mkAssertion
(
!proxyInboundsEnabled
|| !lib.any (port: builtins.elem port derived.proxyInboundPorts) (
[ derived.constants.inboundStatsApiPort ]
++ builtins.attrValues derived.constants.xrayDnsBridgePorts
)
)
"proxy-suite: a proxyInbounds listener port collides with a port proxy-suite uses internally (the inbounds' stats API on ${toString derived.constants.inboundStatsApiPort}, or 18533-18535)"
)
]
++ lib.concatMap (
ib:
let
l = ib.listener;
prefix = "proxy-suite: inbounds listener '${ib.tag}'";
firstUser = if l.users == [ ] then null else builtins.head l.users;
needsUuid = builtins.elem l.type [
"vless"
"vmess"
@@ -327,6 +336,13 @@ let
"http"
];
tlsTerminated = l.tls.enable || l.type == "trojan";
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 [
@@ -340,14 +356,58 @@ let
(mkAssertion (
!proxyInboundsEnabled || l.type == null || l.users != [ ]
) "${prefix}: at least one entry in users is required")
(exactlyOneOf (proxyInboundsEnabled && needsUuid && firstUser != null) [
firstUser.uuid
firstUser.uuidFile
] "${prefix}: ${toString l.type} users need exactly one of uuid or uuidFile")
(exactlyOneOf (proxyInboundsEnabled && needsPassword && firstUser != null) [
firstUser.password
firstUser.passwordFile
] "${prefix}: ${toString l.type} users need exactly one of password or passwordFile")
# 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
@@ -16,6 +16,7 @@ let
inherit (constants)
tunAutoRouteTableIndex
tunAutoRouteRulePriority
xrayTunServiceUserRulePriority
xrayTunPerAppTproxyRulePriority
xrayTunPerAppTunRulePriority
xrayGlobalTunIPv6Address
@@ -57,6 +58,8 @@ in
while ${ip} -4 rule del pref ${toString xrayTunPerAppTproxyRulePriority} 2>/dev/null; do :; done
while ${ip} -4 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done
while ${ip} -4 rule del pref ${toString xrayTunServiceUserRulePriority} 2>/dev/null; do :; done
while ${ip} -6 rule del pref ${toString xrayTunServiceUserRulePriority} 2>/dev/null; do :; done
while ${ip} -4 rule del pref ${toString tunAutoRouteRulePriority} 2>/dev/null; do :; done
while ${ip} -6 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done
while ${ip} -6 rule del pref ${toString tunAutoRouteRulePriority} 2>/dev/null; do :; done
@@ -77,6 +80,9 @@ in
${ip} -4 rule add pref ${toString xrayTunPerAppTunRulePriority} fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable}
${ip} -6 rule add pref ${toString xrayTunPerAppTunRulePriority} fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable}
''}
service_uid=$(${pkgs.coreutils}/bin/id -u ${constants.serviceUser})
${ip} -4 rule add pref ${toString xrayTunServiceUserRulePriority} uidrange "$service_uid-$service_uid" lookup main
${ip} -6 rule add pref ${toString xrayTunServiceUserRulePriority} uidrange "$service_uid-$service_uid" lookup main
${ip} -4 rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex}
${ip} -6 rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex}
'';
@@ -136,6 +142,16 @@ in
family = "-6";
priority = xrayTunPerAppTunRulePriority;
}}
${builders.mkIpRuleDeleteByPriority {
inherit ip;
family = "-4";
priority = xrayTunServiceUserRulePriority;
}}
${builders.mkIpRuleDeleteByPriority {
inherit ip;
family = "-6";
priority = xrayTunServiceUserRulePriority;
}}
${builders.mkIpRouteFlushTable { inherit ip; family = "-4"; table = tunAutoRouteTableIndex; }}
${builders.mkIpRouteFlushTable { inherit ip; family = "-6"; table = tunAutoRouteTableIndex; }}
${builders.mkIpLinkDelete { inherit ip; interface = globalTun.interface; }}
@@ -121,12 +121,15 @@ else
''
+ lib.optionalString proxyInboundsGuardPrivate ''
# inbounds.routing.blockPrivate, enforced here because the listener passes names unresolved.
# Last, so only traffic falling through to a direct final is resolved. Local mixed-
# in clients lose names that resolve private.
| if (.route.final // "direct") == "direct" then
.route.rules += [
{inbound: ["mixed-in"], action: "resolve"},
{inbound: ["mixed-in"], ip_is_private: true, action: "reject"}
]
# Just before the first direct rule (or last, for a direct final), so names that the
# proxy rules above take stay unresolved and none reaches a direct dial unchecked. Local
# mixed-in clients lose names that resolve private.
| [{inbound: ["mixed-in"], action: "resolve"},
{inbound: ["mixed-in"], ip_is_private: true, action: "reject"}] as $guard
| (.route.rules | map((.outbound? // "") == "direct") | index(true)) as $first_direct
| if $first_direct != null then
.route.rules = .route.rules[:$first_direct] + $guard + .route.rules[$first_direct:]
elif (.route.final // "direct") == "direct" then
.route.rules += $guard
else . end
''
@@ -147,6 +147,7 @@ let
enableAutoProxy ? false,
enableOutboundTest ? false,
enableZapretCutoff ? false,
excludeServiceUserFromTun ? false,
}:
pkgs.writeShellScript "proxy-suite-core" ''
set -euo pipefail
@@ -344,6 +345,14 @@ let
--arg xray_dns_remote_client ${lib.escapeShellArg proxyCfg.dns.remote.address} \
-f "$BACKEND_JQ_FILTER" \
"${configFile}" > "$RUNTIME_DIR/config.json"
${lib.optionalString excludeServiceUserFromTun ''
# proxy-suite's own daemons (the inbound XRay, replies to its clients included)
# stay out of the TUN; the uid is only known on this host.
SERVICE_UID=$(${pkgs.coreutils}/bin/id -u ${constants.serviceUser})
${jq} --argjson uid "$SERVICE_UID" '(.inbounds[] | select(.type == "tun") | .exclude_uid) = [$uid]' \
"$RUNTIME_DIR/config.json" > "$RUNTIME_DIR/config.json.next"
mv "$RUNTIME_DIR/config.json.next" "$RUNTIME_DIR/config.json"
''}
# The backend runs as ${constants.serviceUser}: its configs are group-readable.
for backend_config in "$RUNTIME_DIR/config.json" "$RUNTIME_DIR/xray-sidecar.json"; do
[ -e "$backend_config" ] || continue
@@ -416,6 +425,8 @@ let
xrayTunDnsRuntime = pureXrayEnabled;
xraySidecarBasePort = xraySidecarBasePorts.tun;
xrayDnsBridgePort = xrayDnsBridgePorts.tun;
# Pure XRay's TUN routes by ip rules instead: see xrayTunUpScript.
excludeServiceUserFromTun = !pureXrayEnabled;
};
startPerAppTun = mkStartScript {