Skip to content

Commit

Permalink
Rename ApiAccessMethod to AccessMethodSetting
Browse files Browse the repository at this point in the history
`ApiAccessMethod` was just an app-centric wrapper around `AccessMethod`.
  • Loading branch information
MarkusPettersson98 committed Sep 26, 2023
1 parent 80ff123 commit 1fdc5d7
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 56 deletions.
38 changes: 22 additions & 16 deletions mullvad-cli/src/cmds/api_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(())
}
Expand All @@ -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());
Expand All @@ -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))
},
)
}
Expand All @@ -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))
})
}
},
}
Expand Down Expand Up @@ -193,7 +195,7 @@ impl ApiAccess {
async fn get_access_method(
rpc: &mut MullvadProxyClient,
item: &SelectItem,
) -> Result<ApiAccessMethod> {
) -> Result<AccessMethodSetting> {
rpc.get_api_access_methods()
.await?
.get(item.as_array_index()?)
Expand Down Expand Up @@ -315,7 +317,7 @@ mod conversions {

use super::{AddCustomCommands, Socks5AddCommands};

impl TryFrom<AddCustomCommands> for daemon_types::ApiAccessMethod {
impl TryFrom<AddCustomCommands> for daemon_types::AccessMethodSetting {
type Error = Error;

fn try_from(value: AddCustomCommands) -> Result<Self, Self::Error> {
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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 }
}
}
Expand All @@ -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())?;
Expand Down
7 changes: 5 additions & 2 deletions mullvad-daemon/src/access_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,7 +25,10 @@ impl<L> Daemon<L>
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
Expand Down
10 changes: 5 additions & 5 deletions mullvad-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<Vec<ApiAccessMethod>, Error>),
GetApiAccessMethods(ResponseTx<Vec<AccessMethodSetting>, 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
Expand Down Expand Up @@ -2284,15 +2284,15 @@ where
Self::oneshot_send(tx, result, "rename_custom_list response");
}

fn on_get_api_access_methods(&mut self, tx: ResponseTx<Vec<ApiAccessMethod>, Error>) {
fn on_get_api_access_methods(&mut self, tx: ResponseTx<Vec<AccessMethodSetting>, Error>) {
let result = Ok(self.settings.api_access_methods.cloned());
Self::oneshot_send(tx, result, "get_api_access_methods response");
}

async fn on_add_api_access_method(
&mut self,
tx: ResponseTx<(), Error>,
method: ApiAccessMethod,
method: AccessMethodSetting,
) {
let result = self
.add_access_method(method)
Expand Down
2 changes: 1 addition & 1 deletion mullvad-daemon/src/management_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions mullvad-management-interface/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Vec<ApiAccessMethod>> {
pub async fn get_api_access_methods(&mut self) -> Result<Vec<AccessMethodSetting>> {
self.0
.get_settings(())
.await
Expand All @@ -175,15 +175,15 @@ 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()
}

pub async fn get_api_access_method(
&mut self,
id: &ApiAccessMethodId,
) -> Result<ApiAccessMethod> {
) -> Result<AccessMethodSetting> {
self.get_api_access_methods()
.await?
.into_iter()
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub mod settings {
api_access_methods: settings
.api_access_methods
.iter()
.map(api_access::ApiAccessMethod::try_from)
.collect::<Result<Vec<api_access::ApiAccessMethod>, _>>()?,
.map(api_access::AccessMethodSetting::try_from)
.collect::<Result<Vec<api_access::AccessMethodSetting>, _>>()?,
})
}
}
Expand All @@ -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
Expand All @@ -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<proto::ApiAccessMethods> for Vec<ApiAccessMethod> {
impl TryFrom<proto::ApiAccessMethods> for Vec<AccessMethodSetting> {
type Error = FromProtobufTypeError;

fn try_from(value: proto::ApiAccessMethods) -> Result<Self, Self::Error> {
value
.api_access_methods
.iter()
.map(ApiAccessMethod::try_from)
.map(AccessMethodSetting::try_from)
.collect()
}
}

impl TryFrom<proto::ApiAccessMethod> for ApiAccessMethod {
impl TryFrom<proto::ApiAccessMethod> for AccessMethodSetting {
type Error = FromProtobufTypeError;

fn try_from(value: proto::ApiAccessMethod) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -153,7 +153,12 @@ mod data {
}
};

Ok(ApiAccessMethod::with_id(id, name, enabled, access_method))
Ok(AccessMethodSetting::with_id(
id,
name,
enabled,
access_method,
))
}
}

Expand All @@ -171,8 +176,8 @@ mod data {
}
}

impl From<ApiAccessMethod> for proto::ApiAccessMethod {
fn from(value: ApiAccessMethod) -> Self {
impl From<AccessMethodSetting> 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();
Expand Down Expand Up @@ -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<Self, Self::Error> {
ApiAccessMethod::try_from(value.clone())
AccessMethodSetting::try_from(value.clone())
}
}

impl From<Vec<ApiAccessMethod>> for proto::ApiAccessMethods {
fn from(value: Vec<ApiAccessMethod>) -> proto::ApiAccessMethods {
impl From<Vec<AccessMethodSetting>> for proto::ApiAccessMethods {
fn from(value: Vec<AccessMethodSetting>) -> proto::ApiAccessMethods {
proto::ApiAccessMethods {
api_access_methods: value.iter().map(|method| method.clone().into()).collect(),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
}
Loading

0 comments on commit 1fdc5d7

Please sign in to comment.