86 lines
3.0 KiB
Python
86 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
||
"""Build /etc/brov-ops/proxy/xray.json from a nodes file of vless:// links.
|
||
|
||
Usage: gen-xray.py <nodes.txt> <out xray.json>
|
||
|
||
The nodes file holds one vless:// link per line (comments and blanks skipped).
|
||
Subscription contents are accepted too: base64 blob that decodes to vless:// lines.
|
||
Only REALITY+tcp VLESS nodes are used (that is what brov-ops' proxy runs on).
|
||
The first node becomes the outbound; extra nodes go into a round-robin balancer.
|
||
"""
|
||
import base64
|
||
import json
|
||
import sys
|
||
from urllib.parse import parse_qs, urlparse
|
||
|
||
|
||
def links(path: str) -> list[str]:
|
||
raw = open(path, encoding="utf-8").read().strip()
|
||
if "vless://" not in raw: # maybe a base64 subscription blob
|
||
try:
|
||
raw = base64.b64decode(raw).decode("utf-8")
|
||
except Exception:
|
||
pass
|
||
return [ln.strip() for ln in raw.splitlines() if ln.strip().startswith("vless://")]
|
||
|
||
|
||
def outbound(link: str, tag: str) -> dict:
|
||
u = urlparse(link)
|
||
q = {k: v[0] for k, v in parse_qs(u.query).items()}
|
||
if q.get("security") != "reality" or q.get("type", "tcp") != "tcp":
|
||
raise ValueError(f"пропускаю не-REALITY узел: {link[:40]}…")
|
||
return {
|
||
"tag": tag,
|
||
"protocol": "vless",
|
||
"settings": {"vnext": [{
|
||
"address": u.hostname,
|
||
"port": u.port or 443,
|
||
"users": [{"id": u.username, "encryption": q.get("encryption", "none"),
|
||
**({"flow": q["flow"]} if q.get("flow") else {})}],
|
||
}]},
|
||
"streamSettings": {
|
||
"network": "tcp",
|
||
"security": "reality",
|
||
"realitySettings": {
|
||
"serverName": q.get("sni", ""),
|
||
"fingerprint": q.get("fp", "chrome"),
|
||
"publicKey": q["pbk"],
|
||
"shortId": q.get("sid", ""),
|
||
},
|
||
},
|
||
}
|
||
|
||
|
||
def main() -> None:
|
||
nodes_path, out_path = sys.argv[1], sys.argv[2]
|
||
found = links(nodes_path)
|
||
if not found:
|
||
raise SystemExit("✗ в файле узлов нет ни одной vless:// ссылки")
|
||
outbounds, tags = [], []
|
||
for i, link in enumerate(found):
|
||
try:
|
||
ob = outbound(link, f"node{i}")
|
||
except (ValueError, KeyError) as e:
|
||
print(f"⚠ {e}", file=sys.stderr)
|
||
continue
|
||
outbounds.append(ob)
|
||
tags.append(ob["tag"])
|
||
if not outbounds:
|
||
raise SystemExit("✗ ни одного рабочего REALITY-узла")
|
||
cfg = {
|
||
"log": {"loglevel": "warning"},
|
||
"inbounds": [{"listen": "127.0.0.1", "port": 7890, "protocol": "http",
|
||
"settings": {"timeout": 0}}],
|
||
"outbounds": outbounds,
|
||
}
|
||
if len(tags) > 1:
|
||
cfg["routing"] = {"balancers": [{"tag": "out", "selector": tags}]}
|
||
with open(out_path, "w", encoding="utf-8") as f:
|
||
json.dump(cfg, f, indent=1, ensure_ascii=False)
|
||
f.write("\n")
|
||
print(f"✓ xray.json: {len(tags)} узл(а)")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|