gitscope
https://dev.kronoz.win/r/marzneshin.git
app/models/proxy.py · v0.1.0
Source
1 a47eb4c Dawsh 2y ago import json
2 a47eb4c Dawsh 2y ago from enum import Enum
3 a47eb4c Dawsh 2y ago from typing import Optional, Union, List
4 a47eb4c Dawsh 2y ago # from uuid import UUID, uuid4
5 a47eb4c Dawsh 2y ago
6 a47eb4c Dawsh 2y ago from pydantic import ConfigDict, BaseModel, Field, validator
7 a47eb4c Dawsh 2y ago
8 a47eb4c Dawsh 2y ago
9 12b255b Dawsh 2y ago class XTLSFlows(Enum):
10 12b255b Dawsh 2y ago NONE = ''
11 12b255b Dawsh 2y ago VISION = 'xtls-rprx-vision'
12 12b255b Dawsh 2y ago
13 12b255b Dawsh 2y ago
14 12b255b Dawsh 2y ago class ShadowsocksMethods(Enum):
15 12b255b Dawsh 2y ago AES_128_GCM = 'aes-128-gcm'
16 12b255b Dawsh 2y ago AES_256_GCM = 'aes-256-gcm'
17 12b255b Dawsh 2y ago CHACHA20_POLY1305 = 'chacha20-ietf-poly1305'
18 a47eb4c Dawsh 2y ago
19 a47eb4c Dawsh 2y ago
20 a47eb4c Dawsh 2y ago class ProxyTypes(str, Enum):
21 a47eb4c Dawsh 2y ago # proxy_type = protocol
22 a47eb4c Dawsh 2y ago
23 a47eb4c Dawsh 2y ago VMess = "vmess"
24 a47eb4c Dawsh 2y ago VLESS = "vless"
25 a47eb4c Dawsh 2y ago Trojan = "trojan"
26 a47eb4c Dawsh 2y ago Shadowsocks = "shadowsocks"
27 a47eb4c Dawsh 2y ago
28 a47eb4c Dawsh 2y ago @property
29 a47eb4c Dawsh 2y ago def settings_model(self):
30 a47eb4c Dawsh 2y ago if self == self.VMess:
31 a47eb4c Dawsh 2y ago return VMessSettings
32 a47eb4c Dawsh 2y ago if self == self.VLESS:
33 a47eb4c Dawsh 2y ago return VLESSSettings
34 a47eb4c Dawsh 2y ago if self == self.Trojan:
35 a47eb4c Dawsh 2y ago return TrojanSettings
36 a47eb4c Dawsh 2y ago if self == self.Shadowsocks:
37 a47eb4c Dawsh 2y ago return ShadowsocksSettings
38 a47eb4c Dawsh 2y ago
39 a47eb4c Dawsh 2y ago
40 a47eb4c Dawsh 2y ago class ProxySettings(BaseModel):
41 a47eb4c Dawsh 2y ago @classmethod
42 a47eb4c Dawsh 2y ago def from_dict(cls, proxy_type: ProxyTypes, _dict: dict):
43 a47eb4c Dawsh 2y ago return ProxyTypes(proxy_type).settings_model.parse_obj(_dict)
44 a47eb4c Dawsh 2y ago
45 a47eb4c Dawsh 2y ago def dict(self, *, no_obj=False, **kwargs):
46 a47eb4c Dawsh 2y ago if no_obj:
47 a47eb4c Dawsh 2y ago return json.loads(self.json())
48 a47eb4c Dawsh 2y ago return super().dict(**kwargs)
49 a47eb4c Dawsh 2y ago
50 a47eb4c Dawsh 2y ago
51 a47eb4c Dawsh 2y ago class VMessSettings(ProxySettings):
52 a47eb4c Dawsh 2y ago id: str = Field(nullable=False)
53 a47eb4c Dawsh 2y ago
54 a47eb4c Dawsh 2y ago
55 a47eb4c Dawsh 2y ago class VLESSSettings(ProxySettings):
56 a47eb4c Dawsh 2y ago id: str = Field(nullable=False)
57 a47eb4c Dawsh 2y ago flow: XTLSFlows = XTLSFlows.NONE
58 a47eb4c Dawsh 2y ago
59 a47eb4c Dawsh 2y ago
60 a47eb4c Dawsh 2y ago class TrojanSettings(ProxySettings):
61 a47eb4c Dawsh 2y ago password: str = Field(nullable=False)
62 a47eb4c Dawsh 2y ago
63 a47eb4c Dawsh 2y ago
64 a47eb4c Dawsh 2y ago class ShadowsocksSettings(ProxySettings):
65 a47eb4c Dawsh 2y ago password: str = Field(nullable=False)
66 a47eb4c Dawsh 2y ago method: ShadowsocksMethods = ShadowsocksMethods.CHACHA20_POLY1305
67 a47eb4c Dawsh 2y ago
68 a47eb4c Dawsh 2y ago
69 a47eb4c Dawsh 2y ago class InboundHostSecurity(str, Enum):
70 a47eb4c Dawsh 2y ago inbound_default = "inbound_default"
71 a47eb4c Dawsh 2y ago none = "none"
72 a47eb4c Dawsh 2y ago tls = "tls"
73 a47eb4c Dawsh 2y ago
74 a47eb4c Dawsh 2y ago
75 a47eb4c Dawsh 2y ago InboundHostALPN = Enum(
76 a47eb4c Dawsh 2y ago "ProxyHostALPN",
77 a47eb4c Dawsh 2y ago {
78 a47eb4c Dawsh 2y ago "none": "",
79 a47eb4c Dawsh 2y ago "h2": "h2",
80 a47eb4c Dawsh 2y ago "http/1.1": "http/1.1",
81 a47eb4c Dawsh 2y ago "h2,http/1.1": "h2,http/1.1",
82 a47eb4c Dawsh 2y ago },
83 a47eb4c Dawsh 2y ago )
84 a47eb4c Dawsh 2y ago
85 a47eb4c Dawsh 2y ago
86 a47eb4c Dawsh 2y ago InboundHostFingerprint = Enum(
87 a47eb4c Dawsh 2y ago "ProxyHostFingerprint",
88 a47eb4c Dawsh 2y ago {
89 a47eb4c Dawsh 2y ago "none": "",
90 a47eb4c Dawsh 2y ago "chrome": "chrome",
91 a47eb4c Dawsh 2y ago "firefox": "firefox",
92 a47eb4c Dawsh 2y ago "safari": "safari",
93 a47eb4c Dawsh 2y ago "ios": "ios",
94 a47eb4c Dawsh 2y ago "android": "android",
95 a47eb4c Dawsh 2y ago "edge": "edge",
96 a47eb4c Dawsh 2y ago "360": "360",
97 a47eb4c Dawsh 2y ago "qq": "qq",
98 a47eb4c Dawsh 2y ago "random": "random",
99 a47eb4c Dawsh 2y ago "randomized": "randomized",
100 a47eb4c Dawsh 2y ago },
101 a47eb4c Dawsh 2y ago )
102 a47eb4c Dawsh 2y ago
103 a47eb4c Dawsh 2y ago
104 a47eb4c Dawsh 2y ago class FormatVariables(dict):
105 a47eb4c Dawsh 2y ago def __missing__(self, key):
106 a47eb4c Dawsh 2y ago return key.join("{}")
107 a47eb4c Dawsh 2y ago
108 a47eb4c Dawsh 2y ago
109 a47eb4c Dawsh 2y ago class InboundHost(BaseModel):
110 a47eb4c Dawsh 2y ago remark: str
111 a47eb4c Dawsh 2y ago address: str
112 a47eb4c Dawsh 2y ago port: Optional[int] = Field(None, nullable=True)
113 a47eb4c Dawsh 2y ago sni: Optional[str] = Field(None, nullable=True)
114 a47eb4c Dawsh 2y ago host: Optional[str] = Field(None, nullable=True)
115 a47eb4c Dawsh 2y ago path: Optional[str] = Field(None, nullable=True)
116 a47eb4c Dawsh 2y ago security: InboundHostSecurity = InboundHostSecurity.inbound_default
117 a47eb4c Dawsh 2y ago alpn: InboundHostALPN = InboundHostALPN.none
118 a47eb4c Dawsh 2y ago fingerprint: InboundHostFingerprint = InboundHostFingerprint.none
119 a47eb4c Dawsh 2y ago allowinsecure: Union[bool, None] = None
120 a47eb4c Dawsh 2y ago is_disabled: Union[bool, None] = None
121 a47eb4c Dawsh 2y ago model_config = ConfigDict(from_attributes=True)
122 a47eb4c Dawsh 2y ago
123 a47eb4c Dawsh 2y ago # TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
124 a47eb4c Dawsh 2y ago # Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
125 a47eb4c Dawsh 2y ago @validator("remark", pre=False, always=True)
126 a47eb4c Dawsh 2y ago def validate_remark(cls, v):
127 a47eb4c Dawsh 2y ago try:
128 a47eb4c Dawsh 2y ago v.format_map(FormatVariables())
129 a47eb4c Dawsh 2y ago except ValueError as exc:
130 a47eb4c Dawsh 2y ago raise ValueError("Invalid formatting variables")
131 a47eb4c Dawsh 2y ago
132 a47eb4c Dawsh 2y ago return v
133 a47eb4c Dawsh 2y ago
134 a47eb4c Dawsh 2y ago # TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
135 a47eb4c Dawsh 2y ago # Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
136 a47eb4c Dawsh 2y ago @validator("address", pre=False, always=True)
137 a47eb4c Dawsh 2y ago def validate_address(cls, v):
138 a47eb4c Dawsh 2y ago try:
139 a47eb4c Dawsh 2y ago v.format_map(FormatVariables())
140 a47eb4c Dawsh 2y ago except ValueError as exc:
141 a47eb4c Dawsh 2y ago raise ValueError("Invalid formatting variables")
142 a47eb4c Dawsh 2y ago
143 a47eb4c Dawsh 2y ago return v
144 a47eb4c Dawsh 2y ago
145 a47eb4c Dawsh 2y ago # TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
146 a47eb4c Dawsh 2y ago # Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
147 a47eb4c Dawsh 2y ago @validator("path", pre=False, always=True)
148 a47eb4c Dawsh 2y ago def validate_path(cls, v):
149 a47eb4c Dawsh 2y ago if not v:
150 a47eb4c Dawsh 2y ago return v
151 a47eb4c Dawsh 2y ago try:
152 a47eb4c Dawsh 2y ago v.format_map(FormatVariables())
153 a47eb4c Dawsh 2y ago except ValueError:
154 a47eb4c Dawsh 2y ago raise ValueError("Invalid formatting variables")
155 a47eb4c Dawsh 2y ago
156 a47eb4c Dawsh 2y ago return v
157 a47eb4c Dawsh 2y ago
158 a47eb4c Dawsh 2y ago
159 2de9d72 Dawsh 2y ago class InboundHostResponse(InboundHost):
160 2de9d72 Dawsh 2y ago id: int
161 2de9d72 Dawsh 2y ago
162 2de9d72 Dawsh 2y ago
163 a47eb4c Dawsh 2y ago class InboundBase(BaseModel):
164 a47eb4c Dawsh 2y ago id: Optional[int] = Field(None)
165 a47eb4c Dawsh 2y ago tag: str = Field()
166 a47eb4c Dawsh 2y ago protocol: ProxyTypes = Field()
167 4d647dd Dawsh 2y ago config: str
168 a47eb4c Dawsh 2y ago node_id: Optional[int] = Field(None)
169 a47eb4c Dawsh 2y ago model_config = ConfigDict(from_attributes=True)
170 a47eb4c Dawsh 2y ago
171 12b255b Dawsh 2y ago
172 a47eb4c Dawsh 2y ago from app.models.service import ServiceBase
173 a47eb4c Dawsh 2y ago from app.models.node import NodeBase
174 a47eb4c Dawsh 2y ago
175 12b255b Dawsh 2y ago
176 a47eb4c Dawsh 2y ago class Inbound(InboundBase):
177 a47eb4c Dawsh 2y ago services: Optional[List[ServiceBase]] = None
178 a47eb4c Dawsh 2y ago node: Optional[NodeBase] = None