Files
jiedian/panel/links.py
T
2026-06-27 23:52:17 +08:00

52 lines
1.7 KiB
Python

"""分享链接生成。"""
from __future__ import annotations
import os
from pathlib import Path
from urllib.parse import quote
from db import list_nodes
from nodes_util import hy2_port
def load_env() -> dict[str, str]:
root = Path(os.environ.get("JIEDIAN_ROOT", Path(__file__).resolve().parents[1]))
env: dict[str, str] = {}
for line in (root / ".env").read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, _, value = line.partition("=")
env[key.strip()] = value.strip()
return env
def _qs(value: str) -> str:
"""URL-encode query / userinfo values for share links."""
# quote() leaves unreserved chars like '.' unencoded; some clients (v2rayNG)
# misparsed sni=www.microsoft.com — force dots encoded for compatibility.
return quote(value, safe="").replace(".", "%2E")
def build_links(node: dict, env: dict | None = None) -> dict[str, str]:
env = env or load_env()
vps_ip = env["VPS_IP"]
domain = env["DOMAIN"]
reality_sni = env.get("REALITY_SERVER_NAME", "www.microsoft.com")
public_key = env["REALITY_PUBLIC_KEY"]
short_id = env["REALITY_SHORT_ID"]
name = quote(node["name"])
port = hy2_port(node, list_nodes())
vless = (
f"vless://{node['uuid']}@{vps_ip}:443"
f"?encryption=none&flow=xtls-rprx-vision&security=reality"
f"&sni={_qs(reality_sni)}&fp=chrome&pbk={_qs(public_key)}&sid={_qs(short_id)}"
f"&spx=%2F&type=tcp#{name}"
)
hy2 = (
f"hy2://{_qs(node['hy2_password'])}@{domain}:{port}"
f"?sni={_qs(domain)}#{name}-Hy2"
)
return {"vless": vless, "hy2": hy2}