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