summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/cmds/status.rs
blob: b1fb696b7fe08971fefb82f95cbb6171d0e95361 (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
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
use crate::{new_rpc_client, Command, Error, ErrorKind, Result, ResultExt};
use futures::{Future, Stream};
use mullvad_ipc_client::DaemonRpcClient;
use mullvad_types::auth_failed::AuthFailed;
use talpid_types::tunnel::{BlockReason, TunnelStateTransition};

pub struct Status;

impl Command for Status {
    fn name(&self) -> &'static str {
        "status"
    }

    fn clap_subcommand(&self) -> clap::App<'static, 'static> {
        clap::SubCommand::with_name(self.name())
            .about("View the state of the VPN tunnel")
            .subcommand(
                clap::SubCommand::with_name("listen").about("Listen for VPN tunnel state changes"),
            )
    }

    fn run(&self, matches: &clap::ArgMatches) -> Result<()> {
        let mut rpc = new_rpc_client()?;
        let state = rpc.get_state()?;

        print_state(&state);
        print_location(&mut rpc)?;
        if matches.subcommand_matches("listen").is_some() {
            let subscription = rpc
                .new_state_subscribe()
                .wait()
                .map_err(|_err| Error::from(ErrorKind::CantSubscribe))?;
            for new_state in subscription.wait() {
                let new_state = new_state.chain_err(|| "Subscription failed")?;
                print_state(&new_state);

                use self::TunnelStateTransition::*;
                match new_state {
                    Connected(_) | Disconnected => print_location(&mut rpc)?,
                    _ => {}
                }
            }
        }
        Ok(())
    }
}

fn print_state(state: &TunnelStateTransition) {
    use self::TunnelStateTransition::*;
    print!("Tunnel status: ");
    match state {
        Blocked(reason) => print_blocked_reason(reason),
        Connected(_) => println!("Connected"),
        Connecting(_) => println!("Connecting..."),
        Disconnected => println!("Disconnected"),
        Disconnecting(_) => println!("Disconnecting..."),
    }
}

fn print_blocked_reason(reason: &BlockReason) {
    match reason {
        BlockReason::AuthFailed(ref auth_failure) => {
            let auth_failure_str = auth_failure
                .as_ref()
                .map(|s| s.as_str())
                .unwrap_or("Account authentication failed");
            println!("Blocked: {}", AuthFailed::from(auth_failure_str));
        }
        other => println!("Blocked: {}", other),
    }
}

fn print_location(rpc: &mut DaemonRpcClient) -> Result<()> {
    let location = match rpc.get_current_location()? {
        Some(loc) => loc,
        None => {
            println!("Location data unavailable");
            return Ok(());
        }
    };
    let city_and_country = if let Some(city) = location.city {
        format!("{}, {}", city, location.country)
    } else {
        format!("{}", location.country)
    };
    if let Some(hostname) = location.hostname {
        println!("Relay: {}", hostname);
    }
    println!("Location: {}", city_and_country);
    println!(
        "Position: {:.5}°N, {:.5}°W",
        location.latitude, location.longitude
    );
    Ok(())
}