summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/cmds/bridge.rs
blob: 48458c9c1e9bd97d4e01c07e95f33a5bb3806211 (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
use anyhow::{bail, Result};
use clap::Subcommand;
use mullvad_management_interface::MullvadProxyClient;
use mullvad_types::{
    constraints::Constraint,
    relay_constraints::{
        BridgeConstraintsFormatter, BridgeState, BridgeType, LocationConstraint, Ownership,
        Provider, Providers,
    },
    relay_list::RelayEndpointData,
};
use talpid_types::net::proxy::{CustomProxy, Shadowsocks, Socks5Local, Socks5Remote};

use crate::cmds::proxies::pp::CustomProxyFormatter;

use super::{
    proxies::{ProxyEditParams, ShadowsocksAdd, Socks5LocalAdd, Socks5RemoteAdd},
    relay::resolve_location_constraint,
    relay_constraints::LocationArgs,
};

#[derive(Subcommand, Debug)]
pub enum Bridge {
    /// Get current bridge settings
    Get,
    /// Set bridge state and settings, such as provider
    #[clap(subcommand)]
    Set(SetCommands),
    /// List available bridge relays
    List,
}

#[derive(Subcommand, Debug, Clone)]
pub enum SetCommands {
    /// Specify whether to use a bridge
    State { policy: BridgeState },

    /// Set country or city to select relays from.
    /// Use the 'mullvad bridge list' command to show available alternatives.
    #[command(
        override_usage = "mullvad bridge set location <COUNTRY> [CITY] [HOSTNAME] | <HOSTNAME>

  Select bridge using a country:

\tmullvad bridge set location se

  Select bridge using a country and city:

\tmullvad bridge set location se got

  Select bridge using a country, city and hostname:

\tmullvad bridge set location se got se-got-br-001

  Select bridge using only its hostname:

\tmullvad bridge set location se-got-br-001"
    )]
    Location(LocationArgs),

    /// Set custom list to select relays from. Use the 'custom-lists list'
    /// command to show available alternatives.
    CustomList { custom_list_name: String },

    /// Set hosting provider(s) to select relays from. The 'list'
    /// command shows the available relays and their providers.
    Provider {
        /// Either 'any', or provider to select from.
        #[arg(required(true), num_args = 1..)]
        providers: Vec<Provider>,
    },

    /// Filter relays based on ownership. The 'list' command
    /// shows the available relays and whether they're rented.
    Ownership {
        /// Servers to select from: 'any', 'owned', or 'rented'.
        ownership: Constraint<Ownership>,
    },

    /// Configure a SOCKS5 proxy
    #[clap(subcommand)]
    Custom(CustomCommands),
}

#[derive(Subcommand, Debug, Clone)]
pub enum CustomCommands {
    /// Create or update and enable the custom bridge configuration.
    #[clap(subcommand)]
    Set(AddCustomCommands),
    /// Edit an existing custom bridge configuration.
    Edit(ProxyEditParams),
    /// Use an existing custom bridge configuration.
    Use,
    /// Stop using the custom bridge configuration.
    Disable,
}

#[derive(Subcommand, Debug, Clone)]
pub enum AddCustomCommands {
    #[clap(subcommand)]
    Socks5(AddSocks5Commands),
    /// Configure bundled Shadowsocks proxy
    Shadowsocks {
        #[clap(flatten)]
        add: ShadowsocksAdd,
    },
}

#[derive(Subcommand, Debug, Clone)]
pub enum AddSocks5Commands {
    /// Configure a local SOCKS5 proxy
    #[clap(
        about = "Registers a local SOCKS5 proxy. Will allow all local programs to leak traffic *only* to the remote endpoint."
    )]
    Local {
        #[clap(flatten)]
        add: Socks5LocalAdd,
    },

    /// Configure a remote SOCKS5 proxy
    Remote {
        #[clap(flatten)]
        add: Socks5RemoteAdd,
    },
}

impl Bridge {
    pub async fn handle(self) -> Result<()> {
        match self {
            Bridge::Get => Self::get().await,
            Bridge::List => Self::list().await,
            Bridge::Set(subcmd) => Self::set(subcmd).await,
        }
    }

