diff options
| author | Markus Pettersson <markus.pettersson@mullvad.net> | 2025-07-15 12:52:25 +0200 |
|---|---|---|
| committer | Markus Pettersson <markus.pettersson@mullvad.net> | 2025-07-15 14:56:58 +0200 |
| commit | de3b988970d47625cffb4497d4d425fa3a6d0a4b (patch) | |
| tree | b7ee96455768ac483a62ac630166cb35443a8df0 | |
| parent | 3c1ec7e8acaaeddd4ed1ab739a499d9c0d197894 (diff) | |
| download | mullvadvpn-de3b988970d47625cffb4497d4d425fa3a6d0a4b.tar.xz mullvadvpn-de3b988970d47625cffb4497d4d425fa3a6d0a4b.zip | |
Run `crago +nightly clippy --fix`
| -rw-r--r-- | mullvad-cli/src/cmds/tunnel_state.rs | 44 | ||||
| -rw-r--r-- | mullvad-daemon/src/account_history.rs | 12 | ||||
| -rw-r--r-- | mullvad-daemon/src/custom_list.rs | 28 | ||||
| -rw-r--r-- | mullvad-daemon/src/device/mod.rs | 37 | ||||
| -rw-r--r-- | mullvad-daemon/src/lib.rs | 59 | ||||
| -rw-r--r-- | mullvad-daemon/src/main.rs | 8 | ||||
| -rw-r--r-- | mullvad-daemon/src/migrations/v1.rs | 32 | ||||
| -rw-r--r-- | mullvad-daemon/src/migrations/v3.rs | 44 | ||||
| -rw-r--r-- | mullvad-daemon/src/migrations/v5.rs | 30 | ||||
| -rw-r--r-- | mullvad-daemon/src/migrations/v6.rs | 4 | ||||
| -rw-r--r-- | mullvad-daemon/src/settings/mod.rs | 12 |
11 files changed, 149 insertions, 161 deletions
diff --git a/mullvad-cli/src/cmds/tunnel_state.rs b/mullvad-cli/src/cmds/tunnel_state.rs index d746ae3e86..2db5765f54 100644 --- a/mullvad-cli/src/cmds/tunnel_state.rs +++ b/mullvad-cli/src/cmds/tunnel_state.rs @@ -16,15 +16,15 @@ pub async fn connect(wait: bool) -> Result<()> { None }; - if rpc.connect_tunnel().await? { - if let Some(receiver) = listener { - wait_for_tunnel_state(receiver, |state| match state { - TunnelState::Connected { .. } => Ok(true), - TunnelState::Error(_) => Err(anyhow!("Failed to connect")), - _ => Ok(false), - }) - .await?; - } + if rpc.connect_tunnel().await? + && let Some(receiver) = listener + { + wait_for_tunnel_state(receiver, |state| match state { + TunnelState::Connected { .. } => Ok(true), + TunnelState::Error(_) => Err(anyhow!("Failed to connect")), + _ => Ok(false), + }) + .await?; } Ok(()) @@ -39,10 +39,10 @@ pub async fn disconnect(wait: bool) -> Result<()> { None }; - if rpc.disconnect_tunnel().await? { - if let Some(receiver) = listener { - wait_for_tunnel_state(receiver, |state| Ok(state.is_disconnected())).await?; - } + if rpc.disconnect_tunnel().await? + && let Some(receiver) = listener + { + wait_for_tunnel_state(receiver, |state| Ok(state.is_disconnected())).await?; } Ok(()) @@ -60,15 +60,15 @@ pub async fn reconnect(wait: bool) -> Result<()> { None }; - if rpc.reconnect_tunnel().await? { - if let Some(receiver) = listener { - wait_for_tunnel_state(receiver, |state| match state { - TunnelState::Connected { .. } => Ok(true), - TunnelState::Error(_) => Err(anyhow!("Failed to reconnect")), - _ => Ok(false), - }) - .await?; - } + if rpc.reconnect_tunnel().await? + && let Some(receiver) = listener + { + wait_for_tunnel_state(receiver, |state| match state { + TunnelState::Connected { .. } => Ok(true), + TunnelState::Error(_) => Err(anyhow!("Failed to reconnect")), + _ => Ok(false), + }) + .await?; } Ok(()) diff --git a/mullvad-daemon/src/account_history.rs b/mullvad-daemon/src/account_history.rs index 5a8677e464..6a6c0e3ddc 100644 --- a/mullvad-daemon/src/account_history.rs +++ b/mullvad-daemon/src/account_history.rs @@ -73,13 +73,11 @@ impl AccountHistory { let file = io::BufWriter::new(reader.into_inner()); let mut history = AccountHistory { file, number }; - if should_save { - if let Err(error) = history.save_to_disk().await { - log::error!( - "{}", - error.display_chain_with_msg("Failed to save account history after opening it") - ); - } + if should_save && let Err(error) = history.save_to_disk().await { + log::error!( + "{}", + error.display_chain_with_msg("Failed to save account history after opening it") + ); } Ok(history) } diff --git a/mullvad-daemon/src/custom_list.rs b/mullvad-daemon/src/custom_list.rs index 06f5926a2c..ce079c2fff 100644 --- a/mullvad-daemon/src/custom_list.rs +++ b/mullvad-daemon/src/custom_list.rs @@ -129,28 +129,24 @@ impl Daemon { if let Some(endpoint) = self.tunnel_state.endpoint() { match endpoint.tunnel_type { TunnelType::Wireguard => { - if relay_settings.wireguard_constraints.multihop() { - if let Constraint::Only(LocationConstraint::CustomList { list_id }) = + if relay_settings.wireguard_constraints.multihop() + && let Constraint::Only(LocationConstraint::CustomList { list_id }) = &relay_settings.wireguard_constraints.entry_location - { - need_to_reconnect |= - custom_list_id.map(|id| &id == list_id).unwrap_or(true); - } + { + need_to_reconnect |= + custom_list_id.map(|id| &id == list_id).unwrap_or(true); } } TunnelType::OpenVpn => { - if !matches!(self.settings.bridge_state, BridgeState::Off) { - if let Ok(ResolvedBridgeSettings::Normal(bridge_settings)) = + if !matches!(self.settings.bridge_state, BridgeState::Off) + && let Ok(ResolvedBridgeSettings::Normal(bridge_settings)) = self.settings.bridge_settings.resolve() - { - if let Constraint::Only(LocationConstraint::CustomList { list_id }) = - &bridge_settings.location - { - need_to_reconnect |= - custom_list_id.map(|id| &id == list_id).unwrap_or(true); - } - } + && let Constraint::Only(LocationConstraint::CustomList { list_id }) = + &bridge_settings.location + { + need_to_reconnect |= + custom_list_id.map(|id| &id == list_id).unwrap_or(true); } } } diff --git a/mullvad-daemon/src/device/mod.rs b/mullvad-daemon/src/device/mod.rs index 7b0649a4d9..56b910d971 100644 --- a/mullvad-daemon/src/device/mod.rs +++ b/mullvad-daemon/src/device/mod.rs @@ -461,10 +461,10 @@ impl AccountManager { let mut current_api_call = api::CurrentApiCall::new(); loop { - if current_api_call.is_idle() { - if let Some(timed_rotation) = self.spawn_timed_key_rotation() { - current_api_call.set_timed_rotation(Box::pin(timed_rotation)) - } + if current_api_call.is_idle() + && let Some(timed_rotation) = self.spawn_timed_key_rotation() + { + current_api_call.set_timed_rotation(Box::pin(timed_rotation)) } futures::select! { @@ -849,15 +849,15 @@ impl AccountManager { } } - if !self.rotation_requests.is_empty() || !self.validation_requests.is_empty() { - if let Some(updated_config) = self.data.device() { - let device_service = self.device_service.clone(); - let number = updated_config.account_number.clone(); - let device_id = updated_config.device.id.clone(); - api_call.set_oneshot_rotation(Box::pin(async move { - device_service.rotate_key(number, device_id).await - })); - } + if (!self.rotation_requests.is_empty() || !self.validation_requests.is_empty()) + && let Some(updated_config) = self.data.device() + { + let device_service = self.device_service.clone(); + let number = updated_config.account_number.clone(); + let device_id = updated_config.device.id.clone(); + api_call.set_oneshot_rotation(Box::pin(async move { + device_service.rotate_key(number, device_id).await + })); } } @@ -1046,10 +1046,10 @@ impl AccountManager { self.cacher.write(&device_state).await?; self.last_validation = None; - if let Some(old_config) = self.data.logout() { - if device_state.device().map(|d| &d.device.id) != Some(&old_config.device.id) { - tokio::spawn(self.logout_api_call(old_config)); - } + if let Some(old_config) = self.data.logout() + && device_state.device().map(|d| &d.device.id) != Some(&old_config.device.id) + { + tokio::spawn(self.logout_api_call(old_config)); } self.data = device_state; @@ -1374,7 +1374,8 @@ impl TunnelStateChangeHandler { const fn should_check_device_validity_on_attempt(wireguard_retry_attempt: usize) -> bool { // Incorporate a debounce effect where every `WG_DEVICE_CHECK_THRESHOLD` attempt should be // able to trigger a device check. - wireguard_retry_attempt > 0 && (wireguard_retry_attempt % WG_DEVICE_CHECK_THRESHOLD == 0) + wireguard_retry_attempt > 0 + && wireguard_retry_attempt.is_multiple_of(WG_DEVICE_CHECK_THRESHOLD) } fn should_continue_retries(err: &Error) -> bool { diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index 61a3569ed5..533018e540 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -1567,11 +1567,11 @@ impl Daemon { } AccountEvent::Expiry(expiry) if *self.target_state == TargetState::Secured => { if expiry >= &chrono::Utc::now() { - if let TunnelState::Error(ref state) = self.tunnel_state { - if matches!(state.cause(), ErrorStateCause::AuthFailed(_)) { - log::debug!("Reconnecting since the account has time on it"); - self.connect_tunnel(); - } + if let TunnelState::Error(ref state) = self.tunnel_state + && matches!(state.cause(), ErrorStateCause::AuthFailed(_)) + { + log::debug!("Reconnecting since the account has time on it"); + self.connect_tunnel(); } } else if self.get_target_tunnel_type() == Some(TunnelType::Wireguard) { log::debug!("Entering blocking state since the account is out of time"); @@ -1778,10 +1778,10 @@ impl Daemon { let account_manager = self.account_manager.clone(); tokio::spawn(async move { let result = async { - if let Ok(data) = account_manager.data().await { - if data.logged_in() { - return Err(Error::AlreadyLoggedIn); - } + if let Ok(data) = account_manager.data().await + && data.logged_in() + { + return Err(Error::AlreadyLoggedIn); } let token = account_manager .account_service @@ -2808,13 +2808,13 @@ impl Daemon { { Ok(settings_changed) => { Self::oneshot_send(tx, Ok(()), "set_wireguard_mtu response"); - if settings_changed { - if let Some(TunnelType::Wireguard) = self.get_connected_tunnel_type() { - log::info!( - "Initiating tunnel restart because the WireGuard MTU setting changed" - ); - self.reconnect_tunnel(); - } + if settings_changed + && let Some(TunnelType::Wireguard) = self.get_connected_tunnel_type() + { + log::info!( + "Initiating tunnel restart because the WireGuard MTU setting changed" + ); + self.reconnect_tunnel(); } } Err(e) => { @@ -2836,8 +2836,8 @@ impl Daemon { { Ok(settings_changed) => { Self::oneshot_send(tx, Ok(()), "set_wireguard_rotation_interval response"); - if settings_changed { - if let Err(error) = self + if settings_changed + && let Err(error) = self .account_manager .set_rotation_interval(interval.unwrap_or_default()) .await @@ -2847,7 +2847,6 @@ impl Daemon { error.display_chain_with_msg("Failed to update rotation interval") ); } - } } Err(e) => { log::error!("{}", e.display_chain_with_msg("Unable to save settings")); @@ -2872,13 +2871,13 @@ impl Daemon { { Ok(settings_changed) => { Self::oneshot_send(tx, Ok(()), "set_wireguard_allowed_ips response"); - if settings_changed { - if let Some(TunnelType::Wireguard) = self.get_connected_tunnel_type() { - log::info!( - "Initiating tunnel restart because the WireGuard allowed IPs setting changed" - ); - self.reconnect_tunnel(); - } + if settings_changed + && let Some(TunnelType::Wireguard) = self.get_connected_tunnel_type() + { + log::info!( + "Initiating tunnel restart because the WireGuard allowed IPs setting changed" + ); + self.reconnect_tunnel(); } } Err(e) => { @@ -3491,9 +3490,9 @@ fn oneshot_map<T1: Send + 'static, T2: Send + 'static>( /// Remove any old RPC socket (if it exists). #[cfg(not(windows))] pub async fn cleanup_old_rpc_socket(rpc_socket_path: impl AsRef<std::path::Path>) { - if let Err(err) = tokio::fs::remove_file(rpc_socket_path).await { - if err.kind() != std::io::ErrorKind::NotFound { - log::error!("Failed to remove old RPC socket: {}", err); - } + if let Err(err) = tokio::fs::remove_file(rpc_socket_path).await + && err.kind() != std::io::ErrorKind::NotFound + { + log::error!("Failed to remove old RPC socket: {}", err); } } diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index 36af376ed3..2aac80f2b4 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -127,10 +127,10 @@ fn init_daemon_logging(config: &cli::Config) -> Result<Option<PathBuf>, String> fn init_early_boot_logging(config: &cli::Config) { // If it's possible to log to the filesystem - attempt to do so, but failing that mustn't stop // the daemon from starting here. - if let Ok(Some(log_dir)) = get_log_dir(config) { - if init_logger(config, Some(log_dir.join(EARLY_BOOT_LOG_FILENAME))).is_ok() { - return; - } + if let Ok(Some(log_dir)) = get_log_dir(config) + && init_logger(config, Some(log_dir.join(EARLY_BOOT_LOG_FILENAME))).is_ok() + { + return; } let _ = init_logger(config, None); diff --git a/mullvad-daemon/src/migrations/v1.rs b/mullvad-daemon/src/migrations/v1.rs index 44de9d1293..08d3a10d1a 100644 --- a/mullvad-daemon/src/migrations/v1.rs +++ b/mullvad-daemon/src/migrations/v1.rs @@ -44,23 +44,21 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<()> { .cloned() }(); - if let Some(relay_settings) = settings.get_mut("relay_settings") { - if let Some(normal_settings) = relay_settings.get_mut("normal") { - if let Some(openvpn_constraints) = openvpn_constraints { - normal_settings["openvpn_constraints"] = openvpn_constraints; - normal_settings["tunnel_protocol"] = - serde_json::json!(Constraint::<TunnelType>::Any); - } else if let Some(wireguard_constraints) = wireguard_constraints { - normal_settings["wireguard_constraints"] = wireguard_constraints; - normal_settings["tunnel_protocol"] = - serde_json::json!(Constraint::Only(TunnelType::Wireguard)); - } else { - normal_settings["tunnel_protocol"] = - serde_json::json!(Constraint::<TunnelType>::Any); - } - if let Some(object) = normal_settings.as_object_mut() { - object.remove("tunnel"); - } + if let Some(relay_settings) = settings.get_mut("relay_settings") + && let Some(normal_settings) = relay_settings.get_mut("normal") + { + if let Some(openvpn_constraints) = openvpn_constraints { + normal_settings["openvpn_constraints"] = openvpn_constraints; + normal_settings["tunnel_protocol"] = serde_json::json!(Constraint::<TunnelType>::Any); + } else if let Some(wireguard_constraints) = wireguard_constraints { + normal_settings["wireguard_constraints"] = wireguard_constraints; + normal_settings["tunnel_protocol"] = + serde_json::json!(Constraint::Only(TunnelType::Wireguard)); + } else { + normal_settings["tunnel_protocol"] = serde_json::json!(Constraint::<TunnelType>::Any); + } + if let Some(object) = normal_settings.as_object_mut() { + object.remove("tunnel"); } } diff --git a/mullvad-daemon/src/migrations/v3.rs b/mullvad-daemon/src/migrations/v3.rs index f59bc57b98..f4302568af 100644 --- a/mullvad-daemon/src/migrations/v3.rs +++ b/mullvad-daemon/src/migrations/v3.rs @@ -50,29 +50,29 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<()> { let dns_options = || -> Option<&serde_json::Value> { settings.get("tunnel_options")?.get("dns_options") }(); - if let Some(options) = dns_options { - if options.get("state").is_none() { - let new_state = if options - .get("custom") - .map(|custom| custom.as_bool().unwrap_or(false)) - .unwrap_or(false) - { - DnsState::Custom - } else { - DnsState::Default - }; - let addresses = if let Some(addrs) = options.get("addresses") { - serde_json::from_value(addrs.clone()).map_err(|_| Error::InvalidSettingsContent)? - } else { - vec![] - }; + if let Some(options) = dns_options + && options.get("state").is_none() + { + let new_state = if options + .get("custom") + .map(|custom| custom.as_bool().unwrap_or(false)) + .unwrap_or(false) + { + DnsState::Custom + } else { + DnsState::Default + }; + let addresses = if let Some(addrs) = options.get("addresses") { + serde_json::from_value(addrs.clone()).map_err(|_| Error::InvalidSettingsContent)? + } else { + vec![] + }; - settings["tunnel_options"]["dns_options"] = serde_json::json!(DnsOptions { - state: new_state, - default_options: DefaultDnsOptions::default(), - custom_options: CustomDnsOptions { addresses }, - }); - } + settings["tunnel_options"]["dns_options"] = serde_json::json!(DnsOptions { + state: new_state, + default_options: DefaultDnsOptions::default(), + custom_options: CustomDnsOptions { addresses }, + }); } settings["settings_version"] = serde_json::json!(SettingsVersion::V4); diff --git a/mullvad-daemon/src/migrations/v5.rs b/mullvad-daemon/src/migrations/v5.rs index 178832116a..920f43face 100644 --- a/mullvad-daemon/src/migrations/v5.rs +++ b/mullvad-daemon/src/migrations/v5.rs @@ -77,17 +77,17 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<Option<MigrationData> log::info!("Migrating settings format to V6"); if let Some(wireguard_constraints) = get_wireguard_constraints(settings) { - if let Some(location) = wireguard_constraints.get("entry_location") { - if wireguard_constraints.get("use_multihop").is_none() { - if location.is_null() { - // "Null" is no longer valid. It is not an option. - wireguard_constraints - .as_object_mut() - .ok_or(Error::InvalidSettingsContent)? - .remove("entry_location"); - } else { - wireguard_constraints["use_multihop"] = serde_json::json!(true); - } + if let Some(location) = wireguard_constraints.get("entry_location") + && wireguard_constraints.get("use_multihop").is_none() + { + if location.is_null() { + // "Null" is no longer valid. It is not an option. + wireguard_constraints + .as_object_mut() + .ok_or(Error::InvalidSettingsContent)? + .remove("entry_location"); + } else { + wireguard_constraints["use_multihop"] = serde_json::json!(true); } } // The field `pub port: Constraint<TransportPort>` is now `pub port: Constraint<u16>`. @@ -152,10 +152,10 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<Option<MigrationData> } fn get_wireguard_constraints(settings: &mut serde_json::Value) -> Option<&mut serde_json::Value> { - if let Some(relay_settings) = settings.get_mut("relay_settings") { - if let Some(normal) = relay_settings.get_mut("normal") { - return normal.get_mut("wireguard_constraints"); - } + if let Some(relay_settings) = settings.get_mut("relay_settings") + && let Some(normal) = relay_settings.get_mut("normal") + { + return normal.get_mut("wireguard_constraints"); } None } diff --git a/mullvad-daemon/src/migrations/v6.rs b/mullvad-daemon/src/migrations/v6.rs index c481be8d92..6f63a8fc77 100644 --- a/mullvad-daemon/src/migrations/v6.rs +++ b/mullvad-daemon/src/migrations/v6.rs @@ -87,8 +87,7 @@ fn migrate_pq_setting(settings: &mut serde_json::Value) -> Result<()> { if let Some(tunnel_options) = settings .get_mut("tunnel_options") .and_then(|opt| opt.get_mut("wireguard")) - { - if let Some(psk_setting) = tunnel_options + && let Some(psk_setting) = tunnel_options .as_object_mut() .ok_or(Error::InvalidSettingsContent)? .remove("use_pq_safe_psk") @@ -100,7 +99,6 @@ fn migrate_pq_setting(settings: &mut serde_json::Value) -> Result<()> { serde_json::json!(QuantumResistantState::Auto); } } - } Ok(()) } diff --git a/mullvad-daemon/src/settings/mod.rs b/mullvad-daemon/src/settings/mod.rs index 6a70074051..8a0c98478f 100644 --- a/mullvad-daemon/src/settings/mod.rs +++ b/mullvad-daemon/src/settings/mod.rs @@ -118,13 +118,11 @@ impl SettingsPersister { on_change_listeners: vec![], }; - if should_save { - if let Err(error) = persister.save().await { - log::error!( - "{}", - error.display_chain_with_msg("Failed to save updated settings") - ); - } + if should_save && let Err(error) = persister.save().await { + log::error!( + "{}", + error.display_chain_with_msg("Failed to save updated settings") + ); } persister |
