diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-05-19 08:03:55 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-05-19 08:03:55 +0200 |
| commit | 5a29ec07f4d0bc41da0b64435ff156ff37e88c22 (patch) | |
| tree | d406200f56af2118901d657b7efa26886fed3e38 /talpid_core | |
| parent | 2f2c5b853ab376948e61e9f371cc724ac07ab5bb (diff) | |
| parent | 927888f8767f814f98c666af854ba03a09909897 (diff) | |
| download | mullvadvpn-5a29ec07f4d0bc41da0b64435ff156ff37e88c22.tar.xz mullvadvpn-5a29ec07f4d0bc41da0b64435ff156ff37e88c22.zip | |
Merge branch 'openvpn-event-dispatcher'
Diffstat (limited to 'talpid_core')
| -rw-r--r-- | talpid_core/Cargo.toml | 2 | ||||
| -rw-r--r-- | talpid_core/src/lib.rs | 7 | ||||
| -rw-r--r-- | talpid_core/src/process/openvpn.rs | 3 | ||||
| -rw-r--r-- | talpid_core/src/tunnel/mod.rs | 2 | ||||
| -rw-r--r-- | talpid_core/src/tunnel/openvpn.rs | 79 |
5 files changed, 91 insertions, 2 deletions
diff --git a/talpid_core/Cargo.toml b/talpid_core/Cargo.toml index c098d204d9..18c998cf93 100644 --- a/talpid_core/Cargo.toml +++ b/talpid_core/Cargo.toml @@ -8,6 +8,8 @@ description = "Core backend functionality of the Mullvad VPN client" duct = "0.8" error-chain = "0.10" log = "0.3" +jsonrpc-core = { git = "https://github.com/faern/jsonrpc", branch = "bind-zero" } +jsonrpc-macros = { git = "https://github.com/faern/jsonrpc", branch = "bind-zero" } [dependencies.talpid_ipc] path = "../talpid_ipc" diff --git a/talpid_core/src/lib.rs b/talpid_core/src/lib.rs index 1a10532787..8a053bffa8 100644 --- a/talpid_core/src/lib.rs +++ b/talpid_core/src/lib.rs @@ -13,11 +13,18 @@ extern crate log; #[macro_use] extern crate error_chain; +extern crate jsonrpc_core; +#[macro_use] +extern crate jsonrpc_macros; extern crate talpid_ipc; +extern crate openvpn_ffi; /// Working with processes. pub mod process; /// Network primitives. pub mod net; + +/// Abstracts over different VPN tunnel technologies +pub mod tunnel; diff --git a/talpid_core/src/process/openvpn.rs b/talpid_core/src/process/openvpn.rs index d565b085a8..5d40d8e42c 100644 --- a/talpid_core/src/process/openvpn.rs +++ b/talpid_core/src/process/openvpn.rs @@ -6,7 +6,6 @@ use duct; use net::{RemoteAddr, ToRemoteAddrs}; -use std::collections::HashMap; use std::ffi::{OsStr, OsString}; use std::fmt; use std::io; @@ -127,7 +126,7 @@ fn write_argument(fmt: &mut fmt::Formatter, arg: &str) -> fmt::Result { /// Possible events from OpenVPN pub enum OpenVpnEvent { /// An event from the plugin loaded into OpenVPN. - PluginEvent(talpid_ipc::Result<(openvpn_ffi::OpenVpnPluginEvent, HashMap<String, String>)>), + PluginEvent(talpid_ipc::Result<(openvpn_ffi::OpenVpnPluginEvent, openvpn_ffi::OpenVpnEnv)>), /// The OpenVPN process exited. Containing the result of waiting for the process. Shutdown(io::Result<process::ExitStatus>), } diff --git a/talpid_core/src/tunnel/mod.rs b/talpid_core/src/tunnel/mod.rs new file mode 100644 index 0000000000..0b477ca1c4 --- /dev/null +++ b/talpid_core/src/tunnel/mod.rs @@ -0,0 +1,2 @@ +/// A module for all OpenVPN related tunnel management. +pub mod openvpn; diff --git a/talpid_core/src/tunnel/openvpn.rs b/talpid_core/src/tunnel/openvpn.rs new file mode 100644 index 0000000000..35361bb1fd --- /dev/null +++ b/talpid_core/src/tunnel/openvpn.rs @@ -0,0 +1,79 @@ +use jsonrpc_core::{Error, IoHandler}; +use openvpn_ffi::{OpenVpnEnv, OpenVpnPluginEvent}; + +use talpid_ipc; + +/// IPC server for listening to events coming from plugin loaded into OpenVPN. +pub struct OpenVpnEventDispatcher { + server: talpid_ipc::IpcServer, +} + +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, OpenVpnEnv) + Send + Sync + 'static + { + 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() + } + + /// Consumes the server and waits for it to finish. + 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, OpenVpnEnv) -> Result<(), Error>; + } + } +} +use self::api::*; + +struct OpenVpnEventApiImpl<L> + where L: Fn(OpenVpnPluginEvent, OpenVpnEnv) + Send + Sync + 'static +{ + on_event: L, +} + +impl<L> OpenVpnEventApi for OpenVpnEventApiImpl<L> + where L: Fn(OpenVpnPluginEvent, OpenVpnEnv) + Send + Sync + 'static +{ + fn openvpn_event(&self, event: OpenVpnPluginEvent, env: OpenVpnEnv) -> Result<(), Error> { + debug!("OpenVPN event {:?}", event); + (self.on_event)(event, env); + Ok(()) + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[ignore] + fn openvpn_event_dispatcher_server() { + let server = OpenVpnEventDispatcher::start( + |event, env| { + println!("event: {:?}. env: {:?}", event, env); + }, + ) + .unwrap(); + println!("plugin server listening on {}", server.address()); + server.wait().unwrap(); + } +} |
