summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/cmds/split_tunnel/windows.rs
blob: f17a3382f38c8d539f57d13c6d39ae5aac0b6556 (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
use anyhow::Result;
use std::{
    ffi::OsStr,
    path::{Path, PathBuf},
};

use clap::Subcommand;
use mullvad_management_interface::MullvadProxyClient;

use super::super::BooleanOption;

/// Set options for applications to exclude from the tunnel.
#[derive(Subcommand, Debug)]
pub enum SplitTunnel {
    /// Display the split tunnel status and apps
    Get {
        /// List processes that are currently being excluded, as well as whether they are
        /// excluded because of their executable paths or because they're subprocesses of
        /// such processes
        #[arg(long)]
        list_processes: bool,
    },

    /// Enable or disable split tunnel
    Set { policy: BooleanOption },

    /// Manage applications to exclude from the tunnel
    #[clap(subcommand)]
    App(App),
}

#[derive(Subcommand, Debug)]
pub enum App {
    Add { path: PathBuf },
    Remove { path: PathBuf },
    Clear,
}

impl SplitTunnel {
    pub async fn handle(self) -> Result<()> {
        match self {
            SplitTunnel::Get { list_processes } => {
                let mut rpc = MullvadProxyClient::new().await?;
                let settings = rpc.get_settings().await?.split_tunnel;

                let enable_exclusions = BooleanOption::from(settings.enable_exclusions);

                println!("Split tunneling state: {enable_exclusions}");

                println!("Excluded applications:");
                for path in &settings.apps {
                    println!("{}", path.display());
                }

                if list_processes {
                    let processes = rpc.get_excluded_processes().await?;
                    for process in &processes {
                        let subproc = if process.inherited { "subprocess" } else { "" };
                        println!(
                            "{:<7}{subproc:<12}{}",
                            process.pid,
                            Path::new(&process.image)
                                .file_name()
                                .unwrap_or(OsStr::new("unknown"))
                                .to_string_lossy()
                        );
                    }
                }

                Ok(())
            }
            SplitTunnel::Set { policy } => {
                let mut rpc = MullvadProxyClient::new().await?;
                rpc.set_split_tunnel_state(*policy).await?;
                println!("Split tunnel policy: {policy}");
                Ok(())
            }
            SplitTunnel::App(subcmd) => Self::app(subcmd).await,
        }
    }

    async fn app(subcmd: App) -> Result<()> {
        match subcmd {
            App::Add { path } => {
                MullvadProxyClient::new()
                    .await?
                    .add_split_tunnel_app(path)
                    .await?;
                println!("Added path to excluded apps list");
                Ok(())
            }
            App::Remove { path } => {
                MullvadProxyClient::new()
                    .await?
                    .remove_split_tunnel_app(path)
                    .await?;
                println!("Stopped excluding app from tunnel");
                Ok(())
            }
            App::Clear => {
                MullvadProxyClient::new()
                    .await?
                    .clear_split_tunnel_apps()
                    .await?;
                println!("Stopped excluding all apps");
                Ok(())
            }
        }
    }
}