diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-05-22 12:38:39 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-05-23 09:26:32 +0200 |
| commit | 2aa704deb3cb00aee979987d098ac6635f208d15 (patch) | |
| tree | efd50038355294e6b1a6911ca77abc3534c083ae /talpid_cli | |
| parent | 611ffe8ebbc07c127788414ba31add574984d965 (diff) | |
| download | mullvadvpn-2aa704deb3cb00aee979987d098ac6635f208d15.tar.xz mullvadvpn-2aa704deb3cb00aee979987d098ac6635f208d15.zip | |
Change talpid_cli to use new OpenVpnMonitor
Diffstat (limited to 'talpid_cli')
| -rw-r--r-- | talpid_cli/src/main.rs | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/talpid_cli/src/main.rs b/talpid_cli/src/main.rs index cc236677fc..69410ab652 100644 --- a/talpid_cli/src/main.rs +++ b/talpid_cli/src/main.rs @@ -10,9 +10,11 @@ extern crate log; extern crate env_logger; use std::path::Path; +use std::sync::Mutex; use std::sync::mpsc::{self, Receiver}; -use talpid_core::process::openvpn::{OpenVpnCommand, OpenVpnEvent, OpenVpnMonitor}; +use talpid_core::process::openvpn::OpenVpnCommand; +use talpid_core::tunnel::openvpn::{OpenVpnEvent, OpenVpnMonitor}; mod cli; @@ -27,7 +29,7 @@ fn run() -> Result<()> { init_logger()?; let args = cli::parse_args_or_exit(); let command = create_openvpn_command(&args); - main_loop(command, args.plugin_path.as_path()) + main_loop(&command, args.plugin_path.as_path()) } pub fn init_logger() -> Result<()> { @@ -43,9 +45,10 @@ fn create_openvpn_command(args: &Args) -> OpenVpnCommand { command } -fn main_loop(command: OpenVpnCommand, plugin_path: &Path) -> Result<()> { +fn main_loop(command: &OpenVpnCommand, plugin_path: &Path) -> Result<()> { + let (monitor, rx) = create_openvpn_monitor(plugin_path)?; loop { - let (_monitor, rx) = start_monitor(command.clone(), plugin_path)?; + monitor.start(command.clone()).chain_err(|| "Unable to start OpenVPN")?; while let Ok(msg) = rx.recv() { match msg { OpenVpnEvent::Shutdown(result) => { @@ -55,22 +58,24 @@ fn main_loop(command: OpenVpnCommand, plugin_path: &Path) -> Result<()> { ); break; } - OpenVpnEvent::PluginEvent(Ok((event, env))) => { + OpenVpnEvent::PluginEvent(event, env) => { println!("OpenVPN event:\nEvent: {:?}\nENV: {:?}", event, env); } - OpenVpnEvent::PluginEvent(Err(e)) => println!("Read error from plugin: {:?}", e), } } std::thread::sleep(std::time::Duration::from_millis(500)); } } -fn start_monitor(command: OpenVpnCommand, - plugin_path: &Path) - -> Result<(OpenVpnMonitor, Receiver<OpenVpnEvent>)> { +fn create_openvpn_monitor(plugin_path: &Path) -> Result<(OpenVpnMonitor, Receiver<OpenVpnEvent>)> { let (tx, rx) = mpsc::channel(); - let listener = move |event: OpenVpnEvent| tx.send(event).unwrap(); - OpenVpnMonitor::start(command, plugin_path, listener) - .map(|m| (m, rx)) - .chain_err(|| "Unable to start OpenVPN") + let tx_mutex = Mutex::new(tx); + let on_event = move |event: OpenVpnEvent| { + let tx_lock = tx_mutex.lock().expect("Unable to lock tx_mutex"); + tx_lock.send(event).expect("Unable to send on tx_lock"); + println!("talpid_cli on_event fired and DONE") + }; + let monitor = OpenVpnMonitor::new(on_event, plugin_path) + .chain_err(|| "Unable to start OpenVPN monitor")?; + Ok((monitor, rx)) } |
