1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use super::*;
use crate::error::*;
use crate::mdns::*;
use crate::network_type::*;
use crate::udp_network::UDPNetwork;
use crate::url::*;
use util::vnet::net::*;
use std::time::Duration;
pub(crate) const DEFAULT_CHECK_INTERVAL: Duration = Duration::from_millis(200);
pub(crate) const DEFAULT_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(2);
pub(crate) const DEFAULT_DISCONNECTED_TIMEOUT: Duration = Duration::from_secs(5);
pub(crate) const DEFAULT_FAILED_TIMEOUT: Duration = Duration::from_secs(25);
pub(crate) const DEFAULT_HOST_ACCEPTANCE_MIN_WAIT: Duration = Duration::from_secs(0);
pub(crate) const DEFAULT_SRFLX_ACCEPTANCE_MIN_WAIT: Duration = Duration::from_millis(500);
pub(crate) const DEFAULT_PRFLX_ACCEPTANCE_MIN_WAIT: Duration = Duration::from_millis(1000);
pub(crate) const DEFAULT_RELAY_ACCEPTANCE_MIN_WAIT: Duration = Duration::from_millis(2000);
pub(crate) const DEFAULT_MAX_BINDING_REQUESTS: u16 = 7;
pub(crate) const MAX_BUFFER_SIZE: usize = 1000 * 1000;
pub(crate) const MAX_BINDING_REQUEST_TIMEOUT: Duration = Duration::from_millis(4000);
pub(crate) fn default_candidate_types() -> Vec<CandidateType> {
vec![
CandidateType::Host,
CandidateType::ServerReflexive,
CandidateType::Relay,
]
}
pub type InterfaceFilterFn = Box<dyn (Fn(&str) -> bool) + Send + Sync>;
#[derive(Default)]
pub struct AgentConfig {
pub urls: Vec<Url>,
pub udp_network: UDPNetwork,
pub local_ufrag: String,
pub local_pwd: String,
pub multicast_dns_mode: MulticastDnsMode,
pub multicast_dns_host_name: String,
pub multicast_dns_dest_addr: String,
pub disconnected_timeout: Option<Duration>,
pub failed_timeout: Option<Duration>,
pub keepalive_interval: Option<Duration>,
pub network_types: Vec<NetworkType>,
pub candidate_types: Vec<CandidateType>,
pub check_interval: Duration,
pub max_binding_requests: Option<u16>,
pub is_controlling: bool,
pub lite: bool,
pub nat_1to1_ip_candidate_type: CandidateType,
pub nat_1to1_ips: Vec<String>,
pub host_acceptance_min_wait: Option<Duration>,
pub srflx_acceptance_min_wait: Option<Duration>,
pub prflx_acceptance_min_wait: Option<Duration>,
pub relay_acceptance_min_wait: Option<Duration>,
pub net: Option<Arc<Net>>,
pub interface_filter: Arc<Option<InterfaceFilterFn>>,
pub insecure_skip_verify: bool,
}
impl AgentConfig {
pub(crate) fn init_with_defaults(&self, a: &mut AgentInternal) {
if let Some(max_binding_requests) = self.max_binding_requests {
a.max_binding_requests = max_binding_requests;
} else {
a.max_binding_requests = DEFAULT_MAX_BINDING_REQUESTS;
}
if let Some(host_acceptance_min_wait) = self.host_acceptance_min_wait {
a.host_acceptance_min_wait = host_acceptance_min_wait;
} else {
a.host_acceptance_min_wait = DEFAULT_HOST_ACCEPTANCE_MIN_WAIT;
}
if let Some(srflx_acceptance_min_wait) = self.srflx_acceptance_min_wait {
a.srflx_acceptance_min_wait = srflx_acceptance_min_wait;
} else {
a.srflx_acceptance_min_wait = DEFAULT_SRFLX_ACCEPTANCE_MIN_WAIT;
}
if let Some(prflx_acceptance_min_wait) = self.prflx_acceptance_min_wait {
a.prflx_acceptance_min_wait = prflx_acceptance_min_wait;
} else {
a.prflx_acceptance_min_wait = DEFAULT_PRFLX_ACCEPTANCE_MIN_WAIT;
}
if let Some(relay_acceptance_min_wait) = self.relay_acceptance_min_wait {
a.relay_acceptance_min_wait = relay_acceptance_min_wait;
} else {
a.relay_acceptance_min_wait = DEFAULT_RELAY_ACCEPTANCE_MIN_WAIT;
}
if let Some(disconnected_timeout) = self.disconnected_timeout {
a.disconnected_timeout = disconnected_timeout;
} else {
a.disconnected_timeout = DEFAULT_DISCONNECTED_TIMEOUT;
}
if let Some(failed_timeout) = self.failed_timeout {
a.failed_timeout = failed_timeout;
} else {
a.failed_timeout = DEFAULT_FAILED_TIMEOUT;
}
if let Some(keepalive_interval) = self.keepalive_interval {
a.keepalive_interval = keepalive_interval;
} else {
a.keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
}
if self.check_interval == Duration::from_secs(0) {
a.check_interval = DEFAULT_CHECK_INTERVAL;
} else {
a.check_interval = self.check_interval;
}
}
pub(crate) fn init_ext_ip_mapping(
&self,
mdns_mode: MulticastDnsMode,
candidate_types: &[CandidateType],
) -> Result<Option<ExternalIpMapper>> {
if let Some(ext_ip_mapper) =
ExternalIpMapper::new(self.nat_1to1_ip_candidate_type, &self.nat_1to1_ips)?
{
if ext_ip_mapper.candidate_type == CandidateType::Host {
if mdns_mode == MulticastDnsMode::QueryAndGather {
return Err(Error::ErrMulticastDnsWithNat1to1IpMapping);
}
let mut candi_host_enabled = false;
for candi_type in candidate_types {
if *candi_type == CandidateType::Host {
candi_host_enabled = true;
break;
}
}
if !candi_host_enabled {
return Err(Error::ErrIneffectiveNat1to1IpMappingHost);
}
} else if ext_ip_mapper.candidate_type == CandidateType::ServerReflexive {
let mut candi_srflx_enabled = false;
for candi_type in candidate_types {
if *candi_type == CandidateType::ServerReflexive {
candi_srflx_enabled = true;
break;
}
}
if !candi_srflx_enabled {
return Err(Error::ErrIneffectiveNat1to1IpMappingSrflx);
}
}
Ok(Some(ext_ip_mapper))
} else {
Ok(None)
}
}
}