summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/cmds/proxy.rs
blob: c876a25d2ede79ef9a911123e77f88156e019ea5 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use anyhow::{anyhow, Result};
use mullvad_management_interface::MullvadProxyClient;
use mullvad_types::api_access_method::{
    daemon::{ApiAccessMethodReplace, ApiAccessMethodToggle},
    AccessMethod, ObfuscationProtocol,
};
use std::net::IpAddr;

use clap::{Args, Subcommand};
use talpid_types::net::openvpn::SHADOWSOCKS_CIPHERS;

#[derive(Subcommand, Debug, Clone)]
pub enum Proxy {
    /// List the configured API proxies
    List,
    /// Add a custom API proxy
    #[clap(subcommand)]
    Add(AddCustomCommands),
    /// Edit an API proxy
    Edit(EditCustomCommands),
    /// Remove an API proxy
    Remove(RemoveCustomCommands),
    /// Enable an API proxy
    Enable(SelectItem),
    /// Disable an API proxy
    Disable(SelectItem),
    /// Force the use of a specific API proxy.
    Use(SelectItem),
}

impl Proxy {
    pub async fn handle(self) -> Result<()> {
        match self {
            Proxy::List => {
                //println!("Listing the API access methods: ..");
                Self::list().await?;
            }
            Proxy::Add(cmd) => {
                //println!("Adding custom proxy");
                Self::add(cmd).await?;
            }
            Proxy::Edit(cmd) => {
                // Transform human-readable index to 0-based indexing.
                let index = Self::zero_to_one_based_index(cmd.index)?;
                Self::edit(EditCustomCommands { index, ..cmd }).await?
            }
            Proxy::Remove(cmd) => {
                // Transform human-readable index to 0-based indexing.
                let index = Self::zero_to_one_based_index(cmd.index)?;
                Self::remove(RemoveCustomCommands { index }).await?
            }
            Proxy::Enable(cmd) => {
                let index = Self::zero_to_one_based_index(cmd.index)?;
                let enabled = true;
                Self::toggle(index, enabled).await?;
            }
            Proxy::Disable(cmd) => {
                let index = Self::zero_to_one_based_index(cmd.index)?;
                let enabled = false;
                Self::toggle(index, enabled).await?;
            }
            Proxy::Use(cmd) => {
                let index = Self::zero_to_one_based_index(cmd.index)?;
                Self::set(index).await?;
            }
        };
        Ok(())
    }

    /// Show all API access methods.
    async fn list() -> Result<()> {
        use crate::cmds::proxy::pp::AccessMethodFormatter;
        let mut rpc = MullvadProxyClient::new().await?;
        for (index, api_access_method) in rpc.get_api_access_methods().await?.iter().enumerate() {
            println!(
                "{}. {}",
                index + 1,
                AccessMethodFormatter::new(&api_access_method)
            );
        }
        Ok(())
    }

