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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
//! A module dedicated to retrieving the relay list from the Mullvad API.
use crate::rest;
use hyper::{StatusCode, body::Incoming, header};
use mullvad_types::{location, relay_list};
use talpid_types::net::wireguard;
use vec1::Vec1;
use std::{
collections::{BTreeMap, HashSet},
future::Future,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
ops::RangeInclusive,
time::Duration,
};
/// Fetches relay list from <https://api.mullvad.net/app/v1/relays>
#[derive(Clone)]
pub struct RelayListProxy {
handle: rest::MullvadRestHandle,
}
const RELAY_LIST_TIMEOUT: Duration = Duration::from_secs(15);
impl RelayListProxy {
/// Construct a new relay list rest client
pub fn new(handle: rest::MullvadRestHandle) -> Self {
Self { handle }
}
/// Fetch the relay list
pub fn relay_list(
&self,
etag: Option<String>,
) -> impl Future<Output = Result<Option<relay_list::RelayList>, rest::Error>> {
let request = self.relay_list_response(etag.clone());
async move {
let response = request.await?;
if etag.is_some() && response.status() == StatusCode::NOT_MODIFIED {
return Ok(None);
}
let etag = Self::extract_etag(&response);
let relay_list: ServerRelayList = response.deserialize().await?;
Ok(Some(relay_list.into_relay_list(etag)))
}
}
pub fn relay_list_response(
&self,
etag: Option<String>,
) -> impl Future<Output = Result<rest::Response<Incoming>, rest::Error>> {
let service = self.handle.service.clone();
let request = self.handle.factory.get("app/v1/relays");
async move {
let mut request = request?
.timeout(RELAY_LIST_TIMEOUT)
.expected_status(&[StatusCode::NOT_MODIFIED, StatusCode::OK]);
if let Some(ref tag) = etag {
request = request.header(header::IF_NONE_MATCH, tag)?;
}
let response = service.request(request).await?;
Ok(response)
}
}
pub fn extract_etag(response: &rest::Response<Incoming>) -> Option<String> {
response
.headers()
.get(header::ETAG)
.and_then(|tag| match tag.to_str() {
Ok(tag) => Some(tag.to_string()),
Err(_) => {
log::error!("Ignoring invalid tag from server: {:?}", tag.as_bytes());
None
}
})
}
}
#[derive(Debug, serde::Deserialize)]
struct ServerRelayList {
locations: BTreeMap<String, Location>,
openvpn: OpenVpn,
wireguard: Wireguard,
bridge: Bridges,
}
impl ServerRelayList {
fn into_relay_list(self, etag: Option<String>) -> relay_list::RelayList {
let mut countries = BTreeMap::new();
let Self {
locations,
openvpn,
wireguard,
bridge,
} = self;
for (code, location) in locations.into_iter() {
match split_location_code(&code) {
Some((country_code, city_code)) => {
let country_code = country_code.to_lowercase();
let city_code = city_code.to_lowercase();
let country = countries
.entry(country_code.clone())
.or_insert_with(|| location_to_country(&location, country_code));
country.cities.push(location_to_city(&location, city_code));
}
None => {
log::error!("Bad location code:{}", code);
continue;
}
}
}
relay_list::RelayList {
etag: etag.map(|mut tag| {
if tag.starts_with('"') {
tag.insert_str(0, "W/");
}
tag
}),
openvpn: openvpn.extract_relays(&mut countries),
wireguard: wireguard.extract_relays(&mut countries),
bridge: bridge.extract_relays(&mut countries),
countries: countries.into_values().collect(),
}
}
}
/// Splits a location code into a country code and a city code. The input is expected to be in a
/// format like `se-mma`, with `se` being the country code, `mma` being the city code.
fn split_location_code(location: &str) -> Option<(&str, &str)> {
let mut parts = location.split('-');
let country = parts.next()?;
let city = parts.next()?;
Some((country, city))
}
fn location_to_country(location: &Location, code: String) -> relay_list::RelayListCountry {
relay_list::RelayListCountry {
cities: vec![],
name: location.country.clone(),
code,
}
}
fn location_to_city(location: &Location, code: String) -> relay_list::RelayListCity {
relay_list::RelayListCity {
name: location.city.clone(),
code,
latitude: location.latitude,
longitude: location.longitude,
relays: vec![],
}
}
fn into_mullvad_relay(
relay: Relay,
location: location::Location,
endpoint_data: relay_list::RelayEndpointData,
) -> relay_list::Relay {
relay_list::Relay {
hostname: relay.hostname,
ipv4_addr_in: relay.ipv4_addr_in,
ipv6_addr_in: relay.ipv6_addr_in,
overridden_ipv4: false,
overridden_ipv6: false,
include_in_country: relay.include_in_country,
active: relay.active,
owned: relay.owned,
provider: relay.provider,
weight: relay.weight,
endpoint_data,
location,
}
}
#[derive(Debug, serde::Deserialize)]
struct Location {
city: String,
country: String,
latitude: f64,
longitude: f64,
}
#[derive(Debug, serde::Deserialize)]
struct OpenVpn {
#[serde(flatten)]
ports: relay_list::OpenVpnEndpointData,
relays: Vec<Relay>,
}
impl OpenVpn {
/// Consumes `self` and appends all its relays to `countries`.
fn extract_relays(
self,
countries: &mut BTreeMap<String, relay_list::RelayListCountry>,
) -> relay_list::OpenVpnEndpointData {
for mut openvpn_relay in self.relays.into_iter() {
openvpn_relay.convert_to_lowercase();
if let Some((country_code, city_code)) = split_location_code(&openvpn_relay.location)
&& let Some(country) = countries.get_mut(country_code)
&& let Some(city) = country
.cities
.iter_mut()
.find(|city| city.code == city_code)
{
let location = location::Location {
country: country.name.clone(),
country_code: country.code.clone(),
city: city.name.clone(),
city_code: city.code.clone(),
latitude: city.latitude,
longitude: city.longitude,
};
let relay = openvpn_relay.into_openvpn_mullvad_relay(location);
city.relays.push(relay);
};
}
self.ports
}
}
#[derive(Debug, serde::Deserialize)]
struct Relay {
hostname: String,
active: bool,
owned: bool,
location: String,
provider: String,
ipv4_addr_in: Ipv4Addr,
ipv6_addr_in: Option<Ipv6Addr>,
weight: u64,
include_in_country: bool,
}
impl Relay {
fn into_openvpn_mullvad_relay(self, location: location::Location) -> relay_list::Relay {
into_mullvad_relay(self, location, relay_list::RelayEndpointData::Openvpn)
}
fn into_bridge_mullvad_relay(self, location: location::Location) -> relay_list::Relay {
into_mullvad_relay(self, location, relay_list::RelayEndpointData::Bridge)
}
fn convert_to_lowercase(&mut self) {
self.hostname = self.hostname.to_lowercase();
self.location = self.location.to_lowercase();
}
}
#[derive(Debug, serde::Deserialize)]
struct Wireguard {
port_ranges: Vec<(u16, u16)>,
ipv4_gateway: Ipv4Addr,
ipv6_gateway: Ipv6Addr,
/// Shadowsocks port ranges available on all WireGuard relays
#[serde(default)]
shadowsocks_port_ranges: Vec<(u16, u16)>,
relays: Vec<WireGuardRelay>,
}
impl From<&Wireguard> for relay_list::WireguardEndpointData {
fn from(wg: &Wireguard) -> Self {
Self {
port_ranges: inclusive_range_from_pair_set(wg.port_ranges.clone()).collect(),
ipv4_gateway: wg.ipv4_gateway,
ipv6_gateway: wg.ipv6_gateway,
shadowsocks_port_ranges: inclusive_range_from_pair_set(
wg.shadowsocks_port_ranges.clone(),
)
.collect(),
udp2tcp_ports: vec![],
}
}
}
fn inclusive_range_from_pair_set<T>(
set: impl IntoIterator<Item = (T, T)>,
) -> impl Iterator<Item = RangeInclusive<T>> {
set.into_iter().map(inclusive_range_from_pair)
}
fn inclusive_range_from_pair<T>(pair: (T, T)) -> RangeInclusive<T> {
RangeInclusive::new(pair.0, pair.1)
}
impl Wireguard {
/// Consumes `self` and appends all its relays to `countries`.
fn extract_relays(
self,
countries: &mut BTreeMap<String, relay_list::RelayListCountry>,
) -> relay_list::WireguardEndpointData {
let endpoint_data = relay_list::WireguardEndpointData::from(&self);
let relays = self.relays;
for mut wireguard_relay in relays {
wireguard_relay.relay.convert_to_lowercase();
if let Some((country_code, city_code)) =
split_location_code(&wireguard_relay.relay.location)
&& let Some(country) = countries.get_mut(country_code)
&& let Some(city) = country
.cities
.iter_mut()
.find(|city| city.code == city_code)
{
let location = location::Location {
country: country.name.clone(),
country_code: country.code.clone(),
city: city.name.clone(),
city_code: city.code.clone(),
latitude: city.latitude,
longitude: city.longitude,
};
let relay = wireguard_relay.into_mullvad_relay(location);
city.relays.push(relay);
};
}
endpoint_data
}
}
#[derive(Debug, serde::Deserialize)]
struct WireGuardRelay {
#[serde(flatten)]
relay: Relay,
public_key: wireguard::PublicKey,
#[serde(default)]
daita: bool,
#[serde(default)]
shadowsocks_extra_addr_in: Vec<IpAddr>,
#[serde(default)]
features: Features,
}
impl WireGuardRelay {
fn into_mullvad_relay(self, location: location::Location) -> relay_list::Relay {
// Sanity check that new 'features' key is in sync with the old, superceded keys.
// TODO: Remove `self.daita` (and this check 👇) when `features` key has been completely
// rolled out to production.
if self.features.daita.is_some() {
debug_assert!(self.daita)
}
let relay = self.relay;
let endpoint_data =
relay_list::RelayEndpointData::Wireguard(relay_list::WireguardRelayEndpointData {
public_key: self.public_key,
// FIXME: This hack is forward-compatible with 'features' being rolled out.
// Should unwrap to 'false' once 'daita' field is removed.
daita: self.features.daita.map(|_| true).unwrap_or(self.daita),
shadowsocks_extra_addr_in: HashSet::from_iter(self.shadowsocks_extra_addr_in),
quic: self.features.quic.map(relay_list::Quic::from),
lwo: self.features.lwo.is_some(),
});
into_mullvad_relay(relay, location, endpoint_data)
}
}
/// Extra features enabled on some (Wireguard) relay, such as obfuscation daemons or Daita.
#[derive(Debug, Default, Clone, serde::Deserialize)]
struct Features {
daita: Option<Daita>,
quic: Option<Quic>,
lwo: Option<Lwo>,
}
/// DAITA doesn't have any configuration options (exposed by the API).
///
/// Note, an empty struct is not the same as an empty tuple struct according to serde_json!
#[derive(Debug, Clone, serde::Deserialize)]
struct Daita {}
/// Parameters for setting up a QUIC obfuscator (connecting to a masque-proxy running on a relay).
#[derive(Debug, Clone, serde::Deserialize)]
struct Quic {
/// In-addresses for the QUIC obfuscator.
///
/// # Note
///
/// This set must be non-empty.
///
/// The primary IPs of the relay will be included if and only if they are listed here.
addr_in: Vec1<IpAddr>,
/// Authorization token
token: String,
/// Hostname where masque proxy is hosted
domain: String,
}
impl From<Quic> for relay_list::Quic {
fn from(value: Quic) -> Self {
Self::new(value.addr_in, value.token, value.domain)
}
}
/// LWO doesn't have any configuration options (exposed by the API).
///
/// Note, an empty struct is not the same as an empty tuple struct according to serde_json!
#[derive(Debug, Clone, serde::Deserialize)]
struct Lwo {}
#[derive(Debug, serde::Deserialize)]
struct Bridges {
shadowsocks: Vec<relay_list::ShadowsocksEndpointData>,
relays: Vec<Relay>,
}
impl Bridges {
/// Consumes `self` and appends all its relays to `countries`.
fn extract_relays(
self,
countries: &mut BTreeMap<String, relay_list::RelayListCountry>,
) -> relay_list::BridgeEndpointData {
for mut bridge_relay in self.relays {
bridge_relay.convert_to_lowercase();
if let Some((country_code, city_code)) = split_location_code(&bridge_relay.location)
&& let Some(country) = countries.get_mut(country_code)
&& let Some(city) = country
.cities
.iter_mut()
.find(|city| city.code == city_code)
{
let location = location::Location {
country: country.name.clone(),
country_code: country.code.clone(),
city: city.name.clone(),
city_code: city.code.clone(),
latitude: city.latitude,
longitude: city.longitude,
};
let relay = bridge_relay.into_bridge_mullvad_relay(location);
city.relays.push(relay);
};
}
relay_list::BridgeEndpointData {
shadowsocks: self.shadowsocks,
}
}
}
|