Created
December 4, 2020 10:09
-
-
Save zombie110year/13b854d2919bbdd9fa975d7283d634f5 to your computer and use it in GitHub Desktop.
解析 SSR 订阅链接,输出 shadowsocks-rust 的配置文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests as r | |
| from base64 import urlsafe_b64decode as b64decode | |
| from typing import List, Optional | |
| import json | |
| 订阅服务器 = "*" | |
| def query(url: str) -> Optional[str]: | |
| resp = r.get(url, timeout=30) | |
| if resp.status_code == 200: | |
| return resp.text.removeprefix("\ufeff") | |
| def decode(text: str) -> List[str]: | |
| decoded1 = (line for line in b64decode(text).decode().split("\n") if line != "") | |
| noprefix = (line.removeprefix("ssr://") for line in decoded1) | |
| decoded2 = (b64decode(line).decode() for line in noprefix) | |
| links = [f"ssr://{line}" for line in decoded2] | |
| return links | |
| def parse(line: str) -> dict: | |
| ssr_url = line.removeprefix("ssr://") | |
| main, param, *_ = ssr_url.split("/?") | |
| domain, port, protocol, method, obfs, passwd_b64, *_ = main.split(":") | |
| passwd = b64decode(passwd_b64).decode() | |
| params = {} | |
| for item in param.split("&"): | |
| key, value, *_ = item.split("=") | |
| if (lastlen := len(value) % 4) != 0: | |
| params[key] = b64decode(value + (4 - lastlen)*"=").decode() | |
| else: | |
| params[key] = b64decode(value).decode() | |
| return { | |
| "address": domain, | |
| "port": int(port), | |
| "password": passwd, | |
| "method": method, | |
| "timeout": 300, | |
| #"params": params | |
| } | |
| if __name__ == "__main__": | |
| urls = decode(query(订阅服务器)) | |
| links = [] | |
| for i in urls: | |
| try: | |
| parsed = parse(i) | |
| links.append(parsed) | |
| except Exception as e: | |
| print(i) | |
| raise e | |
| config = { | |
| "servers": links, | |
| "local_port": 1080, | |
| "local_address": "0.0.0.0" | |
| } | |
| print(json.dumps(config, indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment