diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-09-18 22:46:47 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-09-18 22:46:47 +0200 |
| commit | 87a0ab01ae8f82ea7f7592b4b59899e479391419 (patch) | |
| tree | 3562177e2a6c567d82e031e2aba3f6c60b6710d4 /talpid-core/src | |
| parent | 5977ca669756600bc39c7148f16e79a2569c36d4 (diff) | |
| download | mullvadvpn-87a0ab01ae8f82ea7f7592b4b59899e479391419.tar.xz mullvadvpn-87a0ab01ae8f82ea7f7592b4b59899e479391419.zip | |
Run rustfmt-nightly 0.2.6 on everything
Diffstat (limited to 'talpid-core/src')
| -rw-r--r-- | talpid-core/src/firewall/macos.rs | 23 | ||||
| -rw-r--r-- | talpid-core/src/firewall/mod.rs | 12 | ||||
| -rw-r--r-- | talpid-core/src/lib.rs | 2 | ||||
| -rw-r--r-- | talpid-core/src/mktemp.rs | 4 | ||||
| -rw-r--r-- | talpid-core/src/mpsc.rs | 10 | ||||
| -rw-r--r-- | talpid-core/src/process/openvpn.rs | 16 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/mod.rs | 45 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/openvpn.rs | 111 |
8 files changed, 124 insertions, 99 deletions
diff --git a/talpid-core/src/firewall/macos.rs b/talpid-core/src/firewall/macos.rs index e3bfa9870f..14c9fb5033 100644 --- a/talpid-core/src/firewall/macos.rs +++ b/talpid-core/src/firewall/macos.rs @@ -16,12 +16,10 @@ pub struct PacketFilter { impl Firewall<Error> for PacketFilter { fn new() -> Result<Self> { - Ok( - PacketFilter { - pf: pfctl::PfCtl::new()?, - pf_was_enabled: None, - }, - ) + Ok(PacketFilter { + pf: pfctl::PfCtl::new()?, + pf_was_enabled: None, + }) } fn apply_policy(&mut self, policy: SecurityPolicy) -> Result<()> { @@ -35,10 +33,9 @@ impl Firewall<Error> for PacketFilter { self.remove_rules(), self.remove_anchor(), self.restore_state(), - ] - .into_iter() - .collect::<Result<Vec<_>>>() - .map(|_| ()) + ].into_iter() + .collect::<Result<Vec<_>>>() + .map(|_| ()) } } @@ -160,11 +157,13 @@ impl PacketFilter { } fn add_anchor(&mut self) -> Result<()> { - self.pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter) + self.pf + .try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter) } fn remove_anchor(&mut self) -> Result<()> { - self.pf.try_remove_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter) + self.pf + .try_remove_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter) } } diff --git a/talpid-core/src/firewall/mod.rs b/talpid-core/src/firewall/mod.rs index acc0f21792..a1fa7edbc3 100644 --- a/talpid-core/src/firewall/mod.rs +++ b/talpid-core/src/firewall/mod.rs @@ -38,7 +38,9 @@ pub enum SecurityPolicy { /// Abstract firewall interaction trait pub trait Firewall<E: ::std::error::Error> { /// Create new instance of Firewall - fn new() -> ::std::result::Result<Self, E> where Self: Sized; + fn new() -> ::std::result::Result<Self, E> + where + Self: Sized; /// Enable firewall and set firewall rules based on SecurityPolicy fn apply_policy(&mut self, policy: SecurityPolicy) -> ::std::result::Result<(), E>; @@ -58,10 +60,14 @@ impl Firewall<Error> for FirewallProxy { } fn apply_policy(&mut self, policy: SecurityPolicy) -> Result<()> { - self.0.apply_policy(policy).chain_err(|| ErrorKind::FirewallConfigurationError) + self.0 + .apply_policy(policy) + .chain_err(|| ErrorKind::FirewallConfigurationError) } fn reset_policy(&mut self) -> Result<()> { - self.0.reset_policy().chain_err(|| ErrorKind::FirewallConfigurationError) + self.0 + .reset_policy() + .chain_err(|| ErrorKind::FirewallConfigurationError) } } diff --git a/talpid-core/src/lib.rs b/talpid-core/src/lib.rs index c4cff6ebfc..c14377919e 100644 --- a/talpid-core/src/lib.rs +++ b/talpid-core/src/lib.rs @@ -29,9 +29,9 @@ extern crate jsonrpc_core; extern crate jsonrpc_macros; extern crate uuid; +extern crate openvpn_plugin; extern crate talpid_ipc; extern crate talpid_types; -extern crate openvpn_plugin; #[cfg(target_os = "macos")] extern crate pfctl; diff --git a/talpid-core/src/mktemp.rs b/talpid-core/src/mktemp.rs index e8ba33c3c5..fc39127069 100644 --- a/talpid-core/src/mktemp.rs +++ b/talpid-core/src/mktemp.rs @@ -13,7 +13,9 @@ pub struct TempFile { impl TempFile { /// Create a new unique `TempFile`. The file will not exist after this. pub fn new() -> Self { - TempFile { path: generate_path() } + TempFile { + path: generate_path(), + } } pub fn to_path_buf(&self) -> PathBuf { diff --git a/talpid-core/src/mpsc.rs b/talpid-core/src/mpsc.rs index d63956c452..533dd857c1 100644 --- a/talpid-core/src/mpsc.rs +++ b/talpid-core/src/mpsc.rs @@ -9,7 +9,8 @@ pub struct IntoSender<T, U> { } impl<T, U> IntoSender<T, U> - where T: Into<U> +where + T: Into<U>, { /// Converts the `T` into a `U` and sends it on the channel. pub fn send(&self, t: T) -> Result<(), mpsc::SendError<U>> { @@ -18,7 +19,8 @@ impl<T, U> IntoSender<T, U> } impl<T, U> From<mpsc::Sender<U>> for IntoSender<T, U> - where T: Into<U> +where + T: Into<U>, { fn from(sender: mpsc::Sender<U>) -> Self { IntoSender { @@ -69,7 +71,9 @@ mod tests { let (tx, rx) = mpsc::channel::<Outer>(); let inner_tx: IntoSender<Inner, Outer> = tx.clone().into(); - thread::spawn(move || { inner_tx.send(Inner::One).unwrap(); }); + thread::spawn(move || { + inner_tx.send(Inner::One).unwrap(); + }); assert_eq!(Outer::Inner(Inner::One), rx.recv().unwrap()); } diff --git a/talpid-core/src/process/openvpn.rs b/talpid-core/src/process/openvpn.rs index c756757a75..ce6039bcb3 100644 --- a/talpid-core/src/process/openvpn.rs +++ b/talpid-core/src/process/openvpn.rs @@ -64,7 +64,8 @@ impl OpenVpnCommand { self } - /// Sets the path to the file where the username and password for user-pass authentication is + /// Sets the path to the file where the username and password for user-pass authentication + /// is /// stored. See the `--auth-user-pass` OpenVPN documentation for details. pub fn user_pass<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { self.user_pass_path = Some(path.as_ref().to_path_buf()); @@ -138,12 +139,10 @@ impl OpenVpnCommand { let mut args: Vec<String> = vec![]; if let Some(ref endpoint) = self.remote { args.push("--proto".to_owned()); - args.push( - match endpoint.protocol { - net::TransportProtocol::Udp => "udp".to_owned(), - net::TransportProtocol::Tcp => "tcp-client".to_owned(), - }, - ); + args.push(match endpoint.protocol { + net::TransportProtocol::Udp => "udp".to_owned(), + net::TransportProtocol::Tcp => "tcp-client".to_owned(), + }); args.push("--remote".to_owned()); args.push(endpoint.address.ip().to_string()); args.push(endpoint.address.port().to_string()); @@ -162,7 +161,8 @@ impl OpenVpnCommand { } impl fmt::Display for OpenVpnCommand { - /// Format the program and arguments of an `OpenVpnCommand` for display. Any non-utf8 data is + /// Format the program and arguments of an `OpenVpnCommand` for display. Any non-utf8 data + /// is /// lossily converted using the utf8 replacement character. fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.write_str(&self.openvpn_bin.to_string_lossy())?; diff --git a/talpid-core/src/tunnel/mod.rs b/talpid-core/src/tunnel/mod.rs index 83212e6874..5f73e5be20 100644 --- a/talpid-core/src/tunnel/mod.rs +++ b/talpid-core/src/tunnel/mod.rs @@ -57,13 +57,15 @@ pub enum TunnelEvent { impl TunnelEvent { /// Converts an `OpenVpnPluginEvent` to a `TunnelEvent`. /// Returns `None` if there is no corresponding `TunnelEvent`. - fn from_openvpn_event(event: &OpenVpnPluginEvent, - env: &HashMap<String, String>) - -> Option<TunnelEvent> { + fn from_openvpn_event( + event: &OpenVpnPluginEvent, + env: &HashMap<String, String>, + ) -> Option<TunnelEvent> { match *event { OpenVpnPluginEvent::Up => { - let tunnel_interface = - env.get("dev").expect("No \"dev\" in tunnel up event").to_owned(); + let tunnel_interface = env.get("dev") + .expect("No \"dev\" in tunnel up event") + .to_owned(); Some(TunnelEvent::Up { tunnel_interface }) } OpenVpnPluginEvent::RoutePredown => Some(TunnelEvent::Down), @@ -84,7 +86,8 @@ impl TunnelMonitor { /// Creates a new `TunnelMonitor` that connects to the given remote and notifies `on_event` /// on tunnel state changes. pub fn new<L>(remote: net::Endpoint, account_token: &str, on_event: L) -> Result<Self> - where L: Fn(TunnelEvent) + Send + Sync + 'static + where + L: Fn(TunnelEvent) + Send + Sync + 'static, { let user_pass_file = Self::create_user_pass_file(account_token) .chain_err(|| ErrorKind::CredentialsWriteError)?; @@ -104,12 +107,10 @@ impl TunnelMonitor { let monitor = openvpn::OpenVpnMonitor::new(cmd, on_openvpn_event, Self::get_plugin_path()?) .chain_err(|| ErrorKind::TunnelMonitoringError)?; - Ok( - TunnelMonitor { - monitor, - _user_pass_file: user_pass_file, - }, - ) + Ok(TunnelMonitor { + monitor, + _user_pass_file: user_pass_file, + }) } fn create_openvpn_cmd(remote: net::Endpoint, user_pass_file: &Path) -> OpenVpnCommand { @@ -117,7 +118,9 @@ impl TunnelMonitor { if let Some(config) = Self::get_config_path() { cmd.config(config); } - cmd.remote(remote).user_pass(user_pass_file).ca(Self::get_ca_path()); + cmd.remote(remote) + .user_pass(user_pass_file) + .ca(Self::get_ca_path()); cmd } @@ -143,8 +146,7 @@ impl TunnelMonitor { } fn get_plugin_path() -> Result<PathBuf> { - let lib_ext = Self::get_library_extension() - .chain_err(|| ErrorKind::PluginNotFound)?; + let lib_ext = Self::get_library_extension().chain_err(|| ErrorKind::PluginNotFound)?; let path = Self::get_install_dir() .unwrap_or(PathBuf::from(".")) @@ -175,7 +177,11 @@ impl TunnelMonitor { .unwrap_or(PathBuf::from(".")) .join("openvpn.conf"); - if path.exists() { Some(path) } else { None } + if path.exists() { + Some(path) + } else { + None + } } fn get_install_dir() -> Option<PathBuf> { @@ -215,7 +221,8 @@ impl TunnelMonitor { Ok(()) } - /// Creates a handle to this monitor, allowing the tunnel to be closed while some other thread + /// Creates a handle to this monitor, allowing the tunnel to be closed while some other + /// thread /// is blocked in `wait`. pub fn close_handle(&self) -> CloseHandle { CloseHandle(self.monitor.close_handle()) @@ -223,7 +230,9 @@ impl TunnelMonitor { /// Consumes the monitor and block until the tunnel exits or there is an error. pub fn wait(self) -> Result<()> { - self.monitor.wait().chain_err(|| ErrorKind::TunnelMonitoringError) + self.monitor + .wait() + .chain_err(|| ErrorKind::TunnelMonitoringError) } } diff --git a/talpid-core/src/tunnel/openvpn.rs b/talpid-core/src/tunnel/openvpn.rs index 368fb20257..c9ca9b49ef 100644 --- a/talpid-core/src/tunnel/openvpn.rs +++ b/talpid-core/src/tunnel/openvpn.rs @@ -8,7 +8,7 @@ use std::io; use std::path::Path; use std::process::ExitStatus; use std::result::Result as StdResult; -use std::sync::{Arc, mpsc}; +use std::sync::{mpsc, Arc}; use std::sync::atomic::{AtomicBool, Ordering}; use std::thread; use std::time::Duration; @@ -50,8 +50,9 @@ impl OpenVpnMonitor<OpenVpnCommand> { /// Creates a new `OpenVpnMonitor` with the given listener and using the plugin at the given /// path. pub fn new<L, P>(cmd: OpenVpnCommand, on_event: L, plugin_path: P) -> Result<Self> - where L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static, - P: AsRef<Path> + where + L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static, + P: AsRef<Path>, { Self::new_internal(cmd, on_event, plugin_path) } @@ -59,25 +60,26 @@ impl OpenVpnMonitor<OpenVpnCommand> { impl<C: OpenVpnBuilder> OpenVpnMonitor<C> { fn new_internal<L, P>(mut cmd: C, on_event: L, plugin_path: P) -> Result<OpenVpnMonitor<C>> - where L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static, - P: AsRef<Path> + where + L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static, + P: AsRef<Path>, { - let event_dispatcher = OpenVpnEventDispatcher::start(on_event) - .chain_err(|| ErrorKind::EventDispatcherError)?; + let event_dispatcher = + OpenVpnEventDispatcher::start(on_event).chain_err(|| ErrorKind::EventDispatcherError)?; cmd.plugin(plugin_path, vec![event_dispatcher.address().to_owned()]); - let child = cmd.start().chain_err(|| ErrorKind::ChildProcessError("Failed to start"))?; + let child = cmd.start() + .chain_err(|| ErrorKind::ChildProcessError("Failed to start"))?; - Ok( - OpenVpnMonitor { - child: Arc::new(child), - event_dispatcher: Some(event_dispatcher), - closed: Arc::new(AtomicBool::new(false)), - }, - ) + Ok(OpenVpnMonitor { + child: Arc::new(child), + event_dispatcher: Some(event_dispatcher), + closed: Arc::new(AtomicBool::new(false)), + }) } - /// Creates a handle to this monitor, allowing the tunnel to be closed while some other thread + /// Creates a handle to this monitor, allowing the tunnel to be closed while some other + /// thread /// is blocked in `wait`. pub fn close_handle(&self) -> OpenVpnCloseHandle<C::ProcessHandle> { OpenVpnCloseHandle { @@ -86,22 +88,21 @@ impl<C: OpenVpnBuilder> OpenVpnMonitor<C> { } } - /// Consumes the monitor and blocks until OpenVPN exits or there is an error in either waiting + /// Consumes the monitor and blocks until OpenVPN exits or there is an error in either + /// waiting /// for the process or in the event dispatcher. pub fn wait(mut self) -> Result<()> { match self.wait_result() { - WaitResult::Child(Ok(exit_status), closed) => { - if exit_status.success() || closed { - debug!( - "OpenVPN exited, as expected, with exit status: {}", - exit_status - ); - Ok(()) - } else { - error!("OpenVPN died unexpectedly with status: {}", exit_status); - Err(ErrorKind::ChildProcessError("Died unexpectedly").into()) - } - } + WaitResult::Child(Ok(exit_status), closed) => if exit_status.success() || closed { + debug!( + "OpenVPN exited, as expected, with exit status: {}", + exit_status + ); + Ok(()) + } else { + error!("OpenVPN died unexpectedly with status: {}", exit_status); + Err(ErrorKind::ChildProcessError("Died unexpectedly").into()) + }, WaitResult::Child(Err(e), _) => { error!("OpenVPN process wait error: {}", e); Err(e).chain_err(|| ErrorKind::ChildProcessError("Error when waiting")) @@ -128,21 +129,19 @@ impl<C: OpenVpnBuilder> OpenVpnMonitor<C> { let (child_tx, rx) = mpsc::channel(); let dispatcher_tx = child_tx.clone(); - thread::spawn( - move || { - let result = child_wait_handle.wait(); - let closed = closed_handle.load(Ordering::SeqCst); - child_tx.send(WaitResult::Child(result, closed)).unwrap(); - dispatcher_handle.close(); - }, - ); - thread::spawn( - move || { - let result = event_dispatcher.wait(); - dispatcher_tx.send(WaitResult::EventDispatcher(result)).unwrap(); - let _ = child_close_handle.close(); - }, - ); + thread::spawn(move || { + let result = child_wait_handle.wait(); + let closed = closed_handle.load(Ordering::SeqCst); + child_tx.send(WaitResult::Child(result, closed)).unwrap(); + dispatcher_handle.close(); + }); + thread::spawn(move || { + let result = event_dispatcher.wait(); + dispatcher_tx + .send(WaitResult::EventDispatcher(result)) + .unwrap(); + let _ = child_close_handle.close(); + }); let result = rx.recv().unwrap(); let _ = rx.recv().unwrap(); @@ -235,7 +234,8 @@ pub struct OpenVpnEventDispatcher { impl OpenVpnEventDispatcher { /// Construct and start the IPC server with the given event listener callback. pub fn start<L>(on_event: L) -> talpid_ipc::Result<Self> - where L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static + where + L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static, { let rpc = OpenVpnEventApiImpl { on_event }; let mut io = IoHandler::new(); @@ -249,7 +249,8 @@ impl OpenVpnEventDispatcher { self.server.address() } - /// Creates a handle to this event dispatcher, allowing the listening server to be closed while + /// Creates a handle to this event dispatcher, allowing the listening server to be closed + /// while /// some other thread is blocked in `wait`. pub fn close_handle(&self) -> talpid_ipc::CloseHandle { self.server.close_handle() @@ -278,18 +279,21 @@ mod api { use self::api::*; struct OpenVpnEventApiImpl<L> - where L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static +where + L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static, { on_event: L, } impl<L> OpenVpnEventApi for OpenVpnEventApiImpl<L> - where L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static +where + L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static, { - fn openvpn_event(&self, - event: OpenVpnPluginEvent, - env: HashMap<String, String>) - -> StdResult<(), Error> { + fn openvpn_event( + &self, + event: OpenVpnPluginEvent, + env: HashMap<String, String>, + ) -> StdResult<(), Error> { debug!("OpenVPN event {:?}", event); (self.on_event)(event, env); Ok(()) @@ -319,7 +323,8 @@ mod tests { } fn start(&self) -> io::Result<Self::ProcessHandle> { - self.process_handle.ok_or(io::Error::new(io::ErrorKind::Other, "failed to start")) + self.process_handle + .ok_or(io::Error::new(io::ErrorKind::Other, "failed to start")) } } |
