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

+15 -4
View File
@@ -125,7 +125,10 @@ def _xray_stream(listener: dict, tag: str) -> dict:
# Behind a web server the socket address is loopback, which XRay will not record as
# online; the forwarded address is only trusted when one of these headers is present.
if transport.get("trustedXForwardedFor"):
stream["sockopt"] = {"trustedXForwardedFor": transport["trustedXForwardedFor"]}
stream.setdefault("sockopt", {})["trustedXForwardedFor"] = transport["trustedXForwardedFor"]
# Behind another listener's fallback, the client address arrives in PROXY protocol.
if listener.get("front"):
stream.setdefault("sockopt", {})["acceptProxyProtocol"] = True
reality = listener["reality"]
tls = listener["tls"]
@@ -193,6 +196,12 @@ def render_xray_inbound(listener: dict) -> dict:
else:
raise ValueError(f"listener '{tag}': unsupported type '{listener_type}'")
if listener.get("fallbacks"):
settings["fallbacks"] = [
{key: value for key, value in fallback.items() if value is not None}
for fallback in listener["fallbacks"]
]
return {
"tag": tag,
"listen": listener["listen"],
@@ -310,7 +319,9 @@ def build_share_link(
fragment = urllib.parse.quote(label, safe="")
endpoint_address = onion_address or server_address
host = f"[{endpoint_address}]" if ":" in endpoint_address else endpoint_address
endpoint = f"{host}:{_share_port(listener)}"
# Behind another listener's fallback, clients dial that one, under its TLS or REALITY.
front = listener.get("front") or listener
endpoint = f"{host}:{_share_port(front)}"
if listener_type == "vmess":
return _vmess_link(listener, secret, server_address, label, endpoint_address)
@@ -342,7 +353,7 @@ def build_share_link(
return f"hysteria2://{urllib.parse.quote(secret, safe='')}@{endpoint}?{query}#{fragment}"
params = _link_transport_params(listener)
params.update(_link_security_params(listener, server_address))
params.update(_link_security_params(front, server_address))
if listener_type == "vless" and listener.get("flow"):
params["flow"] = listener["flow"]
query = urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
@@ -419,7 +430,7 @@ def build_listener(
return {
"tag": tag,
"type": listener["type"],
"port": _share_port(listener),
"port": _share_port(listener.get("front") or listener),
"inbound": inbound,
"links": links,
"onionLinks": onion_links,
+46
View File
@@ -163,6 +163,26 @@ class RenderInboundTests(unittest.TestCase):
{"trustedXForwardedFor": ["X-Real-IP"]},
)
def test_fallbacks_reach_settings_without_nulls(self):
self.assertNotIn("fallbacks", render_xray_inbound(listener())["settings"])
fallbacks = [
{"name": None, "alpn": None, "path": "/ws", "dest": "127.0.0.1:10002", "xver": 2},
{"name": None, "alpn": None, "path": None, "dest": 8080, "xver": 0},
]
self.assertEqual(
render_xray_inbound(listener(fallbacks=fallbacks))["settings"]["fallbacks"],
[{"path": "/ws", "dest": "127.0.0.1:10002", "xver": 2}, {"dest": 8080, "xver": 0}],
)
def test_fallback_target_accepts_proxy_protocol(self):
"""The front sends the client address in PROXY protocol; loopback alone records nobody online."""
transport = {"type": "ws", "path": "/ws", "host": None, "serviceName": "", "trustedXForwardedFor": ["X-Real-IP"]}
front = {"tag": "front-in", "type": "vless", "port": 443, "sharePort": None}
self.assertEqual(
render_xray_inbound(listener(transport=transport, front=front))["streamSettings"]["sockopt"],
{"trustedXForwardedFor": ["X-Real-IP"], "acceptProxyProtocol": True},
)
def test_grpc_transport(self):
ib = render_xray_inbound(
listener(transport={"type": "grpc", "path": "/", "host": None, "serviceName": "GunService"})
@@ -231,6 +251,32 @@ class ShareLinkTests(unittest.TestCase):
self.assertEqual(params["flow"], "xtls-rprx-vision")
self.assertEqual(params["type"], "tcp")
def test_fallback_target_link_carries_the_front(self):
"""Clients dial the front, under its TLS or REALITY, with the target's own transport."""
transport = {"type": "ws", "path": "/ws", "host": "cdn.example.com", "serviceName": "", "trustedXForwardedFor": []}
target = listener(port=10002, listen="127.0.0.1", transport=transport)
tls_front = {
"tag": "front-in",
"type": "trojan",
"port": 8443,
"sharePort": 443,
"tls": {"enable": False, "certificateFile": "/c", "keyFile": "/k", "serverName": "vpn.example.com"},
"reality": listener()["reality"],
}
link = build_share_link(dict(target, front=tls_front), "1.2.3.4")
self.assertTrue(link.startswith("vless://uuid-1@1.2.3.4:443?"))
params = link_params(link)
self.assertEqual(params["security"], "tls")
self.assertEqual(params["sni"], "vpn.example.com")
self.assertEqual((params["type"], params["path"], params["host"]), ("ws", "/ws", "cdn.example.com"))
reality_front = dict(tls_front, type="vless", port=443, sharePort=None, reality=reality())
params = link_params(build_share_link(dict(target, front=reality_front), "1.2.3.4"))
self.assertEqual(
(params["security"], params["pbk"], params["sni"], params["sid"]),
("reality", "public-key", "www.microsoft.com", "0123abcd"),
)
def test_reality_link_without_public_key_fails(self):
with self.assertRaisesRegex(ValueError, "reality.publicKey is required"):
build_share_link(listener(reality=reality(publicKey=None)), "vpn.example.com")