wakezilla 0.2.1

A Wake-on-LAN proxy server written in Rust
Documentation
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
//! OS-native system service installation for the `setup` subcommand.

use anyhow::{anyhow, Context, Result};
use std::net::TcpStream;
use std::process::Command;
use std::time::Duration;

/// Which Wakezilla server the service runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
    Proxy,
    Client,
}

impl Mode {
    /// Parse from a CLI string. Returns `None` for unknown values.
    pub fn from_str_opt(s: &str) -> Option<Self> {
        match s.trim().to_ascii_lowercase().as_str() {
            "proxy" | "proxy-server" => Some(Mode::Proxy),
            "client" | "client-server" => Some(Mode::Client),
            _ => None,
        }
    }

    /// The wakezilla subcommand this mode launches.
    pub fn subcommand(self) -> &'static str {
        match self {
            Mode::Proxy => "proxy-server",
            Mode::Client => "client-server",
        }
    }

    /// systemd unit / Windows service name.
    // Platform-conditional: used by the systemd (Linux) / launchd (macOS) / Windows install paths; some are cfg'd out per-OS.
    #[allow(dead_code)]
    pub fn service_name(self) -> &'static str {
        match self {
            Mode::Proxy => "wakezilla-proxy",
            Mode::Client => "wakezilla-client",
        }
    }

    /// launchd label (reverse-DNS).
    // Platform-conditional: used by the systemd (Linux) / launchd (macOS) / Windows install paths; some are cfg'd out per-OS.
    #[allow(dead_code)]
    pub fn launchd_label(self) -> &'static str {
        match self {
            Mode::Proxy => "dev.wakezilla.proxy",
            Mode::Client => "dev.wakezilla.client",
        }
    }

    /// Default port for this mode.
    pub fn default_port(self) -> u16 {
        match self {
            Mode::Proxy => 3000,
            Mode::Client => 3001,
        }
    }
}

/// Render a systemd unit file. `exe` is the absolute path to the wakezilla binary.
// Platform-conditional: used by the systemd (Linux) / launchd (macOS) / Windows install paths; some are cfg'd out per-OS.
#[allow(dead_code)]
pub fn generate_systemd_unit(mode: Mode, exe: &str) -> String {
    format!(
        "[Unit]\n\
         Description=Wakezilla {desc}\n\
         After=network-online.target\n\
         Wants=network-online.target\n\
         \n\
         [Service]\n\
         Type=simple\n\
         ExecStart={exe} {sub}\n\
         Restart=on-failure\n\
         RestartSec=5\n\
         \n\
         [Install]\n\
         WantedBy=multi-user.target\n",
        desc = mode.subcommand(),
        exe = exe,
        sub = mode.subcommand(),
    )
}

/// Directory where the macOS LaunchDaemon writes its stdout/stderr logs.
pub const MACOS_LOG_DIR: &str = "/Library/Logs/wakezilla";

/// Path the daemon's stdout is redirected to (macOS).
fn macos_stdout_log(mode: Mode) -> String {
    format!("{MACOS_LOG_DIR}/{}.out.log", mode.launchd_label())
}

/// Path the daemon's stderr is redirected to (macOS). Tracing logs go here.
fn macos_stderr_log(mode: Mode) -> String {
    format!("{MACOS_LOG_DIR}/{}.err.log", mode.launchd_label())
}

/// Render a launchd LaunchDaemon plist.
// Platform-conditional: used by the systemd (Linux) / launchd (macOS) / Windows install paths; some are cfg'd out per-OS.
#[allow(dead_code)]
pub fn generate_launchd_plist(mode: Mode, exe: &str) -> String {
    format!(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
         <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
         <plist version=\"1.0\">\n\
         <dict>\n\
         \t<key>Label</key>\n\
         \t<string>{label}</string>\n\
         \t<key>ProgramArguments</key>\n\
         \t<array>\n\
         \t\t<string>{exe}</string>\n\
         \t\t<string>{sub}</string>\n\
         \t</array>\n\
         \t<key>RunAtLoad</key>\n\
         \t<true/>\n\
         \t<key>KeepAlive</key>\n\
         \t<true/>\n\
         \t<key>StandardOutPath</key>\n\
         \t<string>{out}</string>\n\
         \t<key>StandardErrorPath</key>\n\
         \t<string>{err}</string>\n\
         </dict>\n\
         </plist>\n",
        label = mode.launchd_label(),
        exe = exe,
        sub = mode.subcommand(),
        out = macos_stdout_log(mode),
        err = macos_stderr_log(mode),
    )
}

