1use std::io;
2use std::time::Duration;
3
4use crate::socket::LocalInterface;
5use crate::tunnel::recycle::RecycleBuf;
6use crate::tunnel::tcp::{BytesInitCodec, InitCodec};
7use crate::tunnel::udp::Model;
8
9pub(crate) const MAX_SYMMETRIC_SOCKET_COUNT: usize = 200;
10pub(crate) const MAX_MAIN_SOCKET_COUNT: usize = 10;
11pub(crate) const ROUTE_IDLE_TIME: Duration = Duration::from_secs(10);
12
13#[derive(Clone, Copy, Eq, PartialEq, Debug, Default)]
14pub enum LoadBalance {
15 #[default]
17 MinHopLowestLatency,
18 RoundRobin,
20 MostRecent,
22 LowestLatency,
24}
25#[derive(Clone)]
26pub struct TunnelConfig {
27 pub major_socket_count: usize,
28 pub udp_tunnel_config: Option<UdpTunnelConfig>,
29 pub tcp_tunnel_config: Option<TcpTunnelConfig>,
30}
31
32impl Default for TunnelConfig {
33 fn default() -> Self {
34 Self {
35 major_socket_count: MAX_MAJOR_SOCKET_COUNT,
36 udp_tunnel_config: Some(Default::default()),
37 tcp_tunnel_config: Some(Default::default()),
38 }
39 }
40}
41
42pub(crate) const MAX_MAJOR_SOCKET_COUNT: usize = 2;
43pub(crate) const MAX_UDP_SUB_SOCKET_COUNT: usize = 82;
44
45impl TunnelConfig {
46 pub fn new(tcp_init_codec: Box<dyn InitCodec>) -> TunnelConfig {
47 let udp_tunnel_config = Some(UdpTunnelConfig::default());
48 let tcp_tunnel_config = Some(TcpTunnelConfig::new(tcp_init_codec));
49 Self {
50 major_socket_count: MAX_MAJOR_SOCKET_COUNT,
51 udp_tunnel_config,
52 tcp_tunnel_config,
53 }
54 }
55}
56impl TunnelConfig {
57 pub fn none_tcp(self) -> Self {
58 self
59 }
60}
61impl TunnelConfig {
62 pub fn empty() -> Self {
63 Self {
64 major_socket_count: MAX_MAJOR_SOCKET_COUNT,
65 udp_tunnel_config: None,
66 tcp_tunnel_config: None,
67 }
68 }
69
70 pub fn set_tcp_multi_count(mut self, count: usize) -> Self {
71 self.major_socket_count = count;
72 self
73 }
74
75 pub fn set_udp_tunnel_config(mut self, config: UdpTunnelConfig) -> Self {
76 self.udp_tunnel_config.replace(config);
77 self
78 }
79 pub fn set_tcp_tunnel_config(mut self, config: TcpTunnelConfig) -> Self {
80 self.tcp_tunnel_config.replace(config);
81 self
82 }
83 pub fn check(&self) -> io::Result<()> {
84 if let Some(udp_tunnel_config) = self.udp_tunnel_config.as_ref() {
85 udp_tunnel_config.check()?;
86 }
87 if let Some(tcp_tunnel_config) = self.tcp_tunnel_config.as_ref() {
88 tcp_tunnel_config.check()?;
89 }
90 Ok(())
91 }
92}
93
94#[derive(Clone)]
95pub struct TcpTunnelConfig {
96 pub route_idle_time: Duration,
97 pub tcp_multiplexing_limit: usize,
98 pub default_interface: Option<LocalInterface>,
99 pub tcp_port: u16,
100 pub use_v6: bool,
101 pub init_codec: Box<dyn InitCodec>,
102 pub recycle_buf: Option<RecycleBuf>,
103}
104
105impl Default for TcpTunnelConfig {
106 fn default() -> Self {
107 Self {
108 route_idle_time: ROUTE_IDLE_TIME,
109 tcp_multiplexing_limit: MAX_MAJOR_SOCKET_COUNT,
110 default_interface: None,
111 tcp_port: 0,
112 use_v6: true,
113 init_codec: Box::new(BytesInitCodec),
114 recycle_buf: None,
115 }
116 }
117}
118
119impl TcpTunnelConfig {
120 pub fn new(init_codec: Box<dyn InitCodec>) -> TcpTunnelConfig {
121 Self {
122 route_idle_time: ROUTE_IDLE_TIME,
123 tcp_multiplexing_limit: MAX_MAJOR_SOCKET_COUNT,
124 default_interface: None,
125 tcp_port: 0,
126 use_v6: true,
127 init_codec,
128 recycle_buf: None,
129 }
130 }
131 pub fn check(&self) -> io::Result<()> {
132 if self.tcp_multiplexing_limit == 0 {
133 return Err(io::Error::new(
134 io::ErrorKind::Other,
135 "tcp_multiplexing_limit cannot be 0",
136 ));
137 }
138 if self.tcp_multiplexing_limit > MAX_MAIN_SOCKET_COUNT {
139 return Err(io::Error::new(
140 io::ErrorKind::Other,
141 "tcp_multiplexing_limit cannot too large",
142 ));
143 }
144 if self.use_v6 {
145 socket2::Socket::new(socket2::Domain::IPV6, socket2::Type::STREAM, None)?;
146 }
147 Ok(())
148 }
149 pub fn set_tcp_multiplexing_limit(mut self, tcp_multiplexing_limit: usize) -> Self {
150 self.tcp_multiplexing_limit = tcp_multiplexing_limit;
151 self
152 }
153 pub fn set_route_idle_time(mut self, route_idle_time: Duration) -> Self {
154 self.route_idle_time = route_idle_time;
155 self
156 }
157 pub fn set_default_interface(mut self, default_interface: LocalInterface) -> Self {
158 self.default_interface = Some(default_interface.clone());
159 self
160 }
161 pub fn set_tcp_port(mut self, tcp_port: u16) -> Self {
162 self.tcp_port = tcp_port;
163 self
164 }
165 pub fn set_use_v6(mut self, use_v6: bool) -> Self {
166 self.use_v6 = use_v6;
167 self
168 }
169}
170
171#[derive(Clone)]
172pub struct UdpTunnelConfig {
173 pub main_udp_count: usize,
174 pub sub_udp_count: usize,
175 pub model: Model,
176 pub default_interface: Option<LocalInterface>,
177 pub udp_ports: Vec<u16>,
178 pub use_v6: bool,
179 pub recycle_buf: Option<RecycleBuf>,
180}
181
182impl Default for UdpTunnelConfig {
183 fn default() -> Self {
184 Self {
185 main_udp_count: MAX_MAJOR_SOCKET_COUNT,
186 sub_udp_count: MAX_UDP_SUB_SOCKET_COUNT,
187 model: Model::Low,
188 default_interface: None,
189 udp_ports: vec![0, 0],
190 use_v6: true,
191 recycle_buf: None,
192 }
193 }
194}
195
196impl UdpTunnelConfig {
197 pub fn check(&self) -> io::Result<()> {
198 if self.main_udp_count == 0 {
199 return Err(io::Error::new(
200 io::ErrorKind::Other,
201 "main socket count cannot be 0",
202 ));
203 }
204 if self.main_udp_count > MAX_MAIN_SOCKET_COUNT {
205 return Err(io::Error::new(
206 io::ErrorKind::Other,
207 "main socket count is too large",
208 ));
209 }
210 if self.sub_udp_count > MAX_SYMMETRIC_SOCKET_COUNT {
211 return Err(io::Error::new(
212 io::ErrorKind::Other,
213 "socket count for symmetric nat is too large",
214 ));
215 }
216 if self.use_v6 {
217 socket2::Socket::new(socket2::Domain::IPV6, socket2::Type::DGRAM, None)?;
218 }
219 Ok(())
220 }
221 pub fn set_main_udp_count(mut self, count: usize) -> Self {
222 self.main_udp_count = count;
223 self
224 }
225 pub fn set_sub_udp_count(mut self, count: usize) -> Self {
226 self.sub_udp_count = count;
227 self
228 }
229 pub fn set_model(mut self, model: Model) -> Self {
230 self.model = model;
231 self
232 }
233 pub fn set_default_interface(mut self, default_interface: LocalInterface) -> Self {
234 self.default_interface = Some(default_interface.clone());
235 self
236 }
237 pub fn set_udp_ports(mut self, udp_ports: Vec<u16>) -> Self {
238 self.udp_ports = udp_ports;
239 self
240 }
241 pub fn set_simple_udp_port(mut self, udp_port: u16) -> Self {
242 self.udp_ports = vec![udp_port];
243 self
244 }
245 pub fn set_use_v6(mut self, use_v6: bool) -> Self {
246 self.use_v6 = use_v6;
247 self
248 }
249}