-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathtunnel.rs
318 lines (287 loc) · 11.6 KB
/
tunnel.rs
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use std::{
future::Future,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
pin::Pin,
str::FromStr,
sync::Arc,
};
use tokio::sync::Mutex;
use mullvad_relay_selector::{RelaySelector, SelectedBridge, SelectedObfuscator, SelectedRelay};
use mullvad_types::{
endpoint::MullvadEndpoint, location::GeoIpLocation, relay_list::Relay, settings::TunnelOptions,
};
use once_cell::sync::Lazy;
use talpid_core::tunnel_state_machine::TunnelParametersGenerator;
use talpid_types::{
net::{wireguard, TunnelParameters},
tunnel::ParameterGenerationError,
ErrorExt,
};
#[cfg(not(target_os = "android"))]
use talpid_types::net::openvpn;
use crate::device::{AccountManagerHandle, PrivateAccountAndDevice};
/// The IP-addresses that the client uses when it connects to a server that supports the
/// "Same IP" functionality. This means all clients have the same in-tunnel IP on these
/// servers. This improves anonymity since the in-tunnel IP will not be unique to a specific
/// peer.
static SAME_IP_V4: Lazy<IpAddr> =
Lazy::new(|| Ipv4Addr::from_str("10.127.255.254").unwrap().into());
static SAME_IP_V6: Lazy<IpAddr> = Lazy::new(|| {
Ipv6Addr::from_str("fc00:bbbb:bbbb:bb01:ffff:ffff:ffff:ffff")
.unwrap()
.into()
});
#[derive(err_derive::Error, Debug)]
pub enum Error {
#[error(display = "Not logged in on a valid device")]
NoAuthDetails,
#[error(display = "No relay available")]
NoRelayAvailable,
#[error(display = "No bridge available")]
NoBridgeAvailable,
#[error(display = "Failed to resolve hostname for custom relay")]
ResolveCustomHostname,
}
#[derive(Clone)]
pub(crate) struct ParametersGenerator(Arc<Mutex<InnerParametersGenerator>>);
struct InnerParametersGenerator {
relay_selector: RelaySelector,
tunnel_options: TunnelOptions,
account_manager: AccountManagerHandle,
last_generated_relays: Option<LastSelectedRelays>,
}
impl ParametersGenerator {
/// Constructs a new tunnel parameters generator.
pub fn new(
account_manager: AccountManagerHandle,
relay_selector: RelaySelector,
tunnel_options: TunnelOptions,
) -> Self {
Self(Arc::new(Mutex::new(InnerParametersGenerator {
tunnel_options,
relay_selector,
account_manager,
last_generated_relays: None,
})))
}
/// Sets the tunnel options to use when generating new tunnel parameters.
pub async fn set_tunnel_options(&self, tunnel_options: &TunnelOptions) {
self.0.lock().await.tunnel_options = tunnel_options.clone();
}
/// Gets the location associated with the last generated tunnel parameters.
pub async fn get_last_location(&self) -> Option<GeoIpLocation> {
let inner = self.0.lock().await;
let relays = inner.last_generated_relays.as_ref()?;
let hostname;
let bridge_hostname;
let entry_hostname;
let obfuscator_hostname;
let location;
let take_hostname =
|relay: &Option<Relay>| relay.as_ref().map(|relay| relay.hostname.clone());
match relays {
LastSelectedRelays::WireGuard {
wg_entry: entry,
wg_exit: exit,
obfuscator,
} => {
entry_hostname = take_hostname(entry);
hostname = exit.hostname.clone();
obfuscator_hostname = take_hostname(obfuscator);
bridge_hostname = None;
location = exit.location.as_ref().cloned().unwrap();
}
#[cfg(not(target_os = "android"))]
LastSelectedRelays::OpenVpn { relay, bridge } => {
hostname = relay.hostname.clone();
bridge_hostname = take_hostname(bridge);
entry_hostname = None;
obfuscator_hostname = None;
location = relay.location.as_ref().cloned().unwrap();
}
};
Some(GeoIpLocation {
ipv4: None,
ipv6: None,
country: location.country,
city: Some(location.city),
latitude: location.latitude,
longitude: location.longitude,
mullvad_exit_ip: true,
hostname: Some(hostname),
bridge_hostname,
entry_hostname,
obfuscator_hostname,
})
}
}
impl InnerParametersGenerator {
async fn generate(&mut self, retry_attempt: u32) -> Result<TunnelParameters, Error> {
let _data = self.device().await?;
match self.relay_selector.get_relay(retry_attempt) {
Ok((SelectedRelay::Custom(custom_relay), _bridge, _obfsucator)) => {
self.last_generated_relays = None;
custom_relay
// TODO: generate proxy settings for custom tunnels
.to_tunnel_parameters(self.tunnel_options.clone(), None)
.map_err(|e| {
log::error!("Failed to resolve hostname for custom tunnel config: {}", e);
Error::ResolveCustomHostname
})
}
Ok((SelectedRelay::Normal(constraints), bridge, obfuscator)) => {
self.create_tunnel_parameters(
&constraints.exit_relay,
&constraints.entry_relay,
constraints.endpoint,
bridge,
obfuscator,
)
.await
}
Err(mullvad_relay_selector::Error::NoBridge) => Err(Error::NoBridgeAvailable),
Err(_error) => Err(Error::NoRelayAvailable),
}
}
#[cfg_attr(target_os = "android", allow(unused_variables))]
async fn create_tunnel_parameters(
&mut self,
relay: &Relay,
entry_relay: &Option<Relay>,
endpoint: MullvadEndpoint,
bridge: Option<SelectedBridge>,
obfuscator: Option<SelectedObfuscator>,
) -> Result<TunnelParameters, Error> {
let data = self.device().await?;
match endpoint {
#[cfg(not(target_os = "android"))]
MullvadEndpoint::OpenVpn(endpoint) => {
let (bridge_settings, bridge_relay) = match bridge {
Some(SelectedBridge::Normal(bridge)) => {
(Some(bridge.settings), Some(bridge.relay))
}
Some(SelectedBridge::Custom(settings)) => (Some(settings), None),
None => (None, None),
};
self.last_generated_relays = Some(LastSelectedRelays::OpenVpn {
relay: relay.clone(),
bridge: bridge_relay,
});
Ok(openvpn::TunnelParameters {
config: openvpn::ConnectionConfig::new(
endpoint,
data.account_token,
"-".to_string(),
),
options: self.tunnel_options.openvpn.clone(),
generic_options: self.tunnel_options.generic.clone(),
proxy: bridge_settings,
#[cfg(target_os = "linux")]
fwmark: mullvad_types::TUNNEL_FWMARK,
}
.into())
}
#[cfg(target_os = "android")]
MullvadEndpoint::OpenVpn(endpoint) => {
unreachable!("OpenVPN is not supported on Android");
}
MullvadEndpoint::Wireguard(endpoint) => {
let tunnel_ipv4 = data.device.wg_data.addresses.ipv4_address.ip();
let tunnel_ipv6 = data.device.wg_data.addresses.ipv6_address.ip();
let tunnel = wireguard::TunnelConfig {
private_key: data.device.wg_data.private_key,
addresses: vec![IpAddr::from(tunnel_ipv4), IpAddr::from(tunnel_ipv6)],
};
// FIXME: Used for debugging purposes during the migration to same IP. Remove when
// the migration is over.
if tunnel_ipv4 == *SAME_IP_V4 || tunnel_ipv6 == *SAME_IP_V6 {
log::debug!("Same IP is being used");
} else {
log::debug!("Same IP is NOT being used");
}
let (obfuscator_relay, obfuscator_config) = match obfuscator {
Some(obfuscator) => (Some(obfuscator.relay), Some(obfuscator.config)),
None => (None, None),
};
self.last_generated_relays = Some(LastSelectedRelays::WireGuard {
wg_entry: entry_relay.clone(),
wg_exit: relay.clone(),
obfuscator: obfuscator_relay,
});
Ok(wireguard::TunnelParameters {
connection: wireguard::ConnectionConfig {
tunnel,
peer: endpoint.peer,
exit_peer: endpoint.exit_peer,
ipv4_gateway: endpoint.ipv4_gateway,
ipv6_gateway: Some(endpoint.ipv6_gateway),
#[cfg(target_os = "linux")]
fwmark: Some(mullvad_types::TUNNEL_FWMARK),
},
options: self
.tunnel_options
.wireguard
.clone()
.into_talpid_tunnel_options(),
generic_options: self.tunnel_options.generic.clone(),
obfuscation: obfuscator_config,
}
.into())
}
}
}
async fn device(&self) -> Result<PrivateAccountAndDevice, Error> {
self.account_manager
.data()
.await
.map(|s| s.into_device())
.ok()
.flatten()
.ok_or(Error::NoAuthDetails)
}
}
impl TunnelParametersGenerator for ParametersGenerator {
fn generate(
&mut self,
retry_attempt: u32,
) -> Pin<Box<dyn Future<Output = Result<TunnelParameters, ParameterGenerationError>>>> {
let generator = self.0.clone();
Box::pin(async move {
let mut inner = generator.lock().await;
inner
.generate(retry_attempt)
.await
.map_err(|error| match error {
Error::NoBridgeAvailable => ParameterGenerationError::NoMatchingBridgeRelay,
Error::ResolveCustomHostname => {
ParameterGenerationError::CustomTunnelHostResultionError
}
error => {
log::error!(
"{}",
error.display_chain_with_msg("Failed to generate tunnel parameters")
);
ParameterGenerationError::NoMatchingRelay
}
})
})
}
}
/// Contains all relays that were selected last time when tunnel parameters were generated.
enum LastSelectedRelays {
/// Represents all relays generated for a WireGuard tunnel.
/// The traffic flow can look like this:
/// client -> obfuscator -> entry -> exit -> internet
/// But for most users, it will look like this:
/// client -> entry -> internet
WireGuard {
wg_entry: Option<Relay>,
wg_exit: Relay,
obfuscator: Option<Relay>,
},
/// Represents all relays generated for an OpenVPN tunnel.
/// The traffic flows like this:
/// client -> bridge -> relay -> internet
#[cfg(not(target_os = "android"))]
OpenVpn { relay: Relay, bridge: Option<Relay> },
}