diff options
| author | David Lönnhager <david.l@mullvad.net> | 2019-12-11 09:42:26 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2019-12-17 12:30:15 +0100 |
| commit | a360edc98954e41e3280a44bb1a5e54507ad48eb (patch) | |
| tree | b663772d42d34f5faa6ccc0f65ce25f579b2c56e | |
| parent | 23ab1435382e8bca865e154ec440fb8847ab5b78 (diff) | |
| download | mullvadvpn-a360edc98954e41e3280a44bb1a5e54507ad48eb.tar.xz mullvadvpn-a360edc98954e41e3280a44bb1a5e54507ad48eb.zip | |
Format Rust code
| -rw-r--r-- | mullvad-cli/src/cmds/tunnel.rs | 7 | ||||
| -rw-r--r-- | mullvad-daemon/src/lib.rs | 27 | ||||
| -rw-r--r-- | mullvad-daemon/src/management_interface.rs | 10 | ||||
| -rw-r--r-- | mullvad-daemon/src/wireguard.rs | 76 | ||||
| -rw-r--r-- | mullvad-ipc-client/src/lib.rs | 5 | ||||
| -rw-r--r-- | mullvad-types/src/settings/mod.rs | 10 |
6 files changed, 82 insertions, 53 deletions
diff --git a/mullvad-cli/src/cmds/tunnel.rs b/mullvad-cli/src/cmds/tunnel.rs index 24949b95e2..c1e1e174e1 100644 --- a/mullvad-cli/src/cmds/tunnel.rs +++ b/mullvad-cli/src/cmds/tunnel.rs @@ -134,7 +134,9 @@ impl Tunnel { ("generate", _) => Self::process_wireguard_key_generate(), ("automatic-rotation", Some(matches)) => match matches.subcommand() { ("get", _) => Self::process_wireguard_automatic_rotation_get(), - ("set", Some(matches)) => Self::process_wireguard_automatic_rotation_set(matches), + ("set", Some(matches)) => { + Self::process_wireguard_automatic_rotation_set(matches) + } ("unset", _) => Self::process_wireguard_automatic_rotation_unset(), _ => unreachable!("unhandled command"), }, @@ -217,7 +219,8 @@ impl Tunnel { } fn process_wireguard_automatic_rotation_set(matches: &clap::ArgMatches<'_>) -> Result<()> { - let rotate_interval = value_t!(matches.value_of("interval"), u32).unwrap_or_else(|e| e.exit()); + let rotate_interval = + value_t!(matches.value_of("interval"), u32).unwrap_or_else(|e| e.exit()); let mut rpc = new_rpc_client()?; rpc.set_wireguard_automatic_rotation(Some(rotate_interval))?; println!("Wireguard automatic key rotation has been updated"); diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index dff218e9e7..7827bd1edc 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -832,7 +832,9 @@ where SetBridgeState(tx, bridge_state) => self.on_set_bridge_state(tx, bridge_state), SetEnableIpv6(tx, enable_ipv6) => self.on_set_enable_ipv6(tx, enable_ipv6), SetWireguardMtu(tx, mtu) => self.on_set_wireguard_mtu(tx, mtu), - SetWireguardAutomaticRotation(tx, interval) => self.on_set_wireguard_automatic_rotation(tx, interval), + SetWireguardAutomaticRotation(tx, interval) => { + self.on_set_wireguard_automatic_rotation(tx, interval) + } GetSettings(tx) => self.on_get_settings(tx), GenerateWireguardKey(tx) => self.on_generate_wireguard_key(tx), GetWireguardKey(tx) => self.on_get_wireguard_key(tx), @@ -1357,7 +1359,11 @@ where } } - fn on_set_wireguard_automatic_rotation(&mut self, tx: oneshot::Sender<()>, interval: Option<u32>) { + fn on_set_wireguard_automatic_rotation( + &mut self, + tx: oneshot::Sender<()>, + interval: Option<u32>, + ) { let save_result = self.settings.set_wireguard_automatic_rotation(interval); match save_result { Ok(settings_changed) => { @@ -1365,16 +1371,19 @@ where if settings_changed { self.event_listener.notify_settings(self.settings.clone()); - let public_key = self.settings.get_account_token().and_then(|token| - self.account_history.get(&token) + let public_key = self.settings.get_account_token().and_then(|token| { + self.account_history + .get(&token) .unwrap_or(None) .and_then(|entry| entry.wireguard.map(|wg| wg.get_public_key())) - ); - - self.wireguard_key_manager.update_rotation_interval(wireguard::KeyRotationParameters { - public_key, - interval, }); + + self.wireguard_key_manager.update_rotation_interval( + wireguard::KeyRotationParameters { + public_key, + interval, + }, + ); } } Err(e) => error!("{}", e.display_chain_with_msg("Unable to save settings")), diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs index 2af9b441a3..feb1adb8e3 100644 --- a/mullvad-daemon/src/management_interface.rs +++ b/mullvad-daemon/src/management_interface.rs @@ -702,11 +702,17 @@ impl<T: From<ManagementCommand> + 'static + Send> ManagementInterfaceApi } /// Set automatic key rotation interval for wireguard tunnels - fn set_wireguard_automatic_rotation(&self, _: Self::Metadata, interval: Option<u32>) -> BoxFuture<(), Error> { + fn set_wireguard_automatic_rotation( + &self, + _: Self::Metadata, + interval: Option<u32>, + ) -> BoxFuture<(), Error> { log::debug!("set_wireguard_automatic_rotation({:?})", interval); let (tx, rx) = sync::oneshot::channel(); let future = self - .send_command_to_daemon(ManagementCommand::SetWireguardAutomaticRotation(tx, interval)) + .send_command_to_daemon(ManagementCommand::SetWireguardAutomaticRotation( + tx, interval, + )) .and_then(|_| rx.map_err(|_| Error::internal_error())); Box::new(future) } diff --git a/mullvad-daemon/src/wireguard.rs b/mullvad-daemon/src/wireguard.rs index 4f17478143..ee23483906 100644 --- a/mullvad-daemon/src/wireguard.rs +++ b/mullvad-daemon/src/wireguard.rs @@ -1,10 +1,14 @@ use crate::InternalDaemonEvent; -use chrono::{DateTime, offset::Utc}; +use chrono::{offset::Utc, DateTime}; use futures::{future::Executor, sync::oneshot, Async, Future, Poll}; use jsonrpc_client_core::Error as JsonRpcError; use mullvad_types::account::AccountToken; pub use mullvad_types::wireguard::*; -use std::{cmp, sync::mpsc, time::{Duration, Instant}}; +use std::{ + cmp, + sync::mpsc, + time::{Duration, Instant}, +}; pub use talpid_types::net::wireguard::{ ConnectionConfig, PrivateKey, TunnelConfig, TunnelParameters, }; @@ -44,7 +48,6 @@ pub enum Error { pub type Result<T> = std::result::Result<T, Error>; use crate::ManagementCommand; -use talpid_core::tunnel_state_machine::TunnelCommand; use mullvad_types::wireguard; @@ -77,9 +80,12 @@ impl Future for KeyRotationScheduler { _ => { log::error!("Automatic key rotation failed; retrying"); self.key_request_rx = None; - self.delay = Box::new(Delay::new( - Instant::now() + Duration::from_secs(AUTOMATIC_ROTATION_RETRY_DELAY) - ).map_err(|_| ())); + self.delay = Box::new( + Delay::new( + Instant::now() + Duration::from_secs(AUTOMATIC_ROTATION_RETRY_DELAY), + ) + .map_err(|_| ()), + ); } } } @@ -91,10 +97,12 @@ impl Future for KeyRotationScheduler { } let (wg_tx, wg_rx) = oneshot::channel(); - let _ = self.daemon_tx.send(InternalDaemonEvent::ManagementInterfaceEvent( - ManagementCommand::GenerateWireguardKey(wg_tx) - )) - .map_err(|_| Error::RunAutomaticKeyRotation)?; + let _ = self + .daemon_tx + .send(InternalDaemonEvent::ManagementInterfaceEvent( + ManagementCommand::GenerateWireguardKey(wg_tx), + )) + .map_err(|_| Error::RunAutomaticKeyRotation)?; log::debug!("Sent key replacement request"); @@ -111,10 +119,7 @@ impl KeyRotationScheduler { public_key: Option<PublicKey>, interval: Option<u32>, ) -> Result<oneshot::Sender<()>> { - let ( - terminate_auto_rotation_tx, - terminate_auto_rotation_rx - ) = oneshot::channel(); + let (terminate_auto_rotation_tx, terminate_auto_rotation_rx) = oneshot::channel(); let interval = interval.unwrap_or(DEFAULT_AUTOMATIC_KEY_ROTATION); let last_update = public_key.map(|key| key.created.clone()); @@ -127,24 +132,25 @@ impl KeyRotationScheduler { key_request_rx: None, }; - tokio_remote.execute( - fut.map_err(|e| { - log::error!("Failed to run key rotation scheduler: {}", e) - }) - .select(terminate_auto_rotation_rx.map_err(|_| ())) - .map_err(|_| ()) - .map(|_| ()) - ).map_err(|e| { - log::error!("Failed to run key rotation scheduler: {:?}", e); - Error::CreateAutomaticKeyRotationScheduler - })?; + tokio_remote + .execute( + fut.map_err(|e| log::error!("Failed to run key rotation scheduler: {}", e)) + .select(terminate_auto_rotation_rx.map_err(|_| ())) + .map_err(|_| ()) + .map(|_| ()), + ) + .map_err(|e| { + log::error!("Failed to run key rotation scheduler: {:?}", e); + Error::CreateAutomaticKeyRotationScheduler + })?; Ok(terminate_auto_rotation_tx) } - fn next_delay(interval_mins: u32, last_update: Option<DateTime<Utc>>) -> - Box<dyn Future<Item = (), Error = ()> + Send> - { + fn next_delay( + interval_mins: u32, + last_update: Option<DateTime<Utc>>, + ) -> Box<dyn Future<Item = (), Error = ()> + Send> { let mut delay = Duration::from_secs(60u64 * interval_mins as u64); log::debug!( @@ -155,11 +161,9 @@ impl KeyRotationScheduler { if let Some(last_update) = last_update { // Check when the key should expire let key_age = Duration::from_secs( - (Utc::now().signed_duration_since(last_update)).num_seconds() as u64 - ); - let remaining_time = delay.checked_sub(key_age).unwrap_or( - Duration::from_secs(0) + (Utc::now().signed_duration_since(last_update)).num_seconds() as u64, ); + let remaining_time = delay.checked_sub(key_age).unwrap_or(Duration::from_secs(0)); delay = cmp::max(Duration::from_secs(0), cmp::min(remaining_time, delay)); } @@ -202,10 +206,7 @@ impl KeyManager { /// Update automatic key rotation interval (given in hours) /// Passing `None` for the interval will use the default value. /// A value of `0` disables automatic key rotation. - pub fn update_rotation_interval( - &mut self, - automatic_key_rotation: KeyRotationParameters, - ) { + pub fn update_rotation_interval(&mut self, automatic_key_rotation: KeyRotationParameters) { log::debug!("update_rotation_interval"); if self.abort_scheduler_tx.is_some() { // Stop existing scheduler, if one exists @@ -220,7 +221,8 @@ impl KeyManager { self.daemon_tx.clone(), automatic_key_rotation.public_key, automatic_key_rotation.interval, - ).ok(), + ) + .ok(), }; } diff --git a/mullvad-ipc-client/src/lib.rs b/mullvad-ipc-client/src/lib.rs index 60abda2a09..2e08eada83 100644 --- a/mullvad-ipc-client/src/lib.rs +++ b/mullvad-ipc-client/src/lib.rs @@ -199,7 +199,10 @@ impl DaemonRpcClient { self.call("set_wireguard_mtu", &[mtu]) } - pub fn set_wireguard_automatic_rotation(&mut self, automatic_rotation: Option<u32>) -> Result<()> { + pub fn set_wireguard_automatic_rotation( + &mut self, + automatic_rotation: Option<u32>, + ) -> Result<()> { self.call("set_wireguard_automatic_rotation", &[automatic_rotation]) } diff --git a/mullvad-types/src/settings/mod.rs b/mullvad-types/src/settings/mod.rs index e5049f2aa1..893dbee849 100644 --- a/mullvad-types/src/settings/mod.rs +++ b/mullvad-types/src/settings/mod.rs @@ -284,7 +284,10 @@ impl Settings { } } - pub fn set_wireguard_automatic_rotation(&mut self, automatic_rotation: Option<u32>) -> Result<bool> { + pub fn set_wireguard_automatic_rotation( + &mut self, + automatic_rotation: Option<u32>, + ) -> Result<bool> { if self.tunnel_options.wireguard.automatic_rotation != automatic_rotation { self.tunnel_options.wireguard.automatic_rotation = automatic_rotation; self.save().map(|_| true) @@ -343,7 +346,10 @@ impl Default for TunnelOptions { fn default() -> Self { TunnelOptions { openvpn: openvpn::TunnelOptions::default(), - wireguard: wireguard::TunnelOptions { mtu: None, automatic_rotation: None }, + wireguard: wireguard::TunnelOptions { + mtu: None, + automatic_rotation: None, + }, generic: GenericTunnelOptions { enable_ipv6: false }, } } |
