tproxy_config/
tproxy_args.rs

1use std::net::{IpAddr, SocketAddr};
2
3use crate::{IpCidr, PROXY_ADDR, SOCKET_FWMARK_TABLE, TUN_DNS, TUN_GATEWAY, TUN_IPV4, TUN_MTU, TUN_NAME, TUN_NETMASK};
4
5#[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Deserialize, serde::Serialize)]
6pub struct TproxyArgs {
7    pub tun_ip: IpAddr,
8    pub tun_netmask: IpAddr,
9    pub tun_gateway: IpAddr,
10    pub tun_dns: IpAddr,
11    pub tun_mtu: u16,
12    pub tun_name: String,
13    pub proxy_addr: SocketAddr,
14    pub bypass_ips: Vec<IpCidr>,
15    pub ipv4_default_route: bool,
16    pub ipv6_default_route: bool,
17    pub gateway_mode: bool,
18    pub socket_fwmark: Option<u32>,
19    pub socket_fwmark_table: String,
20}
21
22impl Default for TproxyArgs {
23    fn default() -> Self {
24        Self {
25            tun_ip: TUN_IPV4,
26            tun_netmask: TUN_NETMASK,
27            tun_gateway: TUN_GATEWAY,
28            tun_dns: TUN_DNS,
29            tun_mtu: TUN_MTU,
30            tun_name: TUN_NAME.to_string(),
31            proxy_addr: PROXY_ADDR,
32            bypass_ips: vec![],
33            ipv4_default_route: true,
34            ipv6_default_route: false,
35            gateway_mode: false,
36            socket_fwmark: None,
37            socket_fwmark_table: SOCKET_FWMARK_TABLE.to_string(),
38        }
39    }
40}
41
42impl TproxyArgs {
43    pub fn new() -> Self {
44        Self::default()
45    }
46
47    pub fn tun_ip(mut self, tun_ip: IpAddr) -> Self {
48        self.tun_ip = tun_ip;
49        self
50    }
51
52    pub fn tun_netmask(mut self, tun_netmask: IpAddr) -> Self {
53        self.tun_netmask = tun_netmask;
54        self
55    }
56
57    pub fn tun_gateway(mut self, tun_gateway: IpAddr) -> Self {
58        self.tun_gateway = tun_gateway;
59        self
60    }
61
62    pub fn tun_dns(mut self, tun_dns: IpAddr) -> Self {
63        self.tun_dns = tun_dns;
64        self
65    }
66
67    pub fn tun_mtu(mut self, tun_mtu: u16) -> Self {
68        self.tun_mtu = tun_mtu;
69        self
70    }
71
72    pub fn tun_name(mut self, tun_name: &str) -> Self {
73        tun_name.clone_into(&mut self.tun_name);
74        self
75    }
76
77    pub fn proxy_addr(mut self, proxy_addr: SocketAddr) -> Self {
78        self.proxy_addr = proxy_addr;
79        self
80    }
81
82    pub fn bypass_ips(mut self, bypass_ips: &[IpCidr]) -> Self {
83        self.bypass_ips = bypass_ips.to_vec();
84        self
85    }
86
87    pub fn ipv6_default_route(mut self, enabled: bool) -> Self {
88        self.ipv6_default_route = enabled;
89        self
90    }
91
92    pub fn ipv4_default_route(mut self, enabled: bool) -> Self {
93        self.ipv6_default_route = enabled;
94        self
95    }
96
97    pub fn gateway_mode(mut self, gateway_mode: bool) -> Self {
98        self.gateway_mode = gateway_mode;
99        self
100    }
101
102    pub fn socket_fwmark(mut self, socket_fwmark: Option<u32>) -> Self {
103        self.socket_fwmark = socket_fwmark;
104        self
105    }
106
107    pub fn socket_fwmark_table(mut self, socket_fwmark_table: &str) -> Self {
108        self.socket_fwmark_table = socket_fwmark_table.to_string();
109        self
110    }
111}