1use crate::{Proxy, ProxyType};
2
3impl Proxy {
4 pub fn common_construct(
5 proxy_type: ProxyType,
6 group: &str,
7 remark: &str,
8 server: &str,
9 port: u16,
10 udp: Option<bool>,
11 tfo: Option<bool>,
12 scv: Option<bool>,
13 tls13: Option<bool>,
14 underlying_proxy: &str,
15 ) -> Self {
16 Proxy {
17 proxy_type,
18 group: group.to_owned(),
19 remark: remark.to_owned(),
20 hostname: server.to_owned(),
21 port,
22 udp,
23 tcp_fast_open: tfo,
24 allow_insecure: scv,
25 tls13,
26 underlying_proxy: Some(underlying_proxy.to_owned()),
27 ..Default::default()
28 }
29 }
30
31 pub fn vmess_construct(
32 group: &str,
33 remark: &str,
34 add: &str,
35 port: u16,
36 typ: &str,
37 id: &str,
38 aid: u16,
39 net: &str,
40 cipher: &str,
41 path: &str,
42 host: &str,
43 edge: &str,
44 tls: &str,
45 sni: &str,
46 udp: Option<bool>,
47 tfo: Option<bool>,
48 scv: Option<bool>,
49 tls13: Option<bool>,
50 underlying_proxy: &str,
51 ) -> Self {
52 let mut proxy = Proxy::common_construct(
53 ProxyType::VMess,
54 group,
55 remark,
56 add,
57 port,
58 udp,
59 tfo,
60 scv,
61 tls13,
62 underlying_proxy,
63 );
64 proxy.user_id = if id.is_empty() {
65 Some("00000000-0000-0000-0000-000000000000".to_owned())
66 } else {
67 Some(id.to_owned())
68 };
69 proxy.alter_id = aid;
70 proxy.encrypt_method = if cipher.is_empty() {
71 None
72 } else {
73 Some(cipher.to_owned())
74 };
75 proxy.transfer_protocol = Some(if net.is_empty() { "tcp" } else { net }.to_owned());
76 proxy.edge = if edge.is_empty() {
77 None
78 } else {
79 Some(edge.to_owned())
80 };
81 proxy.server_name = if sni.is_empty() {
82 None
83 } else {
84 Some(sni.to_owned())
85 };
86 proxy.tls_secure = tls == "tls";
87
88 if net == "quic" {
89 proxy.quic_secure = Some(host.to_owned());
90 proxy.quic_secret = Some(path.to_owned());
91 } else {
92 proxy.host = Some(
93 if host.is_empty() && !add.parse::<std::net::IpAddr>().is_ok() {
94 add.to_owned()
95 } else {
96 host.trim().to_owned()
97 },
98 );
99 proxy.path = Some(if path.is_empty() { "/" } else { path.trim() }.to_owned());
100 }
101 proxy.fake_type = Some(typ.to_owned());
102
103 proxy
104 }
105
106 pub fn ssr_construct(
107 group: &str,
108 remark: &str,
109 server: &str,
110 port: u16,
111 protocol: &str,
112 method: &str,
113 obfs: &str,
114 password: &str,
115 obfs_param: &str,
116 proto_param: &str,
117 udp: Option<bool>,
118 tfo: Option<bool>,
119 scv: Option<bool>,
120 underlying_proxy: &str,
121 ) -> Self {
122 let mut proxy = Proxy::common_construct(
123 ProxyType::ShadowsocksR,
124 group,
125 remark,
126 server,
127 port,
128 udp,
129 tfo,
130 scv,
131 None,
132 underlying_proxy,
133 );
134 proxy.password = Some(password.to_owned());
135 proxy.encrypt_method = Some(method.to_owned());
136 proxy.protocol = Some(protocol.to_owned());
137 proxy.protocol_param = Some(proto_param.to_owned());
138 proxy.obfs = Some(obfs.to_owned());
139 proxy.obfs_param = Some(obfs_param.to_owned());
140
141 proxy
142 }
143
144 pub fn ss_construct(
145 group: &str,
146 remark: &str,
147 server: &str,
148 port: u16,
149 password: &str,
150 method: &str,
151 plugin: &str,
152 plugin_opts: &str,
153 udp: Option<bool>,
154 tfo: Option<bool>,
155 scv: Option<bool>,
156 tls13: Option<bool>,
157 underlying_proxy: &str,
158 ) -> Self {
159 let mut proxy = Proxy::common_construct(
160 ProxyType::Shadowsocks,
161 group,
162 remark,
163 server,
164 port,
165 udp,
166 tfo,
167 scv,
168 tls13,
169 underlying_proxy,
170 );
171
172 let ss_proxy = crate::models::proxy_node::shadowsocks::ShadowsocksProxy {
174 server: server.to_string(),
175 port,
176 password: password.to_string(),
177 cipher: method.to_string(),
178 udp,
179 tfo,
180 skip_cert_verify: scv,
181 plugin: if plugin.is_empty() {
182 None
183 } else {
184 Some(plugin.to_string())
185 },
186 plugin_opts: if plugin_opts.is_empty() {
187 None
188 } else {
189 Some(plugin_opts.to_string())
190 },
191 udp_over_tcp: None,
192 udp_over_tcp_version: None,
193 client_fingerprint: None,
194 };
195
196 proxy.combined_proxy =
197 Some(crate::models::proxy_node::combined::CombinedProxy::Shadowsocks(ss_proxy));
198
199 proxy.password = Some(password.to_owned());
201 proxy.encrypt_method = Some(method.to_owned());
202 proxy.plugin = Some(plugin.to_owned());
203 proxy.plugin_option = Some(plugin_opts.to_owned());
204
205 proxy
206 }
207
208 pub fn socks_construct(
209 group: &str,
210 remark: &str,
211 server: &str,
212 port: u16,
213 username: &str,
214 password: &str,
215 udp: Option<bool>,
216 tfo: Option<bool>,
217 scv: Option<bool>,
218 underlying_proxy: &str,
219 ) -> Self {
220 let mut proxy = Proxy::common_construct(
221 ProxyType::Socks5,
222 group,
223 remark,
224 server,
225 port,
226 udp,
227 tfo,
228 scv,
229 None,
230 underlying_proxy,
231 );
232 proxy.username = Some(username.to_owned());
233 proxy.password = Some(password.to_owned());
234
235 proxy
236 }
237
238 pub fn http_construct(
239 group: &str,
240 remark: &str,
241 server: &str,
242 port: u16,
243 username: &str,
244 password: &str,
245 tls: bool,
246 tfo: Option<bool>,
247 scv: Option<bool>,
248 tls13: Option<bool>,
249 underlying_proxy: &str,
250 ) -> Self {
251 let mut proxy = Proxy::common_construct(
252 if tls {
253 ProxyType::HTTPS
254 } else {
255 ProxyType::HTTP
256 },
257 group,
258 remark,
259 server,
260 port,
261 None,
262 tfo,
263 scv,
264 tls13,
265 underlying_proxy,
266 );
267 proxy.username = Some(username.to_owned());
268 proxy.password = Some(password.to_owned());
269 proxy.tls_secure = tls;
270
271 proxy
272 }
273
274 pub fn trojan_construct(
275 group: String,
276 remark: String,
277 hostname: String,
278 port: u16,
279 password: String,
280 network: Option<String>,
281 host: Option<String>,
282 path: Option<String>,
283 sni: Option<String>,
284 tls_secure: bool,
285 udp: Option<bool>,
286 tfo: Option<bool>,
287 allow_insecure: Option<bool>,
288 tls13: Option<bool>,
289 underlying_proxy: Option<String>,
290 ) -> Self {
291 Proxy {
292 proxy_type: ProxyType::Trojan,
293 group,
294 remark,
295 hostname,
296 port,
297 password: Some(password),
298 transfer_protocol: network,
299 host,
300 path,
301 sni,
302 tls_secure,
303 udp,
304 tcp_fast_open: tfo,
305 allow_insecure,
306 tls13,
307 underlying_proxy,
308 ..Default::default()
309 }
310 }
311
312 pub fn snell_construct(
313 group: String,
314 remark: String,
315 hostname: String,
316 port: u16,
317 password: String,
318 obfs: String,
319 host: String,
320 version: u16,
321 udp: Option<bool>,
322 tfo: Option<bool>,
323 allow_insecure: Option<bool>,
324 underlying_proxy: Option<String>,
325 ) -> Self {
326 Proxy {
327 proxy_type: ProxyType::Snell,
328 group,
329 remark,
330 hostname,
331 port,
332 password: Some(password),
333 obfs: Some(obfs),
334 host: Some(host),
335 snell_version: version,
336 udp,
337 tcp_fast_open: tfo,
338 allow_insecure,
339 underlying_proxy,
340 ..Default::default()
341 }
342 }
343
344 pub fn wireguard_construct(
345 group: String,
346 remark: String,
347 hostname: String,
348 port: u16,
349 self_ip: String,
350 self_ipv6: String,
351 private_key: String,
352 public_key: String,
353 preshared_key: String,
354 dns_servers: Vec<String>,
355 mtu: Option<u16>,
356 keep_alive: Option<u16>,
357 test_url: String,
358 client_id: String,
359 udp: Option<bool>,
360 underlying_proxy: Option<String>,
361 ) -> Self {
362 let mut dns_set = std::collections::HashSet::new();
363 for dns in dns_servers {
364 dns_set.insert(dns);
365 }
366
367 Proxy {
368 proxy_type: ProxyType::WireGuard,
369 group,
370 remark,
371 hostname,
372 port,
373 self_ip: Some(self_ip),
374 self_ipv6: Some(self_ipv6),
375 private_key: Some(private_key),
376 public_key: Some(public_key),
377 pre_shared_key: Some(preshared_key),
378 dns_servers: dns_set,
379 mtu: mtu.unwrap_or(0),
380 keep_alive: keep_alive.unwrap_or(0),
381 test_url: Some(test_url),
382 client_id: Some(client_id),
383 udp,
384 underlying_proxy,
385 ..Default::default()
386 }
387 }
388
389 pub fn hysteria2_construct(
390 group: String,
391 remark: String,
392 hostname: String,
393 port: u16,
394 ports: Option<String>,
395 up_speed: Option<u32>,
396 down_speed: Option<u32>,
397 password: String,
398 obfs: Option<String>,
399 obfs_param: Option<String>,
400 sni: Option<String>,
401 fingerprint: Option<String>,
402 alpn: Vec<String>,
403 ca: Option<String>,
404 ca_str: Option<String>,
405 cwnd: Option<u32>,
406 tcp_fast_open: Option<bool>,
407 allow_insecure: Option<bool>,
408 underlying_proxy: Option<String>,
409 ) -> Self {
410 let mut alpn_set = std::collections::HashSet::new();
411 for proto in alpn {
412 alpn_set.insert(proto);
413 }
414
415 Proxy {
416 proxy_type: ProxyType::Hysteria2,
417 group,
418 remark,
419 hostname,
420 port,
421 ports,
422 up_speed: up_speed.unwrap_or(0),
423 down_speed: down_speed.unwrap_or(0),
424 password: Some(password),
425 obfs: obfs,
426 obfs_param: obfs_param,
427 sni: sni,
428 fingerprint: fingerprint,
429 alpn: alpn_set,
430 ca: ca,
431 ca_str: ca_str,
432 cwnd: cwnd.unwrap_or(0),
433 tcp_fast_open,
434 allow_insecure,
435 underlying_proxy,
436 ..Default::default()
437 }
438 }
439
440 pub fn hysteria_construct(
441 group: String,
442 remark: String,
443 hostname: String,
444 port: u16,
445 ports: String,
446 protocol: String,
447 obfs_param: String,
448 up_speed: Option<u32>,
449 down_speed: Option<u32>,
450 auth_str: String,
451 obfs: String,
452 sni: String,
453 fingerprint: String,
454 ca: String,
455 ca_str: String,
456 recv_window_conn: Option<u32>,
457 recv_window: Option<u32>,
458 disable_mtu_discovery: Option<bool>,
459 hop_interval: Option<u32>,
460 alpn: Vec<String>,
461 tcp_fast_open: Option<bool>,
462 allow_insecure: Option<bool>,
463 underlying_proxy: Option<String>,
464 ) -> Self {
465 let mut alpn_set = std::collections::HashSet::new();
466 for proto in alpn {
467 alpn_set.insert(proto);
468 }
469
470 Proxy {
471 proxy_type: ProxyType::Hysteria,
472 group,
473 remark,
474 hostname,
475 port,
476 ports: Some(ports),
477 protocol: Some(protocol),
478 obfs_param: Some(obfs_param),
479 up_speed: up_speed.unwrap_or(0),
480 down_speed: down_speed.unwrap_or(0),
481 auth_str: Some(auth_str),
482 obfs: Some(obfs),
483 sni: Some(sni),
484 fingerprint: Some(fingerprint),
485 ca: Some(ca),
486 ca_str: Some(ca_str),
487 recv_window_conn: recv_window_conn.unwrap_or(0),
488 recv_window: recv_window.unwrap_or(0),
489 disable_mtu_discovery,
490 hop_interval: hop_interval.unwrap_or(0),
491 alpn: alpn_set,
492 tcp_fast_open,
493 allow_insecure,
494 underlying_proxy,
495 ..Default::default()
496 }
497 }
498}