feat: hysteria2 x-forward

This commit is contained in:
fufsob committed 2026-10-11 16:27:16 +00:00
1 parent cae589626a
commit 7d96e7c6f8
9 files changed
+54 -6

No files matched your search

+14
View File
@@ -68,6 +68,7 @@ Part of the [proxy-suite options reference](./index.md).
- [enable](#services-proxy-suite-inbounds-listeners-name-hysteria-salamander-enable)
- [password](#services-proxy-suite-inbounds-listeners-name-hysteria-salamander-password)
- [passwordFile](#services-proxy-suite-inbounds-listeners-name-hysteria-salamander-passwordfile)
- [xForwarded](#services-proxy-suite-inbounds-listeners-name-hysteria-xforwarded)
- [jsonFile](#services-proxy-suite-inbounds-listeners-name-jsonfile)
- [method](#services-proxy-suite-inbounds-listeners-name-method)
- [order](#services-proxy-suite-inbounds-listeners-name-order)
@@ -699,6 +700,19 @@ generated on first start and kept in the state directory\.
**Default:** `null`\
**Example:** `"/run/secrets/proxy-inbound-salamander"`
<a id="services-proxy-suite-inbounds-listeners-name-hysteria-xforwarded"></a>
## services\.proxy-suite\.inbounds\.listeners\.\<name>\.hysteria\.xForwarded
Tell ` masquerade ` the client’s address in ` X-Forwarded-For ` (with
` X-Forwarded-Host ` and ` X-Forwarded-Proto `), for a ` masquerade ` that is this
host’s own web server: what it shows or limits is then the client’s, not this
host’s\. The header is XRay’s alone (any the client sent is dropped), so trust it
only from the address XRay connects from\. Another operator’s site would learn
every probe’s address\.
**Type:** boolean\
**Default:** `false`
<a id="services-proxy-suite-inbounds-listeners-name-jsonfile"></a>
## services\.proxy-suite\.inbounds\.listeners\.\<name>\.jsonFile
@@ -604,6 +604,19 @@ let
'';
};
hysteria.xForwarded = mkOption {
type = types.bool;
default = false;
description = ''
Tell `masquerade` the client's address in `X-Forwarded-For` (with
`X-Forwarded-Host` and `X-Forwarded-Proto`), for a `masquerade` that is this
host's own web server: what it shows or limits is then the client's, not this
host's. The header is XRay's alone (any the client sent is dropped), so trust it
only from the address XRay connects from. Another operator's site would learn
every probe's address.
'';
};
hysteria.salamander = {
enable = mkOption {
type = types.bool;
@@ -859,6 +859,9 @@ let
(mkAssertion (
!proxyInboundsEnabled || l.hysteria.rewriteHost || l.hysteria.masquerade != null
) "${prefix}: hysteria.rewriteHost = false needs hysteria.masquerade")
(mkAssertion (
!proxyInboundsEnabled || !l.hysteria.xForwarded || l.hysteria.masquerade != null
) "${prefix}: hysteria.xForwarded needs hysteria.masquerade")
(mkAssertion (
!proxyInboundsEnabled
|| (!l.hysteria.salamander.enable && l.hysteria.portHopping == null)
+3
View File
@@ -1384,6 +1384,9 @@ let
== "https://www.example.com"
))
(ok (lib.head (builtins.filter (l: l.tag == "hy2-in") firewallSpec.listeners)).hysteria.rewriteHost)
(ok (
!(lib.head (builtins.filter (l: l.tag == "hy2-in") firewallSpec.listeners)).hysteria.xForwarded
))
(ok (noFirewallFixture.config.networking.firewall.allowedTCPPorts == [ ]))
# The unit exists, and only waits on the client stack when it relays.
+1
View File
@@ -239,6 +239,7 @@ INBOUND_ADD_FLAGS = {
"--order": "position in links and subscriptions",
"--masquerade": "hysteria2: site shown to anything else",
"--keep-host": "hysteria2: pass --masquerade the name asked for (this host's own web server)",
"--x-forwarded": "hysteria2: tell --masquerade the client's address (this host's own web server)",
"--salamander": "hysteria2: Salamander obfuscation",
"--user": "a user it accepts",
}
+6
View File
@@ -344,6 +344,7 @@ LISTENER_SCHEMA = {
"hysteria": {
"masquerade": _nullable(_nonspace),
"rewriteHost": _is_bool,
"xForwarded": _is_bool,
"salamander": {
"enable": _is_bool,
"password": _nullable(_nonspace),
@@ -669,6 +670,8 @@ def listener_problems(listener: dict, runtime: dict, share_links: bool, shared:
"hysteria.masquerade is for hysteria2 listeners only")
need(hysteria.get("rewriteHost", True) or hysteria.get("masquerade") is not None,
"hysteria.rewriteHost = false needs hysteria.masquerade")
need(not hysteria.get("xForwarded", False) or hysteria.get("masquerade") is not None,
"hysteria.xForwarded needs hysteria.masquerade")
# XRay dials both for anyone, past routing: not this host or its LAN (a local decoy is
# declared). Names are not resolved, so port 443 keeps one that resolves inside to TLS.
if reality["enable"]:
@@ -1303,6 +1306,8 @@ def listener_from_flags(kind: str, args: argparse.Namespace) -> dict:
hysteria["masquerade"] = args.masquerade
if args.keep_host:
hysteria["rewriteHost"] = False
if args.x_forwarded:
hysteria["xForwarded"] = True
if args.salamander:
hysteria["salamander"] = {"enable": True}
if hysteria:
@@ -1464,6 +1469,7 @@ def main(argv: list[str] | None = None) -> int:
("--masquerade", str)):
add.add_argument(flag, type=kind)
add.add_argument("--keep-host", action="store_true")
add.add_argument("--x-forwarded", action="store_true")
add.add_argument("--salamander", action="store_true")
add.add_argument("--user", action="append", default=[])
+2
View File
@@ -132,6 +132,8 @@ def _xray_stream(listener: dict, tag: str) -> dict:
masquerade = hysteria.get("masquerade")
if masquerade:
settings["masquerade"] = {"type": "proxy", "url": masquerade, "rewriteHost": hysteria.get("rewriteHost", True)}
if hysteria.get("xForwarded"):
settings["masquerade"]["xForwarded"] = True
stream = {
"network": "hysteria",
"hysteriaSettings": settings,
+4 -2
View File
@@ -468,8 +468,10 @@ class RoundTripTests(unittest.TestCase):
self.assertEqual(stream["hysteriaSettings"]["masquerade"],
{"type": "proxy", "url": "https://www.example.com", "rewriteHost": True})
# This host's own web server: the name the client asked for goes on to it.
own = render_xray_inbound(dict(spec, hysteria={"masquerade": "https://www.example.com", "rewriteHost": False}))
self.assertFalse(own["streamSettings"]["hysteriaSettings"]["masquerade"]["rewriteHost"])
own = render_xray_inbound(dict(spec, hysteria={"masquerade": "https://www.example.com", "rewriteHost": False,
"xForwarded": True}))
self.assertEqual(own["streamSettings"]["hysteriaSettings"]["masquerade"],
{"type": "proxy", "url": "https://www.example.com", "rewriteHost": False, "xForwarded": True})
link = build_share_link(spec, "vpn.example.com")
self.assertTrue(link.startswith("hysteria2://p%40ss%2Fword@vpn.example.com:8443?"), link)
self.assertEqual(link_params(link)["alpn"], "h3")
+8 -4
View File
@@ -19,7 +19,7 @@ LISTENER_DEFAULTS = {
"address": "::",
"fallbacks": [],
"flow": None,
"hysteria": {"masquerade": None, "portHopping": None, "rewriteHost": True,
"hysteria": {"masquerade": None, "portHopping": None, "rewriteHost": True, "xForwarded": False,
"salamander": {"enable": False, "password": None, "passwordFile": None}},
"method": "2022-blake3-aes-128-gcm",
"order": 1000,
@@ -279,7 +279,7 @@ class RuntimeTest(unittest.TestCase):
port=20001, listen=None, via=None, share_port=None, share_address=None, fingerprint="firefox", order=None, method=None,
flow="vision", transport=None, path=None, host=None, mode=None, service_name=None, tls=None, alpn=None,
sni=None, reality="www.example.com,example.com", reality_dest=None, short_id=None, masquerade=None,
keep_host=False, salamander=False, user=["bob"],
keep_host=False, x_forwarded=False, salamander=False, user=["bob"],
)
entry = rt.listener_from_flags("vless", args)
self.assertEqual(entry["reality"]["dest"], "www.example.com:443")
@@ -349,6 +349,8 @@ class RuntimeTest(unittest.TestCase):
# Nothing to pass the name to: XRay answers 404.
({"type": "hysteria2", "port": 20001, "tls": {"certificate": "main"},
"hysteria": {"rewriteHost": False}}, "rewriteHost = false needs hysteria.masquerade"),
({"type": "hysteria2", "port": 20001, "tls": {"certificate": "main"},
"hysteria": {"xForwarded": True}}, "xForwarded needs hysteria.masquerade"),
({"type": "hysteria2", "port": 20001, "tls": {"certificate": "main"},
"hysteria": {"masquerade": "https://www.example.com", "rewriteHost": "no"}}, "rewriteHost: invalid value"),
# Values XRay refuses, with the declared listeners along with them.
@@ -564,10 +566,12 @@ class RuntimeTest(unittest.TestCase):
self.assertTrue(json.loads(out)["hysteria"]["salamander"]["enable"])
self.assertNotIn("rewriteHost", json.loads(out)["hysteria"])
status, out, _ = self.run_main("add", "hy-own", "hysteria2", "--port", "20007", "--tls", "main",
"--masquerade", "https://www.example.com", "--keep-host", "--user", "alice")
"--masquerade", "https://www.example.com", "--keep-host", "--x-forwarded",
"--user", "alice")
self.assertEqual(status, 0, out)
status, out, _ = self.run_main("show", "hy-own")
self.assertEqual(json.loads(out)["hysteria"], {"masquerade": "https://www.example.com", "rewriteHost": False})
self.assertEqual(json.loads(out)["hysteria"],
{"masquerade": "https://www.example.com", "rewriteHost": False, "xForwarded": True})
def test_check_names_the_awg_unit_when_its_peers_change(self):
users = os.path.join(self.tmp.name, "users.json")