    /// Add a custom API access method.
    async fn add(cmd: AddCustomCommands) -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        let proxy = AccessMethod::try_from(cmd.clone())?;
        rpc.add_access_method(proxy).await?;
        Ok(())
    }

    /// Remove an API access method.
    async fn remove(cmd: RemoveCustomCommands) -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        let access_method = rpc
            .get_api_access_methods()
            .await?
            .get(cmd.index)
            .ok_or(anyhow!(format!(
                "Access method {} does not exist",
                cmd.index + 1
            )))?
            .clone();
        rpc.remove_access_method(access_method)
            .await
            .map_err(Into::<anyhow::Error>::into)
    }

    /// Edit the data of an API access method.
    async fn edit(cmd: EditCustomCommands) -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        // Retrieve the access method to edit
        let access_method = rpc
            .get_api_access_methods()
            .await?
            .get(cmd.index)
            .ok_or(anyhow!(format!(
                "Access method {} does not exist",
                cmd.index + 1
            )))?
            .clone();

        // Create a new access method combining the new params with the previous values
        let access_method = match access_method {
            AccessMethod::BuiltIn(_) => Err(anyhow!("Can not edit built-in access method")),
            AccessMethod::Custom(custom_access_method) => Ok(custom_access_method),
        }?;

        let edited_access_method: AccessMethod = match access_method.access_method {
            ObfuscationProtocol::Shadowsocks(shadowsocks) => {
                let ip = cmd.params.ip.unwrap_or(shadowsocks.peer.ip()).to_string();
                let port = cmd.params.port.unwrap_or(shadowsocks.peer.port());
                let password = cmd.params.password.unwrap_or(shadowsocks.password);
                let cipher = cmd.params.cipher.unwrap_or(shadowsocks.cipher);
                let name = cmd.params.name.unwrap_or(shadowsocks.name);
                let enabled = shadowsocks.enabled;
                mullvad_types::api_access_method::Shadowsocks::from_args(
                    ip, port, cipher, password, enabled, name,
                )
                .map(|x| x.into())
            }
            ObfuscationProtocol::Socks5(socks) => match socks {
                mullvad_types::api_access_method::Socks5::Local(local) => {
                    let ip = cmd.params.ip.unwrap_or(local.peer.ip()).to_string();
                    let port = cmd.params.port.unwrap_or(local.peer.port());
                    let local_port = cmd.params.local_port.unwrap_or(local.port);
                    let name = cmd.params.name.unwrap_or(local.name);
                    let enabled = local.enabled;
                    mullvad_types::api_access_method::Socks5Local::from_args(
                        ip, port, local_port, enabled, name,
                    )
                    .map(|x| x.into())
                }
                mullvad_types::api_access_method::Socks5::Remote(remote) => {
                    let ip = cmd.params.ip.unwrap_or(remote.peer.ip()).to_string();
                    let port = cmd.params.port.unwrap_or(remote.peer.port());
                    let name = cmd.params.name.unwrap_or(remote.name);
                    let enabled = remote.enabled;
                    mullvad_types::api_access_method::Socks5Remote::from_args(
                        ip, port, enabled, name,
                    )
                    .map(|x| x.into())
                }
            },
        }
        .ok_or(anyhow!(
            "Could not edit access method {}, reverting changes.",
            cmd.index
        ))?;

        rpc.replace_access_method(ApiAccessMethodReplace {
            index: cmd.index,
            access_method: edited_access_method,
        })
        .await?;

        Ok(())
    }

    /// Toggle a custom API access method to be enabled or disabled.
    async fn toggle(index: usize, enabled: bool) -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        // Retrieve the access method to edit
        let access_method = rpc
            .get_api_access_methods()
            .await?
            .get(index)
            .ok_or(anyhow!(format!(
                "Access method {} does not exist",
                index + 1
            )))?
            .clone();

        rpc.toggle_access_method(ApiAccessMethodToggle {
            access_method,
            enable: enabled,
        })
        .await?;
        Ok(())
    }

    /// Force the use of a specific access method when trying to reach the
    /// Mullvad API. If this method fails, the daemon will resume the automatic
    /// roll-over behavior (which is the default).
    async fn set(index: usize) -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        let access_method = rpc
            .get_api_access_methods()
            .await?
            .get(index)
            .ok_or(anyhow!(format!(
                "Access method {} does not exist",
                index + 1
            )))?
            .clone();

        rpc.set_access_method(access_method).await?;
        Ok(())
    }

    fn zero_to_one_based_index(index: usize) -> Result<usize> {
        index
            .checked_sub(1)
            .ok_or(anyhow!("Access method 0 does not exist"))
    }
}

#[derive(Subcommand, Debug, Clone)]
pub enum AddCustomCommands {
    /// Configure SOCKS5 proxy
    #[clap(subcommand)]
    Socks5(Socks5AddCommands),

