summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorErik Larkö <erik@mullvad.net>2017-09-08 13:03:44 +0200
committerErik Larkö <erik@mullvad.net>2017-09-13 07:32:26 +0200
commit6d724d021fb14a9885284c302fdbc9718b0338f9 (patch)
treea964b832c613e05ea4c117759878fbff4695f75a
parent4bc6b031bd7bd29d5bc084f07bd6096d205dc7d4 (diff)
downloadmullvadvpn-6d724d021fb14a9885284c302fdbc9718b0338f9.tar.xz
mullvadvpn-6d724d021fb14a9885284c302fdbc9718b0338f9.zip
Set custom relay in cli
-rw-r--r--Cargo.lock1
-rw-r--r--mullvad-cli/Cargo.toml1
-rw-r--r--mullvad-cli/src/cmds/custom_relay.rs62
-rw-r--r--mullvad-cli/src/cmds/mod.rs4
-rw-r--r--mullvad-cli/src/main.rs1
-rw-r--r--talpid-types/src/net.rs13
6 files changed, 82 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 4ea00f8d59..4258357e9f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -642,6 +642,7 @@ dependencies = [
"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",
+ "talpid-types 0.1.0",
]
[[package]]
diff --git a/mullvad-cli/Cargo.toml b/mullvad-cli/Cargo.toml
index 63d11a4656..1ae3afbcfc 100644
--- a/mullvad-cli/Cargo.toml
+++ b/mullvad-cli/Cargo.toml
@@ -19,4 +19,5 @@ serde = "1.0"
serde_json = "1.0"
mullvad-types = { path = "../mullvad-types" }
+talpid-types = { path = "../talpid-types" }
talpid-ipc = { path = "../talpid-ipc" }
diff --git a/mullvad-cli/src/cmds/custom_relay.rs b/mullvad-cli/src/cmds/custom_relay.rs
new file mode 100644
index 0000000000..2272f3b7c2
--- /dev/null
+++ b/mullvad-cli/src/cmds/custom_relay.rs
@@ -0,0 +1,62 @@
+pub struct CustomRelay;
+
+use Command;
+use Result;
+use clap;
+use mullvad_types::relay_endpoint::RelayEndpoint;
+
+use rpc;
+
+use talpid_types::net::TransportProtocol;
+
+impl Command for CustomRelay {
+ fn name(&self) -> &'static str {
+ "relay"
+ }
+
+ fn clap_subcommand(&self) -> clap::App<'static, 'static> {
+ clap::SubCommand::with_name(self.name())
+ .about("Set a custom remote relay to connect to")
+ .setting(clap::AppSettings::SubcommandRequired)
+ .subcommand(clap::SubCommand::with_name("set")
+ .about("Set a custom remote relay")
+ .arg(clap::Arg::with_name("host")
+ .help("The host name or IP of the remote relay")
+ .required(true))
+ .arg(clap::Arg::with_name("port")
+ .help("The port of the remote relay")
+ .required(true))
+ .arg(clap::Arg::with_name("protocol")
+ .help("The transport protocol. UDP is recommended as it usually results in higher throughput that TCP")
+ .possible_values(&["udp", "tcp"])
+ .default_value("udp")))
+ }
+
+ fn run(&self, matches: &clap::ArgMatches) -> Result<()> {
+ if let Some(set_matches) = matches.subcommand_matches("set") {
+ let host = value_t_or_exit!(set_matches.value_of("host"), String);
+ let port = value_t_or_exit!(set_matches.value_of("port"), u16);
+ let protocol = value_t_or_exit!(set_matches.value_of("protocol"), TransportProtocol);
+
+ self.set(host, port, protocol)
+ } else {
+ unreachable!("No sub command given");
+ }
+ }
+}
+
+impl CustomRelay {
+ fn set(&self, host: String, port: u16, protocol: TransportProtocol) -> Result<()> {
+ let relay_endpoint = RelayEndpoint {
+ host,
+ port,
+ protocol,
+ };
+
+ rpc::call(
+ "set_custom_relay",
+ &[relay_endpoint],
+ )
+ .map(|_: Option<()>| println!("Custom remote relay set"))
+ }
+}
diff --git a/mullvad-cli/src/cmds/mod.rs b/mullvad-cli/src/cmds/mod.rs
index c621c0840a..8061344b96 100644
--- a/mullvad-cli/src/cmds/mod.rs
+++ b/mullvad-cli/src/cmds/mod.rs
@@ -13,6 +13,9 @@ pub use self::connect::Connect;
mod disconnect;
pub use self::disconnect::Disconnect;
+mod custom_relay;
+pub use self::custom_relay::CustomRelay;
+
/// 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<Command>> = vec![
@@ -20,6 +23,7 @@ pub fn get_commands() -> HashMap<&'static str, Box<Command>> {
Box::new(Status),
Box::new(Connect),
Box::new(Disconnect),
+ Box::new(CustomRelay),
];
let mut map = HashMap::new();
for cmd in commands {
diff --git a/mullvad-cli/src/main.rs b/mullvad-cli/src/main.rs
index 745d2aea34..8543f7eecb 100644
--- a/mullvad-cli/src/main.rs
+++ b/mullvad-cli/src/main.rs
@@ -10,6 +10,7 @@
#![recursion_limit = "1024"]
extern crate mullvad_types;
+extern crate talpid_types;
extern crate talpid_ipc;
#[macro_use]
extern crate clap;
diff --git a/talpid-types/src/net.rs b/talpid-types/src/net.rs
index e1052d0571..18d9d1a768 100644
--- a/talpid-types/src/net.rs
+++ b/talpid-types/src/net.rs
@@ -1,4 +1,5 @@
use std::net::{IpAddr, SocketAddr};
+use std::str::FromStr;
/// Represents a network layer IP address together with the transport layer protocol and port.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -27,3 +28,15 @@ pub enum TransportProtocol {
/// Represents the TCP transport protocol.
Tcp,
}
+
+impl FromStr for TransportProtocol {
+ type Err = ();
+
+ fn from_str(s: &str) -> ::std::result::Result<TransportProtocol, Self::Err> {
+ match s {
+ "udp" => Ok(TransportProtocol::Udp),
+ "tcp" => Ok(TransportProtocol::Tcp),
+ _ => Err(()),
+ }
+ }
+}