diff options
| author | Markus Pettersson <markus.pettersson@mullvad.net> | 2023-09-18 16:27:06 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2023-10-09 14:40:05 +0200 |
| commit | be5e93c32f3b3e9ec59da18215fad80457dd4d49 (patch) | |
| tree | 817f6d8a2e337e53ef6fdf4fcf3179395fbd8049 /mullvad-cli/src | |
| parent | be3da7e67723c96bdb997fb6e7dcbfebc036919d (diff) | |
| download | mullvadvpn-be5e93c32f3b3e9ec59da18215fad80457dd4d49.tar.xz mullvadvpn-be5e93c32f3b3e9ec59da18215fad80457dd4d49.zip | |
Pretty print custom access methods
`mullvad proxy list` will now pretty print all configured access methods
in a human-readable way
Diffstat (limited to 'mullvad-cli/src')
| -rw-r--r-- | mullvad-cli/src/cmds/proxy.rs | 84 |
1 files changed, 77 insertions, 7 deletions
diff --git a/mullvad-cli/src/cmds/proxy.rs b/mullvad-cli/src/cmds/proxy.rs index 507d5fa46d..48ef302c19 100644 --- a/mullvad-cli/src/cmds/proxy.rs +++ b/mullvad-cli/src/cmds/proxy.rs @@ -63,17 +63,13 @@ impl Proxy { /// Show all API access methods. async fn list() -> Result<()> { + use crate::cmds::proxy::pp::AccessMethodFormatter; let mut rpc = MullvadProxyClient::new().await?; for (index, api_access_method) in rpc.get_api_access_methods().await?.iter().enumerate() { println!( - "{}. {:?} {}", + "{}. {}", index + 1, - api_access_method, - if api_access_method.enabled() { - "[enabled]" - } else { - "[disabled]" - } + AccessMethodFormatter::new(&api_access_method) ); } Ok(()) @@ -386,3 +382,77 @@ mod conversions { } } } + +/// Pretty printing of [`AccessMethod`]s +mod pp { + use mullvad_types::api_access_method::{ + AccessMethod, BuiltInAccessMethod, ObfuscationProtocol, Socks5, + }; + + pub struct AccessMethodFormatter<'a> { + access_method: &'a AccessMethod, + } + + impl<'a> AccessMethodFormatter<'a> { + pub fn new(access_method: &'a AccessMethod) -> AccessMethodFormatter<'a> { + AccessMethodFormatter { access_method } + } + } + + impl<'a> std::fmt::Display for AccessMethodFormatter<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use crate::print_option; + + let write_status = |f: &mut std::fmt::Formatter<'_>, enabled: bool| { + if enabled { + write!(f, " *") + } else { + write!(f, "") + } + }; + + match self.access_method { + AccessMethod::BuiltIn(method) => match method { + BuiltInAccessMethod::Direct(enabled) => { + write!(f, "Direct")?; + write_status(f, *enabled) + } + BuiltInAccessMethod::Bridge(enabled) => { + write!(f, "Mullvad Bridges")?; + write_status(f, *enabled) + } + }, + AccessMethod::Custom(method) => match &method.access_method { + ObfuscationProtocol::Shadowsocks(shadowsocks) => { + write!(f, "{}", shadowsocks.name)?; + write_status(f, shadowsocks.enabled)?; + writeln!(f, "")?; + print_option!("Protocol", format!("Shadowsocks [{}]", shadowsocks.cipher)); + print_option!("Peer", shadowsocks.peer); + print_option!("Password", shadowsocks.password); + Ok(()) + } + ObfuscationProtocol::Socks5(socks) => match socks { + Socks5::Remote(remote) => { + write!(f, "{}", remote.name)?; + write_status(f, remote.enabled)?; + writeln!(f, "")?; + print_option!("Protocol", "Socks5"); + print_option!("Peer", remote.peer); + Ok(()) + } + Socks5::Local(local) => { + write!(f, "{}", local.name)?; + write_status(f, local.enabled)?; + writeln!(f, "")?; + print_option!("Protocol", "Socks5 (local)"); + print_option!("Peer", local.peer); + print_option!("Local port", local.port); + Ok(()) + } + }, + }, + } + } + } +} |
