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
|
//! Relay list updater
use futures::{
Future, FutureExt, SinkExt, StreamExt,
channel::mpsc,
future::{Fuse, FusedFuture},
};
use std::{
path::{Path, PathBuf},
time::{Duration, SystemTime, UNIX_EPOCH},
};
use tokio::fs::File;
use mullvad_api::{RelayListProxy, availability::ApiAvailability, rest::MullvadRestHandle};
use mullvad_relay_selector::RelaySelector;
use mullvad_types::relay_list::RelayList;
use talpid_future::retry::{ExponentialBackoff, Jittered, retry_future};
use talpid_types::ErrorExt;
/// How often the updater should wake up to check the cache of the in-memory cache of relays.
/// This check is very cheap. The only reason to not have it very often is because if downloading
/// constantly fails it will try very often and fill the logs etc.
const UPDATE_CHECK_INTERVAL: Duration = Duration::from_secs(60 * 15);
/// How old the cached relays need to be to trigger an update
const UPDATE_INTERVAL: Duration = Duration::from_secs(60 * 60);
const DOWNLOAD_RETRY_STRATEGY: Jittered<ExponentialBackoff> = Jittered::jitter(
ExponentialBackoff::new(Duration::from_secs(16), 8)
.max_delay(Some(Duration::from_secs(2 * 60 * 60))),
);
/// Where the relay list is cached on disk.
pub(crate) const RELAYS_FILENAME: &str = "relays.json";
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Downloader already shut down")]
DownloaderShutdown,
#[error("Mullvad relay selector error")]
RelaySelector(#[from] mullvad_relay_selector::Error),
}
#[derive(Clone)]
pub struct RelayListUpdaterHandle {
tx: mpsc::Sender<()>,
}
impl RelayListUpdaterHandle {
pub async fn update(&mut self) {
if let Err(error) = self
.tx
.send(())
.await
.map_err(|_| Error::DownloaderShutdown)
{
log::error!(
"{}",
error.display_chain_with_msg("Unable to send update command to relay list updater")
);
}
}
}
pub struct RelayListUpdater {
api_client: RelayListProxy,
cache_path: PathBuf,
relay_selector: RelaySelector,
on_update: Box<dyn Fn(&RelayList) + Send + 'static>,
last_check: SystemTime,
api_availability: ApiAvailability,
}
impl RelayListUpdater {
pub fn spawn(
selector: RelaySelector,
api_handle: MullvadRestHandle,
cache_dir: &Path,
on_update: impl Fn(&RelayList) + Send + 'static,
) -> RelayListUpdaterHandle {
let (tx, cmd_rx) = mpsc::channel(1);
let api_availability = api_handle.availability.clone();
let api_client = RelayListProxy::new(api_handle);
let updater = RelayListUpdater {
api_client,
cache_path: cache_dir.join(RELAYS_FILENAME),
relay_selector: selector,
on_update: Box::new(on_update),
last_check: UNIX_EPOCH,
api_availability,
};
tokio::spawn(updater.run(cmd_rx));
RelayListUpdaterHandle { tx }
}
async fn run(mut self, mut cmd_rx: mpsc::Receiver<()>) {
let mut download_future = Box::pin(Fuse::terminated());
loop {
let next_check = tokio::time::sleep(UPDATE_CHECK_INTERVAL).fuse();
tokio::pin!(next_check);
futures::select! {
_check_update = next_check => {
if download_future.is_terminated() && self.should_update() {
let tag = self.relay_selector.etag();
download_future = Box::pin(Self::download_relay_list(self.api_availability.clone(), self.api_client.clone(), tag).fuse());
self.last_check = SystemTime::now();
}
},
new_relay_list = download_future => {
self.consume_new_relay_list(new_relay_list).await;
},
cmd = cmd_rx.next() => {
match cmd {
Some(()) => {
let tag = self.relay_selector.etag();
download_future = Box::pin(Self::download_relay_list(self.api_availability.clone(), self.api_client.clone(), tag).fuse());
self.last_check = SystemTime::now();
},
None => {
log::trace!("Relay list updater shutting down");
return;
}
}
}
};
}
}
async fn consume_new_relay_list(
&mut self,
result: Result<Option<RelayList>, mullvad_api::Error>,
) {
match result {
Ok(Some(relay_list)) => {
if let Err(err) = self.update_cache(relay_list).await {
log::error!("Failed to update relay list cache: {}", err);
}
}
Ok(None) => log::debug!("Relay list is up-to-date"),
Err(error) => log::error!(
"{}",
error.display_chain_with_msg("Failed to fetch new relay list")
),
}
}
/// Returns true if the current parsed_relays is older than UPDATE_INTERVAL
fn should_update(&mut self) -> bool {
let last_check = std::cmp::max(self.relay_selector.last_updated(), self.last_check);
match SystemTime::now().duration_since(last_check) {
Ok(duration) => duration >= UPDATE_INTERVAL,
// If the clock is skewed we have no idea by how much or when the last update
// actually was, better download again to get in sync and get a `last_updated`
// timestamp corresponding to the new time.
Err(_) => true,
}
}
fn download_relay_list(
api_handle: ApiAvailability,
proxy: RelayListProxy,
tag: Option<String>,
) -> impl Future<Output = Result<Option<RelayList>, mullvad_api::Error>> + use<> {
async fn download_future(
api_handle: ApiAvailability,
proxy: RelayListProxy,
tag: Option<String>,
) -> Result<Option<RelayList>, mullvad_api::Error> {
let available = api_handle.wait_background();
let req = proxy.relay_list(tag);
available.await?;
req.await.map_err(mullvad_api::Error::from)
}
let download_futures =
move || download_future(api_handle.clone(), proxy.clone(), tag.clone());
retry_future(
download_futures,
|result| result.is_err(),
DOWNLOAD_RETRY_STRATEGY,
)
}
async fn update_cache(&mut self, new_relay_list: RelayList) -> Result<(), Error> {
if let Err(error) = Self::cache_relays(&self.cache_path, &new_relay_list).await {
log::error!(
"{}",
error.display_chain_with_msg("Failed to update relay cache on disk")
);
}
self.relay_selector.set_relays(new_relay_list.clone());
(self.on_update)(&new_relay_list);
Ok(())
}
/// Write a `RelayList` to the cache file.
async fn cache_relays(cache_path: &Path, relays: &RelayList) -> Result<(), Error> {
log::debug!("Writing relays cache to {}", cache_path.display());
let mut file = File::create(cache_path)
.await
.map_err(mullvad_relay_selector::Error::OpenRelayCache)?;
let bytes =
serde_json::to_vec_pretty(relays).map_err(mullvad_relay_selector::Error::Serialize)?;
let mut slice: &[u8] = bytes.as_slice();
let _ = tokio::io::copy(&mut slice, &mut file)
.await
.map_err(mullvad_relay_selector::Error::WriteRelayCache)?;
Ok(())
}
}
|