/// Validate the service is up by TCP-connecting to `127.0.0.1:port`.
/// Retries up to `attempts` times with a 500ms pause and per-attempt 1s timeout.
pub fn validate(port: u16, attempts: u32) -> Result<()> {
    let addr = format!("127.0.0.1:{port}");
    let socket = addr
        .parse()
        .with_context(|| format!("invalid validation address {addr}"))?;

    for attempt in 1..=attempts {
        match TcpStream::connect_timeout(&socket, Duration::from_secs(1)) {
            Ok(_) => return Ok(()),
            Err(_) if attempt < attempts => {
                std::thread::sleep(Duration::from_millis(500));
            }
            Err(e) => {
                return Err(anyhow!(
                    "service did not accept connections on {addr} after {attempts} attempts: {e}"
                ));
            }
        }
    }
    Err(anyhow!("service not reachable on {addr}"))
}

/// Returns true if the current process can install a system service.
pub fn is_elevated() -> bool {
    #[cfg(unix)]
    {
        Command::new("id")
            .arg("-u")
            .output()
            .ok()
            .and_then(|o| String::from_utf8(o.stdout).ok())
            .map(|s| s.trim() == "0")
            .unwrap_or(false)
    }
    #[cfg(windows)]
    {
        // `net session` requires admin; non-zero exit if unprivileged.
        Command::new("net")
            .arg("session")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }
}

/// Install, enable, and start the system service for `mode`.
///
/// Idempotent: re-running over an existing install overwrites the unit/plist and
/// reloads/recreates the service so a changed mode or port takes effect.
/// `exe` is the absolute path to the wakezilla binary.
pub fn install(mode: Mode, exe: &str) -> Result<()> {
    #[cfg(target_os = "linux")]
    {
        let unit = generate_systemd_unit(mode, exe);
        let path = format!("/etc/systemd/system/{}.service", mode.service_name());
        std::fs::write(&path, unit).with_context(|| format!("writing {path}"))?;
        run("systemctl", &["daemon-reload"])?;
        run("systemctl", &["enable", mode.service_name()])?;
        // restart (not `enable --now`) so an already-running service picks up the
        // updated unit; restart also starts it if it was stopped.
        run("systemctl", &["restart", mode.service_name()])?;
        Ok(())
    }
    #[cfg(target_os = "macos")]
    {
        // Ensure the log directory exists so launchd can redirect stdout/stderr.
        std::fs::create_dir_all(MACOS_LOG_DIR)
            .with_context(|| format!("creating log dir {MACOS_LOG_DIR}"))?;
        let plist = generate_launchd_plist(mode, exe);
        let path = format!("/Library/LaunchDaemons/{}.plist", mode.launchd_label());
        std::fs::write(&path, plist).with_context(|| format!("writing {path}"))?;
        // Unload any previous instance (ignored if not loaded) so the updated
        // plist is reloaded on a re-run instead of erroring "already loaded".
        run_ignore_err("launchctl", &["unload", &path]);
        run("launchctl", &["load", "-w", &path])?;
        Ok(())
    }
    #[cfg(target_os = "windows")]
    {
        // Best-effort teardown of any previous instance so `sc create` does not
        // error "service already exists" on a re-run.
        run_ignore_err("sc", &["stop", mode.service_name()]);
        run_ignore_err("sc", &["delete", mode.service_name()]);
        let bin_path = format!("\"{exe}\" {}", mode.subcommand());
        run(
            "sc",
            &[
                "create",
                mode.service_name(),
                "binPath=",
                &bin_path,
                "start=",
                "auto",
            ],
        )?;
        run("sc", &["start", mode.service_name()])?;
        Ok(())
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        let _ = (mode, exe);
        Err(anyhow!("service install not supported on this OS"))
    }
}

/// Run a command, returning an error if it exits non-zero.
#[allow(dead_code)]
fn run(cmd: &str, args: &[&str]) -> Result<()> {
    let status = Command::new(cmd)
        .args(args)
        .status()
        .with_context(|| format!("failed to run {cmd}"))?;
    if !status.success() {
        return Err(anyhow!("{cmd} {args:?} exited with {status}"));
    }
    Ok(())
}

/// Run a command, ignoring its outcome. Used for best-effort teardown of a prior
/// install (e.g. unloading/deleting an existing service) before reinstalling.
#[allow(dead_code)]
fn run_ignore_err(cmd: &str, args: &[&str]) {
    let _ = Command::new(cmd).args(args).status();
}

/// Run a command inheriting the terminal's stdio (for streaming logs). Returns an
/// error only if the command could not be spawned; a non-zero exit (e.g. Ctrl-C
/// out of a `tail -f` / `journalctl -f`) is treated as a normal end of streaming.
#[allow(dead_code)]
fn run_inherit(cmd: &str, args: &[&str]) -> Result<()> {
    Command::new(cmd)
        .args(args)
        .status()
        .with_context(|| format!("failed to run {cmd}"))?;
    Ok(())
}

