From b1872fea57d2f71cc335c968a28f03271abe8341 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 28 Dec 2023 11:53:32 +0100 Subject: [PATCH] Improve printing, rename and comment things 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. --- mullvad-cli/src/cmds/api_access.rs | 63 ++----- mullvad-cli/src/cmds/bridge.rs | 108 ++++-------- mullvad-cli/src/cmds/custom_bridge.rs | 164 ++++-------------- mullvad-cli/src/cmds/proxies.rs | 48 +++++ mullvad-daemon/src/lib.rs | 36 ++-- mullvad-daemon/src/management_interface.rs | 6 +- mullvad-management-interface/src/client.rs | 9 +- .../src/types/conversions/custom_proxy.rs | 10 +- .../src/types/conversions/settings.rs | 2 +- mullvad-types/src/settings/mod.rs | 6 +- talpid-core/src/firewall/linux.rs | 16 +- talpid-core/src/firewall/macos.rs | 3 +- talpid-core/src/firewall/windows.rs | 19 +- talpid-types/src/net/proxy.rs | 7 +- 14 files changed, 183 insertions(+), 314 deletions(-) diff --git a/mullvad-cli/src/cmds/api_access.rs b/mullvad-cli/src/cmds/api_access.rs index eca3dcd46c83..51f4f3e0e8d1 100644 --- a/mullvad-cli/src/cmds/api_access.rs +++ b/mullvad-cli/src/cmds/api_access.rs @@ -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, @@ -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, " *") @@ -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(()) + } } } } diff --git a/mullvad-cli/src/cmds/bridge.rs b/mullvad-cli/src/cmds/bridge.rs index aa89bf06e5a3..7020ce692458 100644 --- a/mullvad-cli/src/cmds/bridge.rs +++ b/mullvad-cli/src/cmds/bridge.rs @@ -8,9 +8,11 @@ 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, }; @@ -18,7 +20,11 @@ use super::{ #[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), @@ -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, } @@ -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?; @@ -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); diff --git a/mullvad-cli/src/cmds/custom_bridge.rs b/mullvad-cli/src/cmds/custom_bridge.rs index 1431a9a433ae..a7327c260230 100644 --- a/mullvad-cli/src/cmds/custom_bridge.rs +++ b/mullvad-cli/src/cmds/custom_bridge.rs @@ -1,24 +1,23 @@ -use anyhow::Result; +use anyhow::{bail, Result}; use clap::Subcommand; use super::proxies::{EditParams, ShadowsocksAdd, Socks5LocalAdd, Socks5RemoteAdd}; use mullvad_management_interface::MullvadProxyClient; use talpid_types::net::proxy::{ - CustomProxy, CustomProxySettings, Shadowsocks, Socks5, Socks5Local, Socks5Remote, + CustomBridgeSettings, CustomProxy, Shadowsocks, Socks5, Socks5Local, Socks5Remote, }; #[derive(Subcommand, Debug, Clone)] pub enum CustomCommands { - /// TODO - Get, - /// TODO + /// Remove the saved custom bridge configuration, does not disconnect if the bridge is + /// currently in use. Remove, - /// TODO + /// Connects to the currently saved custom bridge configuration. Set, - /// TODO + /// Add a new custom bridge configuration. #[clap(subcommand)] Add(AddCustomCommands), - /// TODO + /// Edit an already existing custom bridge configuration. Edit(EditParams), } @@ -35,32 +34,9 @@ pub enum AddCustomCommands { #[derive(Subcommand, Debug, Clone)] pub enum AddSocks5Commands { - // TODO: Update with new security requirements /// Configure a local SOCKS5 proxy - // TODO: Fix comment - #[cfg_attr( - target_os = "linux", - clap( - about = "Registers a local SOCKS5 proxy. The server must be excluded using \ - 'mullvad-exclude', or `SO_MARK` must be set to '0x6d6f6c65', in order \ - to bypass firewall restrictions" - ) - )] - // TODO: Fix comment - #[cfg_attr( - target_os = "windows", - clap( - about = "Registers a local SOCKS5 proxy. The server must be excluded using \ - split tunneling in order to bypass firewall restrictions" - ) - )] - // TODO: Fix comment - #[cfg_attr( - target_os = "macos", - clap( - about = "Registers a local SOCKS5 proxy. The server must run as root to bypass \ - firewall restrictions" - ) + #[clap( + about = "Registers a local SOCKS5 proxy. Will allow all local programs to leak traffic *only* to the remote endpoint." )] Local { #[clap(flatten)] @@ -77,35 +53,33 @@ pub enum AddSocks5Commands { impl CustomCommands { pub async fn handle(self) -> Result<()> { match self { - CustomCommands::Get => Self::custom_proxy_get().await, - CustomCommands::Set => Self::custom_proxy_set().await, + CustomCommands::Set => Self::custom_bridge_set().await, CustomCommands::Remove => Self::custom_bridge_remove().await, - CustomCommands::Edit(edit) => Self::custom_proxy_edit(edit).await, + CustomCommands::Edit(edit) => Self::custom_bridge_edit(edit).await, CustomCommands::Add(add_custom_commands) => { - Self::custom_proxy_add(add_custom_commands).await + Self::custom_bridge_add(add_custom_commands).await } } } - async fn custom_proxy_edit(edit: EditParams) -> Result<()> { + async fn custom_bridge_edit(edit: EditParams) -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; let mut custom_bridge = rpc.get_custom_bridge().await?; - custom_bridge.custom_proxy = - custom_bridge - .custom_proxy - .map(|custom_bridge| match custom_bridge { - CustomProxy::Shadowsocks(ss) => { - CustomProxy::Shadowsocks(edit.merge_shadowsocks(ss)) - } - CustomProxy::Socks5(socks) => match socks { - Socks5::Local(local) => { - CustomProxy::Socks5(Socks5::Local(edit.merge_socks_local(local))) - } - Socks5::Remote(remote) => { - CustomProxy::Socks5(Socks5::Remote(edit.merge_socks_remote(remote))) - } - }, - }); + let Some(old_custom_bridge) = custom_bridge.custom_bridge else { + bail!("Can not edit as there is no currently saved custom bridge"); + }; + + custom_bridge.custom_bridge = Some(match old_custom_bridge { + CustomProxy::Shadowsocks(ss) => CustomProxy::Shadowsocks(edit.merge_shadowsocks(ss)), + CustomProxy::Socks5(socks) => match socks { + Socks5::Local(local) => { + CustomProxy::Socks5(Socks5::Local(edit.merge_socks_local(local))) + } + Socks5::Remote(remote) => { + CustomProxy::Socks5(Socks5::Remote(edit.merge_socks_remote(remote))) + } + }, + }); rpc.update_custom_bridge(custom_bridge) .await @@ -114,28 +88,23 @@ impl CustomCommands { async fn custom_bridge_remove() -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - let custom_bridge = CustomProxySettings { custom_proxy: None }; + let custom_bridge = CustomBridgeSettings { + custom_bridge: None, + }; rpc.update_custom_bridge(custom_bridge) .await .map_err(anyhow::Error::from) } - async fn custom_proxy_set() -> Result<()> { + async fn custom_bridge_set() -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; rpc.set_custom_bridge().await.map_err(anyhow::Error::from) } - async fn custom_proxy_get() -> Result<()> { + async fn custom_bridge_add(add_commands: AddCustomCommands) -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - let current_proxy_settings = rpc.get_custom_bridge().await?; - println!("{:?}", current_proxy_settings); - Ok(()) - } - - async fn custom_proxy_add(add_commands: AddCustomCommands) -> Result<()> { - let mut rpc = MullvadProxyClient::new().await?; - let custom_bridge = CustomProxySettings { - custom_proxy: Some(match add_commands { + let custom_bridge = CustomBridgeSettings { + custom_bridge: Some(match add_commands { AddCustomCommands::Socks5(AddSocks5Commands::Local { add }) => { CustomProxy::Socks5(Socks5::Local(Socks5Local::from(add))) } @@ -152,66 +121,3 @@ impl CustomCommands { .map_err(anyhow::Error::from) } } - -//async fn custom_bridge_add(subcmd: AddCustomCommands) -> Result<()> { -// match subcmd { -// AddCustomCommands::Socks5(AddSocks5Commands::Local { -// add, -// }) => { -// let local_proxy = openvpn::LocalProxySettings { -// port: add.local_port, -// peer: SocketAddr::new(add.remote_ip, add.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?; -// } -// AddCustomCommands::Socks5(AddSocks5Commands::Remote { -// add, -// }) => { -// let auth = match add.authentication { -// Some(auth) => Some(openvpn::ProxyAuth { username: auth.username, password: auth.password }), -// _ => None, -// }; -// let proxy = openvpn::RemoteProxySettings { -// address: SocketAddr::new(add.remote_ip, add.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?; -// } -// AddCustomCommands::Shadowsocks { -// add -// } => { -// let proxy = openvpn::ShadowsocksProxySettings { -// peer: SocketAddr::new(add.remote_ip, add.remote_port), -// password: add.password, -// cipher: add.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(()) -//} diff --git a/mullvad-cli/src/cmds/proxies.rs b/mullvad-cli/src/cmds/proxies.rs index ce8687addf87..ab4c0fe50de8 100644 --- a/mullvad-cli/src/cmds/proxies.rs +++ b/mullvad-cli/src/cmds/proxies.rs @@ -157,3 +157,51 @@ impl EditParams { Shadowsocks::new((ip, port), cipher, password) } } + +pub mod pp { + use crate::print_option; + use talpid_types::net::proxy::{CustomProxy, Socks5, SocksAuth}; + + pub struct CustomProxyFormatter<'a> { + pub custom_proxy: &'a CustomProxy, + } + + impl<'a> std::fmt::Display for CustomProxyFormatter<'a> { + fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self.custom_proxy { + CustomProxy::Shadowsocks(shadowsocks) => { + print_option!("Protocol", format!("Shadowsocks [{}]", shadowsocks.cipher)); + print_option!("Peer", shadowsocks.peer); + print_option!("Password", shadowsocks.password); + Ok(()) + } + CustomProxy::Socks5(socks) => match socks { + Socks5::Remote(remote) => { + 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) => { + 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(()) + } + }, + } + } + } +} diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index 5ea0362c5eab..3e01f5af8d25 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -80,7 +80,7 @@ use talpid_types::android::AndroidContext; #[cfg(target_os = "windows")] use talpid_types::split_tunnel::ExcludedProcess; use talpid_types::{ - net::{openvpn::ProxySettings, proxy::CustomProxySettings, TunnelEndpoint, TunnelType}, + net::{openvpn::ProxySettings, proxy::CustomBridgeSettings, TunnelEndpoint, TunnelType}, tunnel::{ErrorStateCause, TunnelStateTransition}, ErrorExt, }; @@ -296,12 +296,12 @@ pub enum DaemonCommand { UpdateApiAccessMethod(ResponseTx<(), Error>, AccessMethodSetting), /// Get the currently used API access method GetCurrentAccessMethod(ResponseTx), - /// TODO - UpdateCustomProxy(ResponseTx<(), Error>, CustomProxySettings), - /// TODO - SetCustomProxy(ResponseTx<(), Error>), - /// TODO - GetCustomProxy(ResponseTx), + /// Used to add, edit and delete the custom bridge configuration + UpdateCustomBridge(ResponseTx<(), Error>, CustomBridgeSettings), + /// Set the current custom bridge configuration to be used as a bridge + SetCustomBridge(ResponseTx<(), Error>), + /// Get the current custom bridge configuration + GetCustomBridge(ResponseTx), /// Get the addresses of all known API endpoints GetApiAddresses(ResponseTx, Error>), /// Get information about the currently running and latest app versions @@ -1143,11 +1143,11 @@ where UpdateApiAccessMethod(tx, method) => self.on_update_api_access_method(tx, method).await, GetCurrentAccessMethod(tx) => self.on_get_current_api_access_method(tx), SetApiAccessMethod(tx, method) => self.on_set_api_access_method(tx, method).await, - UpdateCustomProxy(tx, custom_proxy) => { + UpdateCustomBridge(tx, custom_proxy) => { self.on_update_custom_proxy(tx, custom_proxy).await } - SetCustomProxy(tx) => self.on_select_custom_proxy(tx).await, - GetCustomProxy(tx) => self.on_get_custom_proxy(tx).await, + SetCustomBridge(tx) => self.on_select_custom_proxy(tx).await, + GetCustomBridge(tx) => self.on_get_custom_proxy(tx).await, GetApiAddresses(tx) => self.on_get_api_addresses(tx).await, IsPerformingPostUpgrade(tx) => self.on_is_performing_post_upgrade(tx), GetCurrentVersion(tx) => self.on_get_current_version(tx), @@ -2378,13 +2378,13 @@ where async fn on_update_custom_proxy( &mut self, tx: ResponseTx<(), Error>, - new_custom_proxy_settings: CustomProxySettings, + new_custom_proxy_settings: CustomBridgeSettings, ) { match self .settings .update(|settings| { - settings.custom_proxy.custom_proxy = new_custom_proxy_settings.custom_proxy; - if let Some(new_custom_proxy) = &settings.custom_proxy.custom_proxy { + settings.custom_proxy.custom_bridge = new_custom_proxy_settings.custom_bridge; + if let Some(new_custom_proxy) = &settings.custom_proxy.custom_bridge { settings.bridge_settings = BridgeSettings::Custom(ProxySettings::from( new_custom_proxy.clone(), #[cfg(target_os = "linux")] @@ -2396,7 +2396,7 @@ where .map_err(Error::SettingsError) { Ok(settings_changed) => { - if settings_changed && self.settings.custom_proxy.custom_proxy.is_some() { + if settings_changed && self.settings.custom_proxy.custom_bridge.is_some() { self.reconnect_tunnel(); } Self::oneshot_send(tx, Ok(()), "update_custom_proxy response"); @@ -2409,7 +2409,7 @@ where } async fn on_select_custom_proxy(&mut self, tx: ResponseTx<(), Error>) { - if self.settings.custom_proxy.custom_proxy.is_none() { + if self.settings.custom_proxy.custom_bridge.is_none() { log::info!("Tried to select custom proxy but no custom proxy is saved"); Self::oneshot_send( tx, @@ -2421,7 +2421,7 @@ where match self .settings .update(|settings| { - if let Some(new_custom_proxy) = &settings.custom_proxy.custom_proxy { + if let Some(new_custom_proxy) = &settings.custom_proxy.custom_bridge { settings.bridge_settings = BridgeSettings::Custom(ProxySettings::from( new_custom_proxy.clone(), #[cfg(target_os = "linux")] @@ -2433,7 +2433,7 @@ where .map_err(Error::SettingsError) { Ok(settings_changed) => { - if settings_changed && self.settings.custom_proxy.custom_proxy.is_some() { + if settings_changed && self.settings.custom_proxy.custom_bridge.is_some() { self.reconnect_tunnel(); } Self::oneshot_send(tx, Ok(()), "select_custom_proxy response"); @@ -2445,7 +2445,7 @@ where } } - async fn on_get_custom_proxy(&mut self, tx: ResponseTx) { + async fn on_get_custom_proxy(&mut self, tx: ResponseTx) { Self::oneshot_send( tx, Ok(self.settings.custom_proxy.clone()), diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs index 9f2b1f9ca623..30bf2ca9711e 100644 --- a/mullvad-daemon/src/management_interface.rs +++ b/mullvad-daemon/src/management_interface.rs @@ -744,7 +744,7 @@ impl ManagementService for ManagementServiceImpl { async fn set_custom_bridge(&self, _: Request<()>) -> ServiceResult<()> { log::debug!("set_custom_bridge"); let (tx, rx) = oneshot::channel(); - self.send_command_to_daemon(DaemonCommand::SetCustomProxy(tx))?; + self.send_command_to_daemon(DaemonCommand::SetCustomBridge(tx))?; self.wait_for_result(rx) .await? .map(Response::new) @@ -754,7 +754,7 @@ impl ManagementService for ManagementServiceImpl { async fn get_custom_bridge(&self, _: Request<()>) -> ServiceResult { log::debug!("get_custom_bridge"); let (tx, rx) = oneshot::channel(); - self.send_command_to_daemon(DaemonCommand::GetCustomProxy(tx))?; + self.send_command_to_daemon(DaemonCommand::GetCustomBridge(tx))?; self.wait_for_result(rx) .await? .map(CustomProxySettings::from) @@ -768,7 +768,7 @@ impl ManagementService for ManagementServiceImpl { ) -> ServiceResult<()> { log::debug!("update_custom_bridge"); let (tx, rx) = oneshot::channel(); - self.send_command_to_daemon(DaemonCommand::UpdateCustomProxy( + self.send_command_to_daemon(DaemonCommand::UpdateCustomBridge( tx, custom_proxy.into_inner().try_into()?, ))?; diff --git a/mullvad-management-interface/src/client.rs b/mullvad-management-interface/src/client.rs index 0cbc4c94cc5e..014a77828bf3 100644 --- a/mullvad-management-interface/src/client.rs +++ b/mullvad-management-interface/src/client.rs @@ -20,7 +20,7 @@ use mullvad_types::{ #[cfg(target_os = "windows")] use std::path::Path; use std::str::FromStr; -use talpid_types::net::proxy::CustomProxySettings; +use talpid_types::net::proxy::CustomBridgeSettings; #[cfg(target_os = "windows")] use talpid_types::split_tunnel::ExcludedProcess; use tonic::{Code, Status}; @@ -216,7 +216,7 @@ impl MullvadProxyClient { }) } - pub async fn update_custom_bridge(&mut self, custom_proxy: CustomProxySettings) -> Result<()> { + pub async fn update_custom_bridge(&mut self, custom_proxy: CustomBridgeSettings) -> Result<()> { self.0 .update_custom_bridge(types::CustomProxySettings::from(custom_proxy)) .await @@ -232,14 +232,15 @@ impl MullvadProxyClient { .map(tonic::Response::into_inner) } - pub async fn get_custom_bridge(&mut self) -> Result { + pub async fn get_custom_bridge(&mut self) -> Result { self.0 .get_custom_bridge(()) .await .map_err(Error::Rpc) .map(tonic::Response::into_inner) .and_then(|custom_proxy_settings| { - CustomProxySettings::try_from(custom_proxy_settings).map_err(Error::InvalidResponse) + CustomBridgeSettings::try_from(custom_proxy_settings) + .map_err(Error::InvalidResponse) }) } diff --git a/mullvad-management-interface/src/types/conversions/custom_proxy.rs b/mullvad-management-interface/src/types/conversions/custom_proxy.rs index 9972aac4ec2e..953137524294 100644 --- a/mullvad-management-interface/src/types/conversions/custom_proxy.rs +++ b/mullvad-management-interface/src/types/conversions/custom_proxy.rs @@ -5,22 +5,22 @@ mod settings { use crate::types::{proto, FromProtobufTypeError}; use talpid_types::net::proxy; - impl From for proto::CustomProxySettings { - fn from(settings: proxy::CustomProxySettings) -> Self { + impl From for proto::CustomProxySettings { + fn from(settings: proxy::CustomBridgeSettings) -> Self { Self { custom_proxy: settings - .custom_proxy + .custom_bridge .map(|custom_proxy| custom_proxy.into()), } } } - impl TryFrom for proxy::CustomProxySettings { + impl TryFrom for proxy::CustomBridgeSettings { type Error = FromProtobufTypeError; fn try_from(settings: proto::CustomProxySettings) -> Result { Ok(Self { - custom_proxy: settings + custom_bridge: settings .custom_proxy .and_then(|custom_proxy| proxy::CustomProxy::try_from(custom_proxy).ok()), }) diff --git a/mullvad-management-interface/src/types/conversions/settings.rs b/mullvad-management-interface/src/types/conversions/settings.rs index 5145e752609d..e8a4afe5188e 100644 --- a/mullvad-management-interface/src/types/conversions/settings.rs +++ b/mullvad-management-interface/src/types/conversions/settings.rs @@ -203,7 +203,7 @@ impl TryFrom for mullvad_types::settings::Settings { api_access_methods: mullvad_types::access_method::Settings::try_from( api_access_methods_settings, )?, - custom_proxy: talpid_types::net::proxy::CustomProxySettings::try_from( + custom_proxy: talpid_types::net::proxy::CustomBridgeSettings::try_from( custom_proxy_settings, )?, }) diff --git a/mullvad-types/src/settings/mod.rs b/mullvad-types/src/settings/mod.rs index 7c8a94f67da9..b5b8a32f5134 100644 --- a/mullvad-types/src/settings/mod.rs +++ b/mullvad-types/src/settings/mod.rs @@ -13,7 +13,7 @@ use jnix::IntoJava; use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(target_os = "windows")] use std::{collections::HashSet, path::PathBuf}; -use talpid_types::net::{openvpn, proxy::CustomProxySettings, GenericTunnelOptions}; +use talpid_types::net::{openvpn, proxy::CustomBridgeSettings, GenericTunnelOptions}; mod dns; @@ -82,7 +82,7 @@ pub struct Settings { pub api_access_methods: access_method::Settings, /// A potential custom bridge. #[cfg_attr(target_os = "android", jnix(skip))] - pub custom_proxy: CustomProxySettings, + pub custom_proxy: CustomBridgeSettings, /// If the daemon should allow communication with private (LAN) networks. pub allow_lan: bool, /// Extra level of kill switch. When this setting is on, the disconnected state will block @@ -139,7 +139,7 @@ impl Default for Settings { bridge_state: BridgeState::Auto, custom_lists: CustomListsSettings::default(), api_access_methods: access_method::Settings::default(), - custom_proxy: CustomProxySettings::default(), + custom_proxy: CustomBridgeSettings::default(), allow_lan: false, block_when_disconnected: false, auto_connect: false, diff --git a/talpid-core/src/firewall/linux.rs b/talpid-core/src/firewall/linux.rs index e16dd6b5d6b7..e14eacc7d0a5 100644 --- a/talpid-core/src/firewall/linux.rs +++ b/talpid-core/src/firewall/linux.rs @@ -518,10 +518,7 @@ impl<'a> PolicyBatch<'a> { allowed_endpoint, allowed_tunnel_traffic, } => { - self.add_allow_tunnel_endpoint_rules( - peer_endpoint, - fwmark, - ); + self.add_allow_tunnel_endpoint_rules(peer_endpoint, fwmark); self.add_allow_endpoint_rules(allowed_endpoint); // Important to block DNS after allow relay rule (so the relay can operate @@ -554,10 +551,7 @@ impl<'a> PolicyBatch<'a> { allow_lan, dns_servers, } => { - self.add_allow_tunnel_endpoint_rules( - peer_endpoint, - fwmark, - ); + self.add_allow_tunnel_endpoint_rules(peer_endpoint, fwmark); self.add_allow_dns_rules(tunnel, dns_servers, TransportProtocol::Udp)?; self.add_allow_dns_rules(tunnel, dns_servers, TransportProtocol::Tcp)?; @@ -601,11 +595,7 @@ impl<'a> PolicyBatch<'a> { Ok(()) } - fn add_allow_tunnel_endpoint_rules( - &mut self, - endpoint: &AllowedEndpoint, - fwmark: u32, - ) { + fn add_allow_tunnel_endpoint_rules(&mut self, endpoint: &AllowedEndpoint, fwmark: u32) { let mut prerouting_rule = Rule::new(&self.prerouting_chain); check_endpoint(&mut prerouting_rule, End::Src, &endpoint.endpoint); prerouting_rule.add_expr(&nft_expr!(immediate data fwmark)); diff --git a/talpid-core/src/firewall/macos.rs b/talpid-core/src/firewall/macos.rs index c3f4dfdcf5de..4ff7b061c454 100644 --- a/talpid-core/src/firewall/macos.rs +++ b/talpid-core/src/firewall/macos.rs @@ -121,8 +121,7 @@ impl Firewall { allowed_endpoint, allowed_tunnel_traffic, } => { - let mut rules = - vec![self.get_allow_relay_rule(*peer_endpoint)?]; + let mut rules = vec![self.get_allow_relay_rule(*peer_endpoint)?]; rules.push(self.get_allowed_endpoint_rule(allowed_endpoint)?); // Important to block DNS after allow relay rule (so the relay can operate diff --git a/talpid-core/src/firewall/windows.rs b/talpid-core/src/firewall/windows.rs index e16d7f17e0d7..d10e07103d83 100644 --- a/talpid-core/src/firewall/windows.rs +++ b/talpid-core/src/firewall/windows.rs @@ -156,8 +156,12 @@ impl Firewall { let relay_client_wstrs: Vec<_> = endpoint .clients .iter() - .map(|client| WideCString::from_os_str_truncate(client)).collect(); - let relay_client_wstr_ptrs: Vec<*const u16> = relay_client_wstrs.iter().map(|wstr| wstr.as_ptr()).collect(); + .map(|client| WideCString::from_os_str_truncate(client)) + .collect(); + let relay_client_wstr_ptrs: Vec<*const u16> = relay_client_wstrs + .iter() + .map(|wstr| wstr.as_ptr()) + .collect(); let relay_client_wstr_ptrs_len = relay_client_wstr_ptrs.len(); //let relay_client_wstr_ptr: *const u16 = if let Some(ref wstr) = relay_client_wstr { // wstr.as_ptr() @@ -273,10 +277,15 @@ impl Firewall { None => ptr::null(), }; - let relay_client_wstrs: Vec<_> = endpoint.clients + let relay_client_wstrs: Vec<_> = endpoint + .clients + .iter() + .map(|client| WideCString::from_os_str_truncate(client)) + .collect(); + let relay_client_wstr_ptrs: Vec<*const u16> = relay_client_wstrs .iter() - .map(|client| WideCString::from_os_str_truncate(client)).collect(); - let relay_client_wstr_ptrs: Vec<*const u16> = relay_client_wstrs.iter().map(|wstr| wstr.as_ptr()).collect(); + .map(|wstr| wstr.as_ptr()) + .collect(); let relay_client_wstr_ptrs_len = relay_client_wstr_ptrs.len(); //let relay_client_wstr_ptrs: *const u16 = if let Some(ref wstr) = relay_client_wstr { // wstr.as_ptr() diff --git a/talpid-types/src/net/proxy.rs b/talpid-types/src/net/proxy.rs index 32a1461b1aaa..602dd4e99acb 100644 --- a/talpid-types/src/net/proxy.rs +++ b/talpid-types/src/net/proxy.rs @@ -30,12 +30,11 @@ pub struct ProxyEndpoint { pub proxy_type: ProxyType, } -/// Custom proxy settings, describes both the saved config for the custom proxy and whether it is -/// currently in use. +/// Custom bridge settings, describes the optionally saved custom bridge. #[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] #[serde(rename_all = "snake_case")] -pub struct CustomProxySettings { - pub custom_proxy: Option, +pub struct CustomBridgeSettings { + pub custom_bridge: Option, } // TODO(Jonathan): These end up being duplicates of a lot of types in