Skip to content

Commit

Permalink
Merge branch 'add-json-output-to-mullvad-status-debug-des-1117'
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Aug 7, 2024
2 parents 95fec4f + 9df36ce commit a82466b
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Line wrap the file at 100 chars. Th
## [Unreleased]
### Added
- Add DAITA (Defence against AI-guided Traffic Analysis) setting for Linux and macOS.
- Add `--json` flag to `mullvad status` CLI.

### Changed
- Ignore obfuscation protocol constraints when the obfuscation mode is set to auto.
Expand Down
17 changes: 10 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ windows-sys = "0.52.0"
chrono = { version = "0.4.26", default-features = false }
clap = { version = "4.4.18", features = ["cargo", "derive"] }
once_cell = "1.13"
serde = "1.0.204"
serde_json = "1.0.122"

ipnetwork = "0.20"

Expand Down
2 changes: 1 addition & 1 deletion android/translations-converter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ thiserror = { workspace = true }
htmlize = { version = "1.0.2", features = ["unescape"] }
once_cell = { workspace = true }
regex = "1"
serde = { version = "1", features = ["derive"] }
serde = { workspace = true, features = ["derive"] }
quick-xml = { version = "0.27.1", features = ["serialize"] }
4 changes: 2 additions & 2 deletions mullvad-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ http = "0.2"
hyper = { version = "0.14", features = ["client", "stream", "http1", "tcp" ] }
ipnetwork = { workspace = true }
log = { workspace = true }
serde = "1"
serde_json = "1.0"
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["macros", "time", "rt-multi-thread", "net", "io-std", "io-util", "fs"] }
tokio-rustls = "0.24.1"
tokio-socks = "0.5.1"
Expand Down
2 changes: 2 additions & 0 deletions mullvad-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ talpid-types = { path = "../talpid-types" }

mullvad-management-interface = { path = "../mullvad-management-interface" }
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "fs"] }
serde = { workspace = true }
serde_json = { workspace = true }

[target.'cfg(all(unix, not(target_os = "android")))'.dependencies]
clap_complete = { version = "4.4.8" }
Expand Down
56 changes: 36 additions & 20 deletions mullvad-cli/src/cmds/status.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use anyhow::Result;
use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use futures::StreamExt;
use mullvad_management_interface::{client::DaemonEvent, MullvadProxyClient};
use mullvad_types::{device::DeviceState, states::TunnelState};
use serde::Serialize;
use std::fmt::Debug;

use crate::format;

Expand All @@ -19,8 +21,12 @@ pub struct StatusArgs {
verbose: bool,

/// Enable debug output
#[arg(long, short = 'd')]
#[arg(long, short = 'd', conflicts_with_all = ["verbose", "json"])]
debug: bool,

/// Format output as JSON
#[arg(long, short = 'j', conflicts_with_all = ["verbose", "debug"])]
json: bool,
}

