summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/cmds/relay.rs
blob: 817ae17b3a4233f8c282a18e55a8fc280c2ff02d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use {Command, Result, ResultExt};
use clap;
use std::str::FromStr;

use mullvad_types::relay_constraints::{Constraint, OpenVpnConstraintsUpdate, RelayConstraints,
                                       RelayConstraintsUpdate, TunnelConstraintsUpdate};

use rpc;
use talpid_types::net::TransportProtocol;

pub struct Relay;

impl Command for Relay {
    fn name(&self) -> &'static str {
        "relay"
    }

    fn clap_subcommand(&self) -> clap::App<'static, 'static> {
        clap::SubCommand::with_name(self.name())
            .about("Manage relay and tunnel constraints")
            .setting(clap::AppSettings::SubcommandRequired)
            .subcommand(
                clap::SubCommand::with_name("set")
                    .setting(clap::AppSettings::SubcommandRequired)
                    .subcommand(
                        clap::SubCommand::with_name("host")
                            .about("Set host")
                            .arg(clap::Arg::with_name("host").required(true)),
                    )
                    .subcommand(
                        clap::SubCommand::with_name("port")
                            .about("Set port")
                            .arg(clap::Arg::with_name("port").required(true)),
                    )
                    .subcommand(
                        clap::SubCommand::with_name("protocol")
                            .about("Set protocol")
                            .arg(
                                clap::Arg::with_name("protocol")
                                    .required(true)
                                    .possible_values(&["any", "udp", "tcp"]),
                            ),
                    ),
            )
            .subcommand(clap::SubCommand::with_name("get"))
    }

    fn run(&self, matches: &clap::ArgMatches) -> Result<()> {
        if let Some(set_matches) = matches.subcommand_matches("set") {
            self.set(set_matches)
        } else if let Some(_) = matches.subcommand_matches("get") {
            self.get()
        } else {
            unreachable!("No relay command given");
        }
    }
}

impl Relay {
    fn update_constraints(&self, constraints_update: RelayConstraintsUpdate) -> Result<()> {
        rpc::call("update_relay_constraints", &[constraints_update])
            .map(|_: Option<()>| println!("Relay constraints updated"))
    }

    fn set(&self, matches: &clap::ArgMatches) -> Result<()> {
        if let Some(host_matches) = matches.subcommand_matches("host") {
            let host = value_t_or_exit!(host_matches.value_of("host"), String);

            self.update_constraints(RelayConstraintsUpdate {
                host: Some(Constraint::Only(host)),
                tunnel: TunnelConstraintsUpdate::OpenVpn(OpenVpnConstraintsUpdate {
                    port: None,
                    protocol: None,
                }),
            })
        } else if let Some(port_matches) = matches.subcommand_matches("port") {
            let port = parse_port(port_matches.value_of("port").unwrap())?;

            self.update_constraints(RelayConstraintsUpdate {
                host: None,
                tunnel: TunnelConstraintsUpdate::OpenVpn(OpenVpnConstraintsUpdate {
                    port: Some(port),
                    protocol: None,
                }),
            })
        } else if let Some(protocol_matches) = matches.subcommand_matches("protocol") {
            let protocol = parse_protocol(protocol_matches.value_of("protocol").unwrap());

            self.update_constraints(RelayConstraintsUpdate {
                host: None,
                tunnel: TunnelConstraintsUpdate::OpenVpn(OpenVpnConstraintsUpdate {
                    port: None,
                    protocol: Some(protocol),
                }),
            })
        } else {
            unreachable!("No set relay command given");
        }
    }

    fn get(&self) -> Result<()> {
        let constraints: RelayConstraints = rpc::call("get_relay_constraints", &[] as &[u8; 0])?;
        println!("Current constraints: {:?}", constraints);

        Ok(())
    }
}


fn parse_port(raw_port: &str) -> Result<Constraint<u16>> {
    match raw_port.to_lowercase().as_str() {
        "any" => Ok(Constraint::Any),
        port => Ok(Constraint::Only(
            u16::from_str(port).chain_err(|| "Invalid port")?,
        )),
    }
}

/// Parses a protocol constraint string. Can be infallible because the possible values are limited
/// with clap.
fn parse_protocol(raw_protocol: &str) -> Constraint<TransportProtocol> {
    match raw_protocol.to_lowercase().as_str() {
        "any" => Constraint::Any,
        "udp" => Constraint::Only(TransportProtocol::Udp),
        "tcp" => Constraint::Only(TransportProtocol::Tcp),
        _ => unreachable!(),
    }
}