diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-07-07 16:46:30 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-07-10 10:00:32 +0200 |
| commit | caddbcfdf579d7a91a4e4d8f73791f88ff81c9f6 (patch) | |
| tree | cda3f94de8a75fabc7eebb059836f0fcd24480c4 | |
| parent | ef588b3ef2c0966d4e521058e5b8fadf11bab9f4 (diff) | |
| download | mullvadvpn-caddbcfdf579d7a91a4e4d8f73791f88ff81c9f6.tar.xz mullvadvpn-caddbcfdf579d7a91a4e4d8f73791f88ff81c9f6.zip | |
Add status support in CLI
| -rw-r--r-- | mullvad_cli/Cargo.toml | 4 | ||||
| -rw-r--r-- | mullvad_cli/src/cmds/account.rs | 13 | ||||
| -rw-r--r-- | mullvad_cli/src/cmds/mod.rs | 10 | ||||
| -rw-r--r-- | mullvad_cli/src/cmds/status.rs | 29 | ||||
| -rw-r--r-- | mullvad_cli/src/main.rs | 1 | ||||
| -rw-r--r-- | mullvad_cli/src/rpc.rs | 15 | ||||
| -rw-r--r-- | talpid_openvpn_plugin/src/processing.rs | 2 |
7 files changed, 59 insertions, 15 deletions
diff --git a/mullvad_cli/Cargo.toml b/mullvad_cli/Cargo.toml index 5d758482c9..00eda111e9 100644 --- a/mullvad_cli/Cargo.toml +++ b/mullvad_cli/Cargo.toml @@ -12,6 +12,8 @@ clap = "2.20" error-chain = "0.10" log = "0.3" env_logger = "0.4" -talpid_ipc = { path = "../talpid_ipc" } serde = "1.0" serde_json = "1.0" + +mullvad_types = { path = "../mullvad_types" } +talpid_ipc = { path = "../talpid_ipc" } diff --git a/mullvad_cli/src/cmds/account.rs b/mullvad_cli/src/cmds/account.rs index c6300e0eb3..fcfe9f9901 100644 --- a/mullvad_cli/src/cmds/account.rs +++ b/mullvad_cli/src/cmds/account.rs @@ -2,7 +2,6 @@ use Command; use Result; use clap; use rpc; -use serde_json; pub struct Account; @@ -39,17 +38,17 @@ impl Command for Account { impl Account { fn set(&self, token: &str) -> Result<()> { rpc::call("set_account", &[token]).map( - |_| { - println!("Mullvad account {} set", token); + |_: Option<()>| { + println!("Mullvad account \"{}\" set", token); }, ) } fn get(&self) -> Result<()> { - match rpc::call("get_account", &[] as &[u8; 0])? { - serde_json::Value::String(token) => println!("Mullvad account: {:?}", token), - serde_json::Value::Null => println!("No account configured"), - _ => bail!("Unable to fetch account token"), + let token: Option<String> = rpc::call("get_account", &[] as &[u8; 0])?; + match token { + Some(token) => println!("Mullvad account: {:?}", token), + None => println!("No account configured"), } Ok(()) } diff --git a/mullvad_cli/src/cmds/mod.rs b/mullvad_cli/src/cmds/mod.rs index 3f881bcfb8..aca46b9aa3 100644 --- a/mullvad_cli/src/cmds/mod.rs +++ b/mullvad_cli/src/cmds/mod.rs @@ -2,11 +2,17 @@ use Command; use std::collections::HashMap; mod account; -pub use self::account::*; +pub use self::account::Account; + +mod status; +pub use self::status::Status; /// Returns a map of all available subcommands with their name as key. pub fn get_commands() -> HashMap<&'static str, Box<Command>> { - let commands = vec![Box::new(Account) as Box<Command>]; + let commands = vec![ + Box::new(Account) as Box<Command>, + Box::new(Status) as Box<Command>, + ]; let mut map = HashMap::new(); for cmd in commands { if let Some(_) = map.insert(cmd.name(), cmd) { diff --git a/mullvad_cli/src/cmds/status.rs b/mullvad_cli/src/cmds/status.rs new file mode 100644 index 0000000000..e02773285f --- /dev/null +++ b/mullvad_cli/src/cmds/status.rs @@ -0,0 +1,29 @@ +use Command; +use Result; +use clap; + +use mullvad_types::states::{DaemonState, SecurityState, TargetState}; +use rpc; + +pub struct Status; + +impl Command for Status { + fn name(&self) -> &'static str { + "status" + } + + fn clap_subcommand(&self) -> clap::App<'static, 'static> { + clap::SubCommand::with_name(self.name()).about("View the state of the VPN tunnel") + } + + fn run(&self, _matches: &clap::ArgMatches) -> Result<()> { + let state: DaemonState = rpc::call("get_state", &[] as &[u8; 0])?; + match (state.state, state.target_state) { + (SecurityState::Unsecured, TargetState::Unsecured) => println!("Disconnected"), + (SecurityState::Unsecured, TargetState::Secured) => println!("Connecting..."), + (SecurityState::Secured, TargetState::Unsecured) => println!("Disconnecting..."), + (SecurityState::Secured, TargetState::Secured) => println!("Connected"), + } + Ok(()) + } +} diff --git a/mullvad_cli/src/main.rs b/mullvad_cli/src/main.rs index 3d49be3964..08d5459dd7 100644 --- a/mullvad_cli/src/main.rs +++ b/mullvad_cli/src/main.rs @@ -1,6 +1,7 @@ // `error_chain!` can recurse deeply #![recursion_limit = "1024"] +extern crate mullvad_types; extern crate talpid_ipc; #[macro_use] extern crate clap; diff --git a/mullvad_cli/src/rpc.rs b/mullvad_cli/src/rpc.rs index 11a6da33fb..73c0c64dc1 100644 --- a/mullvad_cli/src/rpc.rs +++ b/mullvad_cli/src/rpc.rs @@ -2,19 +2,26 @@ use {Result, ResultExt}; use serde; -use serde_json; use std::fs::File; use std::io::Read; use talpid_ipc::WsIpcClient; -pub fn call<T>(method: &str, args: &T) -> Result<serde_json::Value> - where T: serde::Serialize +pub fn call<T, O>(method: &str, args: &T) -> Result<O> + where T: serde::Serialize, + O: for<'de> serde::Deserialize<'de> +{ + call_internal(method, args).chain_err(|| "Unable to call backend over RPC") +} + +pub fn call_internal<T, O>(method: &str, args: &T) -> Result<O> + where T: serde::Serialize, + O: for<'de> serde::Deserialize<'de> { let address = read_rpc_address()?; info!("Using RPC address {}", address); let mut rpc_client = WsIpcClient::new(address) .chain_err(|| "Unable to create RPC client")?; - rpc_client.call(method, args).chain_err(|| "Unable to call RPC method") + rpc_client.call(method, args).chain_err(|| format!("Unable to call RPC method {}", method)) } fn read_rpc_address() -> Result<String> { diff --git a/talpid_openvpn_plugin/src/processing.rs b/talpid_openvpn_plugin/src/processing.rs index 8b1591f63d..e33097c633 100644 --- a/talpid_openvpn_plugin/src/processing.rs +++ b/talpid_openvpn_plugin/src/processing.rs @@ -30,7 +30,7 @@ impl EventProcessor { trace!("Processing \"{:?}\" event", event); self.ipc_client .call("openvpn_event", &(event, env)) - .map(|_| ()) + .map(|_: Option<()>| ()) .chain_err(|| ErrorKind::IpcSendingError) } } |
