From 1fdc5d7862134511a8ce56900818efa41c2539c6 Mon Sep 17 00:00:00 2001 From: Markus Pettersson Date: Tue, 26 Sep 2023 10:24:10 +0200 Subject: [PATCH] Rename `ApiAccessMethod` to `AccessMethodSetting` `ApiAccessMethod` was just an app-centric wrapper around `AccessMethod`. --- mullvad-cli/src/cmds/api_access.rs | 38 +++++++++++-------- mullvad-daemon/src/access_method.rs | 7 +++- mullvad-daemon/src/lib.rs | 10 ++--- mullvad-daemon/src/management_interface.rs | 2 +- mullvad-management-interface/src/client.rs | 13 ++++--- .../types/conversions/api_access_method.rs | 35 +++++++++-------- .../src/types/rpc/api_access_method_update.rs | 4 +- mullvad-types/src/api_access.rs | 19 +++++----- 8 files changed, 72 insertions(+), 56 deletions(-) diff --git a/mullvad-cli/src/cmds/api_access.rs b/mullvad-cli/src/cmds/api_access.rs index e18482f34eff..0a859bddd5cc 100644 --- a/mullvad-cli/src/cmds/api_access.rs +++ b/mullvad-cli/src/cmds/api_access.rs @@ -2,7 +2,7 @@ use anyhow::{anyhow, Result}; use mullvad_management_interface::{ types::rpc::api_access_method_update::ApiAccessMethodUpdate, MullvadProxyClient, }; -use mullvad_types::api_access::{AccessMethod, ApiAccessMethod, CustomAccessMethod}; +use mullvad_types::api_access::{AccessMethod, AccessMethodSetting, CustomAccessMethod}; use std::net::IpAddr; use clap::{Args, Subcommand}; @@ -74,7 +74,7 @@ impl ApiAccess { /// Add a custom API access method. async fn add(cmd: AddCustomCommands) -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - let access_method = ApiAccessMethod::try_from(cmd)?; + let access_method = AccessMethodSetting::try_from(cmd)?; rpc.add_access_method(access_method).await?; Ok(()) } @@ -99,7 +99,7 @@ impl ApiAccess { .ok_or(anyhow!("Can not edit built-in access method"))?; // Create a new access method combining the new params with the previous values - let edited_access_method: ApiAccessMethod = match access_method { + let edited_access_method: AccessMethodSetting = match access_method { CustomAccessMethod::Shadowsocks(shadowsocks) => { let ip = cmd.params.ip.unwrap_or(shadowsocks.peer.ip()).to_string(); let port = cmd.params.port.unwrap_or(shadowsocks.peer.port()); @@ -109,7 +109,7 @@ impl ApiAccess { let enabled = api_access_method.enabled; mullvad_types::api_access::Shadowsocks::from_args(ip, port, cipher, password).map( |shadowsocks| { - ApiAccessMethod::new(name, enabled, AccessMethod::from(shadowsocks)) + AccessMethodSetting::new(name, enabled, AccessMethod::from(shadowsocks)) }, ) } @@ -120,16 +120,18 @@ impl ApiAccess { let local_port = cmd.params.local_port.unwrap_or(local.port); let name = cmd.params.name.unwrap_or(api_access_method.get_name()); let enabled = api_access_method.enabled(); - mullvad_types::api_access::Socks5Local::from_args(ip, port, local_port) - .map(|socks| ApiAccessMethod::new(name, enabled, AccessMethod::from(socks))) + mullvad_types::api_access::Socks5Local::from_args(ip, port, local_port).map( + |socks| AccessMethodSetting::new(name, enabled, AccessMethod::from(socks)), + ) } mullvad_types::api_access::Socks5::Remote(remote) => { let ip = cmd.params.ip.unwrap_or(remote.peer.ip()).to_string(); let port = cmd.params.port.unwrap_or(remote.peer.port()); let name = cmd.params.name.unwrap_or(api_access_method.get_name()); let enabled = api_access_method.enabled(); - mullvad_types::api_access::Socks5Remote::from_args(ip, port) - .map(|socks| ApiAccessMethod::new(name, enabled, AccessMethod::from(socks))) + mullvad_types::api_access::Socks5Remote::from_args(ip, port).map(|socks| { + AccessMethodSetting::new(name, enabled, AccessMethod::from(socks)) + }) } }, } @@ -193,7 +195,7 @@ impl ApiAccess { async fn get_access_method( rpc: &mut MullvadProxyClient, item: &SelectItem, - ) -> Result { + ) -> Result { rpc.get_api_access_methods() .await? .get(item.as_array_index()?) @@ -315,7 +317,7 @@ mod conversions { use super::{AddCustomCommands, Socks5AddCommands}; - impl TryFrom for daemon_types::ApiAccessMethod { + impl TryFrom for daemon_types::AccessMethodSetting { type Error = Error; fn try_from(value: AddCustomCommands) -> Result { @@ -338,7 +340,7 @@ mod conversions { ) .ok_or(anyhow!("Could not create a local Socks5 api proxy"))?, ); - daemon_types::ApiAccessMethod::new( + daemon_types::AccessMethodSetting::new( name, enabled, daemon_types::AccessMethod::from(socks_proxy), @@ -357,7 +359,7 @@ mod conversions { ) .ok_or(anyhow!("Could not create a remote Socks5 api proxy"))?, ); - daemon_types::ApiAccessMethod::new( + daemon_types::AccessMethodSetting::new( name, enabled, daemon_types::AccessMethod::from(socks_proxy), @@ -382,7 +384,7 @@ mod conversions { ) .ok_or(anyhow!("Could not create a Shadowsocks api proxy"))?; - daemon_types::ApiAccessMethod::new( + daemon_types::AccessMethodSetting::new( name, enabled, daemon_types::AccessMethod::from(shadowsocks_proxy), @@ -395,14 +397,16 @@ mod conversions { /// Pretty printing of [`ApiAccessMethod`]s mod pp { - use mullvad_types::api_access::{AccessMethod, ApiAccessMethod, CustomAccessMethod, Socks5}; + use mullvad_types::api_access::{ + AccessMethod, AccessMethodSetting, CustomAccessMethod, Socks5, + }; pub struct ApiAccessMethodFormatter<'a> { - api_access_method: &'a ApiAccessMethod, + api_access_method: &'a AccessMethodSetting, } impl<'a> ApiAccessMethodFormatter<'a> { - pub fn new(api_access_method: &'a ApiAccessMethod) -> ApiAccessMethodFormatter<'a> { + pub fn new(api_access_method: &'a AccessMethodSetting) -> ApiAccessMethodFormatter<'a> { ApiAccessMethodFormatter { api_access_method } } } @@ -419,6 +423,8 @@ mod pp { } }; + writeln!(f, "{:?}", self.api_access_method)?; + match &self.api_access_method.access_method { AccessMethod::BuiltIn(method) => { write!(f, "{}", method.canonical_name())?; diff --git a/mullvad-daemon/src/access_method.rs b/mullvad-daemon/src/access_method.rs index f04bc3e529c3..bf68df277fc4 100644 --- a/mullvad-daemon/src/access_method.rs +++ b/mullvad-daemon/src/access_method.rs @@ -3,7 +3,7 @@ use crate::{ Daemon, EventListener, }; use mullvad_management_interface::types::rpc::api_access_method_update::ApiAccessMethodUpdate; -use mullvad_types::api_access::{ApiAccessMethod, ApiAccessMethodId}; +use mullvad_types::api_access::{AccessMethodSetting, ApiAccessMethodId}; #[derive(err_derive::Error, Debug)] pub enum Error { @@ -25,7 +25,10 @@ impl Daemon where L: EventListener + Clone + Send + 'static, { - pub async fn add_access_method(&mut self, access_method: ApiAccessMethod) -> Result<(), Error> { + pub async fn add_access_method( + &mut self, + access_method: AccessMethodSetting, + ) -> Result<(), Error> { self.settings .update(|settings| settings.api_access_methods.append(access_method)) .await diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index e9da450fe298..71fb77a4efe8 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -41,7 +41,7 @@ use mullvad_relay_selector::{ }; use mullvad_types::{ account::{AccountData, AccountToken, VoucherSubmission}, - api_access::{ApiAccessMethod, ApiAccessMethodId}, + api_access::{AccessMethodSetting, ApiAccessMethodId}, auth_failed::AuthFailed, custom_list::{CustomList, CustomListLocationUpdate}, device::{Device, DeviceEvent, DeviceEventCause, DeviceId, DeviceState, RemoveDeviceEvent}, @@ -263,9 +263,9 @@ pub enum DaemonCommand { /// Rename a custom list from the old name to a new name RenameCustomList(ResponseTx<(), Error>, String, String), /// Get API access methods - GetApiAccessMethods(ResponseTx, Error>), + GetApiAccessMethods(ResponseTx, Error>), /// Add API access methods - AddApiAccessMethod(ResponseTx<(), Error>, ApiAccessMethod), + AddApiAccessMethod(ResponseTx<(), Error>, AccessMethodSetting), /// Remove an API access method RemoveApiAccessMethod(ResponseTx<(), Error>, ApiAccessMethodId), /// Set the API access method to use @@ -2284,7 +2284,7 @@ where Self::oneshot_send(tx, result, "rename_custom_list response"); } - fn on_get_api_access_methods(&mut self, tx: ResponseTx, Error>) { + fn on_get_api_access_methods(&mut self, tx: ResponseTx, Error>) { let result = Ok(self.settings.api_access_methods.cloned()); Self::oneshot_send(tx, result, "get_api_access_methods response"); } @@ -2292,7 +2292,7 @@ where async fn on_add_api_access_method( &mut self, tx: ResponseTx<(), Error>, - method: ApiAccessMethod, + method: AccessMethodSetting, ) { let result = self .add_access_method(method) diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs index 8e471ee26b4c..bb9321ae6b22 100644 --- a/mullvad-daemon/src/management_interface.rs +++ b/mullvad-daemon/src/management_interface.rs @@ -626,7 +626,7 @@ impl ManagementService for ManagementServiceImpl { ) -> ServiceResult<()> { log::debug!("add_api_access_method"); let api_access_method = - mullvad_types::api_access::ApiAccessMethod::try_from(request.into_inner())?; + mullvad_types::api_access::AccessMethodSetting::try_from(request.into_inner())?; let (tx, rx) = oneshot::channel(); self.send_command_to_daemon(DaemonCommand::AddApiAccessMethod(tx, api_access_method))?; self.wait_for_result(rx) diff --git a/mullvad-management-interface/src/client.rs b/mullvad-management-interface/src/client.rs index 7b4358c74116..6a6f21533735 100644 --- a/mullvad-management-interface/src/client.rs +++ b/mullvad-management-interface/src/client.rs @@ -4,7 +4,7 @@ use crate::types::{self, rpc}; use futures::{Stream, StreamExt}; use mullvad_types::{ account::{AccountData, AccountToken, VoucherSubmission}, - api_access::{ApiAccessMethod, ApiAccessMethodId}, + api_access::{AccessMethodSetting, ApiAccessMethodId}, custom_list::{CustomList, CustomListLocationUpdate}, device::{Device, DeviceEvent, DeviceId, DeviceState, RemoveDeviceEvent}, location::GeoIpLocation, @@ -164,7 +164,7 @@ impl MullvadProxyClient { mullvad_types::relay_list::RelayList::try_from(list).map_err(Error::InvalidResponse) } - pub async fn get_api_access_methods(&mut self) -> Result> { + pub async fn get_api_access_methods(&mut self) -> Result> { self.0 .get_settings(()) .await @@ -175,7 +175,7 @@ impl MullvadProxyClient { .api_access_methods .into_iter() .map(|api_access_method| { - ApiAccessMethod::try_from(api_access_method).map_err(Error::InvalidResponse) + AccessMethodSetting::try_from(api_access_method).map_err(Error::InvalidResponse) }) .collect() } @@ -183,7 +183,7 @@ impl MullvadProxyClient { pub async fn get_api_access_method( &mut self, id: &ApiAccessMethodId, - ) -> Result { + ) -> Result { self.get_api_access_methods() .await? .into_iter() @@ -523,7 +523,10 @@ impl MullvadProxyClient { Ok(()) } - pub async fn add_access_method(&mut self, api_access_method: ApiAccessMethod) -> Result<()> { + pub async fn add_access_method( + &mut self, + api_access_method: AccessMethodSetting, + ) -> Result<()> { self.0 .add_api_access_method(types::ApiAccessMethod::from(api_access_method)) .await diff --git a/mullvad-management-interface/src/types/conversions/api_access_method.rs b/mullvad-management-interface/src/types/conversions/api_access_method.rs index 7e6490504884..dd9cbc0cfe49 100644 --- a/mullvad-management-interface/src/types/conversions/api_access_method.rs +++ b/mullvad-management-interface/src/types/conversions/api_access_method.rs @@ -33,8 +33,8 @@ pub mod settings { api_access_methods: settings .api_access_methods .iter() - .map(api_access::ApiAccessMethod::try_from) - .collect::, _>>()?, + .map(api_access::AccessMethodSetting::try_from) + .collect::, _>>()?, }) } } @@ -57,7 +57,7 @@ pub mod settings { .ok_or(FromProtobufTypeError::InvalidArgument( "Could not convert Access Method from protobuf", )) - .and_then(api_access::ApiAccessMethod::try_from)?; + .and_then(api_access::AccessMethodSetting::try_from)?; let id = value .id @@ -80,23 +80,23 @@ pub mod settings { mod data { use crate::types::{proto, FromProtobufTypeError}; use mullvad_types::api_access::{ - AccessMethod, ApiAccessMethod, ApiAccessMethodId, BuiltInAccessMethod, CustomAccessMethod, - Shadowsocks, Socks5, Socks5Local, Socks5Remote, + AccessMethod, AccessMethodSetting, ApiAccessMethodId, BuiltInAccessMethod, + CustomAccessMethod, Shadowsocks, Socks5, Socks5Local, Socks5Remote, }; - impl TryFrom for Vec { + impl TryFrom for Vec { type Error = FromProtobufTypeError; fn try_from(value: proto::ApiAccessMethods) -> Result { value .api_access_methods .iter() - .map(ApiAccessMethod::try_from) + .map(AccessMethodSetting::try_from) .collect() } } - impl TryFrom for ApiAccessMethod { + impl TryFrom for AccessMethodSetting { type Error = FromProtobufTypeError; fn try_from(value: proto::ApiAccessMethod) -> Result { @@ -153,7 +153,12 @@ mod data { } }; - Ok(ApiAccessMethod::with_id(id, name, enabled, access_method)) + Ok(AccessMethodSetting::with_id( + id, + name, + enabled, + access_method, + )) } } @@ -171,8 +176,8 @@ mod data { } } - impl From for proto::ApiAccessMethod { - fn from(value: ApiAccessMethod) -> Self { + impl From for proto::ApiAccessMethod { + fn from(value: AccessMethodSetting) -> Self { let id = proto::Uuid::from(value.get_id()); let name = value.get_name(); let enabled = value.enabled(); @@ -229,16 +234,16 @@ mod data { } } - impl TryFrom<&proto::ApiAccessMethod> for ApiAccessMethod { + impl TryFrom<&proto::ApiAccessMethod> for AccessMethodSetting { type Error = FromProtobufTypeError; fn try_from(value: &proto::ApiAccessMethod) -> Result { - ApiAccessMethod::try_from(value.clone()) + AccessMethodSetting::try_from(value.clone()) } } - impl From> for proto::ApiAccessMethods { - fn from(value: Vec) -> proto::ApiAccessMethods { + impl From> for proto::ApiAccessMethods { + fn from(value: Vec) -> proto::ApiAccessMethods { proto::ApiAccessMethods { api_access_methods: value.iter().map(|method| method.clone().into()).collect(), } diff --git a/mullvad-management-interface/src/types/rpc/api_access_method_update.rs b/mullvad-management-interface/src/types/rpc/api_access_method_update.rs index b2708931026f..e74dc9f3cb4b 100644 --- a/mullvad-management-interface/src/types/rpc/api_access_method_update.rs +++ b/mullvad-management-interface/src/types/rpc/api_access_method_update.rs @@ -1,8 +1,8 @@ /// A short-lived datastructure used in the `ApiAccessMethodUpdate` RPC call. -use mullvad_types::api_access::{ApiAccessMethod, ApiAccessMethodId}; +use mullvad_types::api_access::{AccessMethodSetting, ApiAccessMethodId}; /// Argument to gRPC call `UpdateApiAccessMethod`. #[derive(Debug, Clone, PartialEq)] pub struct ApiAccessMethodUpdate { pub id: ApiAccessMethodId, - pub access_method: ApiAccessMethod, + pub access_method: AccessMethodSetting, } diff --git a/mullvad-types/src/api_access.rs b/mullvad-types/src/api_access.rs index a2643a71507d..2fb45d504f8d 100644 --- a/mullvad-types/src/api_access.rs +++ b/mullvad-types/src/api_access.rs @@ -6,12 +6,12 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4}; /// Daemon settings for API access methods. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct Settings { - pub api_access_methods: Vec, + pub api_access_methods: Vec, } impl Settings { /// Append an [`AccessMethod`] to the end of `api_access_methods`. - pub fn append(&mut self, api_access_method: ApiAccessMethod) { + pub fn append(&mut self, api_access_method: AccessMethodSetting) { self.api_access_methods.push(api_access_method) } @@ -21,7 +21,7 @@ impl Settings { } /// Search for a particular [`AccessMethod`] in `api_access_methods`. - pub fn find(&self, element: &ApiAccessMethodId) -> Option<&ApiAccessMethod> { + pub fn find(&self, element: &ApiAccessMethodId) -> Option<&AccessMethodSetting> { self.api_access_methods .iter() .find(|api_access_method| *element == api_access_method.get_id()) @@ -32,7 +32,7 @@ impl Settings { /// If the [`AccessMethod`] is found to be part of `api_access_methods`, a /// mutable reference to that inner element is returned. Otherwise, `None` /// is returned. - pub fn find_mut(&mut self, element: &ApiAccessMethodId) -> Option<&mut ApiAccessMethod> { + pub fn find_mut(&mut self, element: &ApiAccessMethodId) -> Option<&mut AccessMethodSetting> { self.api_access_methods .iter_mut() .find(|api_access_method| *element == api_access_method.get_id()) @@ -41,13 +41,13 @@ impl Settings { /// Equivalent to [`Vec::retain`]. pub fn retain(&mut self, f: F) where - F: FnMut(&ApiAccessMethod) -> bool, + F: FnMut(&AccessMethodSetting) -> bool, { self.api_access_methods.retain(f) } /// Clone the content of `api_access_methods`. - pub fn cloned(&self) -> Vec { + pub fn cloned(&self) -> Vec { self.api_access_methods.clone() } } @@ -58,7 +58,7 @@ impl Default for Settings { api_access_methods: vec![BuiltInAccessMethod::Direct, BuiltInAccessMethod::Bridge] .into_iter() .map(|built_in| { - ApiAccessMethod::new( + AccessMethodSetting::new( built_in.canonical_name(), true, AccessMethod::from(built_in), @@ -72,9 +72,8 @@ impl Default for Settings { /// API Access Method datastructure /// /// Mirrors the protobuf definition -/// TODO(Create a constructor functions for this struct (?)) #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] -pub struct ApiAccessMethod { +pub struct AccessMethodSetting { /// Some unique id (distinct for each `AccessMethod`). id: ApiAccessMethodId, pub name: String, @@ -110,7 +109,7 @@ pub enum AccessMethod { Custom(CustomAccessMethod), } -impl ApiAccessMethod { +impl AccessMethodSetting { pub fn new(name: String, enabled: bool, access_method: AccessMethod) -> Self { Self { id: ApiAccessMethodId::new(),