refactor
This commit is contained in:
13 files changed
+1404
-1299
No files matched your search
@@ -12,38 +12,24 @@ BUILTIN_ACTIVATION_FALLBACKS = {
|
||||
"list-twitter.txt": "general",
|
||||
}
|
||||
|
||||
def _hostlist_remove_pattern(stem: str) -> str:
|
||||
escaped = re.escape(stem)
|
||||
return rf'\s+--hostlist=(?:"[^"]*/hostlists/{escaped}\.txt"|[^ ]*/hostlists/{escaped}\.txt)'
|
||||
|
||||
|
||||
def _mk_family(match_stems: list[str], remove_stems: list[str]) -> dict:
|
||||
return {
|
||||
"match": [f"list-{s}.txt" for s in match_stems],
|
||||
"remove": [_hostlist_remove_pattern(f"list-{s}") for s in remove_stems],
|
||||
}
|
||||
|
||||
|
||||
FAMILY_PATTERNS = {
|
||||
"general": {
|
||||
"match": ["list-general.txt", "list-general-user.txt"],
|
||||
"remove": [
|
||||
r'\s+--hostlist=(?:"[^"]*/hostlists/list-general\.txt"|[^ ]*/hostlists/list-general\.txt)',
|
||||
r'\s+--hostlist=(?:"[^"]*/hostlists/list-general-user\.txt"|[^ ]*/hostlists/list-general-user\.txt)',
|
||||
],
|
||||
},
|
||||
"google": {
|
||||
"match": ["list-google.txt"],
|
||||
"remove": [
|
||||
r'\s+--hostlist=(?:"[^"]*/hostlists/list-google\.txt"|[^ ]*/hostlists/list-google\.txt)',
|
||||
],
|
||||
},
|
||||
"instagram": {
|
||||
"match": ["list-instagram.txt"],
|
||||
"remove": [
|
||||
r'\s+--hostlist=(?:"[^"]*/hostlists/list-instagram\.txt"|[^ ]*/hostlists/list-instagram\.txt)',
|
||||
],
|
||||
},
|
||||
"soundcloud": {
|
||||
"match": ["list-soundcloud.txt"],
|
||||
"remove": [
|
||||
r'\s+--hostlist=(?:"[^"]*/hostlists/list-soundcloud\.txt"|[^ ]*/hostlists/list-soundcloud\.txt)',
|
||||
],
|
||||
},
|
||||
"twitter": {
|
||||
"match": ["list-twitter.txt"],
|
||||
"remove": [
|
||||
r'\s+--hostlist=(?:"[^"]*/hostlists/list-twitter\.txt"|[^ ]*/hostlists/list-twitter\.txt)',
|
||||
],
|
||||
},
|
||||
"general": _mk_family(["general", "general-user"], ["general", "general-user"]),
|
||||
"google": _mk_family(["google"], ["google"]),
|
||||
"instagram": _mk_family(["instagram"], ["instagram"]),
|
||||
"soundcloud": _mk_family(["soundcloud"], ["soundcloud"]),
|
||||
"twitter": _mk_family(["twitter"], ["twitter"]),
|
||||
}
|
||||
|
||||
|
||||
@@ -78,19 +64,32 @@ def add_standard_excludes(line: str, standard_excludes: list[str]) -> str:
|
||||
return line
|
||||
|
||||
|
||||
def _render_args(
|
||||
line: str,
|
||||
hostlist_path: str,
|
||||
standard_excludes: list[str],
|
||||
*,
|
||||
preset_family: str | None = None,
|
||||
) -> str:
|
||||
rendered = strip_trailing_new(line)
|
||||
if preset_family is not None:
|
||||
for pattern in FAMILY_PATTERNS[preset_family]["remove"]:
|
||||
rendered = re.sub(pattern, "", rendered)
|
||||
else:
|
||||
rendered = re.sub(r'\s+--hostlist=(?:"[^"]+"|\S+)', "", rendered)
|
||||
rendered = normalize_spaces(rendered)
|
||||
rendered = f'{rendered} --hostlist="{hostlist_path}"'
|
||||
rendered = add_standard_excludes(rendered, standard_excludes)
|
||||
return f"{normalize_spaces(rendered)} --new"
|
||||
|
||||
|
||||
def render_preset_clone(
|
||||
line: str,
|
||||
family: str,
|
||||
hostlist_path: str,
|
||||
standard_excludes: list[str],
|
||||
) -> str:
|
||||
rendered = strip_trailing_new(line)
|
||||
for pattern in FAMILY_PATTERNS[family]["remove"]:
|
||||
rendered = re.sub(pattern, "", rendered)
|
||||
rendered = normalize_spaces(rendered)
|
||||
rendered = f'{rendered} --hostlist="{hostlist_path}"'
|
||||
rendered = add_standard_excludes(rendered, standard_excludes)
|
||||
return f"{normalize_spaces(rendered)} --new"
|
||||
return _render_args(line, hostlist_path, standard_excludes, preset_family=family)
|
||||
|
||||
|
||||
def render_custom_args(
|
||||
@@ -98,12 +97,7 @@ def render_custom_args(
|
||||
hostlist_path: str,
|
||||
standard_excludes: list[str],
|
||||
) -> str:
|
||||
rendered = strip_trailing_new(fragment)
|
||||
rendered = re.sub(r'\s+--hostlist=(?:"[^"]+"|\S+)', "", rendered)
|
||||
rendered = normalize_spaces(rendered)
|
||||
rendered = f'{rendered} --hostlist="{hostlist_path}"'
|
||||
rendered = add_standard_excludes(rendered, standard_excludes)
|
||||
return f"{normalize_spaces(rendered)} --new"
|
||||
return _render_args(fragment, hostlist_path, standard_excludes)
|
||||
|
||||
|
||||
def clone_family_lines(
|
||||
|
||||
@@ -47,6 +47,22 @@ def _mk_transport(
|
||||
return None
|
||||
|
||||
|
||||
def _mk_tls(server_name: str, fp: "str | None" = None, alpn: "str | None" = None) -> dict:
|
||||
tls: dict = {"enabled": True, "server_name": server_name}
|
||||
if fp:
|
||||
tls["utls"] = {"enabled": True, "fingerprint": fp}
|
||||
if alpn:
|
||||
tls["alpn"] = alpn.split(",")
|
||||
return tls
|
||||
|
||||
|
||||
def _mk_auth(userinfo: str) -> "tuple[str | None, str | None]":
|
||||
if userinfo and ":" in userinfo:
|
||||
username, _, password = userinfo.partition(":")
|
||||
return urllib.parse.unquote(username), urllib.parse.unquote(password)
|
||||
return None, None
|
||||
|
||||
|
||||
def parse_vless(url: str, tag: str) -> dict:
|
||||
userinfo, host, port, params = _parse_url_parts(url, "vless")
|
||||
security = params.get("security", "none")
|
||||
@@ -62,25 +78,14 @@ def parse_vless(url: str, tag: str) -> dict:
|
||||
}
|
||||
|
||||
if security == "reality":
|
||||
ob["tls"] = {
|
||||
ob["tls"] = _mk_tls(params.get("sni", host), fp=params.get("fp", "chrome"))
|
||||
ob["tls"]["reality"] = {
|
||||
"enabled": True,
|
||||
"server_name": params.get("sni", host),
|
||||
"utls": {"enabled": True, "fingerprint": params.get("fp", "chrome")},
|
||||
"reality": {
|
||||
"enabled": True,
|
||||
"public_key": params["pbk"],
|
||||
"short_id": params.get("sid", ""),
|
||||
},
|
||||
"public_key": params["pbk"],
|
||||
"short_id": params.get("sid", ""),
|
||||
}
|
||||
elif security == "tls":
|
||||
ob["tls"] = {
|
||||
"enabled": True,
|
||||
"server_name": params.get("sni", host),
|
||||
}
|
||||
if params.get("fp"):
|
||||
ob["tls"]["utls"] = {"enabled": True, "fingerprint": params["fp"]}
|
||||
if params.get("alpn"):
|
||||
ob["tls"]["alpn"] = params["alpn"].split(",")
|
||||
ob["tls"] = _mk_tls(params.get("sni", host), fp=params.get("fp"), alpn=params.get("alpn"))
|
||||
|
||||
tr = _mk_transport(
|
||||
transport,
|
||||
@@ -123,11 +128,11 @@ def parse_vmess(url: str, tag: str) -> dict:
|
||||
}
|
||||
|
||||
if tls_field in ("tls", "reality"):
|
||||
ob["tls"] = {"enabled": True, "server_name": sni}
|
||||
if data.get("fp"):
|
||||
ob["tls"]["utls"] = {"enabled": True, "fingerprint": str(data["fp"])}
|
||||
if data.get("alpn"):
|
||||
ob["tls"]["alpn"] = str(data["alpn"]).split(",")
|
||||
ob["tls"] = _mk_tls(
|
||||
sni,
|
||||
fp=str(data["fp"]) if data.get("fp") else None,
|
||||
alpn=str(data["alpn"]) if data.get("alpn") else None,
|
||||
)
|
||||
|
||||
path = str(data.get("path") or "/")
|
||||
h_host = str(data.get("host") or host)
|
||||
@@ -154,17 +159,9 @@ def parse_trojan(url: str, tag: str) -> dict:
|
||||
"server": host,
|
||||
"server_port": int(port),
|
||||
"password": urllib.parse.unquote(userinfo),
|
||||
"tls": {
|
||||
"enabled": True,
|
||||
"server_name": params.get("sni", host),
|
||||
},
|
||||
"tls": _mk_tls(params.get("sni", host), fp=params.get("fp"), alpn=params.get("alpn")),
|
||||
}
|
||||
|
||||
if params.get("fp"):
|
||||
ob["tls"]["utls"] = {"enabled": True, "fingerprint": params["fp"]}
|
||||
if params.get("alpn"):
|
||||
ob["tls"]["alpn"] = params["alpn"].split(",")
|
||||
|
||||
tr = _mk_transport(
|
||||
transport,
|
||||
path=params.get("path", "/"),
|
||||
@@ -256,10 +253,10 @@ def parse_socks(url: str, tag: str) -> dict:
|
||||
userinfo, host, port, _ = _parse_url_parts(url, scheme)
|
||||
|
||||
ob: dict = {"type": "socks", "tag": tag, "version": version}
|
||||
if userinfo and ":" in userinfo:
|
||||
username, _, password = userinfo.partition(":")
|
||||
ob["username"] = urllib.parse.unquote(username)
|
||||
ob["password"] = urllib.parse.unquote(password)
|
||||
username, password = _mk_auth(userinfo)
|
||||
if username is not None:
|
||||
ob["username"] = username
|
||||
ob["password"] = password
|
||||
ob["server"] = host
|
||||
ob["server_port"] = int(port)
|
||||
|
||||
@@ -271,10 +268,10 @@ def parse_http_proxy(url: str, tag: str) -> dict:
|
||||
userinfo, host, port, _ = _parse_url_parts(url, scheme)
|
||||
|
||||
ob: dict = {"type": "http", "tag": tag}
|
||||
if userinfo and ":" in userinfo:
|
||||
username, _, password = userinfo.partition(":")
|
||||
ob["username"] = urllib.parse.unquote(username)
|
||||
ob["password"] = urllib.parse.unquote(password)
|
||||
username, password = _mk_auth(userinfo)
|
||||
if username is not None:
|
||||
ob["username"] = username
|
||||
ob["password"] = password
|
||||
ob["server"] = host
|
||||
ob["server_port"] = int(port)
|
||||
|
||||
|
||||
Reference in new issue
Block a user