    /// Configure bundled Shadowsocks proxy
    Shadowsocks {
        /// An easy to remember name for this custom proxy
        name: String,
        /// The IP of the remote Shadowsocks server
        remote_ip: IpAddr,
        /// The port of the remote Shadowsocks server
        #[arg(default_value = "443")]
        remote_port: u16,
        /// Password for authentication
        #[arg(default_value = "mullvad")]
        password: String,
        /// Cipher to use
        #[arg(value_parser = SHADOWSOCKS_CIPHERS, default_value = "aes-256-gcm")]
        cipher: String,
    },
}

/// A minimal wrapper type allowing the user to supply a list index to some
/// Access Method.
#[derive(Args, Debug, Clone)]
pub struct SelectItem {
    /// Which access method to pick
    index: usize,
}

#[derive(Args, Debug, Clone)]
pub struct EditCustomCommands {
    /// Which API proxy to edit
    index: usize,
    /// Editing parameters
    #[clap(flatten)]
    params: EditParams,
}

#[derive(Args, Debug, Clone)]
pub struct RemoveCustomCommands {
    /// Which API proxy to remove
    index: usize,
}

#[derive(Subcommand, Debug, Clone)]
pub enum Socks5AddCommands {
    /// Configure a local SOCKS5 proxy
    Local {
        /// An easy to remember name for this custom proxy
        name: String,
        /// The port that the server on localhost is listening on
        local_port: u16,
        /// The IP of the remote peer
        remote_ip: IpAddr,
        /// The port of the remote peer
        remote_port: u16,
    },
    /// Configure a remote SOCKS5 proxy
    Remote {
        /// An easy to remember name for this custom proxy
        name: String,
        /// The IP of the remote proxy server
        remote_ip: IpAddr,
        /// The port of the remote proxy server
        remote_port: u16,
        /// Username for authentication
        #[arg(requires = "password")]
        username: Option<String>,
        /// Password for authentication
        #[arg(requires = "username")]
        password: Option<String>,
    },
}

#[derive(Args, Debug, Clone)]
pub struct EditParams {
    /// Name of the API proxy in the Mullvad client [All]
    #[arg(long)]
    name: Option<String>,
    /// Username for authentication [Shadowsocks]
    #[arg(long)]
    username: Option<String>,
    /// Password for authentication [Shadowsocks]
    #[arg(long)]
    password: Option<String>,
    /// Cipher to use [Shadowsocks]
    #[arg(value_parser = SHADOWSOCKS_CIPHERS, long)]
    cipher: Option<String>,
    /// The IP of the remote proxy server [Socks5 (Local & Remote proxy), Shadowsocks]
    #[arg(long)]
    ip: Option<IpAddr>,
    /// The port of the remote proxy server [Socks5 (Local & Remote proxy), Shadowsocks]
    #[arg(long)]
    port: Option<u16>,
    /// The port that the server on localhost is listening on [Socks5 (Local proxy)]
    #[arg(long)]
    local_port: Option<u16>,
}

/// Implement conversions from CLI types to Daemon types.
///
/// Since these are not supposed to be used outside of the CLI,
/// we define them in a hidden-away module.
mod conversions {
    use anyhow::{anyhow, Error};
    use mullvad_types::api_access_method as daemon_types;

    use super::{AddCustomCommands, Socks5AddCommands};

    impl TryFrom<AddCustomCommands> for daemon_types::AccessMethod {
        type Error = Error;