impl Status {
Expand All @@ -33,6 +39,10 @@ impl Status {
DaemonEvent::TunnelState(new_state) => {
if args.debug {
println!("New tunnel state: {new_state:#?}");
} else if args.json {
let json = serde_json::to_string(&new_state)
.context("Failed to format output as JSON")?;
println!("{json}");
} else {
// When we enter the connected or disconnected state, am.i.mullvad.net will
// be polled to get exit location. When it arrives, we will get another
Expand Down Expand Up @@ -70,34 +80,22 @@ impl Status {
}
}
DaemonEvent::Settings(settings) => {
if args.debug {
println!("New settings: {settings:#?}");
}
print_debug_or_json(&args, "New settings", &settings)?;
}
DaemonEvent::RelayList(relay_list) => {
if args.debug {
println!("New relay list: {relay_list:#?}");
}
print_debug_or_json(&args, "New relay list", &relay_list)?;
}
DaemonEvent::AppVersionInfo(app_version_info) => {
if args.debug {
println!("New app version info: {app_version_info:#?}");
}
print_debug_or_json(&args, "New app version info", &app_version_info)?;
}
DaemonEvent::Device(device) => {
if args.debug {
println!("Device event: {device:#?}");
}
print_debug_or_json(&args, "Device event", &device)?;
}
DaemonEvent::RemoveDevice(device) => {
if args.debug {
println!("Remove device event: {device:#?}");
}
print_debug_or_json(&args, "Remove device event", &device)?;
}
DaemonEvent::NewAccessMethod(access_method) => {
if args.debug {
println!("New access method: {access_method:#?}");
}
print_debug_or_json(&args, "New access method", &access_method)?;
}
}
}
Expand All @@ -114,6 +112,9 @@ pub async fn handle(cmd: Option<Status>, args: StatusArgs) -> Result<()> {

if args.debug {
println!("Tunnel state: {state:#?}");
} else if args.json {
let json = serde_json::to_string(&state).context("Failed to format output as JSON")?;
println!("{json}");
} else {
format::print_state(&state, args.verbose);
format::print_location(&state);
Expand All @@ -139,3 +140,18 @@ fn print_account_logged_out(state: &TunnelState, device: &DeviceState) {
TunnelState::Disconnected { .. } | TunnelState::Disconnecting(_) => (),
}
}

fn print_debug_or_json<T: Debug + Serialize>(
args: &StatusArgs,
debug_message: &str,
t: &T,
) -> Result<()> {
if args.debug {
println!("{debug_message}: {t:#?}");
} else if args.json {
let json = serde_json::to_string(&t).context("Failed to format output as JSON")?;
println!("{json}");
}

Ok(())
}
4 changes: 2 additions & 2 deletions mullvad-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ once_cell = { workspace = true }
libc = "0.2"
log = { workspace = true }
regex = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["fs", "io-util", "rt-multi-thread", "sync", "time"] }
tokio-stream = "0.1"

Expand Down
2 changes: 1 addition & 1 deletion mullvad-relay-selector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ itertools = "0.12"
log = { workspace = true }
once_cell = { workspace = true }
rand = "0.8.5"
serde_json = "1.0"
serde_json = { workspace = true }

talpid-types = { path = "../talpid-types" }
mullvad-types = { path = "../mullvad-types" }
Expand Down
2 changes: 1 addition & 1 deletion mullvad-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ipnetwork = { workspace = true }
once_cell = { workspace = true }
log = { workspace = true }
regex = "1"
serde = { version = "1.0", features = ["derive"] }
serde = { workspace = true, features = ["derive"] }
uuid = { version = "1.4.1", features = ["v4", "serde" ] }

talpid-types = { path = "../talpid-types" }
Expand Down
6 changes: 3 additions & 3 deletions mullvad-types/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl AccountAndDevice {
}

/// Reason why a [DeviceEvent] was emitted.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum DeviceEventCause {
/// Logged in on a new device.
LoggedIn,
Expand All @@ -101,15 +101,15 @@ pub enum DeviceEventCause {
}

/// Emitted when logging in or out of an account, or when the device changes.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct DeviceEvent {
pub cause: DeviceEventCause,
pub new_state: DeviceState,
}

/// Emitted when a device is removed using the `RemoveDevice` RPC.
/// This is not sent by a normal logout or when it is revoked remotely.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct RemoveDeviceEvent {
pub account_token: AccountToken,
pub new_devices: Vec<Device>,
Expand Down
4 changes: 2 additions & 2 deletions talpid-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pcap = { version = "2.0", features = ["capture-stream"] }
pnet_packet = "0.34"
tun = { version = "0.5.5", features = ["async"] }
nix = { version = "0.28", features = ["socket"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }

[target.'cfg(windows)'.dependencies]
bitflags = "1.2"
Expand Down
2 changes: 1 addition & 1 deletion talpid-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rust-version.workspace = true
workspace = true

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde = { workspace = true, features = ["derive"] }
ipnetwork = { workspace = true }
base64 = "0.22.0"
x25519-dalek = { version = "2.0.1", features = ["static_secrets", "zeroize", "getrandom"] }
Expand Down
13 changes: 7 additions & 6 deletions test/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/connection-checker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ color-eyre = "0.6.2"
eyre = "0.6.12"
ping = "0.5.2"
reqwest = { version = "0.11.24", default-features = false, features = ["blocking", "rustls-tls", "json"] }
serde = { version = "1.0.197", features = ["derive"] }
serde = { workspace = true, features = ["derive"] }
socket2 = { version = "0.5.4", features = ["all"] }

0 comments on commit a82466b

Please sign in to comment.