summaryrefslogtreecommitdiffhomepage
path: root/mullvad-daemon/src/cli.rs
blob: d702c443c581bd5dea2347abf3b29623b0afb849 (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
use clap::{Args, Parser};
use std::sync::LazyLock;

static ENV_DESC: LazyLock<String> = LazyLock::new(|| {
    format!(
"ENV:

    MULLVAD_RESOURCE_DIR       Resource directory (i.e used to locate a root CA certificate)
                               [Default: {}]
    MULLVAD_SETTINGS_DIR       Directory path for storing settings. [Default: {}]
    MULLVAD_CACHE_DIR          Directory path for storing cache. [Default: {}]
    MULLVAD_LOG_DIR            Directory path for storing logs. [Default: {}]
    MULLVAD_RPC_SOCKET_PATH    Location of the management interface device.
                               It refers to Unix domain socket on Unix based platforms, and named pipe on Windows.
                               [Default: {}]

",
        mullvad_paths::get_default_resource_dir().display(),
        mullvad_paths::get_default_settings_dir().map(|dir| dir.display().to_string()).unwrap_or_else(|_| "N/A".to_string()),
        mullvad_paths::get_default_cache_dir().map(|dir| dir.display().to_string()).unwrap_or_else(|_| "N/A".to_string()),
        mullvad_paths::get_default_log_dir().map(|dir| dir.display().to_string()).unwrap_or_else(|_| "N/A".to_string()),
        mullvad_paths::get_default_rpc_socket_path().display()
)
});

#[derive(Debug, Parser)]
#[command(author, version = mullvad_version::VERSION, about, long_about = None, after_help = &*ENV_DESC)]
struct Cli {
    /// Set the level of verbosity
    #[arg(short='v', action = clap::ArgAction::Count)]
    verbosity: u8,
    /// Disable logging to file
    #[arg(long)]
    disable_log_to_file: bool,
    /// Don't log timestamps when logging to stdout, useful when running as a systemd service
    #[arg(long)]
    disable_stdout_timestamps: bool,

    #[command(flatten)]
    command: CommandFlags,
}

#[derive(Debug, Args)]
#[group(multiple = false, required = false)]
pub struct CommandFlags {
    /// Run as a system service
    #[cfg(target_os = "windows")]
    #[arg(long)]
    run_as_service: bool,

    /// Register Mullvad daemon as a system service
    #[cfg(target_os = "windows")]
    #[arg(long)]
    register_service: bool,

    /// Initialize firewall to be used during early boot and exit
    #[cfg(target_os = "linux")]
    #[arg(long)]
    initialize_early_boot_firewall: bool,

    /// Check the status of the launch daemon. The exit code represents the current status
    #[cfg(target_os = "macos")]
    #[arg(long)]
    launch_daemon_status: bool,
}

#[derive(Debug)]
pub struct Config {
    pub log_level: log::LevelFilter,
    pub log_to_file: bool,
    pub log_stdout_timestamps: bool,

    pub command: Command,
}

#[derive(Debug)]
pub enum Command {
    /// Run the standalone daemon.
    Daemon,

    /// Initialize firewall to be used during early boot and exit
    #[cfg(target_os = "linux")]
    InitializeEarlyBootFirewall,

    /// Run the daemon as a system service.
    #[cfg(target_os = "windows")]
    RunAsService,

    /// Register Mullvad daemon as a system service.
    #[cfg(target_os = "windows")]
    RegisterService,

    /// Check the status of the launch daemon. The exit code represents the current status.
    #[cfg(target_os = "macos")]
    LaunchDaemonStatus,
}

impl From<CommandFlags> for Command {
    fn from(f: CommandFlags) -> Self {
        let command_flags = [
            #[cfg(target_os = "linux")]
            (
                f.initialize_early_boot_firewall,
                Command::InitializeEarlyBootFirewall,
            ),
            #[cfg(target_os = "windows")]
            (f.run_as_service, Command::RunAsService),
            #[cfg(target_os = "windows")]
            (f.register_service, Command::RegisterService),
            #[cfg(target_os = "macos")]
            (f.launch_daemon_status, Command::LaunchDaemonStatus),
        ];

        command_flags
            .into_iter()
            .find_map(|(flag, command)| flag.then_some(command))
            .unwrap_or(Command::Daemon)
    }
}

pub fn get_config() -> &'static Config {
    static CONFIG: LazyLock<Config> = LazyLock::new(create_config);
    &CONFIG
}

fn create_config() -> Config {
    let app = Cli::parse();

    let log_level = match app.verbosity {
        0 => log::LevelFilter::Info,
        1 => log::LevelFilter::Debug,
        _ => log::LevelFilter::Trace,
    };

    Config {
        log_level,
        log_to_file: !app.disable_log_to_file,
        log_stdout_timestamps: !app.disable_stdout_timestamps,
        command: app.command.into(),
    }
}