Files
proxy-suite-flake/scripts/proxy_parsing.py
T

370 lines
13 KiB
Python

#!/usr/bin/env python3
import base64
import re
import sys
import urllib.parse
import urllib.request
from proxy_url_parsers import BACKEND_AWARE_PARSERS, PARSERS
def detect_scheme(url: str) -> str:
return url.split("://", 1)[0].lower()
def _parse_url(url: str, tag: str, backend: str) -> dict:
scheme = detect_scheme(url)
if scheme not in PARSERS:
raise ValueError(f"unsupported scheme '{scheme}'")
parser = PARSERS[scheme]
if scheme in BACKEND_AWARE_PARSERS:
return parser(url, tag, backend)
return parser(url, tag)
def _xray_sockopt(routing_mark: int | None) -> dict:
if routing_mark is None:
return {}
return {"streamSettings": {"sockopt": {"mark": routing_mark}}}
def _xray_tls(tls: dict | None) -> tuple[str, dict]:
if not tls or not tls.get("enabled"):
return "none", {}
if tls.get("reality", {}).get("enabled"):
reality = tls["reality"]
settings = {
"serverName": tls.get("server_name", ""),
"publicKey": reality["public_key"],
"shortId": reality.get("short_id", ""),
}
utls = tls.get("utls", {})
if reality.get("spider_x"):
settings["spiderX"] = reality["spider_x"]
if utls.get("enabled") and utls.get("fingerprint"):
settings["fingerprint"] = utls["fingerprint"]
return "reality", {"realitySettings": settings}
settings = {"serverName": tls.get("server_name", "")}
if tls.get("alpn"):
settings["alpn"] = tls["alpn"]
if tls.get("insecure"):
raise ValueError(
"XRay no longer supports tlsSettings.allowInsecure; "
"remove insecure=1 or use a supported certificate verification option"
)
if tls.get("ech_config_list"):
settings["echConfigList"] = tls["ech_config_list"]
utls = tls.get("utls", {})
if utls.get("enabled") and utls.get("fingerprint"):
settings["fingerprint"] = utls["fingerprint"]
return "tls", {"tlsSettings": settings}
def _xray_transport(transport: dict | None) -> tuple[str, dict]:
if not transport:
return "raw", {}
t = transport["type"]
if t == "ws":
return "ws", {"wsSettings": {"path": transport.get("path", "/"), "headers": transport.get("headers", {})}}
if t == "grpc":
return "grpc", {"grpcSettings": {"serviceName": transport.get("service_name", "")}}
if t == "http":
return "http", {"httpSettings": {"host": transport.get("host", []), "path": transport.get("path", "/")}}
if t == "httpupgrade":
return "httpupgrade", {
"httpupgradeSettings": {
"host": transport.get("host", ""),
"path": transport.get("path", "/"),
}
}
if t == "quic":
return "quic", {}
if t == "xhttp":
settings = {"path": transport.get("path", "/")}
if transport.get("host"):
settings["host"] = transport["host"]
if transport.get("mode"):
settings["mode"] = transport["mode"]
if transport.get("extra") is not None:
settings["extra"] = transport["extra"]
return "xhttp", {"xhttpSettings": settings}
return t, {}
def render_xray_outbound(ob: dict, routing_mark: int | None = None) -> dict:
typ = ob["type"]
tag = ob["tag"]
def stream_settings() -> dict:
security, sec = _xray_tls(ob.get("tls"))
network, net = _xray_transport(ob.get("transport"))
stream = {"network": network, "security": security}
stream.update(sec)
stream.update(net)
stream.setdefault("sockopt", {})["domainStrategy"] = "UseIP"
if routing_mark is not None:
stream.setdefault("sockopt", {})["mark"] = routing_mark
return stream
if typ == "vless":
settings = {
"address": ob["server"],
"port": ob["server_port"],
"id": ob["uuid"],
"encryption": "none",
}
if ob.get("flow"):
settings["flow"] = ob["flow"]
return {"protocol": "vless", "tag": tag, "settings": settings, "streamSettings": stream_settings()}
if typ == "vmess":
settings = {
"address": ob["server"],
"port": ob["server_port"],
"id": ob["uuid"],
"security": ob.get("security", "auto"),
"alterId": ob.get("alter_id", 0),
}
return {"protocol": "vmess", "tag": tag, "settings": settings, "streamSettings": stream_settings()}
if typ == "trojan":
settings = {
"address": ob["server"],
"port": ob["server_port"],
"password": ob["password"],
}
return {"protocol": "trojan", "tag": tag, "settings": settings, "streamSettings": stream_settings()}
if typ == "shadowsocks":
settings = {
"address": ob["server"],
"port": ob["server_port"],
"method": ob["method"],
"password": ob["password"],
}
return {"protocol": "shadowsocks", "tag": tag, "settings": settings, **_xray_sockopt(routing_mark)}
if typ == "hysteria2":
stream = stream_settings()
stream["network"] = "hysteria"
stream["security"] = "tls"
stream["hysteriaSettings"] = {"version": 2}
settings = {
"version": 2,
"address": ob["server"],
"port": ob["server_port"],
}
if ob.get("password"):
stream["hysteriaSettings"]["auth"] = ob["password"]
# XRay wants udphop outermost, ahead of any other UDP mask.
if ob.get("server_ports"):
stream.setdefault("finalmask", {}).setdefault("udp", []).append(
{
"type": "udphop",
"settings": {
"mode": "intervalRemote",
"interval": "20-40",
"remotePorts": ",".join(r.replace(":", "-") for r in ob["server_ports"]),
},
}
)
if ob.get("obfs", {}).get("type") == "salamander":
stream.setdefault("finalmask", {}).setdefault("udp", []).append(
{
"type": "salamander",
"settings": {"password": ob["obfs"].get("password", "")},
}
)
return {"protocol": "hysteria", "tag": tag, "settings": settings, "streamSettings": stream}
if typ == "socks":
settings = {"address": ob["server"], "port": ob["server_port"]}
if ob.get("username"):
settings["user"] = ob["username"]
settings["pass"] = ob.get("password", "")
return {"protocol": "socks", "tag": tag, "settings": settings, **_xray_sockopt(routing_mark)}
if typ == "http":
settings = {"address": ob["server"], "port": ob["server_port"]}
if ob.get("username"):
settings["user"] = ob["username"]
settings["pass"] = ob.get("password", "")
outbound = {"protocol": "http", "tag": tag, "settings": settings}
if ob.get("tls"):
security, sec = _xray_tls(ob.get("tls"))
outbound["streamSettings"] = {"security": security, **sec}
if routing_mark is not None:
outbound["streamSettings"].setdefault("sockopt", {})["mark"] = routing_mark
elif routing_mark is not None:
outbound.update(_xray_sockopt(routing_mark))
return outbound
raise ValueError(f"unsupported XRay outbound type '{typ}'")
def build_outbound(
url: str, tag: str, routing_mark: int | None = None, backend: str = "sing-box"
) -> dict:
if backend not in {"sing-box", "xray"}:
raise ValueError(f"unsupported backend '{backend}'")
outbound = _parse_url(url, tag, backend)
if routing_mark is not None:
if backend == "xray":
return render_xray_outbound(outbound, routing_mark)
outbound["routing_mark"] = routing_mark
elif backend == "xray":
return render_xray_outbound(outbound)
return outbound
# A subscription is a list of share links; the biggest real ones are tens of KiB.
# Read runs as root at service start, so a server that never stops sending must not
# take the whole proxy down with it.
MAX_SUBSCRIPTION_BYTES = 8 * 1024 * 1024
def fetch_raw(url: str) -> bytes:
# urlopen also speaks file:, ftp: and data:. This runs as root over a URL that
# reaches it from a group-writable spool, so only the two a subscription is
# ever served over.
if urllib.parse.urlsplit(url).scheme not in ("http", "https"):
raise ValueError("subscription URL must be http:// or https://")
request = urllib.request.Request(
url,
headers={"User-Agent": "v2rayN/6.0"},
)
with urllib.request.urlopen(request, timeout=30) as response:
data = response.read(MAX_SUBSCRIPTION_BYTES + 1)
if len(data) > MAX_SUBSCRIPTION_BYTES:
raise ValueError(f"subscription is larger than {MAX_SUBSCRIPTION_BYTES} bytes")
return data
def decode_subscription(data: bytes) -> list[str]:
text = None
# Line-wrapped and URL-safe base64 both occur; the lenient decoder would drop
# "-" and "_" as junk instead of reading them as "+" and "/".
stripped = b"".join(data.split()).translate(bytes.maketrans(b"-_", b"+/"))
try:
pad = b"=" * (-len(stripped) % 4)
decoded = base64.b64decode(stripped + pad).decode("utf-8")
if any(f"{scheme}://" in decoded for scheme in PARSERS):
text = decoded
except Exception:
pass
if text is None:
text = data.decode("utf-8", errors="replace")
return [line.strip() for line in text.splitlines() if line.strip()]
def slugify_tag(remark: str) -> str:
# \w is Unicode-aware: non-Latin remarks (e.g. Cyrillic country names) keep
# their letters instead of collapsing to a bare "proxy" tag. Tags only ever
# reach JSON configs and the Clash API, both UTF-8 safe.
slug = re.sub(r"[^\w-]", "-", remark)
slug = re.sub(r"-{2,}", "-", slug).strip("-")
return slug[:60] or "proxy"
def make_tag(prefix: str, remark: str, index: int) -> str:
if remark:
return f"{prefix}-{slugify_tag(urllib.parse.unquote(remark))}"
return f"{prefix}-{index}"
def _remark_from_line(line: str) -> str:
return line.split("#", 1)[1] if "#" in line else ""
def _unique_tag(base_tag: str, seen_tags: set[str]) -> str:
if base_tag not in seen_tags:
return base_tag
suffix = 2
while f"{base_tag}-{suffix}" in seen_tags:
suffix += 1
return f"{base_tag}-{suffix}"
def _subscription_tag(line: str, tag_prefix: str, index: int, seen_tags: set[str]) -> str:
return _unique_tag(make_tag(tag_prefix, _remark_from_line(line), index), seen_tags)
def _walk_subscription(
lines: list[str],
tag_prefix: str,
routing_mark: int | None,
backends: list[str],
links: dict[str, str] | None,
keep,
) -> None:
"""Parses every supported line, trying each backend in turn, and hands the result to keep.
A line no backend accepts is reported with every backend's complaint and skipped.
"""
seen_tags: set[str] = set()
for index, line in enumerate(lines):
scheme = detect_scheme(line)
if scheme not in PARSERS:
continue
tag = _subscription_tag(line, tag_prefix, index, seen_tags)
errors = []
for backend in backends:
try:
outbound = build_outbound(line, tag, routing_mark, backend)
except Exception as exc:
errors.append(f"{backend}: {exc}" if len(backends) > 1 else str(exc))
continue
seen_tags.add(tag)
keep(backend, outbound)
if links is not None:
links[tag] = line
break
else:
print(
f"warning: skipping entry {index} ({scheme}): " + "; ".join(errors),
file=sys.stderr,
)
def parse_subscription(
lines: list[str],
tag_prefix: str,
routing_mark: int | None,
backend: str = "sing-box",
links: dict[str, str] | None = None,
) -> list[dict]:
"""links, when given, collects tag -> the line it came from, to share it back."""
outbounds: list[dict] = []
_walk_subscription(
lines, tag_prefix, routing_mark, [backend], links, lambda _, ob: outbounds.append(ob)
)
return outbounds
def parse_hybrid_subscription(
lines: list[str], tag_prefix: str, routing_mark: int | None = None, links: dict[str, str] | None = None
) -> dict[str, list[dict]]:
"""Each line goes to whichever backend can dial it, sing-box first."""
outbounds: dict[str, list[dict]] = {"singBox": [], "xray": []}
_walk_subscription(
lines,
tag_prefix,
routing_mark,
["sing-box", "xray"],
links,
lambda backend, ob: outbounds["singBox" if backend == "sing-box" else "xray"].append(ob),
)
return outbounds