        fn try_from(value: AddCustomCommands) -> Result<Self, Self::Error> {
            // All api proxies are automatically enabled from the start.
            let enabled = true;
            Ok(match value {
                AddCustomCommands::Socks5(variant) => match variant {
                    Socks5AddCommands::Local {
                        local_port,
                        remote_ip,
                        remote_port,
                        name,
                    } => {
                        println!("Adding LOCAL SOCKS5-proxy: localhost:{local_port} => {remote_ip}:{remote_port}");
                        let socks_proxy = daemon_types::Socks5::Local(
                            daemon_types::Socks5Local::from_args(
                                remote_ip.to_string(),
                                remote_port,
                                local_port,
                                enabled,
                                name,
                            )
                            .ok_or(anyhow!("Could not create a local Socks5 api proxy"))?,
                        );
                        socks_proxy.into()
                    }
                    Socks5AddCommands::Remote {
                        remote_ip,
                        remote_port,
                        username,
                        password,
                        name,
                    } => {
                        println!("Adding REMOTE SOCKS5-proxy: {username:?}+{password:?} @ {remote_ip}:{remote_port}");
                        let socks_proxy = daemon_types::Socks5::Remote(
                            daemon_types::Socks5Remote::from_args(
                                remote_ip.to_string(),
                                remote_port,
                                enabled,
                                name,
                            )
                            .ok_or(anyhow!("Could not create a remote Socks5 api proxy"))?,
                        );
                        socks_proxy.into()
                    }
                },
                AddCustomCommands::Shadowsocks {
                    remote_ip,
                    remote_port,
                    password,
                    cipher,
                    name,
                } => {
                    println!(
                "Adding Shadowsocks-proxy: {password} @ {remote_ip}:{remote_port} using {cipher}"
                    );
                    let shadowsocks_proxy = daemon_types::Shadowsocks::from_args(
                        remote_ip.to_string(),
                        remote_port,
                        cipher,
                        password,
                        enabled,
                        name,
                    )
                    .ok_or(anyhow!("Could not create a Shadowsocks api proxy"))?;
                    shadowsocks_proxy.into()
                }
            })
        }
    }
}

/// Pretty printing of [`AccessMethod`]s
mod pp {
    use mullvad_types::api_access_method::{
        AccessMethod, BuiltInAccessMethod, ObfuscationProtocol, Socks5,
    };

    pub struct AccessMethodFormatter<'a> {
        access_method: &'a AccessMethod,
    }

    impl<'a> AccessMethodFormatter<'a> {
        pub fn new(access_method: &'a AccessMethod) -> AccessMethodFormatter<'a> {
            AccessMethodFormatter { access_method }
        }
    }

    impl<'a> std::fmt::Display for AccessMethodFormatter<'a> {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            use crate::print_option;

            let write_status = |f: &mut std::fmt::Formatter<'_>, enabled: bool| {
                if enabled {
                    write!(f, " *")
                } else {
                    write!(f, "")
                }
            };

            match self.access_method {
                AccessMethod::BuiltIn(method) => match method {
                    BuiltInAccessMethod::Direct(enabled) => {
                        write!(f, "Direct")?;
                        write_status(f, *enabled)
                    }
                    BuiltInAccessMethod::Bridge(enabled) => {
                        write!(f, "Mullvad Bridges")?;
                        write_status(f, *enabled)
                    }
                },
                AccessMethod::Custom(method) => match &method.access_method {
                    ObfuscationProtocol::Shadowsocks(shadowsocks) => {
                        write!(f, "{}", shadowsocks.name)?;
                        write_status(f, shadowsocks.enabled)?;
                        writeln!(f, "")?;
                        print_option!("Protocol", format!("Shadowsocks [{}]", shadowsocks.cipher));
                        print_option!("Peer", shadowsocks.peer);
                        print_option!("Password", shadowsocks.password);
                        Ok(())
                    }
                    ObfuscationProtocol::Socks5(socks) => match socks {
                        Socks5::Remote(remote) => {
                            write!(f, "{}", remote.name)?;
                            write_status(f, remote.enabled)?;
                            writeln!(f, "")?;
                            print_option!("Protocol", "Socks5");
                            print_option!("Peer", remote.peer);
                            Ok(())
                        }
                        Socks5::Local(local) => {
                            write!(f, "{}", local.name)?;
                            write_status(f, local.enabled)?;
                            writeln!(f, "")?;
                            print_option!("Protocol", "Socks5 (local)");
                            print_option!("Peer", local.peer);
                            print_option!("Local port", local.port);
                            Ok(())
                        }
                    },
                },
            }
        }
    }
}