summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2020-11-10 13:16:13 +0100
committerDavid Lönnhager <david.l@mullvad.net>2020-11-19 01:35:21 +0100
commit557484f7c7160fcbdf75c4f989c2108110245d79 (patch)
treef736ae7294fb518c08665d0a76268ea585053fab
parente3f66f7dc55fb68efbb605b21aa66336ea653c7a (diff)
downloadmullvadvpn-557484f7c7160fcbdf75c4f989c2108110245d79.tar.xz
mullvadvpn-557484f7c7160fcbdf75c4f989c2108110245d79.zip
Cache the API addresses at build time
-rw-r--r--.gitignore1
-rwxr-xr-xbuild.sh1
-rw-r--r--mullvad-rpc/Cargo.toml3
-rw-r--r--mullvad-rpc/src/bin/address_cache.rs39
-rw-r--r--mullvad-rpc/src/lib.rs4
-rwxr-xr-xupdate-api-address.sh6
6 files changed, 54 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 9db2f0d726..3cbba48396 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,6 +11,7 @@
.DS_Store
*.log
/dist-assets/relays.json
+/dist-assets/api-ip-address.txt
/dist-assets/mullvad
/dist-assets/mullvad.exe
/dist-assets/mullvad-daemon
diff --git a/build.sh b/build.sh
index 6c171998ef..b30e624773 100755
--- a/build.sh
+++ b/build.sh
@@ -210,6 +210,7 @@ fi
./update-relays.sh
+./update-api-address.sh
pushd "$SCRIPT_DIR/gui"
diff --git a/mullvad-rpc/Cargo.toml b/mullvad-rpc/Cargo.toml
index a5acd75be4..fd8d34bc95 100644
--- a/mullvad-rpc/Cargo.toml
+++ b/mullvad-rpc/Cargo.toml
@@ -33,3 +33,6 @@ tempfile = "3.0"
[[bin]]
name = "relay_list"
+
+[[bin]]
+name = "address_cache"
diff --git a/mullvad-rpc/src/bin/address_cache.rs b/mullvad-rpc/src/bin/address_cache.rs
new file mode 100644
index 0000000000..203c5e79bb
--- /dev/null
+++ b/mullvad-rpc/src/bin/address_cache.rs
@@ -0,0 +1,39 @@
+/// Generate a first list of IP addresses for the Mullvad VPN to use to talk to the API.
+use mullvad_rpc::{rest::Error as RestError, ApiProxy, MullvadRpcRuntime};
+use std::process;
+use talpid_types::ErrorExt;
+
+#[tokio::main]
+async fn main() {
+ let mut runtime =
+ MullvadRpcRuntime::new(tokio::runtime::Handle::current()).expect("Failed to load runtime");
+
+ let api_proxy = ApiProxy::new(runtime.mullvad_rest_handle());
+ let request = api_proxy.get_api_addrs().await;
+
+ let api_list = match request {
+ Ok(api_list) => api_list,
+ Err(RestError::TimeoutError(_)) => {
+ eprintln!("Request timed out");
+ process::exit(2);
+ }
+ Err(e @ RestError::DeserializeError(_)) => {
+ eprintln!(
+ "{}",
+ e.display_chain_with_msg("Failed to deserialize API address list")
+ );
+ process::exit(3);
+ }
+ Err(e) => {
+ eprintln!(
+ "{}",
+ e.display_chain_with_msg("Failed to fetch API address list")
+ );
+ process::exit(1);
+ }
+ };
+
+ for address in api_list {
+ println!("{}", address);
+ }
+}
diff --git a/mullvad-rpc/src/lib.rs b/mullvad-rpc/src/lib.rs
index 69f39b6349..323937ea9a 100644
--- a/mullvad-rpc/src/lib.rs
+++ b/mullvad-rpc/src/lib.rs
@@ -426,6 +426,10 @@ pub struct ApiProxy {
}
impl ApiProxy {
+ pub fn new(handle: rest::MullvadRestHandle) -> Self {
+ Self { handle }
+ }
+
pub async fn get_api_addrs(&self) -> Result<Vec<SocketAddr>, rest::Error> {
let service = self.handle.service.clone();
diff --git a/update-api-address.sh b/update-api-address.sh
new file mode 100755
index 0000000000..5013a56f61
--- /dev/null
+++ b/update-api-address.sh
@@ -0,0 +1,6 @@
+#!/usr/bin/env bash
+
+echo "Updating API address cache..."
+set -e
+
+cargo +stable run --bin address_cache --release > dist-assets/api-ip-address.txt