diff options
| author | Markus Pettersson <markus.pettersson@mullvad.net> | 2023-09-18 11:55:41 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2023-10-09 14:40:05 +0200 |
| commit | b66d4bf941cba35375fb6c3ae1665546d0a16d12 (patch) | |
| tree | 2371de7457a5658c5e23a796487460817fcf032d | |
| parent | d767d881e69f7a986272303bda833df5769a9d9b (diff) | |
| download | mullvadvpn-b66d4bf941cba35375fb6c3ae1665546d0a16d12.tar.xz mullvadvpn-b66d4bf941cba35375fb6c3ae1665546d0a16d12.zip | |
Minor code cleanup
- Fix removal of API access methods from daemon settings
When removing an API access method, we now compare the ID (hash) of the
"access method-to-remove" with the access methods stored in the daemon
settings. This is because using the `==`/`!=` operator on two
similiar `AccessMethod`s only differing in the `enabled` field will be
different from hashing the objects and comparing those instead. The hash
does not consider the `enabled` field in its calculation.
| -rw-r--r-- | mullvad-daemon/src/access_methods.rs | 12 | ||||
| -rw-r--r-- | mullvad-daemon/src/api.rs | 4 | ||||
| -rw-r--r-- | mullvad-types/src/api_access_method.rs | 37 |
3 files changed, 36 insertions, 17 deletions
diff --git a/mullvad-daemon/src/access_methods.rs b/mullvad-daemon/src/access_methods.rs index 407c142b47..79afdae054 100644 --- a/mullvad-daemon/src/access_methods.rs +++ b/mullvad-daemon/src/access_methods.rs @@ -40,7 +40,7 @@ where ) -> Result<(), Error> { self.settings .update(|settings| { - if let Some(ref mut access_method) = settings + if let Some(access_method) = settings .api_access_methods .api_access_methods .iter_mut() @@ -58,13 +58,13 @@ where &mut self, access_method: CustomAccessMethod, ) -> Result<(), Error> { - let access_method = AccessMethod::from(access_method); self.settings .update(|settings| { - settings - .api_access_methods - .api_access_methods - .retain(|x| *x != access_method); + settings.api_access_methods.api_access_methods.retain(|x| { + x.as_custom() + .map(|c| c.id != access_method.id) + .unwrap_or(true) + }); }) .await .map(|did_change| self.notify_on_change(did_change)) diff --git a/mullvad-daemon/src/api.rs b/mullvad-daemon/src/api.rs index 4a3bd6fabe..26974ed4ee 100644 --- a/mullvad-daemon/src/api.rs +++ b/mullvad-daemon/src/api.rs @@ -64,7 +64,7 @@ impl Stream for ApiConnectionModeProvider { }; } - let connection_mode = self.new_task(self.retry_attempt); + let connection_mode = self.new_connection_mode(); self.retry_attempt = self.retry_attempt.wrapping_add(1); let cache_dir = self.cache_dir.clone(); @@ -86,10 +86,8 @@ impl ApiConnectionModeProvider { pub(crate) fn new(cache_dir: PathBuf, relay_selector: RelaySelector) -> Self { Self { cache_dir, - relay_selector, retry_attempt: 0, - current_task: None, } } diff --git a/mullvad-types/src/api_access_method.rs b/mullvad-types/src/api_access_method.rs index 5a4cd6cdcf..e3a423db7f 100644 --- a/mullvad-types/src/api_access_method.rs +++ b/mullvad-types/src/api_access_method.rs @@ -50,6 +50,12 @@ pub enum ObfuscationProtocol { } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] +pub enum Socks5 { + Local(Socks5Local), + Remote(Socks5Remote), +} + +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct Shadowsocks { pub peer: SocketAddr, pub password: String, // TODO: Mask the password (using special type)? @@ -57,13 +63,7 @@ pub struct Shadowsocks { pub enabled: bool, } -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] -pub enum Socks5 { - Local(Socks5Local), - Remote(Socks5Remote), -} - -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct Socks5Local { pub peer: SocketAddr, /// Port on localhost where the SOCKS5-proxy listens to. @@ -71,12 +71,33 @@ pub struct Socks5Local { pub enabled: bool, } -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct Socks5Remote { pub peer: SocketAddr, pub enabled: bool, } +impl Hash for Shadowsocks { + fn hash<H: Hasher>(&self, state: &mut H) { + self.peer.hash(state); + self.password.hash(state); + self.cipher.hash(state); + } +} + +impl Hash for Socks5Local { + fn hash<H: Hasher>(&self, state: &mut H) { + self.peer.hash(state); + self.port.hash(state); + } +} + +impl Hash for Socks5Remote { + fn hash<H: Hasher>(&self, state: &mut H) { + self.peer.hash(state); + } +} + impl Settings { // TODO: Do I have to clone? pub fn get_access_methods(&self) -> Vec<AccessMethod> { |
