tailscale-systray 0.1.0

Application Indicator (SNI) for Tailscale
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
use std::{ffi::OsString, io, path::PathBuf, thread};

use arboard::Clipboard;
use clap::Parser;
use ksni::{
    menu::{CheckmarkItem, StandardItem},
    Tray, TrayMethods,
};
use log::{debug, error, info, trace, warn, LevelFilter};
use notify_rust::Notification;
use std::io::{IsTerminal, Write};
use tailscale::{ExitNodeOption, TailscaleExec, TailscalePrefs, TailscaleStatus};

mod installation;
mod tailscale;

#[derive(Debug)]
enum ServiceState {
    Down,
    Running(Box<TailscaleStatus>),
}
struct TailscaleTray {
    exec: TailscaleExec,
    status: ServiceState,
    prefs: Option<Box<TailscalePrefs>>,
    clipboard: Clipboard,
}
impl TailscaleTray {
    fn new(args: &Args) -> Self {
        let exec = TailscaleExec::new(
            args.tailscale_bin.clone(),
            args.socket.as_ref(),
            args.up_arg.clone(),
        );
        let status = TailscaleTray::fetch_status(&exec);
        let prefs = match status {
            ServiceState::Running(_) => TailscaleTray::fetch_prefs(&exec),
            _ => None,
        };

        Self {
            exec,
            status,
            prefs,
            clipboard: Clipboard::new().unwrap(),
        }
    }

    fn fetch_status(exec: &TailscaleExec) -> ServiceState {
        trace!("Fetching Tailscale status");
        exec.status()
            .map(|s| ServiceState::Running(Box::new(s)))
            .unwrap_or_else(|e| {
                warn!("Failed to fetch Tailscale status: {}", e);
                ServiceState::Down
            })
    }

    fn fetch_prefs(exec: &TailscaleExec) -> Option<Box<TailscalePrefs>> {
        trace!("Fetching Tailscale preferences");
        match exec.prefs() {
            Ok(prefs) => Some(Box::new(prefs)),
            Err(e) => {
                warn!("Failed to fetch Tailscale preferences: {}", e);
                None
            }
        }
    }

    fn refetch_status(&mut self) {
        debug!("Refreshing Tailscale status and preferences");
        self.status = Self::fetch_status(&self.exec);
        self.prefs = match &self.status {
            ServiceState::Running(_) => Self::fetch_prefs(&self.exec),
            _ => None,
        };
        debug!("Refreshed status: {:?}", self.status);
    }
}

impl Tray for TailscaleTray {
    const MENU_ON_ACTIVATE: bool = true;

    fn id(&self) -> String {
        "tailscale-systray".into()
    }

    fn title(&self) -> String {
        match &self.status {
            ServiceState::Down => "Tailscale Down".into(),
            ServiceState::Running(status) => {
                let msg = if status.health.is_empty() {
                    "Healthy".into()
                } else {
                    status.health.join(", ")
                };
                format!("Tailscale: {}", msg)
            }
        }
    }

    fn icon_name(&self) -> String {
        match &self.status {
            ServiceState::Down => "tailscale-down",
            ServiceState::Running(status) => {
                if status.self_node.online {
                    if status.exit_node_status.as_ref().is_some_and(|x| x.online) {
                        "tailscale-exit-node"
                    } else {
                        "tailscale-up"
                    }
                } else {
                    "tailscale-down"
                }
            }
        }
        .into()
    }

    fn menu(&self) -> Vec<ksni::MenuItem<Self>> {
        use ksni::menu::*;

        let first_block = || {
            let res: Vec<ksni::MenuItem<TailscaleTray>> = match self.status {
                ServiceState::Down => vec![StandardItem {
                    label: "Tailscale Service Down".into(),
                    disposition: Disposition::Alert,
                    ..Default::default()
                }
                .into()],
                ServiceState::Running(ref tstatus) => {
                    let first_ip = tstatus
                        .tailscale_ips
                        .as_ref()
                        .and_then(|ips| ips.first())
                        .cloned();
                    let mut res: Vec<ksni::MenuItem<TailscaleTray>> = vec![StandardItem {
                        label: format!(
                            "{}: {}",
                            tstatus.self_node.host_name, tstatus.backend_state
                        ),
                        activate: Box::new(move |this: &mut Self| {
                            if let Some(ip) = &first_ip {
                                match this.clipboard.set_text(ip) {
                                    Ok(_) => debug!("Copied IP {} to clipboard", ip),
                                    Err(e) => warn!("Failed to copy IP to clipboard: {}", e),
                                }
                            }
                        }),
                        ..Default::default()
                    }
                    .into()];

                    if tstatus.backend_state == "Running" {
                        res.push(
                            StandardItem {
                                label: "Disable".into(),
                                activate: Box::new(|this: &mut Self| {
                                    info!("User requested to disable Tailscale");
                                    if let Err(err) = this.exec.down() {
                                        error!("Failed to disable Tailscale: {}", err);
                                        notify_of_failure(&err);
                                    } else {
                                        info!("Successfully disabled Tailscale");
                                    }
                                }),
                                ..Default::default()
                            }
                            .into(),
                        );
                    } else if tstatus.backend_state == "NeedsLogin" {
                        res.push(
                            StandardItem {
                                label: "Login".into(),
                                activate: Box::new(|this: &mut Self| {
                                    info!("User requested to login to Tailscale");
                                    this.exec.login();
                                }),
                                ..Default::default()
                            }
                            .into(),
                        );
                    } else {
                        res.push(
                            StandardItem {
                                label: "Enable".into(),
                                activate: Box::new(|this: &mut Self| {
                                    info!("User requested to enable Tailscale");
                                    if let Err(err) = this.exec.up() {
                                        error!("Failed to enable Tailscale: {}", err);
                                        notify_of_failure(&err);
                                    } else {
                                        info!("Successfully enabled Tailscale");
                                    }
                                }),
                                ..Default::default()
                            }
                            .into(),
                        );
                    }

                    res
                }
            };
            res
        };

        let mut res: Vec<ksni::MenuItem<TailscaleTray>> = vec![];
        res.append(&mut first_block());
        res.push(MenuItem::Separator);
        if let ServiceState::Running(ref sta) = self.status {
            if sta.backend_state == "Running" {
                res.push(
                    SubMenu {
                        label: "Online peers".into(),
                        submenu: online_peers(sta),
                        ..Default::default()
                    }
                    .into(),
                );
                if let Some(prefs) = self.prefs.as_ref() {
                    res.push(
                        SubMenu {
                            label: "Exit nodes".into(),
                            submenu: exit_node_menu(sta, prefs),
                            ..Default::default()
                        }
                        .into(),
                    );
                }
            }
        }
        res.push(
            StandardItem {
                label: "Exit".into(),
                icon_name: "application-exit".into(),
                activate: Box::new(|_| {
                    info!("User requested to exit the application");
                    std::process::exit(0)
                }),
                ..Default::default()
            }
            .into(),
        );
        res
    }
}

fn online_peers(status: &TailscaleStatus) -> Vec<ksni::MenuItem<TailscaleTray>> {
    trace!(
        "Building online peers menu with {} peers",
        status.online_peers().len()
    );
    status
        .online_peers()
        .iter()
        .map(|p| {
            let first_ip = p
                .tailscale_ips
                .as_ref()
                .and_then(|ips| ips.first())
                .cloned();
            StandardItem {
                label: p.host_name.clone(),
                activate: Box::new(move |this: &mut TailscaleTray| {
                    if let Some(ip) = &first_ip {
                        match this.clipboard.set_text(ip) {
                            Ok(_) => debug!("Copied peer IP {} to clipboard", ip),
                            Err(e) => warn!("Failed to copy peer IP to clipboard: {}", e),
                        }
                    }
                }),
                ..Default::default()
            }
            .into()
        })
        .collect()
}

fn exit_node_menu(
    status: &TailscaleStatus,
    prefs: &TailscalePrefs,
) -> Vec<ksni::MenuItem<TailscaleTray>> {
    trace!("Building exit node menu");
    let advertising_exit_node = prefs
        .advertise_routes
        .iter()
        .flatten()
        .any(|r| r.ends_with("/0"));

    debug!(
        "Currently advertising as exit node: {}",
        advertising_exit_node
    );
    let online_exit_nodes = status.online_exit_nodes();
    debug!("Available exit nodes: {}", online_exit_nodes.len());

    let mut res: Vec<ksni::MenuItem<TailscaleTray>> = vec![];
    for node in online_exit_nodes {
        let prefs_clone = prefs.clone();
        let eno = if node.exit_node {
            ExitNodeOption::None
        } else {
            ExitNodeOption::UseNode(node.host_name.clone())
        };
        res.push(
            CheckmarkItem {
                label: node.host_name.clone(),
                checked: node.exit_node,
                activate: Box::new(move |this: &mut TailscaleTray| {
                    info!("User changing exit node configuration");
                    if let Err(err) = this.exec.up_reconf(&eno, &prefs_clone) {
                        error!("Failed to reconfigure exit node: {}", err);
                        notify_of_failure(&err);
                    } else {
                        info!("Successfully reconfigured exit node");
                    }
                }),
                ..Default::default()
            }
            .into(),
        );
    }

    let prefs_clone = prefs.clone();
    if !res.is_empty() {
        res.push(ksni::MenuItem::Separator)
    }
    res.push(
        CheckmarkItem {
            label: "Run exit node".into(),
            checked: advertising_exit_node,
            activate: Box::new(move |this: &mut TailscaleTray| {
                let eno = if advertising_exit_node {
                    info!("User disabling exit node advertisement");
                    ExitNodeOption::None
                } else {
                    info!("User enabling exit node advertisement");
                    ExitNodeOption::Advertise
                };
                if let Err(err) = this.exec.up_reconf(&eno, &prefs_clone) {
                    error!("Failed to reconfigure exit node advertisement: {}", err);
                    notify_of_failure(&err);
                } else {
                    info!("Successfully reconfigured exit node advertisement");
                }
            }),
            ..Default::default()
        }
        .into(),
    );

    res
}