/// Path to the on-disk service descriptor (systemd unit / launchd plist).
/// Not defined on Windows, which has no descriptor file (services live in the SCM).
#[cfg(target_os = "macos")]
fn descriptor_path(mode: Mode) -> String {
    format!("/Library/LaunchDaemons/{}.plist", mode.launchd_label())
}
#[cfg(target_os = "linux")]
fn descriptor_path(mode: Mode) -> String {
    format!("/etc/systemd/system/{}.service", mode.service_name())
}

/// True if the service for `mode` appears installed on this host.
pub fn is_installed(mode: Mode) -> bool {
    #[cfg(any(target_os = "linux", target_os = "macos"))]
    {
        std::path::Path::new(&descriptor_path(mode)).exists()
    }
    #[cfg(target_os = "windows")]
    {
        Command::new("sc")
            .args(["query", mode.service_name()])
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        let _ = mode;
        false
    }
}

/// The set of modes currently installed as services on this host.
pub fn installed_modes() -> Vec<Mode> {
    [Mode::Proxy, Mode::Client]
        .into_iter()
        .filter(|m| is_installed(*m))
        .collect()
}

/// Start the installed service for `mode`.
pub fn start(mode: Mode) -> Result<()> {
    #[cfg(target_os = "linux")]
    {
        run("systemctl", &["start", mode.service_name()])
    }
    #[cfg(target_os = "macos")]
    {
        run("launchctl", &["load", "-w", &descriptor_path(mode)])
    }
    #[cfg(target_os = "windows")]
    {
        run("sc", &["start", mode.service_name()])
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        let _ = mode;
        Err(anyhow!("service control not supported on this OS"))
    }
}

/// Stop the installed service for `mode`.
pub fn stop(mode: Mode) -> Result<()> {
    #[cfg(target_os = "linux")]
    {
        run("systemctl", &["stop", mode.service_name()])
    }
    #[cfg(target_os = "macos")]
    {
        run("launchctl", &["unload", &descriptor_path(mode)])
    }
    #[cfg(target_os = "windows")]
    {
        run("sc", &["stop", mode.service_name()])
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        let _ = mode;
        Err(anyhow!("service control not supported on this OS"))
    }
}

/// Restart the installed service for `mode`.
pub fn restart(mode: Mode) -> Result<()> {
    #[cfg(target_os = "linux")]
    {
        run("systemctl", &["restart", mode.service_name()])
    }
    #[cfg(target_os = "macos")]
    {
        run_ignore_err("launchctl", &["unload", &descriptor_path(mode)]);
        run("launchctl", &["load", "-w", &descriptor_path(mode)])
    }
    #[cfg(target_os = "windows")]
    {
        run_ignore_err("sc", &["stop", mode.service_name()]);
        run("sc", &["start", mode.service_name()])
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        let _ = mode;
        Err(anyhow!("service control not supported on this OS"))
    }
}

/// Whether the service for `mode` is currently running.
pub fn is_running(mode: Mode) -> bool {
    #[cfg(target_os = "linux")]
    {
        Command::new("systemctl")
            .args(["is-active", "--quiet", mode.service_name()])
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }
    #[cfg(target_os = "macos")]
    {
        let label = format!("system/{}", mode.launchd_label());
        Command::new("launchctl")
            .args(["print", &label])
            .output()
            .map(|o| {
                o.status.success() && String::from_utf8_lossy(&o.stdout).contains("state = running")
            })
            .unwrap_or(false)
    }
    #[cfg(target_os = "windows")]
    {
        Command::new("sc")
            .args(["query", mode.service_name()])
            .output()
            .map(|o| String::from_utf8_lossy(&o.stdout).contains("RUNNING"))
            .unwrap_or(false)
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        let _ = mode;
        false
    }
}

/// Stream the service's logs to the terminal. `lines` is the tail length;
/// `follow` keeps streaming new output until interrupted.
pub fn logs(mode: Mode, follow: bool, lines: u32) -> Result<()> {
    #[cfg(target_os = "linux")]
    {
        let n = lines.to_string();
        let mut args = vec!["-u", mode.service_name(), "-n", &n];
        if follow {
            args.push("-f");
        }
        run_inherit("journalctl", &args)
    }
    #[cfg(target_os = "macos")]
    {
        let path = macos_stderr_log(mode);
        if !std::path::Path::new(&path).exists() {
            anyhow::bail!(
                "no log file at {path} yet. The service may not have started or \
                 produced any output."
            );
        }
        let n = lines.to_string();
        let mut args = vec!["-n", &n];
        if follow {
            args.push("-f");
        }
        args.push(&path);
        run_inherit("tail", &args)
    }
    #[cfg(target_os = "windows")]
    {
        let _ = (follow, lines);
        println!(
            "Log streaming is not captured for the Windows service ({}). \
             Check the Windows Event Viewer.",
            mode.subcommand()
        );
        Ok(())
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        let _ = (mode, follow, lines);
        Err(anyhow!("log viewing not supported on this OS"))
    }
}