    async fn set(subcmd: SetCommands) -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        match subcmd {
            SetCommands::State { policy } => {
                rpc.set_bridge_state(policy).await?;
                println!("Updated bridge state");
                Ok(())
            }
            SetCommands::Location(location_constraint_args) => {
                let relay_filter = |relay: &mullvad_types::relay_list::Relay| {
                    relay.active && relay.endpoint_data == RelayEndpointData::Bridge
                };
                let location_constraint =
                    resolve_location_constraint(&mut rpc, location_constraint_args, relay_filter)
                        .await?
                        .map(LocationConstraint::from);
                Self::update_bridge_settings(&mut rpc, Some(location_constraint), None, None).await
            }
            SetCommands::CustomList { custom_list_name } => {
                let list =
                    super::custom_list::find_list_by_name(&mut rpc, &custom_list_name).await?;
                let location =
                    Constraint::Only(LocationConstraint::CustomList { list_id: list.id });
                Self::update_bridge_settings(&mut rpc, Some(location), None, None).await
            }
            SetCommands::Ownership { ownership } => {
                Self::update_bridge_settings(&mut rpc, None, None, Some(ownership)).await
            }
            SetCommands::Provider { providers } => {
                let providers = if providers[0].eq_ignore_ascii_case("any") {
                    Constraint::Any
                } else {
                    Constraint::Only(Providers::new(providers.into_iter()).unwrap())
                };
                Self::update_bridge_settings(&mut rpc, None, Some(providers), None).await
            }
            SetCommands::Custom(subcmd) => Self::handle_custom(subcmd).await,
        }
    }

    async fn get() -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        let settings = rpc.get_settings().await?;
        println!("Bridge state: {}", settings.bridge_state);
        println!(
            "Active bridge type: {}",
            settings.bridge_settings.bridge_type
        );

        println!("Normal constraints");
        println!(
            "{:<4}{}",
            "",
            BridgeConstraintsFormatter {
                constraints: &settings.bridge_settings.normal,
                custom_lists: &settings.custom_lists
            }
        );

        if let Some(ref custom_proxy) = settings.bridge_settings.custom {
            println!("Custom proxy");
            println!("{}", CustomProxyFormatter { custom_proxy });
        }

        Ok(())
    }

    async fn list() -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        let relay_list = rpc.get_relay_locations().await?;

        let mut countries = Vec::new();

        for mut country in relay_list.countries {
            country.cities = country
                .cities
                .into_iter()
                .filter_map(|mut city| {
                    city.relays.retain(|relay| {
                        relay.active && matches!(relay.endpoint_data, RelayEndpointData::Bridge)
                    });
                    if !city.relays.is_empty() {
                        Some(city)
                    } else {
                        None
                    }
                })
                .collect();
            if !country.cities.is_empty() {
                countries.push(country);
            }
        }

        countries.sort_by(|c1, c2| natord::compare_ignore_case(&c1.name, &c2.name));
        for mut country in countries {
            country
                .cities
                .sort_by(|c1, c2| natord::compare_ignore_case(&c1.name, &c2.name));
            println!("{} ({})", country.name, country.code);
            for mut city in country.cities {
                city.relays
                    .sort_by(|r1, r2| natord::compare_ignore_case(&r1.hostname, &r2.hostname));
                println!(
                    "\t{} ({}) @ {:.5}°N, {:.5}°W",
                    city.name, city.code, city.latitude, city.longitude
                );
                for relay in &city.relays {
                    let ownership = if relay.owned {
                        "Mullvad-owned"
                    } else {
                        "rented"
                    };
                    println!(
                        "\t\t{} ({}) - hosted by {} ({ownership})",
                        relay.hostname, relay.ipv4_addr_in, relay.provider
                    );
                }
            }
            println!();
        }
        Ok(())
    }

    async fn update_bridge_settings(
        rpc: &mut MullvadProxyClient,
        location: Option<Constraint<LocationConstraint>>,
        providers: Option<Constraint<Providers>>,
        ownership: Option<Constraint<Ownership>>,
    ) -> Result<()> {
        let mut settings = rpc.get_settings().await?.bridge_settings;
        if let Some(new_location) = location {
            settings.normal.location = new_location;
        }
        if let Some(new_providers) = providers {
            settings.normal.providers = new_providers;
        }
        if let Some(new_ownership) = ownership {
            settings.normal.ownership = new_ownership;
        }

        settings.bridge_type = BridgeType::Normal;

        rpc.set_bridge_settings(settings).await?;

        println!("Updated bridge settings");

        Ok(())
    }

    pub async fn handle_custom(subcmd: CustomCommands) -> Result<()> {
        match subcmd {
            CustomCommands::Set(set_custom_commands) => {
                Self::custom_bridge_set(set_custom_commands).await
            }
            CustomCommands::Edit(edit) => Self::custom_bridge_edit(edit).await,
            CustomCommands::Use => Self::custom_bridge_use().await,
            CustomCommands::Disable => Self::custom_bridge_disable().await,
        }
    }

    async fn custom_bridge_edit(edit: ProxyEditParams) -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        let mut settings = rpc.get_settings().await?;

        let Some(ref mut custom_bridge) = settings.bridge_settings.custom else {
            bail!("Can not edit as there is no currently saved custom bridge");
        };

        match custom_bridge {
            CustomProxy::Shadowsocks(ss) => *ss = edit.merge_shadowsocks(ss),
            CustomProxy::Socks5Local(local) => *local = edit.merge_socks_local(local),
            CustomProxy::Socks5Remote(remote) => *remote = edit.merge_socks_remote(remote)?,
        };

        rpc.set_bridge_settings(settings.bridge_settings)
            .await
            .map_err(anyhow::Error::from)
    }

    async fn custom_bridge_use() -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;

        let mut settings = rpc.get_settings().await?;
        if settings.bridge_settings.custom.is_none() {
            bail!("Cannot enable custom bridge as there are no settings");
        }
        settings.bridge_settings.bridge_type = BridgeType::Custom;
        rpc.set_bridge_settings(settings.bridge_settings).await?;

        Ok(())
    }

    async fn custom_bridge_disable() -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        let mut settings = rpc.get_settings().await?;

        settings.bridge_settings.bridge_type = BridgeType::Normal;

        rpc.set_bridge_settings(settings.bridge_settings).await?;
        Ok(())
    }

    async fn custom_bridge_set(set_commands: AddCustomCommands) -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        let mut settings = rpc.get_settings().await?;

        settings.bridge_settings.custom = Some(match set_commands {
            AddCustomCommands::Socks5(AddSocks5Commands::Local { add }) => {
                CustomProxy::Socks5Local(Socks5Local::from(add))
            }
            AddCustomCommands::Socks5(AddSocks5Commands::Remote { add }) => {
                CustomProxy::Socks5Remote(Socks5Remote::try_from(add)?)
            }
            AddCustomCommands::Shadowsocks { add } => {
                CustomProxy::Shadowsocks(Shadowsocks::from(add))
            }
        });

        settings.bridge_settings.bridge_type = BridgeType::Custom;

        rpc.set_bridge_settings(settings.bridge_settings)
            .await
            .map_err(anyhow::Error::from)
    }
}