Paijo commited on
Commit
c1f87fd
·
verified ·
1 Parent(s): 7286792

update app/grabber/patterns.py

Browse files
Files changed (1) hide show
  1. app/grabber/patterns.py +71 -0
app/grabber/patterns.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from typing import List, Tuple
3
+
4
+
5
+ class ProxyPatterns:
6
+ HTTP_IP_PORT = re.compile(
7
+ r"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}"
8
+ r"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
9
+ r":([0-9]{1,5})\b"
10
+ )
11
+
12
+ VMESS_URL = re.compile(r"vmess://[A-Za-z0-9+/=]+")
13
+
14
+ VLESS_URL = re.compile(r"vless://[a-zA-Z0-9-]+@[a-zA-Z0-9.-]+:[0-9]+[^\s]*")
15
+
16
+ TROJAN_URL = re.compile(r"trojan://[^@\s]+@[a-zA-Z0-9.-]+:[0-9]+[^\s]*")
17
+
18
+ SS_URL = re.compile(r"ss://[A-Za-z0-9+/=]+@[a-zA-Z0-9.-]+:[0-9]+")
19
+
20
+ @classmethod
21
+ def extract_http_proxies(cls, text: str) -> List[Tuple[str, str]]:
22
+ result = []
23
+ for match in cls.HTTP_IP_PORT.finditer(text):
24
+ ip_port = match.group(0)
25
+ ip, port = ip_port.rsplit(":", 1)
26
+ result.append((ip, port))
27
+ return result
28
+
29
+ @classmethod
30
+ def is_vmess(cls, url: str) -> bool:
31
+ return bool(cls.VMESS_URL.match(url))
32
+
33
+ @classmethod
34
+ def extract_vmess_urls(cls, text: str) -> List[str]:
35
+ return cls.VMESS_URL.findall(text)
36
+
37
+ @classmethod
38
+ def is_vless(cls, url: str) -> bool:
39
+ return bool(cls.VLESS_URL.match(url))
40
+
41
+ @classmethod
42
+ def extract_vless_urls(cls, text: str) -> List[str]:
43
+ return cls.VLESS_URL.findall(text)
44
+
45
+ @classmethod
46
+ def is_trojan(cls, url: str) -> bool:
47
+ return bool(cls.TROJAN_URL.match(url))
48
+
49
+ @classmethod
50
+ def extract_trojan_urls(cls, text: str) -> List[str]:
51
+ return cls.TROJAN_URL.findall(text)
52
+
53
+ @classmethod
54
+ def is_shadowsocks(cls, url: str) -> bool:
55
+ return bool(cls.SS_URL.match(url))
56
+
57
+ @classmethod
58
+ def extract_ss_urls(cls, text: str) -> List[str]:
59
+ return cls.SS_URL.findall(text)
60
+
61
+ @classmethod
62
+ def is_valid_ip(cls, ip: str) -> bool:
63
+ try:
64
+ octets = ip.split(".")
65
+ return len(octets) == 4 and all(0 <= int(o) <= 255 for o in octets)
66
+ except (ValueError, AttributeError):
67
+ return False
68
+
69
+ @classmethod
70
+ def is_valid_port(cls, port: int) -> bool:
71
+ return 1 <= port <= 65535