summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2017-07-10 09:10:48 +0200
committerLinus Färnstrand <linus@mullvad.net>2017-07-10 09:10:48 +0200
commit5e82c3b62b7bac95e27f7c7782ceed6379537643 (patch)
tree9f8f482b236e321bb617eff87c1582723d769fa8
parent82a5ddf1bd3e697ae165af991ea0a3e2476f7bc8 (diff)
parentec4d8bd1a5e35bd2f653042d673079dec20e9f49 (diff)
downloadmullvadvpn-5e82c3b62b7bac95e27f7c7782ceed6379537643.tar.xz
mullvadvpn-5e82c3b62b7bac95e27f7c7782ceed6379537643.zip
Merge branch 'cli-with-rpc'
-rw-r--r--Cargo.lock98
-rw-r--r--Cargo.toml2
-rw-r--r--mullvad_cli/Cargo.toml (renamed from talpid_cli/Cargo.toml)11
-rw-r--r--mullvad_cli/src/cmds/account.rs56
-rw-r--r--mullvad_cli/src/cmds/mod.rs17
-rw-r--r--mullvad_cli/src/main.rs50
-rw-r--r--mullvad_cli/src/rpc.rs29
-rw-r--r--talpid_cli/src/cli.rs82
-rw-r--r--talpid_cli/src/main.rs62
-rw-r--r--talpid_ipc/src/client.rs21
-rw-r--r--talpid_openvpn_plugin/src/processing.rs1
11 files changed, 273 insertions, 156 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 3ce7dbcc83..7fd6a496da 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -18,11 +18,26 @@ dependencies = [
]
[[package]]
+name = "ansi_term"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "assert_matches"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "atty"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.24 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "backtrace"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -51,6 +66,11 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "bitflags"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "byteorder"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -70,6 +90,22 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "clap"
+version = "2.25.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "textwrap 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "ctrlc"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -323,6 +359,19 @@ dependencies = [
]
[[package]]
+name = "mullvad_cli"
+version = "0.0.0"
+dependencies = [
+ "clap 2.25.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "talpid_ipc 0.1.0",
+]
+
+[[package]]
name = "mullvad_daemon"
version = "0.1.0"
dependencies = [
@@ -552,6 +601,11 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "strsim"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "syn"
version = "0.11.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -604,6 +658,25 @@ dependencies = [
]
[[package]]
+name = "term_size"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.24 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "textwrap"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "thread-id"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -661,6 +734,16 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "unicode-segmentation"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "unicode-width"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "unicode-xid"
version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -706,6 +789,11 @@ dependencies = [
]
[[package]]
+name = "vec_map"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -747,13 +835,17 @@ dependencies = [
[metadata]
"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
+"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
"checksum assert_matches 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9e772942dccdf11b368c31e044e4fca9189f80a773d2f0808379de65894cbf57"
+"checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159"
"checksum backtrace 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "72f9b4182546f4b04ebc4ab7f84948953a118bd6021a1b6a6c909e3e94f6be76"
"checksum backtrace-sys 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "3a0d842ea781ce92be2bf78a9b38883948542749640b8378b3b2f03d1fd9f1ff"
"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
+"checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5"
"checksum byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c40977b0ee6b9885c9013cd41d9feffdd22deb3bb4dc3a71d901cc7a77de18c8"
"checksum bytes 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8b24f16593f445422331a5eed46b72f7f171f910fead4f2ea8f17e727e9c5c14"
"checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de"
+"checksum clap 2.25.0 (registry+https://github.com/rust-lang/crates.io-index)" = "867a885995b4184be051b70a592d4d70e32d7a188db6e8dff626af286a962771"
"checksum ctrlc 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1c663ee353d7e03e4f895af66ff7d3537f94c09deaa341a2f450684a3d45cb46"
"checksum dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97590ba53bcb8ac28279161ca943a924d1fd4a8fb3fa63302591647c4fc5b850"
"checksum dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "80c8b71fd71146990a9742fc06dcbbde19161a267e0ad4e572c35162f4578c90"
@@ -811,20 +903,26 @@ dependencies = [
"checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23"
"checksum smallvec 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "26aa2afb825226fa29f0315de04d5a4af5fd44adadf837296accc01a49929724"
"checksum stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15132e0e364248108c5e2c02e3ab539be8d6f5d52a01ca9bbf27ed657316f02b"
+"checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694"
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
+"checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209"
+"checksum textwrap 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f86300c3e7416ee233abd7cda890c492007a3980f941f79185c753a701257167"
"checksum thread-id 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2af4d6289a69a35c4d3aea737add39685f2784122c28119a7713165a63d68c9d"
"checksum thread_local 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1697c4b57aeeb7a536b647165a2825faddffb1d3bad386d507709bd51a90bb14"
"checksum tokio-core 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6a20ba4738d283cac7495ca36e045c80c2a8df3e05dd0909b17a06646af5a7ed"
"checksum tokio-io 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c2c3ce9739f7387a0fa65b5421e81feae92e04d603f008898f4257790ce8c2db"
"checksum unicode-bidi 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a6a2c4e3710edd365cd7e78383153ed739fa31af19f9172f72d3575060f5a43a"
"checksum unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "51ccda9ef9efa3f7ef5d91e8f9b83bbe6955f9bf86aec89d5cce2c874625920f"
+"checksum unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18127285758f0e2c6cf325bb3f3d138a12fee27de4f23e146cd6a179f26c2cf3"
+"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
"checksum url 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb819346883532a271eb626deb43c4a1bb4c4dd47c519bd78137c3e72a4fe27"
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
"checksum uuid 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "78c590b5bd79ed10aad8fb75f078a59d8db445af6c743e55c4a53227fc01c13f"
"checksum uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcc7e3b898aa6f6c08e5295b6c89258d1331e9ac578cc992fb818759951bdc22"
+"checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
diff --git a/Cargo.toml b/Cargo.toml
index ebdd8443ac..2ade69a1d5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,2 +1,2 @@
[workspace]
-members = ["mullvad_daemon", "talpid_openvpn_plugin", "openvpn_ffi", "talpid_ipc"]
+members = ["mullvad_daemon", "mullvad_cli", "talpid_openvpn_plugin", "openvpn_ffi", "talpid_ipc"]
diff --git a/talpid_cli/Cargo.toml b/mullvad_cli/Cargo.toml
index 8a3f013737..5d758482c9 100644
--- a/talpid_cli/Cargo.toml
+++ b/mullvad_cli/Cargo.toml
@@ -1,14 +1,17 @@
[package]
-name = "talpid_cli"
+name = "mullvad_cli"
version = "0.0.0"
authors = ["Linus Färnstrand <linus@mullvad.net>", "Erik Larkö <erik@mullvad.net>"]
description = "Run Talpid easily from the command line"
+[[bin]]
+name = "mullvad"
+
[dependencies]
clap = "2.20"
error-chain = "0.10"
log = "0.3"
env_logger = "0.4"
-
-[dependencies.talpid_core]
-path = "../talpid_core"
+talpid_ipc = { path = "../talpid_ipc" }
+serde = "1.0"
+serde_json = "1.0"
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");
+}
diff --git a/talpid_cli/src/cli.rs b/talpid_cli/src/cli.rs
deleted file mode 100644
index b61c4112b5..0000000000
--- a/talpid_cli/src/cli.rs
+++ /dev/null
@@ -1,82 +0,0 @@
-use clap::{App, Arg, ArgMatches};
-use std::path::PathBuf;
-
-use talpid_core::net::RemoteAddr;
-
-#[cfg(all(unix, not(target_os="macos")))]
-const DEFAULT_PLUGIN_PATH: &'static str = "./target/debug/libtalpid_openvpn_plugin.so";
-#[cfg(target_os="macos")]
-const DEFAULT_PLUGIN_PATH: &'static str = "./target/debug/libtalpid_openvpn_plugin.dylib";
-#[cfg(windows)]
-const DEFAULT_PLUGIN_PATH: &'static str = "./target/debug/libtalpid_openvpn_plugin.dll";
-
-
-pub struct Args {
- pub binary: String,
- pub plugin_path: PathBuf,
- pub config: PathBuf,
- pub remotes: Vec<RemoteAddr>,
- pub verbosity: u64,
-}
-
-pub fn parse_args_or_exit() -> Args {
- let matches = get_matches();
- let remotes = values_t!(matches.values_of("remotes"), RemoteAddr).unwrap_or_else(|e| e.exit());
- Args {
- binary: matches.value_of("openvpn").unwrap().to_owned(),
- plugin_path: PathBuf::from(matches.value_of("plugin").unwrap()),
- config: PathBuf::from(matches.value_of("config").unwrap()),
- remotes: remotes,
- verbosity: matches.occurrences_of("verbose"),
- }
-}
-
-fn get_matches() -> ArgMatches<'static> {
- let app = create_app();
- app.clone().get_matches()
-}
-
-fn create_app() -> App<'static, 'static> {
- App::new(crate_name!())
- .version(crate_version!())
- .author(crate_authors!())
- .about(crate_description!())
- .arg(
- Arg::with_name("openvpn")
- .long("openvpn")
- .help("Specify what OpenVPN binary to run")
- .default_value("/usr/sbin/openvpn"),
- )
- .arg(
- Arg::with_name("config")
- .short("c")
- .long("config")
- .help("Specify what config file to start OpenVPN with")
- .default_value("./openvpn.conf"),
- )
- .arg(
- Arg::with_name("remotes")
- .short("r")
- .long("remotes")
- .help(
- "Configure what remote(s) to connect to. Accepts anything OpenVPN can use. \
- Format: <address>:<port>",
- )
- .takes_value(true)
- .multiple(true)
- .required(true),
- )
- .arg(
- Arg::with_name("plugin")
- .long("plugin")
- .help("Path to talpid plugin")
- .default_value(DEFAULT_PLUGIN_PATH),
- )
- .arg(
- Arg::with_name("verbose")
- .short("v")
- .long("verbose")
- .multiple(true)
- .help("Sets the level of verbosity"),
- )
-}
diff --git a/talpid_cli/src/main.rs b/talpid_cli/src/main.rs
deleted file mode 100644
index 2f1e5c36dc..0000000000
--- a/talpid_cli/src/main.rs
+++ /dev/null
@@ -1,62 +0,0 @@
-// `error_chain!` can recurse deeply
-#![recursion_limit = "1024"]
-
-extern crate talpid_core;
-#[macro_use]
-extern crate clap;
-#[macro_use]
-extern crate error_chain;
-extern crate log;
-extern crate env_logger;
-
-use std::sync::Mutex;
-use std::sync::mpsc::{self, Receiver};
-use talpid_core::net::RemoteAddr;
-
-use talpid_core::tunnel::{TunnelEvent, TunnelMonitor};
-
-mod cli;
-
-
-error_chain!{}
-
-quick_main!(run);
-
-fn run() -> Result<()> {
- init_logger()?;
- let args = cli::parse_args_or_exit();
- main_loop(&args.remotes)
-}
-
-pub fn init_logger() -> Result<()> {
- env_logger::init().chain_err(|| "Failed to bootstrap logging system")
-}
-
-fn main_loop(remotes: &[RemoteAddr]) -> Result<()> {
- let mut remotes_iter = remotes.iter().cloned().cycle();
- let (monitor, rx) = create_tunnel_monitor()?;
- loop {
- monitor.start(remotes_iter.next().unwrap()).chain_err(|| "Unable to start OpenVPN")?;
- while let Ok(msg) = rx.recv() {
- match msg {
- TunnelEvent::Shutdown => {
- println!("Monitored process exited");
- break;
- }
- TunnelEvent::Up => println!("Tunnel UP"),
- TunnelEvent::Down => println!("Tunnel DOWN"),
- }
- }
- std::thread::sleep(std::time::Duration::from_millis(500));
- }
-}
-
-fn create_tunnel_monitor() -> Result<(TunnelMonitor, Receiver<TunnelEvent>)> {
- let (event_tx, event_rx) = mpsc::channel();
- let event_tx_mutex = Mutex::new(event_tx);
- let on_event = move |event: TunnelEvent| {
- event_tx_mutex.lock().unwrap().send(event).expect("Unable to send on tx_lock");
- };
- let monitor = TunnelMonitor::new(on_event).chain_err(|| "Unable to start OpenVPN monitor")?;
- Ok((monitor, event_rx))
-}
diff --git a/talpid_ipc/src/client.rs b/talpid_ipc/src/client.rs
index 1776d46273..763d5b225e 100644
--- a/talpid_ipc/src/client.rs
+++ b/talpid_ipc/src/client.rs
@@ -12,7 +12,7 @@ pub use self::errors::*;
struct Factory {
request: String,
- result_tx: mpsc::Sender<Result<()>>,
+ result_tx: mpsc::Sender<Result<serde_json::Value>>,
}
impl ws::Factory for Factory {
@@ -34,30 +34,37 @@ impl ws::Factory for Factory {
struct Handler {
sender: ws::Sender,
- result_tx: mpsc::Sender<Result<()>>,
+ result_tx: mpsc::Sender<Result<serde_json::Value>>,
}
impl Handler {
- fn validate_reply(&self, msg: ws::Message) -> ws::Result<()> {
+ fn validate_reply(&self, msg: ws::Message) -> ws::Result<serde_json::Value> {
let json: serde_json::Value = match msg {
ws::Message::Text(s) => serde_json::from_str(&s),
ws::Message::Binary(b) => serde_json::from_slice(&b),
}
.map_err(|e| ws::Error::from(Box::new(e)))?;
debug!("JSON response: {}", json);
+ let result =
+ match json {
+ serde_json::Value::Object(mut map) => map.remove("result"),
+ _ => None,
+ }
+ .ok_or(ws::Error::new(ws::ErrorKind::Protocol, "Invalid reply, no 'result'"),)?;
// TODO(linus): Properly validate reply
- Ok(())
+ Ok(result)
}
}
impl ws::Handler for Handler {
fn on_message(&mut self, msg: ws::Message) -> ws::Result<()> {
- self.validate_reply(msg)?;
+ debug!("WsIpcClient incoming message: {:?}", msg);
+ let reply = self.validate_reply(msg)?;
let close_result = self.sender.close(ws::CloseCode::Normal);
if let Err(e) = close_result.chain_err(|| "Unable to close WebSocket") {
self.result_tx.send(Err(e)).unwrap();
}
- self.result_tx.send(Ok(())).unwrap();
+ self.result_tx.send(Ok(reply)).unwrap();
Ok(())
}
}
@@ -74,7 +81,7 @@ impl WsIpcClient {
Ok(WsIpcClient { url, next_id: 1 })
}
- pub fn call<T>(&mut self, method: &str, params: &T) -> Result<()>
+ pub fn call<T>(&mut self, method: &str, params: &T) -> Result<serde_json::Value>
where T: serde::Serialize
{
let (result_tx, result_rx) = mpsc::channel();
diff --git a/talpid_openvpn_plugin/src/processing.rs b/talpid_openvpn_plugin/src/processing.rs
index dd6967135e..8b1591f63d 100644
--- a/talpid_openvpn_plugin/src/processing.rs
+++ b/talpid_openvpn_plugin/src/processing.rs
@@ -30,6 +30,7 @@ impl EventProcessor {
trace!("Processing \"{:?}\" event", event);
self.ipc_client
.call("openvpn_event", &(event, env))
+ .map(|_| ())
.chain_err(|| ErrorKind::IpcSendingError)
}
}