Some checks failed
Windows project source guards / test (push) Has been cancelled
Owner request 2026-11-26 ("so everything works with Hysteria, AWG and the rest"). The helpers that dial with
their own UDP socket talk to a loopback `direct` UDP inbound of sing-box (override address = the real server) whose
rule leads into the WARP hop: the AmneziaWG helper in both modes (peer 127.0.0.1:relay, new `unbound` mode without the
physical-interface binding, IPv4 tunnel with an inner MTU of 1250, started after the front), the main Hysteria2
helper in the proxy mode (server name kept in the TLS SNI). Slot helpers stay TUN-only. Verified on win10 with the
real components: the real AmneziaWG server brings a real page through the hop in the proxy mode and under the TUN;
Go tests of the helper pass as a Windows exe. Hysteria2 through the relay is unit-tested on real nodes but not live:
the test person's Hysteria2 servers do not answer from the VM with or without the hop.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
281 lines
10 KiB
Python
281 lines
10 KiB
Python
from __future__ import annotations
|
||
|
||
from copy import deepcopy
|
||
from typing import Any, Iterable
|
||
from urllib.parse import parse_qsl, quote, urlsplit, urlunsplit
|
||
|
||
|
||
_TRUE_VALUES = frozenset({"1", "true", "yes", "on", "t"})
|
||
_FALSE_VALUES = frozenset({"0", "false", "no", "off", "f"})
|
||
|
||
|
||
def build_uri_client_config(
|
||
raw_uri: str,
|
||
outbound: dict[str, Any],
|
||
*,
|
||
relay_host: str,
|
||
relay_port: int,
|
||
relay_username: str,
|
||
relay_password: str,
|
||
) -> dict[str, Any]:
|
||
"""Build an ephemeral official-client config without changing saved URI state.
|
||
|
||
The application historically accepted a few Clash/sing-box aliases which
|
||
the official Hysteria URI parser intentionally does not understand. Keep
|
||
the original URI as the source of truth, then express those already parsed
|
||
semantics through official full-config fields for this process only.
|
||
"""
|
||
|
||
pairs = tuple(parse_qsl(urlsplit(raw_uri).query, keep_blank_values=True))
|
||
server_uri = _append_official_query_aliases(raw_uri, pairs)
|
||
server_uri = _runtime_server_uri(server_uri, outbound, pairs)
|
||
config: dict[str, Any] = {
|
||
"server": server_uri,
|
||
"lazy": True,
|
||
# Match the official-core configuration used by the Android client.
|
||
# TLS verification, pinning and the profile obfuscation remain intact.
|
||
"quic": {"disableChromeParrot": True},
|
||
"socks5": {
|
||
"listen": f"{relay_host}:{relay_port}",
|
||
"username": relay_username,
|
||
"password": relay_password,
|
||
"disableUDP": False,
|
||
},
|
||
}
|
||
|
||
tls_outbound = outbound.get("tls")
|
||
tls_source = tls_outbound if isinstance(tls_outbound, dict) else {}
|
||
tls: dict[str, Any] = {}
|
||
sni = _query_value(pairs, "sni", "peer") or str(tls_source.get("server_name") or "")
|
||
if sni:
|
||
tls["sni"] = sni
|
||
|
||
insecure_text = _query_value(
|
||
pairs,
|
||
"insecure",
|
||
"skip-cert-verify",
|
||
"skip_cert_verify",
|
||
"allow_insecure",
|
||
"allowInsecure",
|
||
)
|
||
insecure = _parse_bool(insecure_text)
|
||
if insecure is not None:
|
||
tls["insecure"] = insecure
|
||
elif tls_source.get("insecure"):
|
||
tls["insecure"] = True
|
||
|
||
pin = _query_value(pairs, "pinSHA256", "pin_sha256", "pinsha256")
|
||
if pin:
|
||
tls["pinSHA256"] = pin
|
||
ech = _query_value(pairs, "ech")
|
||
if ech:
|
||
tls["ech"] = ech
|
||
if tls:
|
||
config["tls"] = tls
|
||
|
||
obfs_type = _query_value(pairs, "obfs").strip().lower()
|
||
if obfs_type:
|
||
obfs: dict[str, Any] = {"type": obfs_type}
|
||
if obfs_type in {"salamander", "gecko"}:
|
||
password = _query_value(pairs, "obfs-password", "obfs_password")
|
||
obfs[obfs_type] = {"password": password}
|
||
config["obfs"] = obfs
|
||
|
||
hop_interval = _query_value(pairs, "hopInterval", "hop_interval")
|
||
if hop_interval:
|
||
config["transport"] = {
|
||
"type": "udp",
|
||
"udp": {"hopInterval": hop_interval},
|
||
}
|
||
|
||
bandwidth: dict[str, str] = {}
|
||
for source_key, target_key in (("up_mbps", "up"), ("down_mbps", "down")):
|
||
value = outbound.get(source_key)
|
||
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
||
continue
|
||
bandwidth[target_key] = f"{max(0, int(value))} mbps"
|
||
if bandwidth:
|
||
config["bandwidth"] = bandwidth
|
||
return config
|
||
|
||
|
||
def _append_official_query_aliases(
|
||
raw_uri: str,
|
||
pairs: Iterable[tuple[str, str]],
|
||
) -> str:
|
||
items = tuple(pairs)
|
||
additions: list[tuple[str, str]] = []
|
||
mappings = (
|
||
("sni", ("peer",)),
|
||
("insecure", ("skip-cert-verify", "skip_cert_verify", "allow_insecure", "allowInsecure")),
|
||
("obfs", ()),
|
||
("obfs-password", ("obfs_password",)),
|
||
("pinSHA256", ("pin_sha256", "pinsha256")),
|
||
("ech", ()),
|
||
)
|
||
for canonical, aliases in mappings:
|
||
if any(key == canonical for key, _value in items):
|
||
continue
|
||
value = _query_value(items, canonical, *aliases)
|
||
if not value:
|
||
continue
|
||
if canonical == "insecure":
|
||
parsed = _parse_bool(value)
|
||
if parsed is not None:
|
||
value = "true" if parsed else "false"
|
||
additions.append((canonical, value))
|
||
if not additions:
|
||
return raw_uri
|
||
|
||
parsed_uri = urlsplit(raw_uri)
|
||
suffix = "&".join(f"{key}={quote(value, safe='')}" for key, value in additions)
|
||
query = f"{parsed_uri.query}&{suffix}" if parsed_uri.query else suffix
|
||
return parsed_uri._replace(query=query).geturl()
|
||
|
||
|
||
def _query_value(
|
||
pairs: Iterable[tuple[str, str]],
|
||
canonical: str,
|
||
*aliases: str,
|
||
) -> str:
|
||
items = tuple(pairs)
|
||
empty_value: str | None = None
|
||
for key, value in items:
|
||
if key == canonical:
|
||
if value:
|
||
return value
|
||
empty_value = value
|
||
canonical_folded = canonical.casefold()
|
||
for key, value in items:
|
||
if key.casefold() == canonical_folded:
|
||
if value:
|
||
return value
|
||
if empty_value is None:
|
||
empty_value = value
|
||
for alias in aliases:
|
||
for key, value in items:
|
||
if key == alias:
|
||
if value:
|
||
return value
|
||
if empty_value is None:
|
||
empty_value = value
|
||
alias_folded = alias.casefold()
|
||
for key, value in items:
|
||
if key.casefold() == alias_folded:
|
||
if value:
|
||
return value
|
||
if empty_value is None:
|
||
empty_value = value
|
||
return empty_value or ""
|
||
|
||
|
||
def _parse_bool(value: str) -> bool | None:
|
||
normalized = str(value or "").strip().lower()
|
||
if normalized in _TRUE_VALUES:
|
||
return True
|
||
if normalized in _FALSE_VALUES:
|
||
return False
|
||
return None
|
||
|
||
|
||
def _runtime_server_uri(
|
||
raw_uri: str,
|
||
outbound: dict[str, Any],
|
||
pairs: Iterable[tuple[str, str]],
|
||
) -> str:
|
||
# Query-based mport/ports is a legacy alias. The official URI grammar
|
||
# expects the port union in the authority, so rewrite only the ephemeral
|
||
# process value while leaving Node.link and its fingerprint untouched.
|
||
if not _query_value(pairs, "mport", "ports"):
|
||
return raw_uri
|
||
|
||
server_ports = outbound.get("server_ports")
|
||
if isinstance(server_ports, (list, tuple)) and server_ports:
|
||
ports = [str(item).replace(":", "-") for item in server_ports]
|
||
else:
|
||
port = int(outbound.get("server_port") or 0)
|
||
if port <= 0:
|
||
return raw_uri
|
||
ports = [str(port)]
|
||
|
||
parsed = urlsplit(raw_uri)
|
||
host = str(parsed.hostname or "")
|
||
if not host:
|
||
return raw_uri
|
||
if ":" in host and not host.startswith("["):
|
||
host = f"[{host}]"
|
||
userinfo = parsed.netloc.rsplit("@", 1)[0] + "@" if "@" in parsed.netloc else ""
|
||
netloc = f"{userinfo}{host}:{','.join(ports)}"
|
||
return parsed._replace(netloc=netloc).geturl()
|
||
|
||
|
||
def replace_server_host(uri: str, address: str) -> str:
|
||
"""``hy2://auth@name:443,5000-6000/?…`` → тот же URI с адресом вместо имени."""
|
||
|
||
parts = urlsplit(uri)
|
||
userinfo, at, hostport = parts.netloc.rpartition("@")
|
||
if hostport.startswith("["):
|
||
rest = hostport[hostport.index("]") + 1:]
|
||
else:
|
||
_name, colon, ports = hostport.partition(":")
|
||
rest = colon + ports
|
||
literal = f"[{address}]" if ":" in address else address
|
||
return urlunsplit((parts.scheme, f"{userinfo}{at}{literal}{rest}", parts.path, parts.query, parts.fragment))
|
||
|
||
|
||
def server_host(config: dict[str, Any]) -> str:
|
||
"""Имя сервера из ``server`` конфига официального клиента."""
|
||
|
||
return str(urlsplit(str(config.get("server") or "")).hostname or "")
|
||
|
||
|
||
def pin_server_address(config: dict[str, Any], address: str) -> dict[str, Any]:
|
||
"""Копия конфига: соединение — на ``address``, имя сервера остаётся в SNI.
|
||
|
||
Официальный клиент сам разрешает имя сервера системным DNS, то есть DNS
|
||
провайдера: подменённый ответ («no such host» или чужой адрес) роняет
|
||
подключение к живому серверу. Адрес из доверенного DoH приложения
|
||
подставляется в URI, а TLS по-прежнему проверяет сертификат по имени.
|
||
"""
|
||
|
||
host = server_host(config)
|
||
pinned = deepcopy(config)
|
||
if not host:
|
||
return pinned
|
||
pinned.setdefault("tls", {}).setdefault("sni", host)
|
||
pinned["server"] = replace_server_host(str(config["server"]), address)
|
||
return pinned
|
||
|
||
|
||
def server_endpoint(config: dict[str, Any]) -> tuple[str, int] | None:
|
||
"""The server's name or address and its FIRST port (a port-hopping list keeps only its first port)."""
|
||
|
||
parts = urlsplit(str(config.get("server") or ""))
|
||
host = parts.hostname or ""
|
||
if not host:
|
||
return None
|
||
hostport = parts.netloc.rpartition("@")[2]
|
||
ports = hostport[hostport.index("]") + 1:] if hostport.startswith("[") else hostport.partition(":")[1] + hostport.partition(":")[2]
|
||
first = ports.lstrip(":").replace("-", ",").split(",")[0].strip() if ports else ""
|
||
try:
|
||
return host, int(first) if first else 443
|
||
except ValueError:
|
||
return None
|
||
|
||
|
||
def relay_server_config(config: dict[str, Any], relay_host: str, relay_port: int) -> dict[str, Any]:
|
||
"""Copy of the config whose server is a local UDP relay; the server's name stays in the TLS SNI.
|
||
|
||
Used when the connection enters through the WARP hop in the proxy mode: nothing captures the client's own UDP socket
|
||
there, so it talks to a loopback `direct` inbound of sing-box, which carries the datagrams to the real server through
|
||
the hop. Certificate checks and pinning are unchanged (the SNI is the real name)."""
|
||
|
||
host = server_host(config)
|
||
relayed = deepcopy(config)
|
||
if not host:
|
||
return relayed
|
||
relayed.setdefault("tls", {}).setdefault("sni", host)
|
||
parts = urlsplit(str(config["server"]))
|
||
userinfo, at, _hostport = parts.netloc.rpartition("@")
|
||
relayed["server"] = urlunsplit((parts.scheme, f"{userinfo}{at}{relay_host}:{relay_port}", parts.path, parts.query, parts.fragment))
|
||
return relayed
|