fn notify_of_failure(err: &anyhow::Error) {
    let err_string = err.to_string();
    error!("Tailscale operation failed: {}", err_string);
    thread::spawn(move || {
        match Notification::new()
            .summary("Tailscale error")
            .body(&err_string)
            .icon("network-error")
            .show()
        {
            Ok(_) => debug!("Displayed error notification"),
            Err(e) => error!("Failed to display error notification: {}", e),
        }
    });
}

#[derive(Parser, Debug)]
struct Args {
    /// Tailscale executable
    #[arg(long, default_value = "tailscale")]
    tailscale_bin: PathBuf,

    /// Locally install icons and desktop file
    #[arg(long, default_value_t = false)]
    install: bool,

    /// Path to tailscaled socket
    #[arg(long)]
    socket: Option<PathBuf>,

    /// Extra arguments to pass "tailscale up"
    #[arg(long)]
    up_arg: Vec<OsString>,

    /// Refresh period in seconds
    #[arg(long, default_value_t = 5u64)]
    refresh_period: u64,

    /// Verbosity level (0-5, where 0=error, 1=warn, 2=info, 3=debug, 4=trace, 5=trace+)
    #[arg(short, long, default_value_t = 2u8)]
    verbosity: u8,
}

fn setup_logger(verbosity: u8) {
    let level = match verbosity {
        0 => LevelFilter::Error,
        1 => LevelFilter::Warn,
        2 => LevelFilter::Info,
        3 => LevelFilter::Debug,
        4 | 5 => LevelFilter::Trace,
        _ => LevelFilter::Info,
    };

    let mut builder = env_logger::Builder::new();

    if !io::stdout().is_terminal() {
        builder.format(move |buf, record| writeln!(buf, "[{}] {}", record.level(), record.args()));
    }

    builder.filter_level(level).init();
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let args = Args::parse();

    setup_logger(args.verbosity);

    if args.install {
        match installation::local_install() {
            Ok(_) => {
                info!("Local installation completed successfully");
                return;
            }
            Err(e) => {
                error!("Local installation failed: {}", e);
                std::process::exit(1);
            }
        }
    }

    info!(
        "Starting Tailscale systray with verbosity level {}",
        args.verbosity
    );
    debug!("Using Tailscale binary: {:?}", args.tailscale_bin);
    if let Some(socket) = &args.socket {
        debug!("Using Tailscale socket: {:?}", socket);
    }
    if !args.up_arg.is_empty() {
        debug!("Additional 'up' arguments: {:?}", args.up_arg);
    }

    let tray = TailscaleTray::new(&args);
    info!("Initializing system tray");
    let handle = match tray.spawn().await {
        Ok(h) => {
            info!("System tray initialized successfully");
            h
        }
        Err(e) => {
            error!("Failed to initialize system tray: {}", e);
            panic!("Failed to initialize system tray: {}", e);
        }
    };

    info!(
        "Starting refresh loop with period of {} seconds",
        args.refresh_period
    );
    loop {
        trace!("Waiting for next refresh cycle");
        tokio::time::sleep(std::time::Duration::from_secs(args.refresh_period)).await;

        trace!("Updating tray status");
        if (handle
            .update(|tray: &mut TailscaleTray| tray.refetch_status())
            .await)
            .is_none()
        {
            error!("The tray service has shutdown");
            break;
        } else {
            trace!("Tray update completed");
        }
    }
}