Skip to main content

ts_runtime/
lib.rs

1#![doc = include_str!("../README.md")]
2
3extern crate ts_netstack_smoltcp as netstack;
4
5use core::time::Duration;
6use std::sync::Arc;
7
8use kameo::{
9    actor::{ActorRef, Spawn, WeakActorRef},
10    mailbox::Signal,
11};
12use netstack::netcore::Channel;
13use tokio::sync::watch;
14
15use crate::{
16    control_runner::ControlRunner, dataplane::DataplaneActor, direct::DirectManager,
17    forwarder_actor::ForwarderActor, multiderp::Multiderp, netstack_actor::NetstackActor,
18};
19
20/// Pcap stream framer for debug packet capture (`CapturePcap`).
21pub mod capture;
22/// Control runner.
23pub mod control_runner;
24mod dataplane;
25mod derp_latency;
26/// Device connection-state tracking ([`DeviceState`]) and typed registration outcome
27/// ([`RegistrationError`]).
28pub mod device_state;
29mod direct;
30mod env;
31mod error;
32/// Fallback TCP handler registry (`tsnet.Server.RegisterFallbackTCPHandler` parity).
33pub mod fallback_tcp;
34mod forwarder_actor;
35/// Client-side Funnel ingress termination (`tsnet`'s `ListenFunnel` data path).
36pub mod funnel;
37/// Unified IPN notification bus ([`Notify`] / [`watch_ipn_bus`](Runtime::watch_ipn_bus)), mirroring
38/// Go `ipn` `LocalBackend.WatchNotifications` / the `WatchIPNBus` LocalAPI.
39pub mod ipn_bus;
40mod magic_dns;
41pub use magic_dns::DnsQueryResult;
42mod multiderp;
43mod netstack_actor;
44mod packetfilter;
45pub mod peer_tracker;
46mod peerapi;
47mod peerapi_doh;
48mod route_updater;
49/// Stored Serve config + accept-loop runtime (`tsnet`'s `Get/SetServeConfig` + serving runtime).
50pub mod serve;
51mod src_filter;
52/// Netmap status snapshot, WhoIs, and watcher types.
53pub mod status;
54/// Taildrop peer-to-peer file transfer store.
55pub mod taildrop;
56pub mod taildrop_send;
57/// Tailnet-Lock (TKA) chain-sync orchestration: bootstrap + offer/send driver (the runtime layer
58/// that bridges the `ts_control` sync RPCs and the `ts_tka` chain logic).
59mod tka_sync;
60#[cfg(feature = "tun")]
61mod tun_actor;
62
63pub use device_state::{DeviceState, RegistrationError};
64pub(crate) use env::Env;
65pub use error::{Error, ErrorKind};
66pub use ipn_bus::{IpnBusWatcher, Notify, NotifyWatchOpt};
67pub use status::{FileTarget, NetcheckReport, RegionLatency, Status, StatusNode, WhoIs};
68pub use ts_dataplane::{CaptureHook, CapturePath};
69
70use crate::peer_tracker::PeerTracker;
71
72/// The runtime for a tailscale device.
73pub struct Runtime {
74    /// Reference to the control actor.
75    pub control: ActorRef<ControlRunner>,
76    dataplane: ActorRef<DataplaneActor>,
77    /// Reference to the direct (disco/UDP underlay) manager, retained so [`Runtime::rebind`] can
78    /// ask it to re-bind the underlay socket on a network/link change.
79    direct: ActorRef<DirectManager>,
80    /// Reference to the application netstack actor. `None` in TUN transport mode, where there is
81    /// no userspace application netstack (the application data path is a real kernel TUN device).
82    netstack: Option<WeakActorRef<NetstackActor>>,
83    /// Reference to the peer tracker for peer lookups.
84    pub peer_tracker: WeakActorRef<PeerTracker>,
85    /// Fallback TCP handler registry, bound to the application netstack. `None` in TUN transport
86    /// mode (no application netstack exists to attach it to).
87    fallback_tcp: Option<fallback_tcp::FallbackTcpManager>,
88    /// Reference to the MagicDNS responder, retained so [`Runtime::query_dns`] can run a query
89    /// through the live `100.100.100.100` forward path. `None` in TUN transport mode (no
90    /// `MagicDnsActor` is spawned there — TUN-mode MagicDNS is an in-packet intercept, not an actor).
91    magic_dns: Option<ActorRef<magic_dns::MagicDnsActor>>,
92    /// Reference to the forwarder actor, retained so [`Runtime::set_advertise_routes`] can push a
93    /// new accept/dial route table onto the running forwarder (the local half of advertising
94    /// routes). Without this the strong ref would drop after the startup `GetChannel` and the
95    /// forwarder would be reachable only via the message bus.
96    forwarder: ActorRef<ForwarderActor>,
97    /// Reference to the multiderp manager, retained so [`Runtime::status`] can resolve each
98    /// relayed peer's DERP region id to its region **code** (`ipnstate.PeerStatus.Relay`). Without
99    /// this the strong ref would drop after startup (it is cloned into the direct manager + route
100    /// updater) and the region-code map would be unreachable.
101    multiderp: ActorRef<Multiderp>,
102    env: Env,
103    shutdown: watch::Sender<bool>,
104    /// Sender side of the exit-node selector `watch` cell. Held privately here (not on the cloned
105    /// `Env`, which keeps only the read side) so that only `Runtime::set_exit_node` can mutate the
106    /// selection; the route updater and source filter re-read it via [`Env::exit_node`].
107    exit_node_tx: watch::Sender<Option<ts_control::ExitNodeSelector>>,
108    /// Sender side of the accept-routes preference `watch` cell. Held privately here (same rationale
109    /// as [`exit_node_tx`](Self::exit_node_tx)) so that only [`Runtime::set_accept_routes`] can
110    /// toggle it; the route updater and source filter re-read it via [`Env::accept_routes`].
111    accept_routes_tx: watch::Sender<bool>,
112    /// Sender side of the accept-dns preference `watch` cell. Held privately here (same rationale as
113    /// [`accept_routes_tx`](Self::accept_routes_tx)) so that only [`Runtime::set_accept_dns`] can
114    /// toggle it; the MagicDNS responder re-reads it via [`Env::accept_dns`] when it rebuilds its
115    /// view (the republish that `set_accept_dns` triggers).
116    accept_dns_tx: watch::Sender<bool>,
117    /// Receiver mirroring the *active* (resolved + fail-closed) exit node's stable id, fed by the
118    /// route updater. Read by [`Runtime::status`] / [`Runtime::active_exit_node`] to report which
119    /// exit node traffic is actually egressing through (vs. the merely-configured selector).
120    active_exit_rx: watch::Receiver<Option<ts_control::StableNodeId>>,
121    /// Receiver for the device connection-state cell, fed by the control runner. Read by
122    /// [`Runtime::watch_state`] and [`Runtime::wait_until_running`].
123    state_rx: watch::Receiver<DeviceState>,
124    /// Receiver for the retained peer-capability grants, fed by the packet-filter updater. Read by
125    /// [`Runtime::whois`] to resolve the flow-scoped cap map (Go `apitype.WhoIsResponse.CapMap`).
126    cap_grants_rx: watch::Receiver<packetfilter::CapGrants>,
127    /// Live advertised-route preference (explicit subnet routes + the exit-node flag), seeded from
128    /// the startup config. [`Runtime::set_advertise_routes`] and [`set_advertise_exit_node`] each
129    /// mutate their part under this lock then re-send the composed set, so the two compose.
130    advertise: std::sync::Mutex<AdvertiseState>,
131}
132
133impl Runtime {
134    /// Spawn a new runtime with the given parameters for connecting to a tailnet.
135    pub async fn spawn(
136        config: ts_control::Config,
137        auth_key: Option<String>,
138        keys: ts_keys::NodeState,
139    ) -> Result<Self, Error> {
140        let (shutdown_tx, shutdown_rx) = watch::channel(false);
141
142        // The exit-node selector, accept-routes, and accept-dns preferences are live `watch` cells so
143        // `Device::set_exit_node` / `set_accept_routes` / `set_accept_dns` can change them at runtime.
144        // `new_with_runtime_txs` returns each `Sender` (mutation capability) grouped in `pref_cells`
145        // so they are retained privately on the `Runtime`, while only the `Receiver`s (the readers'
146        // contract) live on the cloned `Env`. Initial values come from `ForwarderConfig`.
147        let (env, pref_cells) = Env::new_with_runtime_txs(
148            keys,
149            shutdown_rx,
150            env::ForwarderConfig::from_control_config(&config),
151        );
152
153        // Both userspace netstacks (application + forwarder) share one netstack config. Honor the
154        // per-deployment TCP buffer knob when set, otherwise fall back to the netstack default.
155        let netstack_config = netstack_config_from(config.tcp_buffer_size);
156
157        let dataplane = DataplaneActor::spawn(env.clone());
158
159        let (netstack_id, netstack_up, netstack_down) =
160            dataplane.ask(dataplane::NewOverlayTransport).await?;
161
162        // A second overlay transport feeds the dedicated any-IP forwarder netstack. Inbound packets
163        // for advertised subnet routes / the exit-node default route are routed here (see
164        // `route_updater`), keeping forwarded flows off the application netstack.
165        let (forwarder_id, forwarder_up, forwarder_down) =
166            dataplane.ask(dataplane::NewOverlayTransport).await?;
167
168        let multiderp = Multiderp::spawn((env.clone(), dataplane.clone()));
169
170        // Spawn the direct (disco) underlay manager before the route updater. Its `on_start`
171        // binds the UDP socket and registers its transport synchronously, so by the time the
172        // route updater asks it for the direct transport id it is guaranteed to be available.
173        let direct = DirectManager::spawn((env.clone(), dataplane.clone(), multiderp.clone()));
174
175        // Spawn the forwarder before the route updater. Its `on_start` builds the forwarder
176        // netstack, enables any-IP acceptance, and starts the per-port accept loops synchronously,
177        // so by the time the route updater begins delivering advertised prefixes to
178        // `forwarder_id` the netstack is already draining its transport.
179        let forwarder = ForwarderActor::spawn((
180            env.clone(),
181            netstack_config.clone(),
182            forwarder_up,
183            forwarder_down,
184        ));
185        // Force `on_start` to finish (any-IP enabled, accept loops live) before the route updater
186        // can route the first inbound flow to `forwarder_id`: an `ask` blocks until the actor has
187        // started.
188        //
189        // The forwarder netstack's overlay `Channel` is reused by the TUN application path for
190        // recursive / exit-node-DoH MagicDNS forwarding (TUN mode has no application netstack of its
191        // own, but the forwarder netstack runs in both modes and egresses over the overlay — the
192        // anti-leak property `forward_query`/`forward_doh` require). Only the `tun` Tun arm consumes
193        // it, so it is unused when the `tun` feature is off — allow that without warn-as-error.
194        #[cfg_attr(not(feature = "tun"), allow(unused_variables))]
195        let (forwarder_channel,) = forwarder.ask(forwarder_actor::GetChannel).await?;
196
197        // The route updater is the single authoritative resolver of the active (resolved,
198        // fail-closed) exit node; it publishes the resolved stable id into this watch cell so
199        // `Runtime::status` can report which exit is actually engaged (not just configured).
200        let (active_exit_tx, active_exit_rx) = watch::channel(None);
201        route_updater::RouteUpdater::spawn((
202            multiderp.clone(),
203            direct.clone(),
204            env.clone(),
205            netstack_id,
206            forwarder_id,
207            active_exit_tx,
208        ));
209        // The packet-filter updater also surfaces the retained cap-grants (for flow-scoped WhoIs)
210        // through a `watch` cell whose receiver the `Runtime` holds — the bus has no replay, so a
211        // `watch` is how `Runtime::whois` reads the current grants on demand.
212        let (cap_grants_tx, cap_grants_rx) = watch::channel(Default::default());
213        packetfilter::PacketfilterUpdater::spawn((env.clone(), cap_grants_tx));
214        src_filter::SourceFilterUpdater::spawn(env.clone());
215        let peer_tracker = PeerTracker::spawn(env.clone()).downgrade();
216
217        // Select the application data path from the transport mode. The forwarder/egress path
218        // above is UNCHANGED in both modes — TUN mode only swaps the application data path, never
219        // the forwarder. `config` is moved into `ControlRunner::spawn` below, so branch on a
220        // borrow and clone the small `TunConfig` where needed before the move.
221        //
222        // - Netstack (the default, and the only reachable arm when the `tun` feature is off):
223        //   spawn the application netstack + MagicDNS responder + fallback-TCP registry, all on
224        //   the `netstack_up`/`netstack_down` overlay seam.
225        // - Tun: spawn `TunActor` on that same overlay seam instead; no application netstack and
226        //   no MagicDNS responder exist, and `netstack`/`fallback_tcp` are `None`.
227        // - Tun requested but built without the `tun` feature: hard-error (a config/build
228        //   mismatch knowable at spawn time). NEVER silently fall back to netstack.
229        let (netstack, fallback_tcp, magic_dns) = match &config.transport_mode {
230            ts_control::TransportMode::Netstack => {
231                let netstack = NetstackActor::spawn((
232                    env.clone(),
233                    netstack_config,
234                    netstack_up,
235                    netstack_down,
236                ));
237
238                // Fetch the netstack channel while we still hold the strong ActorRef, then spawn
239                // the MagicDNS responder on it. Its ActorRef is retained on `Runtime` so
240                // `query_dns` can drive the live forward path; the serve loop itself is owned by the
241                // actor's internal JoinSet.
242                let (channel,) = netstack.ask(netstack_actor::GetChannel).await?;
243                // The fallback-TCP registry attaches to the application netstack — the same one
244                // that carries the embedder's explicit `Device::tcp_listen` sockets — so a
245                // fallback handler sees exactly the inbound flows no explicit listener matched.
246                let fallback_tcp = fallback_tcp::FallbackTcpManager::new(channel.clone());
247                let magic_dns = magic_dns::MagicDnsActor::spawn((env.clone(), channel));
248
249                (
250                    Some(netstack.downgrade()),
251                    Some(fallback_tcp),
252                    Some(magic_dns),
253                )
254            }
255
256            #[cfg(feature = "tun")]
257            ts_control::TransportMode::Tun(tun_cfg) => {
258                // Reuse the same `netstack_up`/`netstack_down` overlay-transport pair that would
259                // have fed the netstack — it is just the application-side overlay seam (the name
260                // is historical). No NetstackActor / MagicDnsActor is spawned.
261                tun_actor::TunActor::spawn((
262                    env.clone(),
263                    tun_cfg.clone(),
264                    netstack_up,
265                    netstack_down,
266                    // Reuse the forwarder netstack's overlay `Channel` for recursive / exit-node-DoH
267                    // MagicDNS forwarding in the TUN datapath (TUN mode has no application netstack
268                    // Channel of its own). Egresses over the overlay — anti-leak preserved.
269                    //
270                    // Host-route gating (subnet routes gated on `--accept-routes`, the host `/0` from
271                    // the selected exit peer) is no longer snapshotted here: `TunActor` reads the live
272                    // `Env` cells (`accept_routes`/`exit_node`) on every host-FIB apply — both the
273                    // device-build path and the `PeerState` re-apply path — and folds the union of
274                    // peers' AllowedIPs (see `tun_actor::host_routes_from_node`). A runtime
275                    // `set_accept_routes` / `set_exit_node` toggle re-broadcasts the peer state, so the
276                    // host routing table is re-steered live (no device rebuild needed).
277                    forwarder_channel.clone(),
278                ));
279
280                (None, None, None)
281            }
282
283            #[cfg(not(feature = "tun"))]
284            ts_control::TransportMode::Tun(_) => {
285                return Err(Error {
286                    kind: ErrorKind::TunUnavailable,
287                    target_actor: None,
288                    message_ty: None,
289                });
290            }
291        };
292
293        // Device connection-state cell. Created here (not inside the actor) so the control runner's
294        // `on_start` can publish `Failed`/`NeedsLogin` and still return `Err` without the sender
295        // being tied to a `Self` that never gets constructed on a hard registration failure.
296        let (state_tx, state_rx) = watch::channel(DeviceState::Connecting);
297
298        // Seed the live advertised-route preference from the startup config before `config` moves
299        // into the control runner, so the runtime setters compose against the configured baseline.
300        let advertise = std::sync::Mutex::new(AdvertiseState {
301            routes: config.advertise_routes.clone(),
302            exit_node: config.advertise_exit_node,
303        });
304
305        let control = ControlRunner::spawn(control_runner::Params {
306            config,
307            auth_key,
308            env: env.clone(),
309            state_tx,
310        });
311
312        Ok(Self {
313            control,
314            dataplane,
315            direct,
316            peer_tracker,
317            fallback_tcp,
318            magic_dns,
319            forwarder,
320            multiderp,
321            netstack,
322            env,
323            shutdown: shutdown_tx,
324            exit_node_tx: pref_cells.exit_node,
325            accept_routes_tx: pref_cells.accept_routes,
326            accept_dns_tx: pref_cells.accept_dns,
327            active_exit_rx,
328            state_rx,
329            cap_grants_rx,
330            advertise,
331        })
332    }
333
334    /// Register a fallback TCP handler consulted for every inbound TCP flow that matches no
335    /// explicit listener (`tsnet.Server.RegisterFallbackTCPHandler` parity).
336    ///
337    /// The returned [`fallback_tcp::FallbackTcpHandle`] deregisters the handler when dropped. See
338    /// [`fallback_tcp`] for the dispatch contract and anti-leak guarantees.
339    ///
340    /// Returns [`ErrorKind::UnsupportedInTunMode`] in TUN transport mode, where there is no
341    /// application netstack to attach a fallback handler to.
342    pub fn register_fallback_tcp_handler(
343        &self,
344        cb: Arc<
345            dyn Fn(core::net::SocketAddr, core::net::SocketAddr) -> fallback_tcp::FallbackDecision
346                + Send
347                + Sync,
348        >,
349    ) -> Result<fallback_tcp::FallbackTcpHandle, Error> {
350        Ok(self
351            .fallback_tcp
352            .as_ref()
353            .ok_or(Error {
354                kind: ErrorKind::UnsupportedInTunMode,
355                target_actor: None,
356                message_ty: None,
357            })?
358            .register(cb))
359    }
360
361    /// Get a channel to send commands to the netstack.
362    ///
363    /// Returns [`ErrorKind::UnsupportedInTunMode`] in TUN transport mode, where there is no
364    /// application netstack.
365    pub async fn channel(&self) -> Result<Channel, Error> {
366        let (channel,) = self
367            .netstack
368            .as_ref()
369            .ok_or(Error {
370                kind: ErrorKind::UnsupportedInTunMode,
371                target_actor: None,
372                message_ty: None,
373            })?
374            .upgrade()
375            .ok_or(Error {
376                kind: ErrorKind::ActorGone,
377                target_actor: None,
378                message_ty: None,
379            })?
380            .ask(netstack_actor::GetChannel)
381            .await?;
382
383        Ok(channel)
384    }
385
386    /// Resolve `name` for `qtype` through the live MagicDNS responder (the `100.100.100.100`
387    /// forward path), returning the raw DNS response, its RCODE, and the upstream resolver(s)
388    /// consulted (analogue of Go `LocalClient.QueryDNS`).
389    ///
390    /// This drives the *real* responder — the same `decide`/forward logic an on-the-wire query
391    /// hits — so the answer and its anti-leak posture (a tailnet-suffix name never egresses; a
392    /// recursive forward delegates to the active exit node's DoH; only IPv4 upstreams are dialed)
393    /// match exactly what a tailnet client observes. `qtype` is the raw RFC 1035 TYPE (`1`=A,
394    /// `28`=AAAA, `12`=PTR, or any other).
395    ///
396    /// Returns [`ErrorKind::UnsupportedInTunMode`] in TUN transport mode, where MagicDNS is an
397    /// in-packet intercept on the host's own resolver rather than an actor that can be queried, and
398    /// [`ErrorKind::ActorGone`] if the responder has shut down.
399    pub async fn query_dns(
400        &self,
401        name: &str,
402        qtype: u16,
403    ) -> Result<magic_dns::DnsQueryResult, Error> {
404        let result = self
405            .magic_dns
406            .as_ref()
407            .ok_or(Error {
408                kind: ErrorKind::UnsupportedInTunMode,
409                target_actor: None,
410                message_ty: None,
411            })?
412            .ask(magic_dns::Query {
413                name: name.to_owned(),
414                qtype,
415            })
416            .await?;
417
418        Ok(result)
419    }
420
421    /// The Taildrop file store, if Taildrop is enabled (`taildrop_dir` configured and the store
422    /// initialized). `None` when disabled — fail-closed. Shared with the peerAPI Taildrop server so
423    /// the embedder's read APIs and the receive path see the same on-disk store.
424    pub fn taildrop_store(&self) -> Option<Arc<crate::taildrop::TaildropStore>> {
425        self.env.taildrop_store.clone()
426    }
427
428    /// The shared Funnel ingress slot the peerAPI `/v0/ingress` route reads per connection.
429    ///
430    /// `Device::listen_funnel` installs a [`FunnelManager`](crate::funnel::FunnelManager)'s sink here
431    /// to make the route live (the peerAPI server is already running from startup). Returns a clone of
432    /// the runtime-lifetime `Arc` so the device can write the slot without restarting the server. See
433    /// [`crate::funnel`] for the ingress data path.
434    pub fn funnel_ingress_slot(&self) -> crate::funnel::FunnelIngressSlot {
435        self.env.funnel_ingress.clone()
436    }
437
438    /// The shared "Funnel ingress listener active" flag (the same `Arc` the control session reads to
439    /// set `HostInfo.IngressEnabled`). `Device::listen_funnel` flips it `true` while a funnel listener
440    /// is up so control routes Funnel traffic to this node; clearing it advertises no live endpoint.
441    pub fn ingress_active_flag(&self) -> std::sync::Arc<std::sync::atomic::AtomicBool> {
442        self.env.ingress_active.clone()
443    }
444
445    /// Install (`Some`) or clear (`None`) the debug packet-capture hook on the running dataplane.
446    /// `Some(hook)` tees every plaintext packet crossing the datapath to `hook` until it is cleared;
447    /// `None` stops capture. Mirrors Go `tstun.Wrapper.InstallCaptureHook` / `ClearCaptureSink`.
448    pub async fn install_capture(
449        &self,
450        hook: Option<ts_dataplane::CaptureHook>,
451    ) -> Result<(), Error> {
452        self.dataplane
453            .ask(dataplane::InstallCapture { hook })
454            .await
455            .map_err(Into::into)
456    }
457
458    /// Re-bind the underlay UDP socket after a network/link change (Wi-Fi switch, sleep/wake). The
459    /// embedder's own link monitor calls this (the engine owns the socket re-bind; the embedder owns
460    /// OS netmon). Re-binds the socket (same-port-preferred, IPv4-only invariant preserved) and
461    /// resets the now-stale local NAT mapping — clearing learned reflexive addresses and every
462    /// confirmed direct path while keeping candidate endpoints, so peers re-probe over the new socket
463    /// and relay over DERP (never a direct host dial) until a path re-confirms. Peers, control, the
464    /// netmap, disco state, and DERP are untouched. A no-op when the underlay is inert (bind failed
465    /// at startup, DERP-only). Mirrors Go magicsock `Conn.Rebind` + `resetEndpointStates`.
466    pub async fn rebind(&self) -> Result<(), Error> {
467        self.direct.ask(direct::Rebind).await.map_err(Error::from)
468    }
469
470    /// A snapshot of the local netmap: this node plus every known peer.
471    ///
472    /// Combines the self node held by the control runner with the peer set held by the peer
473    /// tracker. Mirrors tsnet's `LocalClient::Status`.
474    ///
475    /// `self_node` is `None` until the first netmap update has been received from control. Peer
476    /// entries carry no online/user/capability data (see the [`status`] module docs for that gap).
477    pub async fn status(&self) -> Result<Status, Error> {
478        let self_node_domain = self.control.ask(control_runner::SelfNode).await?;
479        // The MagicDNS suffix is the self node's FQDN minus its host label — already split into
480        // `Node.tailnet` at decode time (Go derives it the same way in `NetworkMap.MagicDNSSuffix`).
481        // Capture it before the domain `Node` is mapped away into a `StatusNode`.
482        let magic_dns_suffix = self_node_domain.as_ref().and_then(|n| n.tailnet.clone());
483        let self_node = self_node_domain.as_ref().map(StatusNode::from_node);
484
485        let peers_with_ids = self
486            .peer_tracker
487            .upgrade()
488            .ok_or(Error {
489                kind: ErrorKind::ActorGone,
490                target_actor: None,
491                message_ty: None,
492            })?
493            .ask(peer_tracker::GetStatus)
494            .await?;
495
496        // Join per-peer connectivity (Go `PeerStatus.CurAddr`): one batched query to the direct
497        // manager for every peer's current trusted direct endpoint, then fill `cur_addr` on each
498        // `StatusNode`. A peer absent from the map is relayed via DERP (`cur_addr = None`). This is a
499        // live snapshot — the direct path can expire/re-confirm between calls (matches Go's snapshot
500        // semantics). The `watch_netmap` stream intentionally carries no connectivity (it is a netmap
501        // watch, not a path-state watch, and does not re-fire on direct↔relay flips).
502        let ids: Vec<ts_transport::PeerId> = peers_with_ids.iter().map(|(id, _)| *id).collect();
503        let best_addrs = self
504            .direct
505            .ask(direct::BestAddrs { ids: ids.clone() })
506            .await
507            .unwrap_or_default();
508
509        // For the peers with NO direct path (relayed via DERP), resolve the region CODE they relay
510        // through (Go `PeerStatus.Relay`). One batched ask to multiderp; `cur_addr` and `relay` are
511        // mutually exclusive for a routed peer, mirroring Go's empty-vs-set strings.
512        let relay_ids: Vec<ts_transport::PeerId> = ids
513            .into_iter()
514            .filter(|id| !best_addrs.contains_key(id))
515            .collect();
516        let relay_codes = if relay_ids.is_empty() {
517            Default::default()
518        } else {
519            self.multiderp
520                .ask(multiderp::RelayCodesForPeers { ids: relay_ids })
521                .await
522                .unwrap_or_default()
523        };
524
525        let peers = peers_with_ids
526            .into_iter()
527            .map(|(id, mut node)| match best_addrs.get(&id).copied() {
528                Some(addr) => {
529                    node.cur_addr = Some(addr);
530                    node
531                }
532                None => {
533                    node.relay = relay_codes.get(&id).cloned();
534                    node
535                }
536            })
537            .collect();
538
539        Ok(Status {
540            self_node,
541            peers,
542            active_exit_node: self.active_exit_node(),
543            magic_dns_suffix,
544        })
545    }
546
547    /// List the tailnet peers this node can Taildrop a file *to* (Go LocalAPI `FileTargets`).
548    ///
549    /// Mirrors the upstream send-path filter (`feature/taildrop` `Extension::FileTargets`): a peer
550    /// qualifies when it advertises a reachable peerAPI **and** is either owned by the same user as
551    /// this node **or** explicitly granted the file-sharing-target capability. The whole list is
552    /// gated on this node holding the file-sharing capability (control sets it when the admin enables
553    /// Taildrop) — absent that, an empty list (fail-closed, not an error, matching how the receive
554    /// store returns empty when disabled). Results are sorted by the peer's MagicDNS name.
555    ///
556    /// Targets are listed regardless of current online state (upstream's `FileTargets` does not gate
557    /// on online either; an offline target's send will simply time out). The self node is never
558    /// included. Returns empty before the first netmap.
559    ///
560    /// Divergence from Go: the upstream filter also excludes `tvOS` peers, which this fork cannot
561    /// reproduce (the domain node carries no OS string); the impact is negligible — the actual send
562    /// fail-closes if such a peer refused the transfer.
563    pub async fn file_targets(&self) -> Result<Vec<FileTarget>, Error> {
564        // Node-level gate: this node must hold the file-sharing capability (Taildrop enabled by the
565        // admin). Read it off the self node's cap map, like Go's `hasCapFileSharing()`.
566        let self_node = self.control.ask(control_runner::SelfNode).await?;
567        let Some(self_node) = self_node else {
568            return Ok(Vec::new()); // no netmap yet
569        };
570        if !self_node.can_share_files() {
571            return Ok(Vec::new()); // Taildrop not enabled for the tailnet — fail-closed
572        }
573        let self_user_id = self_node.user_id;
574
575        let peers = self
576            .peer_tracker
577            .upgrade()
578            .ok_or(Error {
579                kind: ErrorKind::ActorGone,
580                target_actor: None,
581                message_ty: None,
582            })?
583            .ask(peer_tracker::AllPeers)
584            .await?;
585
586        // Eligibility + ordering live in `build_file_targets` (pure, unit-tested in `status`).
587        Ok(status::build_file_targets(peers, self_user_id))
588    }
589
590    /// The stable id of the exit node traffic is currently egressing through, or `None` if none is
591    /// engaged. This is the route updater's resolved + fail-closed answer (see
592    /// [`Status::active_exit_node`](crate::status::Status::active_exit_node)): it differs from the
593    /// configured [`exit_node`](Self::exit_node) selector, which may name a peer that is absent or
594    /// no longer advertising a default route (in which case egress is dropped and this returns
595    /// `None`).
596    pub fn active_exit_node(&self) -> Option<ts_control::StableNodeId> {
597        self.active_exit_rx.borrow().clone()
598    }
599
600    /// Request an OIDC ID token from control scoped to `audience` (workload-identity federation).
601    ///
602    /// Returns the signed JWT, or the token RPC's own [`ts_control::IdTokenError`]. The kameo
603    /// delegated-reply send error is flattened: a handler error carries the real `IdTokenError`,
604    /// any other send failure (actor shutdown / mailbox closed) is surfaced as
605    /// [`ts_control::IdTokenError::NetworkError`].
606    pub async fn fetch_id_token(
607        &self,
608        audience: String,
609    ) -> Result<String, ts_control::IdTokenError> {
610        self.control
611            .ask(control_runner::FetchIdToken { audience })
612            .await
613            .map_err(flatten_send_err)
614    }
615
616    /// Log this node out of the tailnet: deregister it by expiring its current node key.
617    ///
618    /// Forwards to the control runner, which re-POSTs `/machine/register` with a past expiry over a
619    /// fresh Noise channel. This is a control-plane state change only — it does NOT shut the runtime
620    /// down (the caller follows with [`graceful_shutdown`](Self::graceful_shutdown)) and does not
621    /// touch the on-disk node key. The kameo delegated-reply send error is flattened the same way as
622    /// `fetch_id_token`: a handler error carries the real
623    /// [`ts_control::LogoutError`]; any other send failure (actor shutdown / mailbox closed) is
624    /// surfaced as [`ts_control::LogoutError::NetworkError`].
625    pub async fn logout(&self) -> Result<(), ts_control::LogoutError> {
626        self.control
627            .ask(control_runner::Logout)
628            .await
629            .map_err(flatten_logout_send_err)
630    }
631
632    /// Publish a `TXT` DNS record for this node via control's `/machine/set-dns` (Go
633    /// `LocalClient.SetDNS`).
634    ///
635    /// Forwards to the control runner, which POSTs the record over a fresh Noise channel. The kameo
636    /// delegated-reply send error is flattened the same way as `fetch_id_token`:
637    /// a handler error carries the real [`ts_control::SetDnsError`]; any other send failure (actor
638    /// shutdown / mailbox closed) is surfaced as [`ts_control::SetDnsError::NetworkError`].
639    pub async fn set_dns(
640        &self,
641        name: String,
642        value: String,
643    ) -> Result<(), ts_control::SetDnsError> {
644        self.control
645            .ask(control_runner::SetDns { name, value })
646            .await
647            .map_err(flatten_set_dns_send_err)
648    }
649
650    /// Sign `node_key` with this node's network-lock key and submit the signature to control
651    /// (Go `tka.sign` Direct case → `/machine/tka/sign`).
652    ///
653    /// Submits only — the local [`Authority`](ts_tka::Authority) is **not** mutated here; it advances
654    /// via the existing verified-sync path. A handler error carries the real [`ts_control::TkaSyncError`];
655    /// any other send failure (actor shutdown / mailbox closed) is surfaced as
656    /// [`ts_control::TkaSyncError::NetworkError`].
657    pub async fn tka_sign(&self, node_key: [u8; 32]) -> Result<(), ts_control::TkaSyncError> {
658        self.control
659            .ask(control_runner::TkaSign { node_key })
660            .await
661            .map_err(flatten_tka_send_err)
662    }
663
664    /// Disable Tailnet Lock by presenting the `disablement_secret` to control (Go `tka.disable` →
665    /// `/machine/tka/disable`), targeting the current authority head.
666    ///
667    /// Submits only — the local [`Authority`](ts_tka::Authority) is **not** mutated here. A handler
668    /// error carries the real [`ts_control::TkaSyncError`] (incl. [`Unsupported`] when there is no
669    /// known TKA head to disable); any other send failure collapses to
670    /// [`NetworkError`](ts_control::TkaSyncError::NetworkError).
671    pub async fn tka_disable(
672        &self,
673        disablement_secret: Vec<u8>,
674    ) -> Result<(), ts_control::TkaSyncError> {
675        self.control
676            .ask(control_runner::TkaDisable { disablement_secret })
677            .await
678            .map_err(flatten_tka_send_err)
679    }
680
681    /// Initialize Tailnet Lock with this node as the sole initial trusted key, gated by
682    /// `disablement_secret` (Go `tka` init → `/machine/tka/init/{begin,finish}`).
683    ///
684    /// Submits only — does not seed the local [`Authority`](ts_tka::Authority); the node picks up the
685    /// new lock via the existing verified netmap-sync. A handler error carries the real
686    /// [`ts_control::TkaSyncError`] ([`Unsupported`] if control needs other nodes re-signed — the
687    /// single-node "lock yourself in" subset only); any other send failure collapses to
688    /// [`NetworkError`](ts_control::TkaSyncError::NetworkError).
689    pub async fn tka_init(
690        &self,
691        disablement_secret: Vec<u8>,
692    ) -> Result<(), ts_control::TkaSyncError> {
693        self.control
694            .ask(control_runner::TkaInit { disablement_secret })
695            .await
696            .map_err(flatten_tka_send_err)
697    }
698
699    /// Issue a real Let's Encrypt certificate for this node's MagicDNS `name` (`acme` feature).
700    ///
701    /// Mirrors `fetch_id_token`: forwards to the control runner, which runs
702    /// the client-side ACME DNS-01 flow on a spawned task and publishes the challenge TXT via the
703    /// node's set-dns RPC. The kameo delegated-reply send error is flattened — a handler error
704    /// carries the real [`ts_control::CertError`]; any other send failure (actor shutdown / mailbox
705    /// closed) is surfaced as a [`ts_control::CertError::Io`]. SaaS-only: a self-hosted control
706    /// plane 501s on set-dns.
707    #[cfg(feature = "acme")]
708    pub async fn get_certificate(
709        &self,
710        name: String,
711    ) -> Result<ts_control::tls::CertifiedKey, ts_control::CertError> {
712        self.control
713            .ask(control_runner::GetCertificate { name })
714            .await
715            .map_err(flatten_cert_send_err)
716    }
717
718    /// Issue a real Let's Encrypt certificate for this node's MagicDNS `name` and return the
719    /// **PEM pair** `(cert_chain_pem, key_pem)` — the analog of Go's
720    /// `LocalClient.CertPairWithValidity`, for writing the daemon's on-disk `.crt` + `.key`
721    /// (`tnet cert`). `acme` feature.
722    ///
723    /// Same issuance as [`get_certificate`](Self::get_certificate) (one client-side ACME DNS-01
724    /// order, challenge published via the node's set-dns RPC) — only the result shape differs: this
725    /// returns the leaf+chain PEM and the leaf-key PEM instead of the opaque
726    /// [`CertifiedKey`](ts_control::tls::CertifiedKey). The second element is the **leaf private
727    /// key** PEM; it is never logged anywhere on this path.
728    ///
729    /// **`min_validity` (honest "always fresh").** Go's `CertPairWithValidity` reuses a cached cert
730    /// when it has at least `min_validity` of its lifetime left, and re-issues otherwise. This fork
731    /// has **no cert cache** — every call performs a fresh issuance — so `min_validity` is accepted
732    /// for signature compatibility but does not change behavior: a freshly issued cert (full
733    /// lifetime) trivially satisfies any `min_validity`. A reuse cache is separate future work; this
734    /// does NOT fake one.
735    ///
736    /// Mirrors [`get_certificate`](Self::get_certificate)'s error handling: the kameo
737    /// delegated-reply send error is flattened — a handler error carries the real
738    /// [`ts_control::CertError`]; any other send failure (actor shutdown / mailbox closed) collapses
739    /// to a [`ts_control::CertError::Io`]. SaaS-only: a self-hosted control plane 501s on set-dns.
740    #[cfg(feature = "acme")]
741    pub async fn cert_pair(
742        &self,
743        name: String,
744        min_validity: Option<Duration>,
745    ) -> Result<(String, String), ts_control::CertError> {
746        // No cert cache exists in this fork (every issuance is fresh), so `min_validity` is honored
747        // trivially by always issuing a full-lifetime cert. Bound (unused beyond this contract) so
748        // the parameter is explicitly accounted for rather than silently ignored.
749        let _ = min_validity;
750        self.control
751            .ask(control_runner::GetCertPair { name })
752            .await
753            .map_err(flatten_cert_send_err)
754    }
755
756    /// Resolve which node owns a tailnet source address.
757    ///
758    /// Maps the destination IP of `addr` to its owning node. Mirrors tsnet's `LocalClient::WhoIs`.
759    /// Returns `None` if no peer holds that tailnet IP.
760    ///
761    /// The returned [`WhoIs`] additionally carries the **flow-scoped** peer-capability grants
762    /// ([`WhoIs::cap_map`], Go `apitype.WhoIsResponse.CapMap`): the caps control's packet-filter
763    /// application rules authorize for traffic from THIS node (the flow source) to `addr` (the
764    /// destination). Empty when no grant matches. (The node-level cap map rides
765    /// [`WhoIs::capabilities`].)
766    pub async fn whois(&self, addr: core::net::SocketAddr) -> Result<Option<WhoIs>, Error> {
767        let whois = self
768            .peer_tracker
769            .upgrade()
770            .ok_or(Error {
771                kind: ErrorKind::ActorGone,
772                target_actor: None,
773                message_ty: None,
774            })?
775            .ask(peer_tracker::Whois { addr })
776            .await?;
777
778        let Some(mut whois) = whois else {
779            return Ok(None);
780        };
781
782        // Fill the flow-scoped cap map: src = this node's own tailnet IP (of the dst's family),
783        // dst = the queried address. A grant applies when its source matches the flow source — `src`
784        // ∈ its src prefixes OR this node holds one of its source node-caps — AND `dst` ∈ its dst
785        // prefixes (Go `Filter.CapsWithValues`). Resolve our own IP + cap map from the self node; if
786        // it isn't known yet, leave the map empty (no grants resolvable without a source).
787        let dst = addr.ip();
788        if let Some(self_node) = self.control.ask(control_runner::SelfNode).await? {
789            let src: core::net::IpAddr = if dst.is_ipv6() {
790                self_node.tailnet_address.ipv6.addr().into()
791            } else {
792                self_node.tailnet_address.ipv4.addr().into()
793            };
794            let grants = self.cap_grants_rx.borrow();
795            whois.cap_map = ts_packetfilter_state::caps_for(&grants, src, dst, |cap| {
796                self_node.has_node_attr(cap)
797            });
798        }
799
800        Ok(Some(whois))
801    }
802
803    /// The current direct-path status to the peer holding tailnet IP `dst`: its confirmed direct UDP
804    /// endpoint and that path's last-measured RTT, or `None` when there is no direct path right now
805    /// (the peer is relayed via DERP, is unknown, or has no disco key).
806    ///
807    /// The latency is the RTT of the most recent disco ping/pong that confirmed the path — a live
808    /// snapshot up to one probe interval stale, NOT a fresh on-demand round-trip (that is a separate,
809    /// heavier capability). Mirrors the direct-path latency Go surfaces for `ipnstate.PeerStatus`.
810    pub async fn direct_path(
811        &self,
812        dst: core::net::IpAddr,
813    ) -> Result<Option<(core::net::SocketAddr, Duration)>, Error> {
814        let peer_tracker = self.peer_tracker.upgrade().ok_or(Error {
815            kind: ErrorKind::ActorGone,
816            target_actor: None,
817            message_ty: None,
818        })?;
819
820        // Resolve the tailnet IP to its node, then to its disco key. No node / no disco key ⇒ no
821        // direct path is possible (a peer with no disco key can only be reached via DERP).
822        let Some(node) = peer_tracker
823            .ask(peer_tracker::PeerByTailnetIp { ip: dst })
824            .await?
825        else {
826            return Ok(None);
827        };
828        let Some(disco) = node.disco_key else {
829            return Ok(None);
830        };
831
832        self.direct
833            .ask(direct::DirectPathLatency { disco })
834            .await
835            .map_err(Into::into)
836    }
837
838    /// Send a disco ping to the peer holding tailnet IP `dst` **now** and await the pong, returning
839    /// the fresh round-trip latency and the endpoint that answered, or `None` if no pong arrives
840    /// within `timeout` (or the peer is unknown / has no disco key / no candidate path). This is the
841    /// true on-demand `PingType::Disco` (Go `tailscale ping`), as opposed to
842    /// [`direct_path`](Self::direct_path) which reports the last periodic probe's RTT.
843    ///
844    /// The ping round-trip is awaited OFF the direct manager's mailbox (we take a `MagicSock` handle
845    /// and await on it directly), so a slow/timing-out ping never blocks the actor.
846    pub async fn ping_disco(
847        &self,
848        dst: core::net::IpAddr,
849        timeout: Duration,
850    ) -> Result<Option<(core::net::SocketAddr, Duration)>, Error> {
851        let peer_tracker = self.peer_tracker.upgrade().ok_or(Error {
852            kind: ErrorKind::ActorGone,
853            target_actor: None,
854            message_ty: None,
855        })?;
856
857        let Some(node) = peer_tracker
858            .ask(peer_tracker::PeerByTailnetIp { ip: dst })
859            .await?
860        else {
861            return Ok(None);
862        };
863        let Some(disco) = node.disco_key else {
864            return Ok(None);
865        };
866
867        // Cheap synchronous handle fetch, then await the ping OFF the actor mailbox.
868        let Some(sock) = self.direct.ask(direct::SockHandle).await? else {
869            return Ok(None);
870        };
871        // A `ping_now` error is an underlay UDP send failure (not an actor problem); surface it as a
872        // reply-level error. A timed-out / unanswered ping is `Ok(None)`, not an error.
873        sock.ping_now(&disco, timeout).await.map_err(|_| Error {
874            kind: ErrorKind::ReplyErr,
875            target_actor: None,
876            message_ty: None,
877        })
878    }
879
880    /// Change the selected exit node at runtime (the equivalent of Go `tsnet`'s
881    /// `LocalClient.EditPrefs(ExitNodeID/ExitNodeIP)`), without recreating the device.
882    ///
883    /// Updates the live exit-node selector, then asks the peer tracker to re-broadcast the current
884    /// peer set so the route updater and source filter re-resolve the new selector immediately.
885    /// `None` clears the exit node (internet-bound traffic is then dropped, fail-closed, unless this
886    /// node egresses directly). The selection is re-resolved against the live peer set, so passing a
887    /// selector for a peer not yet in the netmap simply takes effect once that peer appears.
888    pub async fn set_exit_node(
889        &self,
890        selector: Option<ts_control::ExitNodeSelector>,
891    ) -> Result<(), Error> {
892        // Update the live cell every reader borrows from. `send_replace` keeps the value current
893        // even with no active receivers (none can have dropped while the runtime is up, but it is
894        // the right non-failing primitive here).
895        self.exit_node_tx.send_replace(selector);
896
897        // Trigger an immediate re-resolution: the route updater (outbound routes + DoH delegation)
898        // and the source filter (inbound validation) both recompute on an `Arc<PeerState>`, so a
899        // re-broadcast applies the new exit without waiting for the next netmap update.
900        self.peer_tracker
901            .upgrade()
902            .ok_or(Error {
903                kind: ErrorKind::ActorGone,
904                target_actor: None,
905                message_ty: None,
906            })?
907            .ask(peer_tracker::RepublishState)
908            .await
909            .map_err(Into::into)
910    }
911
912    /// The currently-selected exit node, or `None` if none is selected.
913    pub fn exit_node(&self) -> Option<ts_control::ExitNodeSelector> {
914        self.env.exit_node()
915    }
916
917    /// Toggle whether this node accepts peer-advertised subnet routes at runtime (the equivalent of
918    /// Go `tsnet`'s `LocalClient.EditPrefs(RouteAll)` / `tailscale set --accept-routes`), without
919    /// recreating the device.
920    ///
921    /// `accept-routes` is a purely **local** preference — unlike advertised routes it is never
922    /// reported to control (no `Hostinfo` / MapRequest side), so this only re-runs the local
923    /// route/source-filter recompute, mirroring [`set_exit_node`](Self::set_exit_node) rather than
924    /// [`set_advertise_routes`](Self::set_advertise_routes). Updates the live cell, then asks the peer
925    /// tracker to re-broadcast the current peer set so the route updater (outbound routes) and the
926    /// source filter (inbound validation) re-filter against the new value immediately: turning it on
927    /// installs newly-accepted subnet routes (and widens the source filter to match); turning it off
928    /// removes them from BOTH in lock-step (never accepting a source for a route no longer installed).
929    /// Self routes and the exit-node default `/0` are unaffected (the latter is gated by the exit-node
930    /// selection, not this flag).
931    ///
932    /// In TUN transport mode the host routing table is also re-steered live: the `RepublishState`
933    /// kicked below re-broadcasts the peer set to the `TunActor`, whose `PeerState` handler re-reads
934    /// `accept_routes` (and the exit selection) from `Env` and re-applies the host routes — so the
935    /// toggle takes effect without rebuilding the device (the apply is an idempotent add-new/
936    /// remove-gone diff). The exit-node default `/0` is still keyed on the exit selection, not this flag.
937    pub async fn set_accept_routes(&self, accept: bool) -> Result<(), Error> {
938        // Update the live cell every reader borrows from (same primitive/rationale as set_exit_node).
939        self.accept_routes_tx.send_replace(accept);
940
941        // Trigger an immediate re-filter: the route updater and source filter both recompute on an
942        // `Arc<PeerState>`, so a re-broadcast applies the new preference without waiting for the next
943        // netmap update. Both re-read the same live cell, so the outbound route set and the inbound
944        // source filter stay coupled (the anti-leak invariant).
945        self.peer_tracker
946            .upgrade()
947            .ok_or(Error {
948                kind: ErrorKind::ActorGone,
949                target_actor: None,
950                message_ty: None,
951            })?
952            .ask(peer_tracker::RepublishState)
953            .await
954            .map_err(Into::into)
955    }
956
957    /// Whether this node currently accepts peer-advertised subnet routes (`--accept-routes`).
958    pub fn accept_routes(&self) -> bool {
959        self.env.accept_routes()
960    }
961
962    /// Toggle whether this node accepts the tailnet's DNS configuration at runtime (the equivalent of
963    /// Go `tsnet`'s `LocalClient.EditPrefs(CorpDNS)` / `tailscale set --accept-dns`), without
964    /// recreating the device.
965    ///
966    /// Like [`set_accept_routes`](Self::set_accept_routes), `accept-dns` is a purely **local**
967    /// preference — it is never reported to control (no `Hostinfo` / MapRequest side), so this only
968    /// re-runs the local MagicDNS view rebuild. Updates the live cell, then asks the peer tracker to
969    /// re-broadcast the current peer set; the resulting `PeerState` rebuild re-applies the gate on the
970    /// MagicDNS responder (and the peerAPI DoH server that shares its view). When `false`, the
971    /// responder ignores the control-pushed DNS config and answers every query `REFUSED`, mirroring Go
972    /// applying an empty `dns.Config` when `CorpDNS` is off; flipping it back to `true` restores
973    /// serving from the still-current config (the real config is never destroyed — only gated at the
974    /// read site), so the OFF→ON restore is automatic.
975    pub async fn set_accept_dns(&self, accept: bool) -> Result<(), Error> {
976        // Update the live cell every reader borrows from (same primitive/rationale as set_accept_routes).
977        self.accept_dns_tx.send_replace(accept);
978
979        // Trigger an immediate view rebuild: the MagicDNS responder re-reads `Env::accept_dns()` when
980        // it handles a `PeerState`, so a re-broadcast re-applies the gate on both the netstack
981        // responder and the peerAPI DoH server (which share the view) without waiting for the next
982        // control/peer update. Mirrors `set_accept_routes`'s republish.
983        self.peer_tracker
984            .upgrade()
985            .ok_or(Error {
986                kind: ErrorKind::ActorGone,
987                target_actor: None,
988                message_ty: None,
989            })?
990            .ask(peer_tracker::RepublishState)
991            .await
992            .map_err(Into::into)
993    }
994
995    /// Whether this node currently accepts the tailnet's DNS configuration (`--accept-dns` / `CorpDNS`).
996    pub fn accept_dns(&self) -> bool {
997        self.env.accept_dns()
998    }
999
1000    /// Change the set of subnet routes this node advertises at runtime (Go `tailscale set
1001    /// --advertise-routes`). Applies BOTH halves together so the wire and the data path agree:
1002    ///
1003    /// 1. **Wire** — re-advertise `Hostinfo.RoutableIPs` to control on the live map-poll connection
1004    ///    (so control grants the node the subnet-router role for exactly these prefixes).
1005    /// 2. **Local** — swap the forwarder's accept/dial route table (so the node actually forwards the
1006    ///    prefixes it advertises). New flows see the new set; in-flight flows keep their routing.
1007    ///
1008    /// `routes` is filtered to the IPv4-only, deduplicated set this fork can honor (IPv6 prefixes are
1009    /// dropped under the IPv6-off posture — we never advertise a route we won't forward), so the wire
1010    /// and forwarder are fed the identical final set. This sets the explicit subnet prefixes only; it
1011    /// does NOT touch the exit-node `0.0.0.0/0` advertisement (a separate concern).
1012    pub async fn set_advertise_routes(&self, routes: Vec<ipnet::IpNet>) -> Result<(), Error> {
1013        // Update the explicit-subnet part of the live preference, keep the exit-node flag, and
1014        // re-send the composed set. Composes with `set_advertise_exit_node` (neither clobbers the
1015        // other's contribution to `Hostinfo.RoutableIPs`).
1016        let composed = {
1017            let mut adv = self.advertise.lock().unwrap_or_else(|p| p.into_inner());
1018            adv.routes = routes;
1019            compose_advertised_routes(adv.routes.clone(), adv.exit_node)
1020        };
1021        self.apply_advertised_routes(composed).await
1022    }
1023
1024    /// Advertise (or stop advertising) this node as an **exit node** — the `0.0.0.0/0` default route
1025    /// (Go `tailscale set --advertise-exit-node`). Composes with
1026    /// [`set_advertise_routes`](Self::set_advertise_routes): toggling the exit node re-sends the
1027    /// explicit subnet routes plus (when `enable`) `0.0.0.0/0`, so the two preferences are
1028    /// independent. Like `set_advertise_routes`, this both re-advertises `Hostinfo.RoutableIPs` to
1029    /// control AND updates the forwarder's accept/dial set, applied together. Control still gates
1030    /// whether the advertised exit node is actually *usable* by peers (this only advertises it).
1031    pub async fn set_advertise_exit_node(&self, enable: bool) -> Result<(), Error> {
1032        let composed = {
1033            let mut adv = self.advertise.lock().unwrap_or_else(|p| p.into_inner());
1034            adv.exit_node = enable;
1035            compose_advertised_routes(adv.routes.clone(), adv.exit_node)
1036        };
1037        self.apply_advertised_routes(composed).await
1038    }
1039
1040    /// Push a freshly-composed advertised-route set to BOTH halves: the forwarder's accept/dial
1041    /// table (local) FIRST — so the node forwards a prefix before control grants it, never the
1042    /// reverse — then re-advertise `Hostinfo.RoutableIPs` to control on the live map-poll connection
1043    /// (wire). `composed` is already filtered + exit-node-folded by [`compose_advertised_routes`].
1044    async fn apply_advertised_routes(&self, composed: Vec<ipnet::IpNet>) -> Result<(), Error> {
1045        self.forwarder
1046            .ask(forwarder_actor::UpdateRoutes {
1047                routes: composed.clone(),
1048            })
1049            .await?;
1050        self.control
1051            .ask(control_runner::SetAdvertiseRoutes { routes: composed })
1052            .await
1053            .map_err(Into::into)
1054    }
1055
1056    /// Change this node's hostname at runtime (Go `tailscale set --hostname`), re-reporting
1057    /// `Hostinfo.Hostname` to control on the live map-poll connection. Hostname is display-only
1058    /// (control reflects it in the netmap), so there is no dataplane half. The new value is also
1059    /// what a subsequent re-registration reports, so it persists across a reconnect.
1060    pub async fn set_hostname(&self, hostname: String) -> Result<(), Error> {
1061        self.control
1062            .ask(control_runner::SetHostname { hostname })
1063            .await
1064            .map_err(Into::into)
1065    }
1066
1067    /// Subscribe to netmap peer-change events: the **narrow** peer-set view.
1068    ///
1069    /// Returns a [`watch::Receiver`] whose value is the current set of peer [`StatusNode`]s,
1070    /// updated on every netmap state update from control. Await
1071    /// [`watch::Receiver::changed`](tokio::sync::watch::Receiver::changed) to react to peers
1072    /// joining, leaving, or changing. For the unified Go-`WatchIPNBus` feed that merges this with
1073    /// device-state and the interactive-login URL, see [`watch_ipn_bus`](Self::watch_ipn_bus); this
1074    /// method is the peer-only projection of the same underlying cell.
1075    pub async fn watch_netmap(&self) -> Result<watch::Receiver<Vec<StatusNode>>, Error> {
1076        self.peer_tracker
1077            .upgrade()
1078            .ok_or(Error {
1079                kind: ErrorKind::ActorGone,
1080                target_actor: None,
1081                message_ty: None,
1082            })?
1083            .ask(peer_tracker::WatchNetmap)
1084            .await
1085            .map_err(Into::into)
1086    }
1087
1088    /// The current device connection-[`DeviceState`].
1089    pub fn device_state(&self) -> DeviceState {
1090        self.state_rx.borrow().clone()
1091    }
1092
1093    /// Watch the device connection-[`DeviceState`] (`Connecting` → `Running` / `NeedsLogin` /
1094    /// `Expired` / `Failed`).
1095    ///
1096    /// Returns a [`watch::Receiver`]; await
1097    /// [`changed`](tokio::sync::watch::Receiver::changed) to react push-style to control connection
1098    /// transitions instead of polling [`status`](Self::status). The initial value is the current
1099    /// state. Note: a transient per-reconnect dip back to `Connecting` is **not** currently
1100    /// emitted (control transparently reconnects below this layer); the state reflects registration
1101    /// outcome and node-key expiry.
1102    pub fn watch_state(&self) -> watch::Receiver<DeviceState> {
1103        self.state_rx.clone()
1104    }
1105
1106    /// Wait until the device finishes registering, returning a typed outcome.
1107    ///
1108    /// Resolves `Ok(())` once the device reaches [`DeviceState::Running`]. Returns a typed
1109    /// [`RegistrationError`] otherwise — the actionable distinction between "retry", "re-pair", and
1110    /// "drive interactive login" that replaces polling the device's `ipv4_addr` in a loop:
1111    /// - `AuthRejected` — bad/expired/unknown auth key. **Permanent** (re-pair).
1112    /// - `NeedsLogin(url)` — interactive authorization required (no usable auth key). **Not
1113    ///   permanent**: the runtime keeps retrying and will reach `Running` once the user authorizes
1114    ///   the URL. An **auth-key** caller should treat this as a failure; an **interactive** caller
1115    ///   should ignore this return and instead drive the flow via [`watch_state`](Self::watch_state)
1116    ///   (this method returns the URL eagerly rather than blocking for the whole login).
1117    /// - `NetworkUnreachable` — control unreachable. **Transient** (retry).
1118    /// - `Timeout` — no settled state within `timeout`.
1119    ///
1120    /// `KeyExpired` is not produced by this initial wait (a node key expires only *after* it has
1121    /// come up); observe post-registration expiry via [`watch_state`](Self::watch_state).
1122    /// `timeout` of `None` waits indefinitely for a settled state.
1123    pub async fn wait_until_running(
1124        &self,
1125        timeout: Option<Duration>,
1126    ) -> Result<(), RegistrationError> {
1127        device_state::wait_for_running(self.state_rx.clone(), timeout).await
1128    }
1129
1130    /// Subscribe to the unified IPN notification bus (Go `ipn` `WatchIPNBus` /
1131    /// `LocalBackend.WatchNotifications`).
1132    ///
1133    /// Returns an [`IpnBusWatcher`]; await [`next`](IpnBusWatcher::next) to receive [`Notify`]
1134    /// events that coalesce device-[`DeviceState`] changes (including the interactive-login URL as
1135    /// `browse_to_url`) and netmap peer-set changes into one feed. `mask`
1136    /// ([`NotifyWatchOpt`]) selects which current-state fields are front-loaded as an initial
1137    /// snapshot on subscribe (`INITIAL_STATE` / `INITIAL_NETMAP`), exactly like Go's
1138    /// `NotifyInitialState` / `NotifyInitialNetMap`.
1139    ///
1140    /// This composes the same `watch` cells as [`watch_state`](Self::watch_state),
1141    /// [`watch_netmap`](Self::watch_netmap), and `pop_browser_url` — one source of truth, so the
1142    /// merged feed cannot diverge from those narrow views. Besides the registration-time login URL
1143    /// (carried by `NeedsLogin`), `browse_to_url` also streams the mid-session
1144    /// `MapResponse.PopBrowserURL` (re-auth / consent on an already-running node). Delivery is
1145    /// best-effort/lossy (a bounded per-watcher buffer; a notification is dropped rather than
1146    /// blocking the runtime if a slow consumer's buffer fills), matching Go's bus. The stream ends
1147    /// (`next` returns `None`) on runtime shutdown or when the watcher is dropped.
1148    pub async fn watch_ipn_bus(&self, mask: NotifyWatchOpt) -> Result<IpnBusWatcher, Error> {
1149        // The peer-set cell lives on the peer-tracker actor; obtain a receiver the same way
1150        // `watch_netmap` does. State + shutdown cells are held here.
1151        let peer_rx = self
1152            .peer_tracker
1153            .upgrade()
1154            .ok_or(Error {
1155                kind: ErrorKind::ActorGone,
1156                target_actor: None,
1157                message_ty: None,
1158            })?
1159            .ask(peer_tracker::WatchNetmap)
1160            .await?;
1161        // The running-node consent-URL cell lives on the control runner; obtain its receiver the
1162        // same way (the control actor ref is strong, so no upgrade needed).
1163        let browser_rx = self.control.ask(control_runner::WatchBrowserUrl).await?;
1164        Ok(ipn_bus::spawn_watcher(
1165            mask,
1166            self.state_rx.clone(),
1167            peer_rx,
1168            browser_rx,
1169            self.shutdown.subscribe(),
1170        ))
1171    }
1172
1173    /// Attempt to shut down the runtime gracefully.
1174    ///
1175    /// Returns false if the shutdown timed out. It is still shut down if it timed out, just
1176    /// more violently and with possible resource leaks.
1177    pub async fn graceful_shutdown(self, timeout: Option<Duration>) -> bool {
1178        self.shutdown.send_replace(true);
1179
1180        async fn _shutdown_all(runtime: Runtime) {
1181            // See the note in `Drop` for why we only need to stop these actors to bring down the
1182            // whole runtime.
1183
1184            let _ignore = runtime.control.stop_gracefully().await;
1185            let _ignore = runtime.dataplane.stop_gracefully().await;
1186            let _ignore = runtime.env.bus.stop_gracefully().await;
1187
1188            tokio::join![
1189                runtime.control.wait_for_shutdown(),
1190                runtime.dataplane.wait_for_shutdown(),
1191                runtime.env.bus.wait_for_shutdown(),
1192            ];
1193        }
1194
1195        let fut = _shutdown_all(self);
1196
1197        match timeout {
1198            Some(timeout) => tokio::time::timeout(timeout, fut).await.is_ok(),
1199            None => {
1200                fut.await;
1201                true
1202            }
1203        }
1204    }
1205}
1206
1207impl Drop for Runtime {
1208    fn drop(&mut self) {
1209        // We must have already run `graceful_shutdown`: on the happy path, this does nothing, but
1210        // if it timed out, we need to make sure the actors are dead so we don't leak them and their
1211        // dependents.
1212        if *self.shutdown.borrow() {
1213            self.control.kill();
1214            self.dataplane.kill();
1215            self.env.bus.kill();
1216            return;
1217        }
1218
1219        self.shutdown.send_replace(true);
1220
1221        // Actors shut down when the last ActorRef to them is dropped (as nothing can send them
1222        // messages anymore). If we don't hold an ActorRef in Runtime, in general the only thing
1223        // that has one is the MessageBus, which each actor subscribes to for a subset of messages.
1224        // Hence, if we shut down the bus, most actors die as well.
1225
1226        // First shut down the actors we have an ActorRef to:
1227        try_shutdown(&self.control);
1228        try_shutdown(&self.dataplane);
1229
1230        // Then shutdown the message bus, stopping the rest of the actors:
1231        try_shutdown(&self.env.bus);
1232    }
1233}
1234
1235fn try_shutdown(a: &ActorRef<impl kameo::Actor>) {
1236    if let Err(e) = a.mailbox_sender().try_send(Signal::Stop) {
1237        tracing::error!(error = %e, "graceful shutdown failed, killing actor");
1238        a.kill();
1239    }
1240}
1241
1242/// Build the netstack config shared by both userspace netstacks (application + forwarder) from the
1243/// per-deployment `tcp_buffer_size` knob.
1244///
1245/// `None` keeps the netstack default (256 KiB/direction); `Some(n)` overrides it (e.g. a smaller
1246/// window on a memory-constrained exit node forwarding many concurrent flows — see
1247/// [`netstack::netcore::Config::tcp_buffer_size`]). Factored out of [`Runtime::spawn`] so the
1248/// None-default / Some-override mapping is unit-testable without standing up the actor system.
1249fn netstack_config_from(tcp_buffer_size: Option<usize>) -> netstack::netcore::Config {
1250    let mut c = netstack::netcore::Config::default();
1251    if let Some(tcp_buffer_size) = tcp_buffer_size {
1252        c.tcp_buffer_size = tcp_buffer_size;
1253    }
1254    c
1255}
1256
1257/// Filter a requested advertise-route set to the IPv4-only, deduplicated set this fork can honor,
1258/// mirroring [`ts_control::Config::advertised_routes`] so a runtime `set_advertise_routes` feeds the
1259/// wire (control grant) and the forwarder (accept/dial table) the identical final set. IPv6 prefixes
1260/// are dropped under the IPv6-off posture — we never advertise a route we won't forward. Order is
1261/// preserved (first occurrence wins). Factored out so the filter is unit-testable without an actor.
1262fn filter_advertise_routes(routes: Vec<ipnet::IpNet>) -> Vec<ipnet::IpNet> {
1263    let mut filtered: Vec<ipnet::IpNet> = Vec::new();
1264    for net in routes {
1265        if matches!(net, ipnet::IpNet::V4(_)) {
1266            if !filtered.contains(&net) {
1267                filtered.push(net);
1268            }
1269        } else {
1270            tracing::warn!(prefix = %net, "dropping IPv6 advertise route (IPv6-off posture)");
1271        }
1272    }
1273    filtered
1274}
1275
1276/// Compose the final advertised-route set from the explicit subnet `routes` and the exit-node flag,
1277/// mirroring [`ts_control::Config::advertised_routes`]: the IPv4-only, deduplicated subnet prefixes,
1278/// plus `0.0.0.0/0` appended when `exit_node` is set. This is the single source of truth both
1279/// runtime advertise mutators (`set_advertise_routes`, `set_advertise_exit_node`) feed, so the two
1280/// compose instead of clobbering. Factored out so the composition is unit-testable without an actor.
1281fn compose_advertised_routes(routes: Vec<ipnet::IpNet>, exit_node: bool) -> Vec<ipnet::IpNet> {
1282    let mut filtered = filter_advertise_routes(routes);
1283    if exit_node {
1284        let default_v4 = ipnet::IpNet::V4(
1285            ipnet::Ipv4Net::new(core::net::Ipv4Addr::UNSPECIFIED, 0)
1286                .expect("0.0.0.0/0 is a valid prefix"),
1287        );
1288        if !filtered.contains(&default_v4) {
1289            filtered.push(default_v4);
1290        }
1291    }
1292    filtered
1293}
1294
1295/// The runtime's live advertised-route preference: the explicit subnet routes plus whether this node
1296/// advertises itself as an exit node. Held behind a `Mutex` on the [`Runtime`] so
1297/// [`Runtime::set_advertise_routes`] and [`Runtime::set_advertise_exit_node`] each mutate their own
1298/// part and re-send the composed set — they compose rather than clobber (Go `EditPrefs` keeps
1299/// `AdvertiseRoutes` and the exit-node advertisement as independent prefs that both feed
1300/// `Hostinfo.RoutableIPs`).
1301#[derive(Debug, Default, Clone)]
1302struct AdvertiseState {
1303    /// The explicit subnet prefixes (pre-filter; the last value passed to `set_advertise_routes`).
1304    routes: Vec<ipnet::IpNet>,
1305    /// Whether this node advertises the exit-node default route (`0.0.0.0/0`).
1306    exit_node: bool,
1307}
1308
1309/// Flatten a kameo delegated-reply [`SendError`] for the id-token RPC into the RPC's own
1310/// [`ts_control::IdTokenError`].
1311///
1312/// A [`SendError::HandlerError`](kameo::error::SendError::HandlerError) carries the real
1313/// `IdTokenError` produced by the handler and is surfaced verbatim. Any other send failure (actor
1314/// not running / stopped, mailbox full, send timeout) is a delivery problem rather than an RPC
1315/// result, so it collapses to a transient [`ts_control::IdTokenError::NetworkError`]. Factored out
1316/// of [`Runtime::fetch_id_token`] so this mapping is unit-testable without standing up an actor.
1317fn flatten_send_err<M>(
1318    e: kameo::error::SendError<M, ts_control::IdTokenError>,
1319) -> ts_control::IdTokenError {
1320    match e {
1321        kameo::error::SendError::HandlerError(err) => err,
1322        _ => ts_control::IdTokenError::NetworkError,
1323    }
1324}
1325
1326/// Flatten a kameo `SendError` from the `Logout` ask into a [`ts_control::LogoutError`].
1327///
1328/// A `HandlerError` carries the real `LogoutError` from the control RPC and is surfaced verbatim;
1329/// any other send failure (actor not running / stopped, mailbox full, send timeout) — a delivery
1330/// problem, not a logout result — collapses to the transient [`ts_control::LogoutError::NetworkError`]
1331/// (logout is idempotent, so a retry after a delivery failure is safe). Factored out of
1332/// [`Runtime::logout`] so the mapping is unit-testable without standing up an actor.
1333fn flatten_logout_send_err<M>(
1334    e: kameo::error::SendError<M, ts_control::LogoutError>,
1335) -> ts_control::LogoutError {
1336    match e {
1337        kameo::error::SendError::HandlerError(err) => err,
1338        _ => ts_control::LogoutError::NetworkError,
1339    }
1340}
1341
1342/// Flatten a kameo `SendError` from the `SetDns` ask into a [`ts_control::SetDnsError`].
1343///
1344/// A `HandlerError` carries the real `SetDnsError` from the set-dns RPC and is surfaced verbatim;
1345/// any other send failure (actor not running / stopped, mailbox full, send timeout) — a delivery
1346/// problem, not a publish result — collapses to the transient
1347/// [`ts_control::SetDnsError::NetworkError`]. Factored out of [`Runtime::set_dns`] so the mapping is
1348/// unit-testable without standing up an actor.
1349fn flatten_set_dns_send_err<M>(
1350    e: kameo::error::SendError<M, ts_control::SetDnsError>,
1351) -> ts_control::SetDnsError {
1352    match e {
1353        kameo::error::SendError::HandlerError(err) => err,
1354        _ => ts_control::SetDnsError::NetworkError,
1355    }
1356}
1357
1358/// Flatten a kameo `SendError` from a TKA mutation ask (`TkaSign`/`TkaDisable`) into a
1359/// [`ts_control::TkaSyncError`]. A `HandlerError` carries the real RPC error; any other send failure
1360/// (actor shutdown / mailbox closed) is surfaced as the transient
1361/// [`ts_control::TkaSyncError::NetworkError`]. Generic over the message type so both share it.
1362fn flatten_tka_send_err<M>(
1363    e: kameo::error::SendError<M, ts_control::TkaSyncError>,
1364) -> ts_control::TkaSyncError {
1365    match e {
1366        kameo::error::SendError::HandlerError(err) => err,
1367        _ => ts_control::TkaSyncError::NetworkError,
1368    }
1369}
1370
1371/// Flatten a kameo `SendError` from the `GetCertificate` / `GetCertPair` ask into a
1372/// [`ts_control::CertError`].
1373///
1374/// A `HandlerError` carries the real `CertError` produced by the ACME issuance and is surfaced
1375/// verbatim. `CertError` has no transient-network variant, so any other send failure (actor not
1376/// running / stopped, mailbox full, send timeout) — a delivery problem rather than an issuance
1377/// result — collapses to a [`ts_control::CertError::Io`]. Generic over the message type, so it
1378/// serves both [`Runtime::get_certificate`] and [`Runtime::cert_pair`]; factored out so the mapping
1379/// is unit-testable without standing up an actor.
1380#[cfg(feature = "acme")]
1381fn flatten_cert_send_err<M>(
1382    e: kameo::error::SendError<M, ts_control::CertError>,
1383) -> ts_control::CertError {
1384    match e {
1385        kameo::error::SendError::HandlerError(err) => err,
1386        _ => ts_control::CertError::Io(std::io::Error::other(
1387            "control runner unavailable for certificate issuance",
1388        )),
1389    }
1390}
1391
1392#[cfg(test)]
1393mod tests {
1394    use super::*;
1395
1396    /// `None` must leave the netstack's own default TCP window in place (the 256 KiB throughput
1397    /// default), and must not silently coerce to some other value.
1398    #[test]
1399    fn netstack_config_none_uses_netstack_default() {
1400        let default = netstack::netcore::Config::default();
1401        let built = netstack_config_from(None);
1402        assert_eq!(
1403            built.tcp_buffer_size, default.tcp_buffer_size,
1404            "None must inherit the netstack default TCP buffer size"
1405        );
1406    }
1407
1408    /// `Some(n)` must override the TCP window (the memory-vs-throughput knob exit-node operators
1409    /// reach for), reaching the config that both netstacks are built from.
1410    #[test]
1411    fn netstack_config_some_overrides_buffer() {
1412        let built = netstack_config_from(Some(64 * 1024));
1413        assert_eq!(
1414            built.tcp_buffer_size,
1415            64 * 1024,
1416            "Some(n) must override the TCP buffer size that both netstacks use"
1417        );
1418    }
1419
1420    /// `set_advertise_routes` must feed the wire and the forwarder the IDENTICAL filtered set:
1421    /// IPv4-only (IPv6 dropped under the IPv6-off posture), deduplicated, order preserved.
1422    #[test]
1423    fn filter_advertise_routes_keeps_v4_dedups_drops_v6() {
1424        let v4a: ipnet::IpNet = "10.0.0.0/24".parse().unwrap();
1425        let v4b: ipnet::IpNet = "192.168.1.0/24".parse().unwrap();
1426        let v6: ipnet::IpNet = "2001:db8::/32".parse().unwrap();
1427
1428        // Mixed input with a duplicate v4 and a v6 prefix.
1429        let out = filter_advertise_routes(vec![v4a, v6, v4b, v4a]);
1430
1431        assert_eq!(
1432            out,
1433            vec![v4a, v4b],
1434            "v6 dropped, duplicate v4 collapsed, first-occurrence order preserved"
1435        );
1436    }
1437
1438    /// An all-IPv6 request filters to empty (we never advertise a route we won't forward) rather
1439    /// than erroring — clearing the advertised set is a legitimate outcome.
1440    #[test]
1441    fn filter_advertise_routes_all_v6_is_empty() {
1442        let v6: ipnet::IpNet = "2001:db8::/32".parse().unwrap();
1443        assert!(filter_advertise_routes(vec![v6]).is_empty());
1444    }
1445
1446    /// `compose_advertised_routes` folds the exit-node `0.0.0.0/0` onto the filtered subnet routes
1447    /// when (and only when) the exit-node flag is set — so `set_advertise_routes` and
1448    /// `set_advertise_exit_node` compose. The two preferences are independent.
1449    #[test]
1450    fn compose_advertised_routes_folds_exit_node() {
1451        let subnet: ipnet::IpNet = "10.0.0.0/24".parse().unwrap();
1452        let default_v4: ipnet::IpNet = "0.0.0.0/0".parse().unwrap();
1453
1454        // Exit node off: just the (filtered) subnet routes.
1455        assert_eq!(
1456            compose_advertised_routes(vec![subnet], false),
1457            vec![subnet],
1458            "exit-node off ⇒ no default route"
1459        );
1460        // Exit node on: subnet routes PLUS 0.0.0.0/0.
1461        assert_eq!(
1462            compose_advertised_routes(vec![subnet], true),
1463            vec![subnet, default_v4],
1464            "exit-node on ⇒ 0.0.0.0/0 appended"
1465        );
1466        // Exit node on with NO subnet routes: just the default route.
1467        assert_eq!(
1468            compose_advertised_routes(vec![], true),
1469            vec![default_v4],
1470            "exit-node alone advertises only 0.0.0.0/0"
1471        );
1472        // Idempotent: an explicit 0.0.0.0/0 already in the routes isn't duplicated by the fold.
1473        assert_eq!(
1474            compose_advertised_routes(vec![default_v4], true),
1475            vec![default_v4],
1476            "the exit-node fold dedups against an explicit default route"
1477        );
1478    }
1479
1480    /// A `HandlerError` carries the real `IdTokenError` from the RPC handler and must pass through
1481    /// verbatim, not be flattened to a generic network error. Using an `Internal(_)` payload (not
1482    /// `NetworkError`) makes the passthrough observable: a buggy flatten that always returned
1483    /// `NetworkError` would fail this assertion.
1484    #[test]
1485    fn flatten_send_err_handler_error_passes_through() {
1486        // Build an `Internal(_)` payload via the public `From<Utf8Error>` conversion (no extra
1487        // deps): it is distinct from the `_ => NetworkError` fallback, so a buggy flatten that
1488        // always returned `NetworkError` would fail this assertion.
1489        // Route the invalid bytes through a runtime Vec so the `invalid_from_utf8` lint (which only
1490        // fires on compile-time-known literals) doesn't flag this intentional bad input.
1491        let bytes = vec![0xffu8, 0xfe];
1492        let utf8_err = core::str::from_utf8(&bytes).unwrap_err();
1493        let inner = ts_control::IdTokenError::from(utf8_err);
1494        assert!(matches!(inner, ts_control::IdTokenError::Internal(_)));
1495        let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
1496            kameo::error::SendError::HandlerError(inner.clone());
1497        assert_eq!(flatten_send_err(e), inner);
1498    }
1499
1500    /// A non-handler send failure (actor stopped) is a delivery problem, not an RPC result, so it
1501    /// must collapse to a transient `NetworkError`.
1502    #[test]
1503    fn flatten_send_err_actor_stopped_is_network_error() {
1504        let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
1505            kameo::error::SendError::ActorStopped;
1506        assert_eq!(flatten_send_err(e), ts_control::IdTokenError::NetworkError);
1507    }
1508
1509    /// `ActorNotRunning` (the message bounces back undelivered) is likewise a delivery failure and
1510    /// must map to a transient `NetworkError`.
1511    #[test]
1512    fn flatten_send_err_actor_not_running_is_network_error() {
1513        let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
1514            kameo::error::SendError::ActorNotRunning(control_runner::FetchIdToken {
1515                audience: "sts.amazonaws.com".to_string(),
1516            });
1517        assert_eq!(flatten_send_err(e), ts_control::IdTokenError::NetworkError);
1518    }
1519
1520    /// A `HandlerError` from the logout RPC carries the real `LogoutError` and must pass through
1521    /// verbatim. An `Internal(_)` payload (distinct from the `_ => NetworkError` fallback) makes the
1522    /// passthrough observable.
1523    #[test]
1524    fn flatten_logout_send_err_handler_error_passes_through() {
1525        let inner = ts_control::LogoutError::Internal(ts_control::LogoutInternalErrorKind::Http);
1526        assert!(matches!(inner, ts_control::LogoutError::Internal(_)));
1527        let e: kameo::error::SendError<control_runner::Logout, ts_control::LogoutError> =
1528            kameo::error::SendError::HandlerError(inner.clone());
1529        assert_eq!(flatten_logout_send_err(e), inner);
1530    }
1531
1532    /// A non-handler send failure (actor stopped) is a delivery problem, not a logout result, and
1533    /// collapses to a transient `NetworkError` (logout is idempotent, so a retry is safe).
1534    #[test]
1535    fn flatten_logout_send_err_actor_stopped_is_network_error() {
1536        let e: kameo::error::SendError<control_runner::Logout, ts_control::LogoutError> =
1537            kameo::error::SendError::ActorStopped;
1538        assert_eq!(
1539            flatten_logout_send_err(e),
1540            ts_control::LogoutError::NetworkError
1541        );
1542    }
1543
1544    /// A `HandlerError` from the set-dns RPC carries the real `SetDnsError` and must pass through
1545    /// verbatim. An `Internal(_)` payload (distinct from the `_ => NetworkError` fallback) makes the
1546    /// passthrough observable.
1547    #[test]
1548    fn flatten_set_dns_send_err_handler_error_passes_through() {
1549        let inner = ts_control::SetDnsError::Internal(ts_control::SetDnsInternalErrorKind::Http);
1550        assert!(matches!(inner, ts_control::SetDnsError::Internal(_)));
1551        let e: kameo::error::SendError<control_runner::SetDns, ts_control::SetDnsError> =
1552            kameo::error::SendError::HandlerError(inner.clone());
1553        assert_eq!(flatten_set_dns_send_err(e), inner);
1554    }
1555
1556    /// A non-handler send failure (actor stopped) is a delivery problem, not a publish result, and
1557    /// collapses to a transient `NetworkError`.
1558    #[test]
1559    fn flatten_set_dns_send_err_actor_stopped_is_network_error() {
1560        let e: kameo::error::SendError<control_runner::SetDns, ts_control::SetDnsError> =
1561            kameo::error::SendError::ActorStopped;
1562        assert_eq!(
1563            flatten_set_dns_send_err(e),
1564            ts_control::SetDnsError::NetworkError
1565        );
1566    }
1567
1568    /// A `HandlerError` from a TKA mutation RPC carries the real `TkaSyncError` and must pass through
1569    /// verbatim (an `Unsupported` payload makes the passthrough observable, distinct from the
1570    /// `_ => NetworkError` fallback).
1571    #[test]
1572    fn flatten_tka_send_err_handler_error_passes_through() {
1573        let e: kameo::error::SendError<control_runner::TkaSign, ts_control::TkaSyncError> =
1574            kameo::error::SendError::HandlerError(ts_control::TkaSyncError::Unsupported);
1575        assert_eq!(
1576            flatten_tka_send_err(e),
1577            ts_control::TkaSyncError::Unsupported
1578        );
1579    }
1580
1581    /// A non-handler send failure (actor stopped) collapses to a transient `NetworkError`.
1582    #[test]
1583    fn flatten_tka_send_err_actor_stopped_is_network_error() {
1584        let e: kameo::error::SendError<control_runner::TkaSign, ts_control::TkaSyncError> =
1585            kameo::error::SendError::ActorStopped;
1586        assert_eq!(
1587            flatten_tka_send_err(e),
1588            ts_control::TkaSyncError::NetworkError
1589        );
1590    }
1591
1592    /// The same flatten works for the `TkaDisable` message type (the helper is generic over `M`).
1593    #[test]
1594    fn flatten_tka_send_err_works_for_disable() {
1595        let e: kameo::error::SendError<control_runner::TkaDisable, ts_control::TkaSyncError> =
1596            kameo::error::SendError::HandlerError(ts_control::TkaSyncError::Unsupported);
1597        assert_eq!(
1598            flatten_tka_send_err(e),
1599            ts_control::TkaSyncError::Unsupported
1600        );
1601    }
1602}