diff options
Diffstat (limited to 'mullvad-cli/src')
| -rw-r--r-- | mullvad-cli/src/cmds/status.rs | 63 | ||||
| -rw-r--r-- | mullvad-cli/src/format.rs | 25 |
2 files changed, 44 insertions, 44 deletions
diff --git a/mullvad-cli/src/cmds/status.rs b/mullvad-cli/src/cmds/status.rs index a4a8add15e..f833868516 100644 --- a/mullvad-cli/src/cmds/status.rs +++ b/mullvad-cli/src/cmds/status.rs @@ -18,10 +18,7 @@ pub struct StatusArgs { #[arg(long, short = 'v')] verbose: bool, - /// Print the current location and IP, based on GeoIP lookups - #[arg(long, short = 'l')] - location: bool, - + // TODO: changelog about removing location flag /// Enable debug output #[arg(long, short = 'd')] debug: bool, @@ -29,22 +26,29 @@ pub struct StatusArgs { impl Status { pub async fn listen(mut rpc: MullvadProxyClient, args: StatusArgs) -> Result<()> { + let mut previous_tunnel_state = None; + while let Some(event) = rpc.events_listen().await?.next().await { match event? { DaemonEvent::TunnelState(new_state) => { if args.debug { println!("New tunnel state: {new_state:#?}"); } else { - format::print_state(&new_state, args.verbose); - } - - match new_state { - TunnelState::Connected { .. } | TunnelState::Disconnected => { - if args.location { - print_location(&mut rpc).await?; - } + // When we enter the connected or disconnected state, am.i.mullvad.net will + // be polled to get IP information. When it arrives, we will get another + // tunnel state of the same enum type, but with the IP filled in. This + // match statement checks for duplicate tunnel states and skips the second + // print to avoid spamming the user. + match (&previous_tunnel_state, &new_state) { + (Some(TunnelState::Disconnected(_)), TunnelState::Disconnected(_)) + | ( + Some(TunnelState::Connected { .. }), + TunnelState::Connected { .. }, + ) => continue, + _ => {} } - _ => {} + format::print_state(&new_state, args.verbose); + previous_tunnel_state = Some(new_state); } } DaemonEvent::Settings(settings) => { @@ -88,11 +92,9 @@ pub async fn handle(cmd: Option<Status>, args: StatusArgs) -> Result<()> { if args.debug { println!("Tunnel state: {state:#?}"); } else { + // TODO: respect location arg? format::print_state(&state, args.verbose); - } - - if args.location { - print_location(&mut rpc).await?; + format::print_location(&state); } if cmd == Some(Status::Listen) { @@ -101,31 +103,6 @@ pub async fn handle(cmd: Option<Status>, args: StatusArgs) -> Result<()> { Ok(()) } -async fn print_location(rpc: &mut MullvadProxyClient) -> Result<()> { - let location = match rpc.get_current_location().await { - Ok(location) => location, - Err(error) => match &error { - mullvad_management_interface::Error::NoLocationData => { - println!("Location data unavailable"); - return Ok(()); - } - _ => return Err(error.into()), - }, - }; - if let Some(ipv4) = location.ipv4 { - println!("IPv4: {ipv4}"); - } - if let Some(ipv6) = location.ipv6 { - println!("IPv6: {ipv6}"); - } - - println!( - "Position: {:.5}°N, {:.5}°W", - location.latitude, location.longitude - ); - Ok(()) -} - fn print_account_loggedout(state: &TunnelState, device: &DeviceState) { match state { TunnelState::Connecting { .. } | TunnelState::Connected { .. } | TunnelState::Error(_) => { @@ -137,6 +114,6 @@ fn print_account_loggedout(state: &TunnelState, device: &DeviceState) { DeviceState::LoggedIn(_) => (), } } - TunnelState::Disconnected | TunnelState::Disconnecting(_) => (), + TunnelState::Disconnected(_) | TunnelState::Disconnecting(_) => (), } } diff --git a/mullvad-cli/src/format.rs b/mullvad-cli/src/format.rs index 2938842cef..0e920b6218 100644 --- a/mullvad-cli/src/format.rs +++ b/mullvad-cli/src/format.rs @@ -37,11 +37,34 @@ pub fn print_state(state: &TunnelState, verbose: bool) { format_relay_connection(endpoint, location.as_ref(), verbose) ); } - Disconnected => println!("Disconnected"), + Disconnected(_) => { + println!("Disconnected"); + } Disconnecting(_) => println!("Disconnecting..."), } } +pub fn print_location(state: &TunnelState) { + let location = match state { + TunnelState::Disconnected(location) => location, + TunnelState::Connected { location, .. } => location, + _ => return, + }; + if let Some(location) = location { + print!("Your connection appears from: {}", location.country); + if let Some(city) = &location.city { + print!(", {}", city); + } + if let Some(ipv4) = location.ipv4 { + print!(". IPv4: {ipv4}"); + } + if let Some(ipv6) = location.ipv6 { + print!(", IPv6: {ipv6}"); + } + println!(); + } +} + fn format_relay_connection( endpoint: &TunnelEndpoint, location: Option<&GeoIpLocation>, |
