diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-06-27 08:25:52 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-06-27 08:25:52 -0300 |
| commit | fce918ebab9d686946af4d46bc00536eb3dd2f4d (patch) | |
| tree | 7110ed96b747832a094b78d263787d1d65873330 | |
| parent | 0f0ef6ff2447ce684d75920d259d84747962440f (diff) | |
| parent | 728038bee9237e4e039e3cc9fd772950541a0184 (diff) | |
| download | mullvadvpn-fce918ebab9d686946af4d46bc00536eb3dd2f4d.tar.xz mullvadvpn-fce918ebab9d686946af4d46bc00536eb3dd2f4d.zip | |
Merge branch 'cli-subscribe'
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/status.rs | 76 |
2 files changed, 56 insertions, 21 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index b33994a45e..abd478364a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ Line wrap the file at 100 chars. Th when DNS resolution fails. - Automatic rotation of the daemon log. The existing log is renamed to `daemon.old.log` on daemon startup. +- Add `status listen` subcommand in the CLI to continuously monitor the tunnel state. #### macOS - Add colors to terminal output. 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(()) +} |
