feat(inbounds): fallbacks

This commit is contained in:
fufsob committed 2026-09-25 08:09:13 +00:00
1 parent ba5f960b2e
commit 53fc86dd83
7 files changed
+547 -5

No files matched your search

+45
View File
@@ -35,6 +35,32 @@ let
inherit (xrayTemplates) mkDnsServer;
};
# Fallbacks to another listener are resolved here, where every listener is known.
inboundByTag = tag: lib.findFirst (ib: ib.tag == tag) null derived.proxyInbounds;
fallbackFront =
tag:
lib.findFirst (
ib: lib.any (fb: fb.listener == tag) ib.listener.fallbacks
) null derived.proxyInbounds;
renderFallback =
fb:
{
inherit (fb) name alpn path;
}
// (
if fb.listener == null then
{ inherit (fb) dest xver; }
else
let
target = (inboundByTag fb.listener).listener;
host = if lib.hasInfix ":" target.address then "[${target.address}]" else target.address;
in
{
dest = "${host}:${toString target.port}";
xver = 2;
}
);
# Listener specification handed to build-inbound.py at service start. Secrets
# appear here only as paths; the script reads them at runtime.
proxyInboundsSpec = {
@@ -63,6 +89,25 @@ let
jsonFile
;
listen = ib.listener.address;
fallbacks = map renderFallback ib.listener.fallbacks;
# The listener in front, whose port and TLS or REALITY the share links carry.
front =
let
f = fallbackFront ib.tag;
in
if f == null then
null
else
{
inherit (f) tag;
inherit (f.listener)
type
port
sharePort
tls
reality
;
};
}
# Only on AmneziaWG listeners: the defaults of the others need not type-check.
// lib.optionalAttrs (ib.listener.type == "amneziawg") {
@@ -244,6 +244,55 @@ let
};
};
fallbackType = types.submodule {
options = {
name = nullStr "SNI to match. Null matches any." "www.example.com";
alpn = mkOption {
type = types.nullOr (
types.enum [
"h2"
"http/1.1"
]
);
default = null;
description = "Negotiated ALPN to match. Null matches any.";
example = "h2";
};
path = nullStr ''
Request path to match. Null matches any. XRay reads it from HTTP/1.1 only, so a `listener`
must be on ws or httpupgrade, with this as its transport.path; xhttp and grpc clients speak h2
and go by alpn = "h2" or a catch-all instead.
'' "/ws";
dest = mkOption {
type = types.nullOr (types.either types.port types.str);
default = null;
description = "Where matching connections go: a port, host:port, or a unix socket path. Exclusive with listener.";
example = "127.0.0.1:8080";
};
listener = nullStr ''
Tag of another listener that matching connections go to, instead of dest. It must be a
vless listener without tls or reality, on a loopback address: it gets the connection
decrypted, with the client address in PROXY protocol, and its share links advertise this
listener's port and TLS or REALITY. Behind REALITY it must be xhttp or grpc, the only
transports REALITY clients run besides raw.
'' "ws-in";
xver = mkOption {
type = types.enum [
0
1
2
];
default = 0;
description = "PROXY protocol version sent to dest; 0 sends none. A listener always gets 2.";
};
};
};
inboundType = types.submodule (
{ name, ... }:
{
@@ -335,6 +384,24 @@ let
description = "REALITY.";
};
fallbacks = mkOption {
type = types.listOf fallbackType;
default = [ ];
description = ''
Where XRay sends connections that are not this listener's protocol, so several share
one port (vless or trojan on the raw transport). Matched in order on SNI, ALPN and path;
the first entry without any is the catch-all, such as a decoy web server. Browsers
negotiate h2 unless tls.alpn says otherwise, so a dest that speaks only HTTP/1.1 needs
tls.alpn = [ "http/1.1" ], or an alpn = "h2" entry to one that speaks h2c.
'';
example = lib.literalExpression ''
[
{ path = "/ws"; listener = "ws-in"; } # ws-in: vless, ws, address = "127.0.0.1"
{ dest = 8080; }
]
'';
};
xrayJson = mkOption {
type = types.nullOr types.attrs;
default = null;
+79 -1
View File
@@ -609,8 +609,86 @@ let
(mkAssertion (
!proxyInboundsEnabled || l.hysteria.masquerade == null || l.type == "hysteria2"
) "${prefix}: hysteria.masquerade is for hysteria2 listeners only")
(mkAssertion (
!proxyInboundsEnabled
|| l.fallbacks == [ ]
|| (
builtins.elem l.type [
"vless"
"trojan"
]
&& l.transport.type == "raw"
)
) "${prefix}: fallbacks run only on a vless or trojan listener with transport.type = \"raw\"")
]
) proxyInbounds;
++ map (
fb:
exactlyOneOf proxyInboundsEnabled [
fb.dest
fb.listener
] "${prefix}: fallbacks each need exactly one of dest or listener"
) l.fallbacks
# The target gets the connection decrypted and in PROXY protocol, which only
# the front may send it, and its share links take the front's security.
++ lib.concatMap (
fb:
let
target = lib.findFirst (other: other.tag == fb.listener) null proxyInbounds;
t = target.listener;
in
[
(mkAssertion
(
!proxyInboundsEnabled
|| (
target != null
&& target.tag != ib.tag
&& t.type == "vless"
&& !t.tls.enable
&& !t.reality.enable
&& (lib.hasPrefix "127." t.address || t.address == "::1")
)
)
"${prefix}: fallback listener '${fb.listener}' must be another vless listener, without tls or reality, on a loopback address"
)
# XRay matches path on HTTP/1.1 only; xhttp and grpc clients speak h2 under TLS.
(mkAssertion
(
!proxyInboundsEnabled
|| fb.path == null
|| target == null
|| (
builtins.elem t.transport.type [
"ws"
"httpupgrade"
]
&& fb.path == t.transport.path
)
)
"${prefix}: fallback path matches only a ws or httpupgrade listener '${fb.listener}', and must be its transport.path"
)
# REALITY clients run raw, xhttp and grpc only, and raw would be the front's own protocol.
(mkAssertion
(
!proxyInboundsEnabled
|| !l.reality.enable
|| target == null
|| builtins.elem t.transport.type [
"xhttp"
"grpc"
]
)
"${prefix}: a REALITY listener's fallback listener '${fb.listener}' must use the xhttp or grpc transport"
)
]
) (builtins.filter (fb: fb.listener != null) l.fallbacks)
) proxyInbounds
# Its share links can carry only one front's port and security.
++ [
(uniqueValues proxyInboundsEnabled (lib.concatMap (
ib: builtins.filter (tag: tag != null) (map (fb: fb.listener) ib.listener.fallbacks)
) proxyInbounds) "proxy-suite: a listener can be the fallback listener of only one other")
];
# AmneziaWG listeners run an interface each and give their clients addresses in a subnet.
awgListeners = builtins.filter (ib: ib.listener.type == "amneziawg") proxyInbounds;