diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-06-26 19:09:11 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-06-27 08:16:06 -0300 |
| commit | 0082dbaa1133faa389853d98a44e52982fef79b5 (patch) | |
| tree | e12e4d6aafd2e0b226e884490fb9c472005a7870 | |
| parent | 0f0ef6ff2447ce684d75920d259d84747962440f (diff) | |
| download | mullvadvpn-0082dbaa1133faa389853d98a44e52982fef79b5.tar.xz mullvadvpn-0082dbaa1133faa389853d98a44e52982fef79b5.zip | |
Add `status listen` sub-command to Mullvad CLI
| -rw-r--r-- | mullvad-cli/src/cmds/status.rs | 76 |
1 files changed, 55 insertions, 21 deletions
diff --git a/mullvad-cli/src/cmds/status.rs b/mullvad-cli/src/cmds/status.rs index f6a41f8813..0b6b7f05ef 100644 --- a/mullvad-cli/src/cmds/status.rs +++ b/mullvad-cli/src/cmds/status.rs @@ -3,7 +3,17 @@ use Command; use Result; use mullvad_ipc_client::DaemonRpcClient; -use mullvad_types::states::{SecurityState, TargetState}; +use mullvad_types::states::{DaemonState, SecurityState, TargetState}; + +const DISCONNECTED: DaemonState = DaemonState { + state: SecurityState::Unsecured, + target_state: TargetState::Unsecured, +}; + +const CONNECTED: DaemonState = DaemonState { + state: SecurityState::Secured, + target_state: TargetState::Secured, +}; pub struct Status; @@ -13,32 +23,56 @@ impl Command for Status { } fn clap_subcommand(&self) -> clap::App<'static, 'static> { - clap::SubCommand::with_name(self.name()).about("View the state of the VPN tunnel") + 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<()> { + fn run(&self, matches: &clap::ArgMatches) -> Result<()> { let mut rpc = DaemonRpcClient::new()?; let state = rpc.get_state()?; - print!("Tunnel status: "); - match (state.state, state.target_state) { - (SecurityState::Unsecured, TargetState::Unsecured) => println!("Disconnected"), - (SecurityState::Unsecured, TargetState::Secured) => println!("Connecting..."), - (SecurityState::Secured, TargetState::Unsecured) => println!("Disconnecting..."), - (SecurityState::Secured, TargetState::Secured) => println!("Connected"), + + print_state(state); + print_location(&mut rpc)?; + + if matches.subcommand_matches("listen").is_some() { + for new_state in rpc.new_state_subscribe()? { + print_state(new_state); + + if new_state == CONNECTED || new_state == DISCONNECTED { + print_location(&mut rpc)?; + } + } } - let location = rpc.get_current_location()?; - let city_and_country = if let Some(city) = location.city { - format!("{}, {}", city, location.country) - } else { - format!("{}", location.country) - }; - println!("Location: {}", city_and_country); - println!( - "Position: {:.5}°N, {:.5}°W", - location.latitude, location.longitude - ); - println!("IP: {}", location.ip); Ok(()) } } + +fn print_state(state: DaemonState) { + print!("Tunnel status: "); + match (state.state, state.target_state) { + (SecurityState::Unsecured, TargetState::Unsecured) => println!("Disconnected"), + (SecurityState::Unsecured, TargetState::Secured) => println!("Connecting..."), + (SecurityState::Secured, TargetState::Unsecured) => println!("Disconnecting..."), + (SecurityState::Secured, TargetState::Secured) => println!("Connected"), + } +} + +fn print_location(rpc: &mut DaemonRpcClient) -> Result<()> { + let location = rpc.get_current_location()?; + let city_and_country = if let Some(city) = location.city { + format!("{}, {}", city, location.country) + } else { + format!("{}", location.country) + }; + println!("Location: {}", city_and_country); + println!( + "Position: {:.5}°N, {:.5}°W", + location.latitude, location.longitude + ); + println!("IP: {}", location.ip); + Ok(()) +} |
