summaryrefslogtreecommitdiffhomepage
path: root/mullvad-daemon/src
diff options
context:
space:
mode:
authorMarkus Pettersson <markus.pettersson@mullvad.net>2023-09-27 09:39:44 +0200
committerDavid Lönnhager <david.l@mullvad.net>2023-10-09 14:40:15 +0200
commit998fd39aaebc1435065f1f4886f4bd40f5889e5a (patch)
treeaf2816f531a38a65e494d0376001eff7666756f2 /mullvad-daemon/src
parent189c8d0273c036681142f73dfe3a5c619a0e0d28 (diff)
downloadmullvadvpn-998fd39aaebc1435065f1f4886f4bd40f5889e5a.tar.xz
mullvadvpn-998fd39aaebc1435065f1f4886f4bd40f5889e5a.zip
Code cleanup
- Get rid of extraneous calls to `clone` - Address nit: combine similar match arms into a single match arm - Fix `clippy` lint "unused `async` for function with no await statements" - Fix protobuf field numbers should start from 1 - This was not the case for `Socks5Local` & `Shadowsocks` - Refactor code for opening proxy connections - Cut down on duplicated code for setting up a proxied connection in `mullvad-api`. The difference between different connection modes is how they wrap the underlying `TCP` stream. - Remove `enable_access_method` & `disable_access_method` from RPC-client
Diffstat (limited to 'mullvad-daemon/src')
-rw-r--r--mullvad-daemon/src/access_method.rs2
-rw-r--r--mullvad-daemon/src/api.rs80
-rw-r--r--mullvad-daemon/src/lib.rs8
3 files changed, 42 insertions, 48 deletions
diff --git a/mullvad-daemon/src/access_method.rs b/mullvad-daemon/src/access_method.rs
index 73fd35fcbd..ea096cf5ba 100644
--- a/mullvad-daemon/src/access_method.rs
+++ b/mullvad-daemon/src/access_method.rs
@@ -102,7 +102,7 @@ where
/// Return the [`AccessMethodSetting`] which is currently used to access the
/// Mullvad API.
- pub async fn get_current_access_method(&mut self) -> Result<AccessMethodSetting, Error> {
+ pub fn get_current_access_method(&mut self) -> Result<AccessMethodSetting, Error> {
let connections_modes = self.connection_modes.lock().unwrap();
Ok(connections_modes.peek())
}
diff --git a/mullvad-daemon/src/api.rs b/mullvad-daemon/src/api.rs
index d44bdd0849..c548f0a293 100644
--- a/mullvad-daemon/src/api.rs
+++ b/mullvad-daemon/src/api.rs
@@ -10,7 +10,7 @@ use mullvad_api::{
ApiEndpointUpdateCallback,
};
use mullvad_relay_selector::RelaySelector;
-use mullvad_types::access_method::AccessMethodSetting;
+use mullvad_types::access_method::{AccessMethod, AccessMethodSetting, BuiltInAccessMethod};
use std::{
net::SocketAddr,
path::PathBuf,
@@ -106,10 +106,13 @@ impl ApiConnectionModeProvider {
log::debug!("Rotating Access mode!");
let access_method = {
let mut access_methods_picker = self.connection_modes.lock().unwrap();
- access_methods_picker.next()
+ access_methods_picker
+ .next()
+ .map(|access_method_setting| access_method_setting.access_method)
+ .unwrap_or(AccessMethod::from(BuiltInAccessMethod::Direct))
};
- let connection_mode = self.from(access_method.as_ref());
+ let connection_mode = self.from(access_method);
log::info!("New API connection mode selected: {}", connection_mode);
connection_mode
}
@@ -118,45 +121,40 @@ impl ApiConnectionModeProvider {
/// [`ApiConnectionMode`]s require extra logic/data from
/// [`ApiConnectionModeProvider`] the standard [`std::convert::From`] trait
/// can not be implemented.
- fn from(&mut self, access_method: Option<&AccessMethodSetting>) -> ApiConnectionMode {
- use mullvad_types::access_method::{self, AccessMethod, BuiltInAccessMethod};
+ fn from(&mut self, access_method: AccessMethod) -> ApiConnectionMode {
+ use mullvad_types::access_method;
match access_method {
- None => ApiConnectionMode::Direct,
- Some(access_method) => match &access_method.access_method {
- AccessMethod::BuiltIn(access_method) => match access_method {
- BuiltInAccessMethod::Direct => ApiConnectionMode::Direct,
- BuiltInAccessMethod::Bridge => self
- .relay_selector
- .get_bridge_forced()
- .and_then(|settings| match settings {
- ProxySettings::Shadowsocks(ss_settings) => {
- let ss_settings: access_method::Shadowsocks =
- access_method::Shadowsocks::new(
- ss_settings.peer,
- ss_settings.cipher,
- ss_settings.password,
- );
- Some(ApiConnectionMode::Proxied(ProxyConfig::Shadowsocks(
- ss_settings,
- )))
- }
- _ => {
- log::error!("Received unexpected proxy settings type");
- None
- }
- })
- .unwrap_or(ApiConnectionMode::Direct),
- },
- AccessMethod::Custom(access_method) => match &access_method {
- access_method::CustomAccessMethod::Shadowsocks(shadowsocks_config) => {
- ApiConnectionMode::Proxied(ProxyConfig::Shadowsocks(
- shadowsocks_config.clone(),
- ))
- }
- access_method::CustomAccessMethod::Socks5(socks_config) => {
- ApiConnectionMode::Proxied(ProxyConfig::Socks(socks_config.clone()))
- }
- },
+ AccessMethod::BuiltIn(access_method) => match access_method {
+ BuiltInAccessMethod::Direct => ApiConnectionMode::Direct,
+ BuiltInAccessMethod::Bridge => self
+ .relay_selector
+ .get_bridge_forced()
+ .and_then(|settings| match settings {
+ ProxySettings::Shadowsocks(ss_settings) => {
+ let ss_settings: access_method::Shadowsocks =
+ access_method::Shadowsocks::new(
+ ss_settings.peer,
+ ss_settings.cipher,
+ ss_settings.password,
+ );
+ Some(ApiConnectionMode::Proxied(ProxyConfig::Shadowsocks(
+ ss_settings,
+ )))
+ }
+ _ => {
+ log::error!("Received unexpected proxy settings type");
+ None
+ }
+ })
+ .unwrap_or(ApiConnectionMode::Direct),
+ },
+ AccessMethod::Custom(access_method) => match access_method {
+ access_method::CustomAccessMethod::Shadowsocks(shadowsocks_config) => {
+ ApiConnectionMode::Proxied(ProxyConfig::Shadowsocks(shadowsocks_config))
+ }
+ access_method::CustomAccessMethod::Socks5(socks_config) => {
+ ApiConnectionMode::Proxied(ProxyConfig::Socks(socks_config))
+ }
},
}
}
diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs
index 0a00639c09..3def0855c2 100644
--- a/mullvad-daemon/src/lib.rs
+++ b/mullvad-daemon/src/lib.rs
@@ -1076,7 +1076,7 @@ where
}
RemoveApiAccessMethod(tx, method) => self.on_remove_api_access_method(tx, method).await,
UpdateApiAccessMethod(tx, method) => self.on_update_api_access_method(tx, method).await,
- GetCurrentAccessMethod(tx) => self.on_get_current_api_access_method(tx).await,
+ GetCurrentAccessMethod(tx) => self.on_get_current_api_access_method(tx),
SetApiAccessMethod(tx, method) => self.on_set_api_access_method(tx, method),
GetApiAddresses(tx) => self.on_get_api_addresses(tx).await,
IsPerformingPostUpgrade(tx) => self.on_is_performing_post_upgrade(tx),
@@ -2307,13 +2307,9 @@ where
Self::oneshot_send(tx, result, "update_api_access_method response");
}
- async fn on_get_current_api_access_method(
- &mut self,
- tx: ResponseTx<AccessMethodSetting, Error>,
- ) {
+ fn on_get_current_api_access_method(&mut self, tx: ResponseTx<AccessMethodSetting, Error>) {
let result = self
.get_current_access_method()
- .await
.map_err(Error::AccessMethodError);
Self::oneshot_send(tx, result, "get_current_api_access_method response");
}