File size: 20,894 Bytes
3cea5fb 5c805bb e75c28d 3cea5fb 6daa00c db7f8ff 5c805bb 3cea5fb 5c805bb 3cea5fb 5c805bb 3cea5fb 5c805bb 8a9820f b0cd379 4b912bd 63c0962 4b912bd 8a9820f 4b912bd 5c805bb 4b912bd 5c805bb 4b912bd 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb 8a9820f 5c805bb e75c28d 5c805bb 4b912bd 3cea5fb 5c805bb 3cea5fb 5c805bb 3cea5fb 5c805bb 3cea5fb 5c805bb 3cea5fb 584b65a 3cea5fb 5c805bb 3cea5fb 5c805bb 3cea5fb 5c805bb 3cea5fb 584b65a 3cea5fb 4b912bd 8a9820f 4b912bd 8a9820f 4b912bd 8a9820f 4b912bd e75c28d 051d1cc 5c805bb db7f8ff 051d1cc 5c805bb 051d1cc 5c805bb 051d1cc 3cea5fb 5c805bb 3cea5fb 584b65a 3cea5fb 70bfbb3 d6df7c5 70bfbb3 3cea5fb 15e46c6 3cea5fb | 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | import requests
import base64
import json
import yaml
from urllib.parse import urlencode, quote
from fastapi import FastAPI, Response
import uvicorn
app = FastAPI()
extra_sub_url = "https://zuodengfabu0000.zeroplm.sbs/x/0420"
# ── 工具函数 ────────────────────────────────────────────────
def get_ip_location(ip_address):
try:
api_url = f"http://ip-api.com/json/{ip_address}?lang=zh-CN"
response = requests.get(api_url, timeout=5)
if response.status_code == 200:
data = response.json()
if data.get('status') == 'success':
country = data.get('country', '')
city = data.get('city', '')
return f"{country} {city}".strip()
except:
pass
return "未知位置"
def fetch_config(path_type, index):
"""抓取 hysteria / hysteria2 配置"""
urls = [
f"https://www.gitlabip.xyz/Alvin9999/PAC/refs/heads/master/backup/img/1/2/ipp/{path_type}/{index}/config.json",
f"https://gitlab.com/free9999/ipupdate/-/raw/master/backup/img/1/2/ipp/{path_type}/{index}/config.json"
]
for url in urls:
try:
res = requests.get(url, timeout=5)
if res.status_code == 200:
return res.json()
except:
continue
return None
def fetch_xray_config(index):
"""抓取 xray 配置,路径格式:xray/{index}/config.json"""
urls = [
f"https://www.gitlabip.xyz/Alvin9999/PAC/refs/heads/master/backup/img/1/2/ipp/xray/{index}/config.json",
f"https://gitlab.com/free9999/ipupdate/-/raw/master/backup/img/1/2/ipp/xray/{index}/config.json"
]
for url in urls:
try:
res = requests.get(url, timeout=5)
if res.status_code == 200:
return res.json()
except:
continue
return None
# ── Xray 链接构建 ────────────────────────────────────────────
# 非代理协议/tag,直接跳过
_SKIP_PROTOCOLS = {"freedom", "blackhole", "dns", "loopback"}
_SKIP_TAGS = {"direct", "block", "dns-out"}
def _stream_params(stream):
"""从 streamSettings 提取通用传输层参数,返回 dict(已去空值)"""
network = stream.get("network", "tcp")
security = stream.get("security", "none")
params = {"type": network, "security": security}
if security == "tls":
ts = stream.get("tlsSettings", {})
sni = ts.get("serverName", "")
if sni:
params["sni"] = sni
fp = ts.get("fingerprint", "")
if fp:
params["fp"] = fp
alpn = ts.get("alpn", [])
if alpn:
params["alpn"] = ",".join(alpn)
elif security == "reality":
rs = stream.get("realitySettings", {})
params["pbk"] = rs.get("publicKey", "")
params["fp"] = rs.get("fingerprint", "chrome")
params["sni"] = rs.get("serverName", "")
params["sid"] = rs.get("shortId", "")
spx = rs.get("spiderX", "")
if spx:
params["spx"] = spx
if network == "ws":
ws = stream.get("wsSettings", {})
params["path"] = ws.get("path", "/")
host = ws.get("headers", {}).get("Host", "")
if host:
params["host"] = host
elif network in ("xhttp", "splithttp"):
xh = stream.get("xhttpSettings", stream.get("splithttpSettings", {}))
params["path"] = xh.get("path", "/")
host = xh.get("host", "")
if host:
params["host"] = host
elif network == "grpc":
gr = stream.get("grpcSettings", {})
params["serviceName"] = gr.get("serviceName", "")
elif network in ("h2", "http"):
h2 = stream.get("httpSettings", {})
params["path"] = h2.get("path", "/")
hosts = h2.get("host", [])
if hosts:
params["host"] = hosts[0]
elif network == "tcp":
tcp = stream.get("tcpSettings", {})
header_type = tcp.get("header", {}).get("type", "")
if header_type and header_type != "none":
params["headerType"] = header_type
return {k: v for k, v in params.items() if v not in ("", None, "none")}
def _build_vless(settings, stream, label):
vnext = settings.get("vnext", [])
if not vnext:
return None
srv = vnext[0]
address = srv.get("address", "")
port = srv.get("port", 443)
users = srv.get("users", [{}])
uid = users[0].get("id", "")
flow = users[0].get("flow", "")
if not uid:
return None
params = _stream_params(stream)
if flow:
params["flow"] = flow
loc = get_ip_location(address)
remark = quote(f"{loc} {label}")
return f"vless://{uid}@{address}:{port}?{urlencode(params)}#{remark}"
def _build_vmess(settings, stream, label):
vnext = settings.get("vnext", [])
if not vnext:
return None
srv = vnext[0]
address = srv.get("address", "")
port = srv.get("port", 443)
users = srv.get("users", [{}])
uid = users[0].get("id", "")
alter_id = users[0].get("alterId", 0)
security = users[0].get("security", "auto")
if not uid:
return None
network = stream.get("network", "tcp")
tls_sec = stream.get("security", "")
obj = {
"v": "2", "ps": label,
"add": address, "port": str(port),
"id": uid, "aid": str(alter_id),
"scy": security, "net": network,
"type": "none",
"tls": "tls" if tls_sec == "tls" else ""
}
if network == "ws":
ws = stream.get("wsSettings", {})
obj["path"] = ws.get("path", "/")
obj["host"] = ws.get("headers", {}).get("Host", "")
elif network in ("h2", "http"):
h2 = stream.get("httpSettings", {})
obj["path"] = h2.get("path", "/")
hosts = h2.get("host", [])
obj["host"] = hosts[0] if hosts else ""
elif network == "grpc":
gr = stream.get("grpcSettings", {})
obj["path"] = gr.get("serviceName", "")
if tls_sec == "tls":
ts = stream.get("tlsSettings", {})
obj["sni"] = ts.get("serverName", "")
loc = get_ip_location(address)
obj["ps"] = f"{loc} {label}"
encoded = base64.b64encode(json.dumps(obj, ensure_ascii=False).encode()).decode()
return f"vmess://{encoded}"
def _build_trojan(settings, stream, label):
servers = settings.get("servers", [])
if not servers:
return None
srv = servers[0]
address = srv.get("address", "")
port = srv.get("port", 443)
password = srv.get("password", "")
if not password:
return None
params = _stream_params(stream)
loc = get_ip_location(address)
remark = quote(f"{loc} {label}")
return f"trojan://{password}@{address}:{port}?{urlencode(params)}#{remark}"
def _build_ss(settings, label):
servers = settings.get("servers", [])
if not servers:
return None
srv = servers[0]
address = srv.get("address", "")
port = srv.get("port", 443)
method = srv.get("method", "aes-256-gcm")
password = srv.get("password", "")
if not password:
return None
loc = get_ip_location(address)
userinfo = base64.b64encode(f"{method}:{password}".encode()).decode()
remark = quote(f"{loc} {label}")
return f"ss://{userinfo}@{address}:{port}#{remark}"
def parse_xray_config(cfg, index):
"""解析 xray config.json,从 outbounds 提取代理链接"""
links = []
if not cfg:
return links
label = f"Xray_发财路上 {index}"
for ob in cfg.get("outbounds", []):
protocol = ob.get("protocol", "")
tag = ob.get("tag", "")
if protocol in _SKIP_PROTOCOLS or tag in _SKIP_TAGS:
continue
settings = ob.get("settings", {})
stream = ob.get("streamSettings", {})
try:
link = None
if protocol == "vless":
link = _build_vless(settings, stream, label)
elif protocol == "vmess":
link = _build_vmess(settings, stream, label)
elif protocol == "trojan":
link = _build_trojan(settings, stream, label)
elif protocol == "shadowsocks":
link = _build_ss(settings, label)
if link:
links.append(link)
except Exception:
continue
return links
# ── Clash.meta2 YAML 解析 ────────────────────────────────────
def fetch_clash_config(index):
"""抓取 clash.meta2 配置,路径格式:clash.meta2/{index}/config.yaml"""
urls = [
f"https://www.gitlabip.xyz/Alvin9999/PAC/refs/heads/master/backup/img/1/2/ipp/clash.meta2/{index}/config.yaml",
f"https://gitlab.com/free9999/ipupdate/-/raw/master/backup/img/1/2/ipp/clash.meta2/{index}/config.yaml"
]
for url in urls:
try:
res = requests.get(url, timeout=5)
if res.status_code == 200:
return yaml.safe_load(res.text)
except:
continue
return None
def parse_clash_proxies(cfg, index):
"""解析 clash.meta2 config.yaml 中的 proxies,生成通用链接"""
links = []
if not cfg:
return links
label = f"Clash_财源滚滚 {index}"
for p in cfg.get("proxies", []):
ptype = p.get("type", "")
server = p.get("server", "")
port = p.get("port", 443)
loc = get_ip_location(server)
remark = quote(f"{loc} {label}")
try:
if ptype == "vless":
uid = p.get("uuid", "")
network = p.get("network", "tcp")
tls = p.get("tls", False)
flow = p.get("flow", "") or ""
sni = p.get("servername", "") or p.get("sni", "")
fp = p.get("client-fingerprint", "")
reality = p.get("reality-opts", {})
params = {"type": network}
if reality:
params["security"] = "reality"
params["pbk"] = reality.get("public-key", "")
params["sid"] = reality.get("short-id", "")
params["sni"] = sni
if fp: params["fp"] = fp
elif tls:
params["security"] = "tls"
if sni: params["sni"] = sni
if fp: params["fp"] = fp
else:
params["security"] = "none"
if flow: params["flow"] = flow
ws_opts = p.get("ws-opts", {})
grpc_opts = p.get("grpc-opts", {})
if network == "ws":
params["path"] = ws_opts.get("path", "/")
host = ws_opts.get("headers", {}).get("Host", "")
if host: params["host"] = host
elif network == "grpc":
params["serviceName"] = grpc_opts.get("grpc-service-name", "")
params = {k: v for k, v in params.items() if v not in ("", None)}
links.append(f"vless://{uid}@{server}:{port}?{urlencode(params)}#{remark}")
elif ptype == "vmess":
uid = p.get("uuid", "")
alter_id = p.get("alterId", 0)
cipher = p.get("cipher", "auto")
network = p.get("network", "tcp")
tls = p.get("tls", False)
sni = p.get("servername", "") or p.get("sni", "")
ws_opts = p.get("ws-opts", {})
obj = {
"v": "2", "ps": f"{loc} {label}",
"add": server, "port": str(port),
"id": uid, "aid": str(alter_id),
"scy": cipher, "net": network,
"type": "none",
"tls": "tls" if tls else "", "sni": sni,
}
if network == "ws":
obj["path"] = ws_opts.get("path", "/")
obj["host"] = ws_opts.get("headers", {}).get("Host", "")
encoded = base64.b64encode(json.dumps(obj).encode()).decode()
links.append(f"vmess://{encoded}")
elif ptype == "trojan":
password = p.get("password", "")
sni = p.get("sni", "") or p.get("servername", "")
network = p.get("network", "tcp")
fp = p.get("client-fingerprint", "")
params = {"security": "tls", "type": network}
if sni: params["sni"] = sni
if fp: params["fp"] = fp
ws_opts = p.get("ws-opts", {})
if network == "ws":
params["path"] = ws_opts.get("path", "/")
params = {k: v for k, v in params.items() if v not in ("", None)}
links.append(f"trojan://{password}@{server}:{port}?{urlencode(params)}#{remark}")
elif ptype == "ss":
method = p.get("cipher", "aes-256-gcm")
password = p.get("password", "")
userinfo = base64.b64encode(f"{method}:{password}".encode()).decode()
links.append(f"ss://{userinfo}@{server}:{port}#{remark}")
elif ptype == "hysteria":
auth = p.get("auth-str", "")
sni = p.get("sni", "")
up = str(p.get("up", "11")).replace(" Mbps","").replace(" mbps","")
down = str(p.get("down", "55")).replace(" Mbps","").replace(" mbps","")
insecure = 1 if p.get("skip-cert-verify") else 0
params = {"auth": auth, "peer": sni, "insecure": insecure,
"upmbps": up, "downmbps": down, "alpn": "h3"}
params = {k: v for k, v in params.items() if v not in ("", None, 0)}
links.append(f"hysteria://{server}:{port}?{urlencode(params)}#{remark}")
elif ptype == "hysteria2":
auth = p.get("auth", "") or p.get("password", "")
sni = p.get("sni", "")
up = str(p.get("up", "")).replace(" Mbps","").replace(" mbps","")
down = str(p.get("down", "")).replace(" Mbps","").replace(" mbps","")
insecure = 1 if p.get("skip-cert-verify") else 0
params = {"sni": sni, "insecure": insecure, "up": up, "down": down}
params = {k: v for k, v in params.items() if v not in ("", None, 0)}
links.append(f"hysteria2://{auth}@{server}:{port}?{urlencode(params)}#{remark}")
elif ptype == "tuic":
uid = p.get("uuid", "")
password = p.get("password", "")
sni = p.get("sni", "")
alpn = p.get("alpn", ["h3"])
insecure = 1 if p.get("skip-cert-verify") else 0
params = {
"sni": sni,
"alpn": ",".join(alpn) if isinstance(alpn, list) else alpn,
"insecure": insecure,
"congestion_control": p.get("congestion-controller", "bbr")
}
params = {k: v for k, v in params.items() if v not in ("", None, 0)}
links.append(f"tuic://{uid}:{password}@{server}:{port}?{urlencode(params)}#{remark}")
except Exception:
continue
return links
# ── 主生成函数 ───────────────────────────────────────────────
def generate_links():
links = []
# 处理 Hy1
for i in range(1, 5):
cfg = fetch_config("hysteria", i)
if cfg:
server = cfg.get("server", "")
if ":" in server:
host, port = (server.split("]:")[0] + "]", server.split("]:")[1]) if "[" in server else server.split(":")
loc = get_ip_location(host.strip("[]"))
params = {
"upmbps": cfg.get("up_mbps", 11),
"downmbps": cfg.get("down_mbps", 55),
"auth": cfg.get("auth_str", ""),
"peer": cfg.get("server_name", ""),
"insecure": 1 if cfg.get("insecure") else 0,
"alpn": "h3"
}
params = {k: v for k, v in params.items() if v}
link = f"hysteria://{host}:{port}?{urlencode(params)}#{quote(f'{loc} Hy1_发财进行时 {i}')}"
links.append(link)
# 处理 Hy2
for i in range(1, 5):
cfg = fetch_config("hysteria2", i)
if cfg:
server = cfg.get("server", "").split(",")[0]
if ":" in server:
host, port = (server.split("]:")[0] + "]", server.split("]:")[1]) if "[" in server else server.split(":")
loc = get_ip_location(host.strip("[]"))
tls = cfg.get("tls", {})
bw = cfg.get("bandwidth", {})
params = {
"sni": tls.get("sni", ""),
"insecure": 1 if tls.get("insecure") else 0,
"up": bw.get("up", "").replace(" mbps", ""),
"down": bw.get("down", "").replace(" mbps", "")
}
params = {k: v for k, v in params.items() if v}
auth = cfg.get("auth", "")
link = f"hysteria2://{auth}@{host}:{port}?{urlencode(params)}#{quote(f'{loc} Hy2_好运充值成功 {i}')}"
links.append(link)
# 处理 Xray(新增)
for i in range(1, 5):
cfg = fetch_xray_config(i)
if cfg:
xray_links = parse_xray_config(cfg, i)
links.extend(xray_links)
# 处理 Clash.meta2(新增)
for i in range(1, 5):
cfg = fetch_clash_config(i)
if cfg:
clash_links = parse_clash_proxies(cfg, i)
links.extend(clash_links)
# 获取本地生成的链接字符串
local_links_str = "\n".join(links)
# 抓取并处理外部订阅
extra_links_str = ""
try:
extra_res = requests.get(extra_sub_url, timeout=10)
if extra_res.status_code == 200:
extra_links_str = base64.b64decode(extra_res.text).decode('utf-8')
except Exception:
pass
# 合并所有链接
combined_content = []
if local_links_str:
combined_content.append(local_links_str)
if extra_links_str:
combined_content.append(extra_links_str.strip())
final_str = "\n".join(combined_content)
# Base64 编码返回
return base64.b64encode(final_str.encode()).decode()
# ── FastAPI 路由 ─────────────────────────────────────────────
@app.get("/")
def home():
return {"我是小可爱~"}
@app.get("/debug")
def debug():
"""排查各类型各序号的 URL 是否能正常抓取"""
结果 = {}
检测项 = [
("Hysteria1", "hysteria", range(1, 5), "config.json", "json"),
("Hysteria2", "hysteria2", range(1, 5), "config.json", "json"),
("Xray", "xray", range(1, 5), "config.json", "json"),
("Clash.meta2", "clash.meta2", range(1, 5), "config.yaml", "yaml"),
]
for 类型名, 路径名, 序号范围, 文件名, _ in 检测项:
结果[类型名] = {}
for i in 序号范围:
主url = f"https://www.gitlabip.xyz/Alvin9999/PAC/refs/heads/master/backup/img/1/2/ipp/{路径名}/{i}/{文件名}"
备url = f"https://gitlab.com/free9999/ipupdate/-/raw/master/backup/img/1/2/ipp/{路径名}/{i}/{文件名}"
条目 = {}
for 标签, url in [("主URL", 主url), ("备用URL", 备url)]:
try:
res = requests.get(url, timeout=5)
if res.status_code == 200:
条目[标签] = "✅ 正常(200)"
else:
条目[标签] = f"❌ 异常(HTTP {res.status_code})"
except Exception as e:
条目[标签] = f"❌ 请求失败:{str(e)}"
结果[类型名][f"第{i}个"] = 条目
return 结果
@app.get("/sub")
def get_sub():
content = generate_links()
return Response(content=content, media_type="text/plain")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |