Skip to main content

agentmux_launcher/lan_firewall/
mod.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `agentmux.exe --configure-lan-firewall` and `--remove-lan-firewall`: the one elevated step that lets other computers
5//! reach AgentMux on Windows (`docs/specs/SPEC_LAN_FIREWALL_SETUP_2026_10_01.md` 4.1).
6//!
7//! Started by a person's first LAN enable, the installer's "Allow LAN access" task and the uninstaller. It reads the
8//! firewall without elevation, and asks Windows for administrator permission only when something has to change, so a
9//! machine that is already set up sees no prompt. What the rules are, and what to change to reach them, is
10//! `agentmux_common::lan_firewall_rules`; this module reads the machine, applies the plan and says how it went in its
11//! exit code.
12
13use agentmux_common::lan_firewall_rules::Mode;
14
15#[cfg(windows)]
16mod win;
17
18/// The flag, and the exit codes the helper says how it went with, are shared with the server, which runs the helper when
19/// the person asks for the setup.
20#[cfg_attr(not(windows), allow(unused_imports))]
21pub use agentmux_common::lan_firewall_rules::{CONFIGURE_FLAG, EXIT_CANCELLED, EXIT_FAILED, EXIT_MANAGED, EXIT_OK, EXIT_UNVERIFIED};
22/// Remove the two port rules (the uninstaller).
23pub const REMOVE_FLAG: &str = "--remove-lan-firewall";
24/// Given to the copy this one starts with administrator permission, so that copy never asks again.
25#[cfg_attr(not(windows), allow(dead_code))]
26pub const ELEVATED_FLAG: &str = "--lan-firewall-elevated";
27
28
29/// The mode the arguments ask for, if either flag is there. Configure wins when both are.
30fn requested_mode(args: &[String]) -> Option<Mode> {
31    let has = |f: &str| args.iter().any(|a| a == f);
32    if has(CONFIGURE_FLAG) {
33        Some(Mode::Configure)
34    } else if has(REMOVE_FLAG) {
35        Some(Mode::Remove)
36    } else {
37        None
38    }
39}
40
41/// Run the helper when `args` ask for it, and give its exit code. `None` when they do not.
42pub fn run_if_requested(args: &[String]) -> Option<i32> {
43    let mode = requested_mode(args)?;
44    Some(run(mode, args))
45}
46
47#[cfg(windows)]
48fn run(mode: Mode, args: &[String]) -> i32 {
49    win::run(mode, args.iter().any(|a| a == ELEVATED_FLAG))
50}
51
52#[cfg(not(windows))]
53fn run(_mode: Mode, _args: &[String]) -> i32 {
54    eprintln!("the LAN firewall helper is only for Windows Defender Firewall");
55    EXIT_FAILED
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    fn args(a: &[&str]) -> Vec<String> {
63        a.iter().map(|s| s.to_string()).collect()
64    }
65
66    #[test]
67    fn only_the_two_flags_start_the_helper() {
68        assert_eq!(requested_mode(&args(&["agentmux.exe"])), None);
69        assert_eq!(requested_mode(&args(&["agentmux.exe", "--version"])), None);
70        assert_eq!(requested_mode(&args(&["agentmux.exe", CONFIGURE_FLAG])), Some(Mode::Configure));
71        assert_eq!(requested_mode(&args(&["agentmux.exe", REMOVE_FLAG])), Some(Mode::Remove));
72        // The copy started with permission carries the flag and the mode, and is still that mode.
73        assert_eq!(requested_mode(&args(&["agentmux.exe", REMOVE_FLAG, ELEVATED_FLAG])), Some(Mode::Remove));
74        // A link that merely contains the text is not a flag.
75        assert_eq!(requested_mode(&args(&["agentmux.exe", "--", "agentmux://--configure-lan-firewall"])), None);
76        assert_eq!(run_if_requested(&args(&["agentmux.exe"])), None);
77    }
78
79    #[test]
80    fn the_exit_codes_are_distinct_and_the_cancel_is_windows_own() {
81        let codes = [EXIT_OK, EXIT_CANCELLED, EXIT_MANAGED, EXIT_FAILED, EXIT_UNVERIFIED];
82        for (i, a) in codes.iter().enumerate() {
83            assert!(codes[i + 1..].iter().all(|b| a != b));
84        }
85        assert_eq!(EXIT_CANCELLED, 1223, "ERROR_CANCELLED");
86    }
87}