summaryrefslogtreecommitdiffhomepage
path: root/mullvad-api/src/bin/relay_list.rs
blob: aea9e61bd7172d2891127e4b52a56074d582995e (plain)
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
//! Fetches and prints the full relay list in JSON.
//! Used by the installer artifact packer to bundle the latest available
//! relay list at the time of creating the installer.

#[cfg(not(target_os = "android"))]
mod imp {
    use mullvad_api::{
        ApiEndpoint, RelayListProxy, proxy::ApiConnectionMode, rest::Error as RestError,
    };
    use std::process;
    use talpid_types::ErrorExt;

    pub async fn main() {
        let api_endpoint = ApiEndpoint::from_env_vars();
        let runtime = mullvad_api::Runtime::new(tokio::runtime::Handle::current(), &api_endpoint);

        let relay_list_request = RelayListProxy::new(
            runtime.mullvad_rest_handle(ApiConnectionMode::Direct.into_provider()),
        )
        .relay_list(None)
        .await;

        let relay_list = match relay_list_request {
            Ok(relay_list) => relay_list,
            Err(RestError::TimeoutError) => {
                eprintln!("Request timed out");
                process::exit(2);
            }
            Err(e @ RestError::DeserializeError(_)) => {
                eprintln!(
                    "{}",
                    e.display_chain_with_msg("Failed to deserialize relay list")
                );
                process::exit(3);
            }
            Err(e) => {
                eprintln!("{}", e.display_chain_with_msg("Failed to fetch relay list"));
                process::exit(1);
            }
        };
        println!("{}", serde_json::to_string_pretty(&relay_list).unwrap());
    }
}

#[tokio::main]
async fn main() {
    imp::main().await
}

#[cfg(target_os = "android")]
mod imp {
    pub async fn main() {}
}