diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-07-10 09:10:48 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-07-10 09:10:48 +0200 |
| commit | 5e82c3b62b7bac95e27f7c7782ceed6379537643 (patch) | |
| tree | 9f8f482b236e321bb617eff87c1582723d769fa8 /mullvad_cli/src | |
| parent | 82a5ddf1bd3e697ae165af991ea0a3e2476f7bc8 (diff) | |
| parent | ec4d8bd1a5e35bd2f653042d673079dec20e9f49 (diff) | |
| download | mullvadvpn-5e82c3b62b7bac95e27f7c7782ceed6379537643.tar.xz mullvadvpn-5e82c3b62b7bac95e27f7c7782ceed6379537643.zip | |
Merge branch 'cli-with-rpc'
Diffstat (limited to 'mullvad_cli/src')
| -rw-r--r-- | mullvad_cli/src/cmds/account.rs | 56 | ||||
| -rw-r--r-- | mullvad_cli/src/cmds/mod.rs | 17 | ||||
| -rw-r--r-- | mullvad_cli/src/main.rs | 50 | ||||
| -rw-r--r-- | mullvad_cli/src/rpc.rs | 29 |
4 files changed, 152 insertions, 0 deletions
diff --git a/mullvad_cli/src/cmds/account.rs b/mullvad_cli/src/cmds/account.rs new file mode 100644 index 0000000000..c6300e0eb3 --- /dev/null +++ b/mullvad_cli/src/cmds/account.rs @@ -0,0 +1,56 @@ +use Command; +use Result; +use clap; +use rpc; +use serde_json; + +pub struct Account; + +impl Command for Account { + fn name(&self) -> &'static str { + "account" + } + + fn clap_subcommand(&self) -> clap::App<'static, 'static> { + clap::SubCommand::with_name(self.name()) + .about("Control and display information about your Mullvad account") + .setting(clap::AppSettings::SubcommandRequired) + .subcommand(clap::SubCommand::with_name("set") + .about("Change account") + .arg(clap::Arg::with_name("token") + .help("The Mullvad account token to configure the client with") + .required(true))) + .subcommand(clap::SubCommand::with_name("get") + .about("Display information about the currently configured account")) + } + + fn run(&self, matches: &clap::ArgMatches) -> Result<()> { + if let Some(set_matches) = matches.subcommand_matches("set") { + let token = value_t_or_exit!(set_matches.value_of("token"), String); + self.set(&token) + } else if let Some(_matches) = matches.subcommand_matches("get") { + self.get() + } else { + unreachable!("No account command given"); + } + } +} + +impl Account { + fn set(&self, token: &str) -> Result<()> { + rpc::call("set_account", &[token]).map( + |_| { + 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"), + } + Ok(()) + } +} diff --git a/mullvad_cli/src/cmds/mod.rs b/mullvad_cli/src/cmds/mod.rs new file mode 100644 index 0000000000..3f881bcfb8 --- /dev/null +++ b/mullvad_cli/src/cmds/mod.rs @@ -0,0 +1,17 @@ +use Command; +use std::collections::HashMap; + +mod account; +pub use self::account::*; + +/// 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 mut map = HashMap::new(); + for cmd in commands { + if let Some(_) = map.insert(cmd.name(), cmd) { + panic!("Multiple commands with the same name"); + } + } + map +} diff --git a/mullvad_cli/src/main.rs b/mullvad_cli/src/main.rs new file mode 100644 index 0000000000..3d49be3964 --- /dev/null +++ b/mullvad_cli/src/main.rs @@ -0,0 +1,50 @@ +// `error_chain!` can recurse deeply +#![recursion_limit = "1024"] + +extern crate talpid_ipc; +#[macro_use] +extern crate clap; +#[macro_use] +extern crate error_chain; +#[macro_use] +extern crate log; +extern crate env_logger; +extern crate serde; +extern crate serde_json; + +mod rpc; +mod cmds; + + +error_chain!{} + +quick_main!(run); + +fn run() -> Result<()> { + env_logger::init().chain_err(|| "Failed to bootstrap logging system")?; + + let commands = cmds::get_commands(); + + let app = clap::App::new(crate_name!()) + .version(crate_version!()) + .author(crate_authors!()) + .about(crate_description!()) + .setting(clap::AppSettings::SubcommandRequired) + .subcommands(commands.values().map(|cmd| cmd.clap_subcommand())); + + let app_matches = app.get_matches(); + let (subcommand_name, subcommand_matches) = app_matches.subcommand(); + if let Some(cmd) = commands.get(subcommand_name) { + cmd.run(subcommand_matches.expect("No command matched")) + } else { + unreachable!("No command matched"); + } +} + +pub trait Command { + fn name(&self) -> &'static str; + + fn clap_subcommand(&self) -> clap::App<'static, 'static>; + + fn run(&self, matches: &clap::ArgMatches) -> Result<()>; +} diff --git a/mullvad_cli/src/rpc.rs b/mullvad_cli/src/rpc.rs new file mode 100644 index 0000000000..11a6da33fb --- /dev/null +++ b/mullvad_cli/src/rpc.rs @@ -0,0 +1,29 @@ + + +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 +{ + 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") +} + +fn read_rpc_address() -> Result<String> { + for path in &["./.mullvad_rpc_address", "../.mullvad_rpc_address"] { + debug!("Trying to read RPC address at {}", path); + let mut address = String::new(); + if let Ok(_) = File::open(path).and_then(|mut file| file.read_to_string(&mut address)) { + return Ok(address); + } + } + bail!("Unable to read RPC address"); +} |
