Skip to content

Commit

Permalink
enhance decode and status check
Browse files Browse the repository at this point in the history
  • Loading branch information
wzdnzd committed Nov 10, 2024
1 parent 4fe9141 commit bbf96a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 16 additions & 2 deletions subscribe/airport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# @Author : wzdnzd
# @Time : 2022-07-15

import base64
import concurrent.futures
import json
import os
Expand Down Expand Up @@ -622,6 +623,14 @@ def parse(
)
return []

@staticmethod
def check_protocol(link: str) -> bool:
return re.match(
r"^(vmess|trojan|ss|ssr|vless|hysteria|hysteria2|tuic|snell)://[a-zA-Z0-9:.?+=@%&#_\-/]{10,}",
utils.trim(link).replace("\r", ""),
flags=re.I,
)

@staticmethod
def decode(
text: str, program: str, artifact: str = "", ignore: bool = False, special: bool = False, throw: bool = False
Expand All @@ -647,9 +656,10 @@ def clean_text(document: str) -> str:
if not text:
return []

is_b64encode, is_json = False, False
if (
utils.isb64encode(text)
or (text.startswith("{") and text.endswith("}"))
(is_b64encode := utils.isb64encode(text))
or (is_json := (text.startswith("{") and text.endswith("}")))
or not re.search(r"^proxies:([\s\r\n]+)?$", text, flags=re.MULTILINE)
):
artifact = utils.trim(text=artifact)
Expand All @@ -659,6 +669,10 @@ def clean_text(document: str) -> str:
v2ray_file = os.path.join(PATH, "subconverter", f"{artifact}.txt")
clash_file = os.path.join(PATH, "subconverter", f"{artifact}.yaml")

# base64 encoding if all lines start with valid protocol
if not is_b64encode and not is_json and all(AirPort.check_protocol(x) for x in text.split("\n") if x):
text = base64.b64encode(text.encode(encoding="UTF8")).decode(encoding="UTF8")

try:
with open(v2ray_file, "w+", encoding="UTF8") as f:
f.write(text)
Expand Down
6 changes: 5 additions & 1 deletion subscribe/crawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ def extract_subscribes(
limits, collections, proxies = max(1, limits), {}, []
sub_regex = r"https?://(?:[a-zA-Z0-9\u4e00-\u9fa5\-]+\.)+[a-zA-Z0-9\u4e00-\u9fa5\-]+(?:(?:(?:/index.php)?/api/v1/client/subscribe\?token=[a-zA-Z0-9]{16,32})|(?:/link/[a-zA-Z0-9]+\?(?:sub|mu|clash)=\d))|https://jmssub\.net/members/getsub\.php\?service=\d+&id=[a-zA-Z0-9\-]{36}(?:\S+)?"
extra_regex = r"https?://(?:[a-zA-Z0-9\u4e00-\u9fa5\-]+\.)+[a-zA-Z0-9\u4e00-\u9fa5\-]+/sub\?(?:\S+)?target=\S+"
protocal_regex = r"(?:vmess|trojan|ss|ssr|snell|hysteria2|vless|hysteria)://[a-zA-Z0-9:.?+=@%&#_\-/]{10,}"
protocal_regex = r"(?:vmess|trojan|ss|ssr|snell|hysteria2|vless|hysteria|tuic)://[a-zA-Z0-9:.?+=@%&#_\-/]{10,}"

regex = f"{sub_regex}|{extra_regex}"

Expand Down Expand Up @@ -1278,6 +1278,10 @@ def check_status(
yaml.add_multi_constructor("str", lambda loader, suffix, node: str(node.value), Loader=yaml.SafeLoader)
proxies = yaml.load(content, Loader=yaml.FullLoader).get("proxies", [])
except:
if all(airport.AirPort.check_protocol(x) for x in content.split("\n") if x):
return True, False

# TODO: 如果配置文件为 singbox、quanx、loon、surge等,需要解析出代理节点信息,并判断是否过期
proxies = []

if proxies is None or len(proxies) == 0:
Expand Down

0 comments on commit bbf96a4

Please sign in to comment.