diff options
Diffstat (limited to 'mullvad-api/src')
| -rw-r--r-- | mullvad-api/src/access.rs | 12 | ||||
| -rw-r--r-- | mullvad-api/src/proxy.rs | 14 | ||||
| -rw-r--r-- | mullvad-api/src/relay_list.rs | 109 | ||||
| -rw-r--r-- | mullvad-api/src/rest.rs | 25 |
4 files changed, 77 insertions, 83 deletions
diff --git a/mullvad-api/src/access.rs b/mullvad-api/src/access.rs index 6c875ad1e8..1ee6ea612d 100644 --- a/mullvad-api/src/access.rs +++ b/mullvad-api/src/access.rs @@ -155,12 +155,12 @@ impl AccessTokenStore { /// Remove an access token if the API response calls for it. pub fn check_response<T>(&self, account: &AccountNumber, response: &Result<T, rest::Error>) { - if let Err(rest::Error::ApiError(_status, code)) = response { - if code == crate::INVALID_ACCESS_TOKEN { - let _ = self - .tx - .unbounded_send(StoreAction::InvalidateToken(account.to_owned())); - } + if let Err(rest::Error::ApiError(_status, code)) = response + && code == crate::INVALID_ACCESS_TOKEN + { + let _ = self + .tx + .unbounded_send(StoreAction::InvalidateToken(account.to_owned())); } } } diff --git a/mullvad-api/src/proxy.rs b/mullvad-api/src/proxy.rs index 4ad842ee85..ab05e25021 100644 --- a/mullvad-api/src/proxy.rs +++ b/mullvad-api/src/proxy.rs @@ -154,13 +154,13 @@ impl ApiConnectionMode { /// Attempts to remove `CURRENT_CONFIG_FILENAME`, if it exists. pub async fn try_delete_cache(cache_dir: &Path) { let path = cache_dir.join(CURRENT_CONFIG_FILENAME); - if let Err(err) = fs::remove_file(path).await { - if err.kind() != std::io::ErrorKind::NotFound { - log::error!( - "{}", - err.display_chain_with_msg("Failed to remove old API config") - ); - } + if let Err(err) = fs::remove_file(path).await + && err.kind() != std::io::ErrorKind::NotFound + { + log::error!( + "{}", + err.display_chain_with_msg("Failed to remove old API config") + ); } } diff --git a/mullvad-api/src/relay_list.rs b/mullvad-api/src/relay_list.rs index 799a55dc1a..abca6dd2b7 100644 --- a/mullvad-api/src/relay_list.rs +++ b/mullvad-api/src/relay_list.rs @@ -209,26 +209,24 @@ impl OpenVpn { ) -> relay_list::OpenVpnEndpointData { for mut openvpn_relay in self.relays.into_iter() { openvpn_relay.convert_to_lowercase(); - if let Some((country_code, city_code)) = split_location_code(&openvpn_relay.location) { - if let Some(country) = countries.get_mut(country_code) { - if let Some(city) = country - .cities - .iter_mut() - .find(|city| city.code == city_code) - { - let location = location::Location { - country: country.name.clone(), - country_code: country.code.clone(), - city: city.name.clone(), - city_code: city.code.clone(), - latitude: city.latitude, - longitude: city.longitude, - }; - let relay = openvpn_relay.into_openvpn_mullvad_relay(location); - city.relays.push(relay); - } + if let Some((country_code, city_code)) = split_location_code(&openvpn_relay.location) + && let Some(country) = countries.get_mut(country_code) + && let Some(city) = country + .cities + .iter_mut() + .find(|city| city.code == city_code) + { + let location = location::Location { + country: country.name.clone(), + country_code: country.code.clone(), + city: city.name.clone(), + city_code: city.code.clone(), + latitude: city.latitude, + longitude: city.longitude, }; - } + let relay = openvpn_relay.into_openvpn_mullvad_relay(location); + city.relays.push(relay); + }; } self.ports } @@ -321,27 +319,24 @@ impl Wireguard { wireguard_relay.relay.convert_to_lowercase(); if let Some((country_code, city_code)) = split_location_code(&wireguard_relay.relay.location) + && let Some(country) = countries.get_mut(country_code) + && let Some(city) = country + .cities + .iter_mut() + .find(|city| city.code == city_code) { - if let Some(country) = countries.get_mut(country_code) { - if let Some(city) = country - .cities - .iter_mut() - .find(|city| city.code == city_code) - { - let location = location::Location { - country: country.name.clone(), - country_code: country.code.clone(), - city: city.name.clone(), - city_code: city.code.clone(), - latitude: city.latitude, - longitude: city.longitude, - }; - - let relay = wireguard_relay.into_mullvad_relay(location); - city.relays.push(relay); - } + let location = location::Location { + country: country.name.clone(), + country_code: country.code.clone(), + city: city.name.clone(), + city_code: city.code.clone(), + latitude: city.latitude, + longitude: city.longitude, }; - } + + let relay = wireguard_relay.into_mullvad_relay(location); + city.relays.push(relay); + }; } endpoint_data @@ -394,27 +389,25 @@ impl Bridges { ) -> relay_list::BridgeEndpointData { for mut bridge_relay in self.relays { bridge_relay.convert_to_lowercase(); - if let Some((country_code, city_code)) = split_location_code(&bridge_relay.location) { - if let Some(country) = countries.get_mut(country_code) { - if let Some(city) = country - .cities - .iter_mut() - .find(|city| city.code == city_code) - { - let location = location::Location { - country: country.name.clone(), - country_code: country.code.clone(), - city: city.name.clone(), - city_code: city.code.clone(), - latitude: city.latitude, - longitude: city.longitude, - }; - - let relay = bridge_relay.into_bridge_mullvad_relay(location); - city.relays.push(relay); - } + if let Some((country_code, city_code)) = split_location_code(&bridge_relay.location) + && let Some(country) = countries.get_mut(country_code) + && let Some(city) = country + .cities + .iter_mut() + .find(|city| city.code == city_code) + { + let location = location::Location { + country: country.name.clone(), + country_code: country.code.clone(), + city: city.name.clone(), + city_code: city.code.clone(), + latitude: city.latitude, + longitude: city.longitude, }; - } + + let relay = bridge_relay.into_bridge_mullvad_relay(location); + city.relays.push(relay); + }; } relay_list::BridgeEndpointData { diff --git a/mullvad-api/src/rest.rs b/mullvad-api/src/rest.rs index 5e7d13047e..3854f66ef4 100644 --- a/mullvad-api/src/rest.rs +++ b/mullvad-api/src/rest.rs @@ -101,11 +101,12 @@ impl Error { pub fn is_offline(&self) -> bool { match self { Error::LegacyHyperError(error) if error.is_connect() => { - if let Some(cause) = error.source() { - if let Some(err) = cause.downcast_ref::<std::io::Error>() { - return err.raw_os_error() == Some(libc::ENETUNREACH); - } + if let Some(cause) = error.source() + && let Some(err) = cause.downcast_ref::<std::io::Error>() + { + return err.raw_os_error() == Some(libc::ENETUNREACH); } + false } // TODO: Currently, we use the legacy hyper client for all REST requests. If this @@ -252,14 +253,14 @@ impl<T: ConnectionModeProvider + 'static> RequestService<T> { let response = request_future.await.map_err(|error| error.map_aborted()); // Switch API endpoint if the request failed due to a network error - if let Err(err) = &response { - if err.is_network_error() && !api_availability.is_offline() { - log::error!("{}", err.display_chain_with_msg("HTTP request failed")); - if let Some(tx) = tx { - let _ = tx.unbounded_send(RequestCommand::NextApiConfig( - connection_mode_generation, - )); - } + if let Err(err) = &response + && err.is_network_error() + && !api_availability.is_offline() + { + log::error!("{}", err.display_chain_with_msg("HTTP request failed")); + if let Some(tx) = tx { + let _ = tx + .unbounded_send(RequestCommand::NextApiConfig(connection_mode_generation)); } } |
