diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-09-26 23:08:04 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-09-26 23:08:04 +0200 |
| commit | 309fb57ff25726245b7662070de6fe1fa30fe0d8 (patch) | |
| tree | b012bdb6e1bcb03580c5f0d4b22acacb65366f45 | |
| parent | bcb69a48802ce84116e61285e6f1b582ed717779 (diff) | |
| download | mullvadvpn-309fb57ff25726245b7662070de6fe1fa30fe0d8.tar.xz mullvadvpn-309fb57ff25726245b7662070de6fe1fa30fe0d8.zip | |
Remove OpenVpnEventDispatcher and use IpcServer directly
| -rw-r--r-- | talpid-core/src/tunnel/openvpn.rs | 91 |
1 files changed, 30 insertions, 61 deletions
diff --git a/talpid-core/src/tunnel/openvpn.rs b/talpid-core/src/tunnel/openvpn.rs index c9ca9b49ef..caa6399849 100644 --- a/talpid-core/src/tunnel/openvpn.rs +++ b/talpid-core/src/tunnel/openvpn.rs @@ -1,5 +1,4 @@ use duct; -use jsonrpc_core::{Error, IoHandler}; use openvpn_plugin::types::OpenVpnPluginEvent; use process::openvpn::OpenVpnCommand; @@ -7,7 +6,6 @@ use std::collections::HashMap; use std::io; use std::path::Path; use std::process::ExitStatus; -use std::result::Result as StdResult; use std::sync::{mpsc, Arc}; use std::sync::atomic::{AtomicBool, Ordering}; use std::thread; @@ -42,7 +40,7 @@ lazy_static!{ #[derive(Debug)] pub struct OpenVpnMonitor<C: OpenVpnBuilder = OpenVpnCommand> { child: Arc<C::ProcessHandle>, - event_dispatcher: Option<OpenVpnEventDispatcher>, + event_dispatcher: Option<talpid_ipc::IpcServer>, closed: Arc<AtomicBool>, } @@ -65,7 +63,7 @@ impl<C: OpenVpnBuilder> OpenVpnMonitor<C> { P: AsRef<Path>, { let event_dispatcher = - OpenVpnEventDispatcher::start(on_event).chain_err(|| ErrorKind::EventDispatcherError)?; + event_server::start(on_event).chain_err(|| ErrorKind::EventDispatcherError)?; cmd.plugin(plugin_path, vec![event_dispatcher.address().to_owned()]); let child = cmd.start() @@ -108,7 +106,7 @@ impl<C: OpenVpnBuilder> OpenVpnMonitor<C> { Err(e).chain_err(|| ErrorKind::ChildProcessError("Error when waiting")) } WaitResult::EventDispatcher(result) => { - error!("OpenVpnEventDispatcher exited unexpectedly: {:?}", result); + error!("OpenVPN Event server exited unexpectedly: {:?}", result); match result { Ok(()) => Err(ErrorKind::EventDispatcherError.into()), Err(e) => Err(e).chain_err(|| ErrorKind::EventDispatcherError), @@ -225,78 +223,49 @@ impl ProcessHandle for duct::Handle { } -/// IPC server for listening to events coming from plugin loaded into OpenVPN. -#[derive(Debug)] -pub struct OpenVpnEventDispatcher { - server: talpid_ipc::IpcServer, -} -impl OpenVpnEventDispatcher { +mod event_server { + use super::OpenVpnPluginEvent; + use jsonrpc_core::{Error, IoHandler}; + use std::collections::HashMap; + use talpid_ipc; + /// Construct and start the IPC server with the given event listener callback. - pub fn start<L>(on_event: L) -> talpid_ipc::Result<Self> + pub fn start<L>(on_event: L) -> talpid_ipc::Result<talpid_ipc::IpcServer> where L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static, { - let rpc = OpenVpnEventApiImpl { on_event }; + let rpc = OpenVpnEventApiImpl(on_event); let mut io = IoHandler::new(); io.extend_with(rpc.to_delegate()); - let server = talpid_ipc::IpcServer::start(io.into())?; - Ok(OpenVpnEventDispatcher { server }) - } - - /// Returns the local address this server is listening on. - pub fn address(&self) -> &str { - self.server.address() + talpid_ipc::IpcServer::start(io.into()) } - /// 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() - } - - /// Consumes the server and waits for it to finish. Returns an error if the server exited - /// due to an error. - pub fn wait(self) -> talpid_ipc::Result<()> { - self.server.wait() - } -} - - -mod api { - use super::*; build_rpc_trait! { pub trait OpenVpnEventApi { #[rpc(name = "openvpn_event")] - fn openvpn_event(&self, - OpenVpnPluginEvent, - HashMap<String, String>) - -> StdResult<(), Error>; + fn openvpn_event(&self, OpenVpnPluginEvent, HashMap<String, String>) + -> Result<(), Error>; } } -} -use self::api::*; -struct OpenVpnEventApiImpl<L> -where - L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static, -{ - on_event: L, -} + struct OpenVpnEventApiImpl<L>(L) + where + L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static; -impl<L> OpenVpnEventApi for OpenVpnEventApiImpl<L> -where - L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static, -{ - fn openvpn_event( - &self, - event: OpenVpnPluginEvent, - env: HashMap<String, String>, - ) -> StdResult<(), Error> { - debug!("OpenVPN event {:?}", event); - (self.on_event)(event, env); - Ok(()) + impl<L> OpenVpnEventApi for OpenVpnEventApiImpl<L> + where + L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static, + { + fn openvpn_event( + &self, + event: OpenVpnPluginEvent, + env: HashMap<String, String>, + ) -> Result<(), Error> { + debug!("OpenVPN event {:?}", event); + (self.0)(event, env); + Ok(()) + } } } |
