Skip to content

Commit

Permalink
Improve printing, rename and comment things
Browse files Browse the repository at this point in the history
Printing is better and refactored to be shared with access methods,
renamed and made a clear distinction between what is called a "Custom
Bridge" and what is called a "Custom Proxy", the former is the specific
feature, the latter is the shared type between custom bridges and api
access methods.

Create doc comments.
  • Loading branch information
Jontified committed Dec 28, 2023
1 parent eb62e2e commit b1872fe
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 314 deletions.
63 changes: 12 additions & 51 deletions mullvad-cli/src/cmds/api_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ mod conversions {

/// Pretty printing of [`ApiAccessMethod`]s
mod pp {
use crate::cmds::proxies::pp::CustomProxyFormatter;
use mullvad_types::access_method::{AccessMethod, AccessMethodSetting};
use talpid_types::net::proxy::{CustomProxy, Socks5, SocksAuth};

pub struct ApiAccessMethodFormatter<'a> {
api_access_method: &'a AccessMethodSetting,
Expand Down Expand Up @@ -530,8 +530,6 @@ mod pp {

impl<'a> std::fmt::Display for ApiAccessMethodFormatter<'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, " *")
Expand All @@ -548,55 +546,18 @@ mod pp {
}
Ok(())
}
AccessMethod::Custom(method) => match &method {
CustomProxy::Shadowsocks(shadowsocks) => {
write!(f, "{}", self.api_access_method.get_name())?;
if self.settings.write_enabled {
write_status(f, self.api_access_method.enabled())?;
}
writeln!(f)?;
print_option!("Protocol", format!("Shadowsocks [{}]", shadowsocks.cipher));
print_option!("Peer", shadowsocks.peer);
print_option!("Password", shadowsocks.password);
Ok(())
AccessMethod::Custom(method) => {
write!(f, "{}", self.api_access_method.get_name())?;
if self.settings.write_enabled {
write_status(f, self.api_access_method.enabled())?;
}
CustomProxy::Socks5(socks) => match socks {
Socks5::Remote(remote) => {
write!(f, "{}", self.api_access_method.get_name())?;
if self.settings.write_enabled {
write_status(f, self.api_access_method.enabled())?;
}
writeln!(f)?;
print_option!("Protocol", "Socks5");
print_option!("Peer", remote.peer);
match &remote.authentication {
Some(SocksAuth { username, password }) => {
print_option!("Username", username);
print_option!("Password", password);
}
None => (),
}
Ok(())
}
Socks5::Local(local) => {
write!(f, "{}", self.api_access_method.get_name())?;
if self.settings.write_enabled {
write_status(f, self.api_access_method.enabled())?;
}
writeln!(f)?;
print_option!("Protocol", "Socks5 (local)");
print_option!(
"Peer",
format!(
"{}/{}",
local.remote_endpoint.address, local.remote_endpoint.protocol
)
);
print_option!("Local port", local.local_port);
Ok(())
}
},
},
writeln!(f)?;
let formatter = CustomProxyFormatter {
custom_proxy: &method,
};
write!(f, "{}", formatter)?;
Ok(())
}
}
}
}
Expand Down
108 changes: 32 additions & 76 deletions mullvad-cli/src/cmds/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ use mullvad_types::{
},
relay_list::RelayEndpointData,
};
use std::net::{IpAddr, SocketAddr};
use std::net::IpAddr;
use talpid_types::net::openvpn::{self, SHADOWSOCKS_CIPHERS};

use crate::cmds::proxies::pp::CustomProxyFormatter;

use super::{
custom_bridge::CustomCommands, relay::find_relay_by_hostname, relay_constraints::LocationArgs,
};

#[derive(Subcommand, Debug)]
pub enum Bridge {
/// Get current bridge settings
Get,
Get {
/// Display the saved custom bridge configuration
#[clap(long)]
custom: bool,
},
/// Set bridge state and settings, such as provider
#[clap(subcommand)]
Set(SetCommands),
Expand Down Expand Up @@ -148,7 +154,13 @@ pub enum SetCustomCommands {
impl Bridge {
pub async fn handle(self) -> Result<()> {
match self {
Bridge::Get => Self::get().await,
Bridge::Get { custom } => {
if custom {
Self::get_custom().await
} else {
Self::get().await
}
}
Bridge::List => Self::list().await,
Bridge::Set(subcmd) => Self::set(subcmd).await,
}
Expand Down Expand Up @@ -195,79 +207,6 @@ impl Bridge {
}
}

async fn set_custom(subcmd: SetCustomCommands) -> Result<()> {
match subcmd {
SetCustomCommands::Local {
local_port,
remote_ip,
remote_port,
} => {
let local_proxy = openvpn::LocalProxySettings {
port: local_port,
peer: SocketAddr::new(remote_ip, remote_port),
};
let packed_proxy = openvpn::ProxySettings::Local(local_proxy);
if let Err(error) = openvpn::validate_proxy_settings(&packed_proxy) {
panic!("{}", error);
}

let mut rpc = MullvadProxyClient::new().await?;
rpc.set_bridge_settings(BridgeSettings::Custom(packed_proxy))
.await?;
}
SetCustomCommands::Remote {
remote_ip,
remote_port,
username,
password,
} => {
let auth = match (username, password) {
(Some(username), Some(password)) => {
Some(openvpn::ProxyAuth { username, password })
}
_ => None,
};
let proxy = openvpn::RemoteProxySettings {
address: SocketAddr::new(remote_ip, remote_port),
auth,
};
let packed_proxy = openvpn::ProxySettings::Remote(proxy);
if let Err(error) = openvpn::validate_proxy_settings(&packed_proxy) {
panic!("{}", error);
}

let mut rpc = MullvadProxyClient::new().await?;
rpc.set_bridge_settings(BridgeSettings::Custom(packed_proxy))
.await?;
}
SetCustomCommands::Shadowsocks {
remote_ip,
remote_port,
password,
cipher,
} => {
let proxy = openvpn::ShadowsocksProxySettings {
peer: SocketAddr::new(remote_ip, remote_port),
password,
cipher,
#[cfg(target_os = "linux")]
fwmark: None,
};
let packed_proxy = openvpn::ProxySettings::Shadowsocks(proxy);
if let Err(error) = openvpn::validate_proxy_settings(&packed_proxy) {
panic!("{}", error);
}

let mut rpc = MullvadProxyClient::new().await?;
rpc.set_bridge_settings(BridgeSettings::Custom(packed_proxy))
.await?;
}
}

println!("Updated bridge settings");
Ok(())
}

async fn get() -> Result<()> {
let mut rpc = MullvadProxyClient::new().await?;
let settings = rpc.get_settings().await?;
Expand Down Expand Up @@ -295,6 +234,23 @@ impl Bridge {
Ok(())
}

async fn get_custom() -> Result<()> {
let mut rpc = MullvadProxyClient::new().await?;
let current_custom_bridge_settings = rpc.get_custom_bridge().await?;
match current_custom_bridge_settings.custom_bridge {
Some(custom_bridge) => {
let formatter = CustomProxyFormatter {
custom_proxy: &custom_bridge,
};
println!("Saved custom bridge settings:\n{}", formatter);
}
None => {
println!("Saved custom bridge settings: None");
}
}
Ok(())
}

fn print_local_proxy(proxy: &openvpn::LocalProxySettings) {
println!("proxy: local");
println!(" local port: {}", proxy.port);
Expand Down
Loading

0 comments on commit b1872fe

Please sign in to comment.