paijo77 commited on
Commit
7286792
·
verified ·
1 Parent(s): c756a01

update app/grabber/parsers.py

Browse files
Files changed (1) hide show
  1. app/grabber/parsers.py +85 -0
app/grabber/parsers.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import json
3
+ from typing import Optional
4
+ from app.models.proxy import Proxy
5
+
6
+
7
+ class VMessParser:
8
+ @staticmethod
9
+ def parse(url: str) -> Proxy:
10
+ if not url.startswith("vmess://"):
11
+ raise ValueError("Invalid VMess URL")
12
+
13
+ try:
14
+ encoded = url.replace("vmess://", "")
15
+ missing_padding = len(encoded) % 4
16
+ if missing_padding:
17
+ encoded += "=" * (4 - missing_padding)
18
+
19
+ decoded = base64.b64decode(encoded).decode("utf-8")
20
+ config = json.loads(decoded)
21
+
22
+ return Proxy(
23
+ ip=config.get("add", ""),
24
+ port=int(config.get("port", 0)),
25
+ protocol="vmess",
26
+ source="subscription",
27
+ )
28
+ except Exception as e:
29
+ raise ValueError(f"Failed to parse VMess URL: {e}")
30
+
31
+
32
+ class VLESSParser:
33
+ @staticmethod
34
+ def parse(url: str) -> Proxy:
35
+ if not url.startswith("vless://"):
36
+ raise ValueError("Invalid VLESS URL")
37
+
38
+ try:
39
+ url_parts = url.replace("vless://", "")
40
+ uuid_part, server_part = url_parts.split("@", 1)
41
+ server_port = server_part.split("?")[0]
42
+ server, port = server_port.rsplit(":", 1)
43
+
44
+ return Proxy(
45
+ ip=server, port=int(port), protocol="vless", source="subscription"
46
+ )
47
+ except Exception as e:
48
+ raise ValueError(f"Failed to parse VLESS URL: {e}")
49
+
50
+
51
+ class TrojanParser:
52
+ @staticmethod
53
+ def parse(url: str) -> Proxy:
54
+ if not url.startswith("trojan://"):
55
+ raise ValueError("Invalid Trojan URL")
56
+
57
+ try:
58
+ url_parts = url.replace("trojan://", "")
59
+ password_part, server_part = url_parts.split("@", 1)
60
+ server_port = server_part.split("?")[0]
61
+ server, port = server_port.rsplit(":", 1)
62
+
63
+ return Proxy(
64
+ ip=server, port=int(port), protocol="trojan", source="subscription"
65
+ )
66
+ except Exception as e:
67
+ raise ValueError(f"Failed to parse Trojan URL: {e}")
68
+
69
+
70
+ class SSParser:
71
+ @staticmethod
72
+ def parse(url: str) -> Proxy:
73
+ if not url.startswith("ss://"):
74
+ raise ValueError("Invalid Shadowsocks URL")
75
+
76
+ try:
77
+ url_parts = url.replace("ss://", "")
78
+ config_part, server_part = url_parts.split("@", 1)
79
+ server, port = server_part.split(":", 1)
80
+
81
+ return Proxy(
82
+ ip=server, port=int(port), protocol="shadowsocks", source="subscription"
83
+ )
84
+ except Exception as e:
85
+ raise ValueError(f"Failed to parse Shadowsocks URL: {e}")