1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| import requests import yaml import json import os
YAML_URL = "https://example.com/clash.yaml" OUTPUT_JSON = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json")
OUTPUT_NAME = os.path.join(os.path.dirname(os.path.abspath(__file__)), "proxy.csv")
START_PORT = 20000
resp = requests.get(YAML_URL, timeout=15) resp.raise_for_status() yaml_data = yaml.safe_load(resp.text)
proxies = yaml_data.get("proxies", []) if not proxies: raise RuntimeError("YAML中未找到proxies")
inbounds = [] outbounds = [] routes = [] tmp = ""
for idx, p in enumerate(proxies): port = START_PORT + idx in_tag = f"in_{idx}" out_tag = p["name"]
inbounds.append({ "type": "mixed", "tag": in_tag, "listen": "0.0.0.0", "listen_port": port })
ptype = p.get("type")
if ptype == "vmess": outbound = { "type": "vmess", "tag": out_tag, "server": p["server"], "server_port": int(p["port"]), "uuid": p["uuid"], "security": "auto", "alter_id": 0, "transport": { "type": "ws", "path": "/ws", "headers": { "Host": "example.com" } } }
elif ptype == "vless": outbound = { "type": "vless", "tag": out_tag, "server": p["server"], "server_port": int(p["port"]), "uuid": p["uuid"], "security": "auto", "alter_id": 0, "transport": { "type": "ws", "path": "/ws", "headers": { "Host": "example.com" } } }
elif ptype == "trojan": outbound = { "type": "trojan", "tag": out_tag, "server": p["server"], "server_port": int(p["port"]), "password": p["password"], "security": "auto" }
elif ptype == "ss": outbound = { "type": "shadowsocks", "tag": out_tag, "server": p["server"], "server_port": int(p["port"]), "method": p["cipher"], "password": p["password"] }
else: continue
tmp += f"{in_tag},{port},{out_tag}".strip() + "\n"
outbounds.append(outbound)
routes.append({ "inbound": in_tag, "outbound": out_tag })
config = { "log": { "level": "info" }, "inbounds": inbounds, "outbounds": outbounds, "route": { "rules": routes } }
with open(OUTPUT_JSON, "w", encoding="utf-8") as f: json.dump(config, f, ensure_ascii=False, indent=2)
with open(OUTPUT_NAME, "w", encoding="utf-8") as f: f.write(tmp)
print(tmp) print(f"端口范围:{START_PORT} – {START_PORT + len(outbounds) - 1}")
|