Skip to main content

agentmux_srv_core/backend/lan/
ports.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The fixed port range srv's listeners live in, so a firewall rule can name
5//! them (`docs/specs/SPEC_LAN_FIREWALL_SETUP_2026_10_01.md` ยง4.1).
6//!
7//! srv used to let the OS choose its web and ws ports on every start. A
8//! firewall rule that must survive updates, new portable builds and reinstalls
9//! cannot name a port that changes, and a program rule cannot either: the
10//! sidecar's filename embeds its version, so a per-exe rule stops matching on
11//! every update and re-registering it needs another elevation. A port range
12//! does neither.
13//!
14//! The loopback and LAN listeners share a port (see `lan/listeners.rs`), so
15//! choosing the *startup* ports from the range is enough: every LAN listener
16//! added later lands in the range too, and mDNS advertises the real port.
17//!
18//! Several instances on one host (channels, portable builds, `task dev`) each
19//! take the next free ports. If fewer than two are free srv does not fail: it
20//! falls back to OS-chosen ports exactly as before, and
21//! [`StartupListeners::in_range`] is `false` so the LAN indicator can say the
22//! rule does not cover this instance.
23
24use std::net::Ipv4Addr;
25use std::ops::RangeInclusive;
26
27use tokio::net::TcpListener;
28
29/// TCP ports srv's web and ws listeners are taken from.
30///
31/// **It must sit below 32768.** The OS hands ephemeral ports to *outbound*
32/// connections, and the defaults are 49152-65535 on Windows and macOS but
33/// 32768-60999 on Linux. A port in either range can be in use as the local end
34/// of an outgoing connection on a LAN address while it is free on loopback, and
35/// the startup bind only tests loopback: the LAN listener would then fail to
36/// bind that address later and LAN would stay partly off until the connection
37/// closed (Codex P2 on #4120; the first draft used 47892-47991, inside Linux's
38/// range). Below 32768 is outside all three defaults.
39///
40/// Within that, the block is chosen away from well-known neighbours: Syncthing
41/// (21027, 22000), Synergy (24800), Minecraft (25565), Steam and Source games
42/// (27000s), MongoDB (27017) and Kubernetes NodePorts (30000-32767). One
43/// hundred ports hold about fifty instances. Another program that happens to
44/// listen in the range is not harmed, but a firewall rule on the range would
45/// expose it to the local subnet, which is why the block is not larger.
46pub const LAN_PORT_RANGE: RangeInclusive<u16> = 29700..=29799;
47
48/// The two startup listeners, plus where they came from.
49pub struct StartupListeners {
50    pub web: TcpListener,
51    pub ws: TcpListener,
52    pub in_range: bool,
53}
54
55/// Bind the web and ws listeners on loopback, from [`LAN_PORT_RANGE`] when two
56/// ports are free there, else OS-chosen. Panics only if even an OS-chosen
57/// loopback bind fails, as the code this replaces did.
58pub async fn bind_startup_listeners() -> StartupListeners {
59    bind_startup_listeners_from(LAN_PORT_RANGE).await
60}
61
62/// [`bind_startup_listeners`] over an explicit candidate list. Separate so the
63/// tests can drive it with ports they control instead of racing real
64/// instances for the production range.
65pub(crate) async fn bind_startup_listeners_from(
66    candidates: impl IntoIterator<Item = u16>,
67) -> StartupListeners {
68    if let Some((web, ws)) = first_two_free(candidates).await {
69        return StartupListeners {
70            web,
71            ws,
72            in_range: true,
73        };
74    }
75    let any = (Ipv4Addr::LOCALHOST, 0);
76    let web = TcpListener::bind(any)
77        .await
78        .unwrap_or_else(|e| panic!("failed to bind web listener on 127.0.0.1:0: {e}"));
79    let ws = TcpListener::bind(any)
80        .await
81        .unwrap_or_else(|e| panic!("failed to bind ws listener on 127.0.0.1:0: {e}"));
82    StartupListeners {
83        web,
84        ws,
85        in_range: false,
86    }
87}
88
89/// The first two loopback ports among `candidates` that bind, in order. Not
90/// necessarily adjacent: an instance that started earlier may hold one in the
91/// middle. `None` when fewer than two are free; nothing is left bound then.
92async fn first_two_free(
93    candidates: impl IntoIterator<Item = u16>,
94) -> Option<(TcpListener, TcpListener)> {
95    let mut bound = Vec::with_capacity(2);
96    for port in candidates {
97        // A port another instance (or anything else) holds simply fails: skip it.
98        if let Ok(listener) = TcpListener::bind((Ipv4Addr::LOCALHOST, port)).await {
99            bound.push(listener);
100            if bound.len() == 2 {
101                let ws = bound.pop()?;
102                let web = bound.pop()?;
103                return Some((web, ws));
104            }
105        }
106    }
107    // Fewer than two: `bound` drops here, so a lone free port is not left held.
108    None
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    #[test]
116    fn the_firewall_rules_name_the_ports_the_listeners_and_discovery_use() {
117        use agentmux_common::lan_firewall_rules::{TCP_PORTS, UDP_PORTS};
118        // The rule the helper writes is the range the listeners take their ports from, and its UDP list holds every
119        // UDP port the firewall check needs: a change to either must change the rules, or LAN breaks silently.
120        assert_eq!(TCP_PORTS, format!("{}-{}", LAN_PORT_RANGE.start(), LAN_PORT_RANGE.end()));
121        let udp: Vec<u16> = UDP_PORTS.split(',').map(|p| p.parse().unwrap()).collect();
122        let needed = [5353, super::super::discovery::UDP_DISCOVERY_PORT, super::super::discovery::DESKTOP_DISCOVERY_PORT];
123        for port in needed {
124            assert!(udp.contains(&port), "UDP port {port} is not in the firewall rule");
125        }
126        assert_eq!(udp.len(), needed.len(), "the rule names a UDP port nothing needs: {udp:?}");
127    }
128
129
130    /// These tests bind and release ports to learn which are free. Run in
131    /// parallel, one can be handed a port another just released, and the
132    /// candidate list it built then holds fewer free ports than it thinks.
133    /// One at a time (Codex P2 on #4120).
134    static PORT_TESTS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
135
136    /// A port that is free right now (bound, noted, released).
137    fn free_port() -> u16 {
138        std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
139            .unwrap()
140            .local_addr()
141            .unwrap()
142            .port()
143    }
144
145    /// Two distinct free ports.
146    fn two_free_ports() -> (u16, u16) {
147        let a = free_port();
148        loop {
149            let b = free_port();
150            if b != a {
151                return (a, b);
152            }
153        }
154    }
155
156    #[tokio::test]
157    async fn takes_the_first_two_free_ports_in_order() {
158        let _serial = PORT_TESTS.lock().await;
159        let (a, b) = two_free_ports();
160        let got = bind_startup_listeners_from([a, b]).await;
161        assert!(got.in_range);
162        assert_eq!(got.web.local_addr().unwrap().port(), a);
163        assert_eq!(got.ws.local_addr().unwrap().port(), b);
164    }
165
166    #[tokio::test]
167    async fn skips_a_port_another_instance_holds() {
168        let _serial = PORT_TESTS.lock().await;
169        // `taken` stands for an instance that started earlier and is still running.
170        let taken = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
171        let taken_port = taken.local_addr().unwrap().port();
172        let (b, c) = two_free_ports();
173        let got = bind_startup_listeners_from([taken_port, b, c]).await;
174        assert!(
175            got.in_range,
176            "two free ports remain, so it is still in range"
177        );
178        assert_eq!(got.web.local_addr().unwrap().port(), b);
179        assert_eq!(got.ws.local_addr().unwrap().port(), c);
180        drop(taken);
181    }
182
183    #[tokio::test]
184    async fn a_taken_port_between_two_free_ones_is_skipped_not_paired() {
185        let _serial = PORT_TESTS.lock().await;
186        // Held FIRST: choosing the free ports afterwards means the OS cannot
187        // hand `taken_port` back as one of them.
188        let taken = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
189        let taken_port = taken.local_addr().unwrap().port();
190        let (a, c) = two_free_ports();
191        let got = bind_startup_listeners_from([a, taken_port, c]).await;
192        assert!(got.in_range);
193        assert_eq!(got.web.local_addr().unwrap().port(), a);
194        assert_eq!(
195            got.ws.local_addr().unwrap().port(),
196            c,
197            "not adjacent, and that is fine"
198        );
199        drop(taken);
200    }
201
202    #[tokio::test]
203    async fn fewer_than_two_free_falls_back_to_os_chosen_ports() {
204        let _serial = PORT_TESTS.lock().await;
205        let taken = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
206        let taken_port = taken.local_addr().unwrap().port();
207        let only_one_free = free_port();
208        let got = bind_startup_listeners_from([taken_port, only_one_free]).await;
209        assert!(!got.in_range, "one free port is not enough for the pair");
210        let (web, ws) = (got.web.local_addr().unwrap(), got.ws.local_addr().unwrap());
211        assert!(web.ip().is_loopback() && ws.ip().is_loopback());
212        assert_ne!(web.port(), ws.port());
213        assert_ne!(web.port(), taken_port);
214        // The lone free port was released again, not left half-bound.
215        assert!(std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, only_one_free)).is_ok());
216        drop(taken);
217    }
218
219    #[tokio::test]
220    async fn an_empty_candidate_list_falls_back() {
221        let _serial = PORT_TESTS.lock().await;
222        let got = bind_startup_listeners_from(std::iter::empty()).await;
223        assert!(!got.in_range);
224        assert!(got.web.local_addr().unwrap().ip().is_loopback());
225    }
226
227    #[test]
228    fn the_range_is_one_a_rule_can_rely_on() {
229        let (start, end) = (*LAN_PORT_RANGE.start(), *LAN_PORT_RANGE.end());
230        assert!(start >= 1024, "not a well-known port");
231        assert!(
232            end < 32768,
233            "must stay below Linux's default ephemeral start (32768); Windows and macOS start at 49152"
234        );
235        for neighbour in [21027, 22000, 24800, 25565, 27015, 27017, 28015, 30000] {
236            assert!(
237                !LAN_PORT_RANGE.contains(&neighbour),
238                "{neighbour} is a well-known neighbour; a rule on the range would expose it"
239            );
240        }
241        assert!(
242            usize::from(end - start) + 1 >= 100,
243            "room for about fifty instances (two ports each)"
244        );
245    }
246}