Skip to main content

tailscale/
lib.rs

1//! A work-in-progress [Tailscale](https://tailscale.com/blog/how-tailscale-works) library.
2//!
3//! `tailscale` allows Rust programs to connect to a tailnet and exchange traffic with peers over
4//! TCP and UDP. It can communicate with other `tailscale`-based peers, `tailscaled` (the Tailscale
5//! Go client), `tsnet`, and `libtailscale` via public DERP servers.
6//!
7//! <div class="warning">
8//! `tailscale` is unstable and insecure.
9//!
10//! We welcome enthusiasm and interest, but please **do not** build production software using these
11//! libraries or rely on it for data privacy until we have a chance to batten down some hatches and
12//! complete a third-party audit.
13//!
14//! See the [Caveats section](#caveats) for more details.
15//! </div>
16//!
17//! For language bindings, see the following crates:
18//!
19//! - C: [ts_ffi](https://docs.rs/ts_ffi)
20//! - Python: [ts_python](https://docs.rs/ts_python)
21//! - Elixir: [ts_elixir](https://docs.rs/ts_elixir)
22//!
23//! For instructions on how to run tests, lints, etc., see [CONTRIBUTING.md]. For the high-level
24//! architecture and repository layout, see [ARCHITECTURE.md].
25//!
26//! ## Code Sample
27//!
28//! A simple UDP client that periodically sends messages to a tailnet peer at `100.64.0.1:5678`:
29//!
30//! ```no_run
31//! # use std::{
32//! #     time::Duration,
33//! #     net::Ipv4Addr,
34//! #     error::Error,
35//! # };
36//! #
37//! # #[tokio::main]
38//! # async fn main() -> Result<(), Box<dyn Error>> {
39//! // Open a new connection to the tailnet
40//! let dev = tailscale::Device::new(
41//!     &tailscale::Config::default_with_key_file("tsrs_keys.json").await?,
42//!     Some("YOUR_AUTH_KEY_HERE".to_owned()),
43//! ).await?;
44//!
45//! // Bind a UDP socket on our tailnet IP, port 1234
46//! let sock = dev.udp_bind((dev.ipv4_addr().await?, 1234).into()).await?;
47//!
48//! // Send a packet containing "hello, world!" to 100.64.0.1:5678 once per second
49//! loop {
50//!     sock.send_to((Ipv4Addr::new(100, 64, 0, 1), 5678).into(), b"hello, world!").await?;
51//!     tokio::time::sleep(Duration::from_secs(1)).await;
52//! }
53//! # }
54//! ```
55//!
56//! Additional examples of using the `tailscale` crate can be found in the [`examples/`] directory.
57//!
58//! ## Using `tailscale`
59//!
60//! To use this crate or the language bindings, you will need to set the `TS_RS_EXPERIMENT` env var
61//! to `this_is_unstable_software`. We'll remove this requirement after a third-party code/cryptography
62//! audit and any necessary fixes.
63//!
64//! Under the hood, we use Tokio for our async runtime. You must also use Tokio, any kind and most
65//! configurations of Tokio runtimes should work, but there must be one available when you call any
66//! async API functions. The easiest way to do this is to use `#[tokio::main]`, see the
67//! [Tokio docs](https://docs.rs/tokio) for more information. In the future, we would like to limit
68//! our reliance on Tokio so that there are alternatives for users of other async runtimes.
69//!
70//! ## Caveats
71//!
72//! This software is still a work-in-progress! We are providing it in the open at this stage out of
73//! a belief in open-source and to see where the community runs with it, but please be aware of a
74//! few important considerations:
75//!
76//! - This implementation contains unaudited cryptography and hasn't undergone a comprehensive
77//!   security analysis. Conservatively, assume there could be a critical security hole meaning
78//!   anything you send or receive could be in the clear on the public Internet.
79//! - There are no compatibility guarantees at the moment. This is early-days software - we may
80//!   break dependent code in order to get things right.
81//! - Direct peer-to-peer connections via NAT traversal are implemented (STUN-discovered endpoints
82//!   and Disco, with `CallMeMaybe` hole-punching over DERP), with DERP relays as the fallback when
83//!   no direct path is available. Hard/symmetric NATs get the same single fixed-local-port candidate
84//!   (`EndpointSTUN4LocalPort`) Go Tailscale uses; behind a NAT with no static port mapping a flow
85//!   may still stay relayed through DERP, which caps its throughput. (Upstream Go does **not** do a
86//!   "256-port birthday-paradox spray" — that is a common misconception; the single-candidate guess
87//!   is the actual behavior, and this fork matches it.)
88//!
89//! ## Feature Flags
90//!
91//! - `axum`: enables the `axum` module, which enables you to run an `axum` HTTP server on top
92//!   of a [`netstack::TcpListener`].
93//!
94//! ## Platform Support
95//!
96//! `tailscale` currently supports the following platforms:
97//!
98//! - Linux (x86_64 and ARM64)
99//! - macOS (ARM64)
100//!
101//! ## Component crates
102//!
103//! The following crates are part of the tailscale-rs project and are dependencies of this one. For
104//! many tasks, just this crate should be sufficient and these other crates are an implementation detail.
105//! There are other crates too, see [ARCHITECTURE.md]
106//! or the [GitHub repo](https://github.com/tailscale/tailscale-rs).
107//!
108//! - [ts_runtime](https://docs.rs/ts_runtime): for each API-level `Device`, the runtime uses an actor
109//!   architecture to manage the lifecycle of the control client, data plane components, netstack, etc.
110//!   A message bus passes updates and communications between these top-level actors.
111//! - [ts_netcheck](https://docs.rs/ts_netcheck): checks network availability and reports latency to
112//!   DERP servers in different regions.
113//! - [ts_netstack_smoltcp](https://docs.rs/ts_netstack_smoltcp): a [smoltcp](https://docs.rs/smoltcp)-based
114//!   network stack that processes Layer 3+ packets to/from the overlay network.
115//! - [ts_control](https://docs.rs/ts_control): control plane client that handles registration,
116//!   authorization/authentication, configuration, and streaming updates.
117//! - [ts_dataplane](https://docs.rs/ts_dataplane): wires all the individual data plane functions together,
118//!   flowing inbound and outbound packets through the components in the correct order.
119//! - [ts_tunnel](https://docs.rs/ts_tunnel): a partial implementation of the WireGuard specification
120//!   that protects all data plane traffic, and is interoperable with other WireGuard clients, including Tailscale clients.
121//! - [ts_cli_util](https://docs.rs/ts_cli_util): helpers for writing command line tools and initializing
122//!   logging, used in examples.
123//! - [ts_disco_protocol](https://docs.rs/ts_disco_protocol): incomplete implementation of Tailscale's
124//!   discovery protocol (disco).
125//!
126//! [ARCHITECTURE.md]: https://github.com/tailscale/tailscale-rs/blob/main/ARCHITECTURE.md
127//! [CONTRIBUTING.md]: https://github.com/tailscale/tailscale-rs/blob/main/CONTRIBUTING.md
128//! [`examples/`]: https://github.com/tailscale/tailscale-rs/blob/main/examples/README.md
129//! [open an issue]: https://github.com/tailscale/tailscale-rs/issues
130//! [`axum` HTTP server]: https://docs.rs/axum/latest/axum/
131
132use std::{
133    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
134    time::Duration,
135};
136
137#[doc(inline)]
138pub use config::Config;
139#[doc(inline)]
140pub use error::{Error, InternalErrorKind};
141// Re-exported so a downstream crate depending only on `tailscale` can name the auth-key secret type
142// for [`Device::new_with_secret`] without taking a separate, version-pinned dependency on `secrecy`
143// (which would risk a `SecretString`-type mismatch if the two `secrecy` majors diverged). Callers
144// pass `tailscale::SecretString`; `secrecy` is a pure-Rust wrapper (no aws-lc/openssl/ring).
145pub use secrecy::SecretString;
146#[doc(inline)]
147pub use ts_control::ExitNodeSelector;
148#[doc(inline)]
149pub use ts_control::Node as NodeInfo;
150#[doc(inline)]
151pub use ts_control::tls::{CertifiedKey, TlsAcceptor, TlsStream};
152#[doc(inline)]
153pub use ts_control::{CertError, MISSING_CERT_RPC, ServeConfig, ServeState, ServeTarget};
154#[doc(inline)]
155pub use ts_control::{ExitProxyConfig, ExitProxyScheme};
156pub use ts_control::{
157    IdTokenError, LogoutError, ServiceError, ServiceMode, SshAccept, SshAction, SshConnIdentity,
158    SshDecision, SshDenyReason, SshPolicy, SshPrincipal, SshRule, StableNodeId,
159};
160// Re-exported so the application data-path transport can be selected through the `tailscale`
161// facade alone: `Config::transport_mode` is `TransportMode` (default `Netstack`; `Tun(TunConfig {
162// name, mtu })` for a real kernel TUN interface). Both are `pub` in `ts_control` but were not
163// reachable through this facade, forcing downstream crates to depend on `ts_control` directly just
164// to name them.
165pub use ts_control::{TransportMode, TunConfig};
166#[doc(inline)]
167pub use ts_netstack_smoltcp::PingError;
168use ts_netstack_smoltcp::{CreateSocket, netcore::Channel};
169#[doc(inline)]
170pub use ts_runtime::fallback_tcp::{
171    FallbackConnFuture, FallbackConnHandler, FallbackDecision, FallbackTcpHandle,
172};
173#[doc(inline)]
174pub use ts_runtime::taildrop::WaitingFile;
175#[doc(inline)]
176pub use ts_runtime::{DeviceState, RegistrationError, Status, StatusNode, WhoIs};
177
178#[cfg(feature = "axum")]
179pub mod axum;
180pub mod config;
181mod error;
182mod loopback;
183#[cfg(feature = "ssh")]
184pub mod ssh;
185
186#[doc(inline)]
187pub use loopback::LoopbackHandle;
188
189/// How a program connects to a tailnet and communicates with peers.
190///
191/// The `Device` connects to the control plane, registers itself with the tailnet, and communicates
192/// with tailnet peers. Its tailnet identity is determined by the key state provided at
193/// construction-time.
194pub struct Device {
195    runtime: ts_runtime::Runtime,
196    /// Command channel to the application netstack. `None` in TUN transport mode, where there is
197    /// no userspace application netstack; the channel-driven socket APIs ([`Device::udp_bind`],
198    /// [`Device::tcp_listen`], [`Device::tcp_connect`], [`Device::ping`]) are unsupported there.
199    channel: Option<Channel>,
200    /// Whether IPv6 is enabled on the tailnet overlay (the `Config::enable_ipv6` gate, default
201    /// `false`). Captured at construction; used by [`Device::listen_service`] to decide whether an
202    /// IPv6 VIP-service address is bindable (the netstack only accepts IPv6 overlay addresses when
203    /// this is set).
204    enable_ipv6: bool,
205    /// The stored Serve config + its live per-port accept loops (`tsnet`'s `Get/SetServeConfig` +
206    /// serving runtime). Built lazily on the first [`Device::set_serve_config`] (it needs this
207    /// node's overlay IPv4, only known after registration). Held here so its accept loops abort when
208    /// the `Device` drops; `None` (empty config) until the first `set`.
209    serve: std::sync::Mutex<Option<ts_runtime::serve::ServeManager>>,
210    /// The live Funnel ingress manager (`tsnet`'s `ListenFunnel` data path), built on
211    /// [`Device::listen_funnel`](crate::Device::listen_funnel). Held here so its TLS-termination pump and the installed peerAPI
212    /// ingress sink stay alive for the device's life (and tear down when a new `listen_funnel`
213    /// replaces it, or the `Device` drops). `None` until the first `listen_funnel`.
214    funnel: std::sync::Mutex<Option<ts_runtime::funnel::FunnelManager>>,
215}
216
217/// Map a [`ts_runtime::taildrop::TaildropError`] to the device-facing [`Error`]. `Error` is a
218/// `Copy` enum with no payload, so the I/O detail string is dropped, but the *kind* is preserved so
219/// a caller can still distinguish the actionable cases: an invalid name →
220/// [`InternalErrorKind::BadRequest`], an in-progress conflict → [`InternalErrorKind::AlreadyExists`],
221/// a missing file → [`InternalErrorKind::NotFound`], and any other filesystem failure →
222/// [`InternalErrorKind::Io`].
223fn taildrop_err(e: ts_runtime::taildrop::TaildropError) -> Error {
224    use ts_runtime::taildrop::TaildropError;
225    match e {
226        TaildropError::InvalidFileName => Error::Internal(InternalErrorKind::BadRequest),
227        TaildropError::FileExists => Error::Internal(InternalErrorKind::AlreadyExists),
228        TaildropError::Io(io) if io.kind() == std::io::ErrorKind::NotFound => {
229            Error::Internal(InternalErrorKind::NotFound)
230        }
231        TaildropError::Io(_) => Error::Internal(InternalErrorKind::Io),
232    }
233}
234
235/// Map a [`ts_runtime::taildrop_send::TaildropSendError`] (the Taildrop *sender*) to the
236/// device-facing [`Error`]. The send-side conflict/forbidden/unexpected-status cases all reduce to
237/// `BadRequest` (the peer refused the transfer for a request-level reason), a dial failure or
238/// timeout to `Timeout`, an invalid name to `BadRequest`, and any stream I/O failure to `Io`.
239fn taildrop_send_err(e: ts_runtime::taildrop_send::TaildropSendError) -> Error {
240    use ts_runtime::taildrop_send::TaildropSendError;
241    match e {
242        TaildropSendError::Connect | TaildropSendError::Timeout => Error::Timeout,
243        TaildropSendError::InvalidName
244        | TaildropSendError::Forbidden
245        | TaildropSendError::Conflict
246        | TaildropSendError::UnexpectedStatus(_) => Error::Internal(InternalErrorKind::BadRequest),
247        TaildropSendError::Io => Error::Internal(InternalErrorKind::Io),
248    }
249}
250
251/// Resolve the effective registration auth key from `auth_key` plus the config's
252/// workload-identity-federation (WIF) / OAuth-client fields.
253///
254/// With the `identity-federation` feature enabled, an OAuth client secret (`tskey-client-…`) or a
255/// `client_id` + (`id_token` | `audience`) is exchanged for a Tailscale auth key against the SaaS
256/// admin API before registration (Go `tsnet.Server`'s `resolveAuthKey`). Without the feature this is
257/// a pure pass-through: `auth_key` is returned unchanged and the WIF config fields are ignored, so
258/// the default build is byte-identical to before.
259#[cfg(feature = "identity-federation")]
260async fn resolve_auth_key(
261    config: &Config,
262    auth_key: Option<String>,
263) -> Result<Option<String>, Error> {
264    let wif = ts_control::WifConfig {
265        auth_key,
266        client_id: config.client_id.clone(),
267        client_secret: config.client_secret.clone(),
268        id_token: config.id_token.clone(),
269        audience: config.audience.clone(),
270        tags: config.requested_tags.clone(),
271    };
272    ts_control::resolve_auth_key(&wif, &config.control_server_url)
273        .await
274        .map_err(|e| {
275            tracing::error!(error = %e, "resolving auth key via workload-identity federation");
276            Error::Internal(InternalErrorKind::BadRequest)
277        })
278}
279
280/// Pass-through when the `identity-federation` feature is disabled: the auth key is used as-is and
281/// the WIF config fields have no effect (matching Go, where the federation path is compiled out
282/// unless its optional feature is linked).
283#[cfg(not(feature = "identity-federation"))]
284async fn resolve_auth_key(
285    _config: &Config,
286    auth_key: Option<String>,
287) -> Result<Option<String>, Error> {
288    Ok(auth_key)
289}
290
291impl Device {
292    /// Create a device from the given [`Config`] and auth key.
293    ///
294    /// Internally, this will spawn multiple asynchronous actors onto a Tokio runtime.
295    ///
296    /// # Example
297    ///
298    /// ```rust,no_run
299    /// # #[tokio::main]
300    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
301    /// # use tailscale::*;
302    /// let dev = Device::new(
303    ///     &Config::default_with_key_file("tsrs_keys.json").await?,
304    ///     Some("MY_AUTH_KEY".to_string()),
305    /// ).await?;
306    /// # Ok(()) }
307    /// ```
308    pub async fn new(config: &Config, auth_key: Option<String>) -> Result<Self, Error> {
309        check_magic_env()?;
310
311        // Resolve the effective registration auth key. The explicit `auth_key` argument wins; if it
312        // is `None`, fall back to `config.auth_key` (Go `tsnet.Server.AuthKey`). When the
313        // `identity-federation` feature is enabled, the resolved key is further passed through the
314        // WIF / OAuth-client bootstrap, which exchanges an OAuth client secret (`tskey-client-…`) or
315        // an IdP-issued OIDC token for a Tailscale auth key before registration (SaaS-only).
316        let auth_key = auth_key.or_else(|| config.auth_key.clone());
317        let auth_key = resolve_auth_key(config, auth_key).await?;
318
319        let rt =
320            ts_runtime::Runtime::spawn(config.into(), auth_key, (&config.key_state).into()).await?;
321        // In TUN transport mode there is no application netstack, so the runtime has no command
322        // channel: that surfaces as `UnsupportedInTunMode`, which we map to a `None` channel rather
323        // than an error (the device is still usable for control-plane and peer-lookup APIs).
324        let channel = match rt.channel().await {
325            Ok(c) => Some(c),
326            Err(e) if e.kind == ts_runtime::ErrorKind::UnsupportedInTunMode => None,
327            Err(e) => return Err(e.into()),
328        };
329
330        Ok(Self {
331            runtime: rt,
332            channel,
333            enable_ipv6: config.enable_ipv6,
334            serve: std::sync::Mutex::new(None),
335            funnel: std::sync::Mutex::new(None),
336        })
337    }
338
339    /// Create a device from the given [`Config`] and a [`SecretString`] auth key.
340    ///
341    /// This is a back-compat-preserving convenience over [`new`](Self::new) for callers that already
342    /// hold the registration auth key as a [`secrecy::SecretString`] (e.g. a daemon that keeps the
343    /// pre-auth key wrapped end-to-end). It lets the caller avoid materializing a plain `String` at
344    /// the engine boundary: the secret is exposed only on the last inch, immediately before being
345    /// handed to [`new`](Self::new).
346    ///
347    /// # Honesty about the plaintext window
348    ///
349    /// This closes the *caller's* boundary, **not** the engine's internal handling. The engine still
350    /// resolves the auth key to a plain `String` internally for registration (the plaintext `String`
351    /// window inside the engine is identical to calling [`new`](Self::new) directly) — this method
352    /// does not make the engine itself secret-clean. If you call [`new`](Self::new) you create that
353    /// `String` yourself; if you call this you do not, but the engine creates one either way.
354    ///
355    /// Passing `None` is equivalent to `new(config, None)` (falls back to `config.auth_key`).
356    ///
357    /// # Example
358    ///
359    /// ```rust,no_run
360    /// # #[tokio::main]
361    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
362    /// # use tailscale::*;
363    /// let dev = Device::new_with_secret(
364    ///     &Config::default_with_key_file("tsrs_keys.json").await?,
365    ///     Some(SecretString::from("MY_AUTH_KEY")),
366    /// ).await?;
367    /// # Ok(()) }
368    /// ```
369    pub async fn new_with_secret(
370        config: &Config,
371        auth_key: Option<SecretString>,
372    ) -> Result<Self, Error> {
373        use secrecy::ExposeSecret as _;
374
375        // Expose the secret on the last inch and delegate to `new`, so the spawn/registration path
376        // is shared verbatim (no duplicated runtime-spawn logic) and the engine-internal plaintext
377        // window is byte-for-byte identical to a direct `new` call.
378        let plain = auth_key.map(|s| s.expose_secret().to_string());
379        Self::new(config, plain).await
380    }
381
382    /// The application netstack command channel, or an error in TUN transport mode (no application
383    /// netstack exists).
384    fn channel(&self) -> Result<&Channel, Error> {
385        self.channel
386            .as_ref()
387            .ok_or(Error::Internal(InternalErrorKind::UnsupportedInTunMode))
388    }
389
390    /// Get this [`Device`]'s IPv4 tailnet address.
391    pub async fn ipv4_addr(&self) -> Result<Ipv4Addr, Error> {
392        self.runtime
393            .control
394            .ask(ts_runtime::control_runner::Ipv4)
395            .await
396            .map_err(ts_runtime::Error::from)?
397            .ok_or(Error::Internal(InternalErrorKind::Actor))
398    }
399
400    /// Get this [`Device`]'s IPv6 tailnet address.
401    pub async fn ipv6_addr(&self) -> Result<Ipv6Addr, Error> {
402        self.runtime
403            .control
404            .ask(ts_runtime::control_runner::Ipv6)
405            .await
406            .map_err(ts_runtime::Error::from)?
407            .ok_or(Error::Internal(InternalErrorKind::Actor))
408    }
409
410    /// Bind a UDP socket to the specified [`SocketAddr`].
411    ///
412    /// Returns an error in TUN transport mode (there is no application netstack to bind on).
413    pub async fn udp_bind(&self, socket_addr: SocketAddr) -> Result<netstack::UdpSocket, Error> {
414        self.channel()?
415            .udp_bind(socket_addr)
416            .await
417            .map_err(Into::into)
418    }
419
420    /// Bind a TCP listener to the specified [`SocketAddr`].
421    ///
422    /// Returns an error in TUN transport mode (there is no application netstack to listen on).
423    pub async fn tcp_listen(
424        &self,
425        socket_addr: SocketAddr,
426    ) -> Result<netstack::TcpListener, Error> {
427        self.channel()?
428            .tcp_listen(socket_addr)
429            .await
430            .map_err(Into::into)
431    }
432
433    /// Register a fallback TCP handler (like `tsnet`'s `RegisterFallbackTCPHandler`).
434    ///
435    /// The callback is consulted for every inbound TCP flow that matches **no** explicit
436    /// [`Device::tcp_listen`] listener, with the flow's `(src, dst)` addresses. It returns
437    /// `(handler, intercept)`:
438    /// - `(_, false)` — decline; the next registered callback is tried.
439    /// - `(Some(h), true)` — claim the flow; `h` is handed the accepted [`netstack::TcpStream`].
440    /// - `(None, true)` — claim and reject the flow (the connection is closed).
441    ///
442    /// Multiple handlers may be registered; they are consulted in registration order and the first
443    /// to intercept wins. The returned [`FallbackTcpHandle`] deregisters the handler when dropped.
444    ///
445    /// Handlers serve flows over the overlay netstack only — never a host socket — and a flow no
446    /// handler claims is closed (fail-closed), never direct-dialed.
447    ///
448    /// Returns an error in TUN transport mode (there is no application netstack to attach to).
449    pub fn register_fallback_tcp_handler<F>(&self, cb: F) -> Result<FallbackTcpHandle, Error>
450    where
451        F: Fn(SocketAddr, SocketAddr) -> FallbackDecision + Send + Sync + 'static,
452    {
453        self.runtime
454            .register_fallback_tcp_handler(std::sync::Arc::new(cb))
455            .map_err(Into::into)
456    }
457
458    /// Resolve a tailnet peer (or this node) by MagicDNS name to its tailnet IPv4 address.
459    ///
460    /// This is an in-process lookup against the netmap we already hold — like `tsnet`'s in-memory
461    /// `dnsMap`, it does not query any DNS server (there is no `100.100.100.100` resolver). The
462    /// `name` may be a bare hostname or a fully-qualified MagicDNS name, with or without a trailing
463    /// dot, in any case (matching is case-insensitive). Returns `Ok(None)` if no tailnet node has
464    /// that name.
465    ///
466    /// Only MagicDNS names are resolved; names outside the tailnet are not looked up here, so the
467    /// caller's system resolver remains responsible for them. IPv6 is intentionally not resolved —
468    /// this fork operates IPv4-only on the tailnet.
469    pub async fn resolve(&self, name: &str) -> Result<Option<Ipv4Addr>, Error> {
470        if let Some(peer) = self.peer_by_name(name).await? {
471            return Ok(Some(peer.tailnet_address.ipv4.addr()));
472        }
473
474        // tsnet's dnsMap also resolves our own name; fall back to self when no peer matches.
475        let me = self.self_node().await?;
476        if me.matches_name(name) {
477            return Ok(Some(me.tailnet_address.ipv4.addr()));
478        }
479
480        Ok(None)
481    }
482
483    /// Connect to a tailnet peer by MagicDNS name and port over TCP.
484    ///
485    /// Resolves `name` via [`Device::resolve`] (an in-process netmap lookup, no DNS server), then
486    /// dials the resulting tailnet IPv4 address. Returns [`InternalErrorKind::BadRequest`] if the
487    /// name does not resolve to a tailnet node.
488    pub async fn connect_by_name(
489        &self,
490        name: &str,
491        port: u16,
492    ) -> Result<netstack::TcpStream, Error> {
493        let addr = self
494            .resolve(name)
495            .await?
496            .ok_or(Error::Internal(InternalErrorKind::BadRequest))?;
497
498        self.tcp_connect((addr, port).into()).await
499    }
500
501    /// Connect to a TCP socket at the remote address.
502    ///
503    /// Returns an error in TUN transport mode (there is no application netstack to dial from).
504    pub async fn tcp_connect(&self, remote: SocketAddr) -> Result<netstack::TcpStream, Error> {
505        let channel = self.channel()?;
506
507        let ip: IpAddr = match remote.is_ipv4() {
508            true => self.ipv4_addr().await?.into(),
509            false => self.ipv6_addr().await?.into(),
510        };
511
512        // TODO(npry): collision checking
513        let ephemeral_port = rand::random_range(49152..=u16::MAX);
514
515        channel
516            .tcp_connect((ip, ephemeral_port).into(), remote)
517            .await
518            .map_err(Into::into)
519    }
520
521    /// Start a SOCKS5 proxy on a host loopback address that dials into the tailnet (Go
522    /// `tsnet.Server.Loopback`, SOCKS5 half).
523    ///
524    /// Binds a TCP listener on `127.0.0.1:0` (host loopback only — never an external interface) and
525    /// serves SOCKS5 (RFC 1928) with required username/password auth (RFC 1929): username `tsnet`,
526    /// password = the returned `proxy_cred`. Each `CONNECT` is dialed INTO the overlay via
527    /// [`Device::connect_by_name`] / [`Device::tcp_connect`] and spliced to the accepted host socket, so
528    /// a non-Rust host process can reach tailnet peers through the proxy. Returns the bound address, the
529    /// proxy credential, and a [`LoopbackHandle`] whose drop stops the listener.
530    ///
531    /// Anti-leak: the listener is loopback-only and every connection egresses over the overlay, never a
532    /// host socket — the host's real origin IP is never used to reach the destination. Unlike Go, the
533    /// LocalAPI HTTP surface is not served (this fork exposes status/whois/id-token natively on
534    /// `Device`); only the SOCKS5 proxy is provided.
535    ///
536    /// Returns an error in TUN transport mode (no application netstack to dial from).
537    pub async fn loopback(&self) -> Result<(std::net::SocketAddr, String, LoopbackHandle), Error> {
538        // Capture only cloneable pieces — never `&self` — for the spawned accept loop: a clone of the
539        // netstack command channel, this device's own overlay IPv4 (fetched once), and a boxed
540        // resolver closure over clones of the control + peer-tracker actor refs. The resolver
541        // replicates `Device::resolve` (peer-by-name, falling back to this node's own name).
542        let channel = self.channel()?.clone();
543        let self_ipv4 = self.ipv4_addr().await?;
544
545        let control = self.runtime.control.clone();
546        let peer_tracker = self.runtime.peer_tracker.clone();
547        let resolve: loopback::Resolver = std::sync::Arc::new(move |name: String| {
548            let control = control.clone();
549            let peer_tracker = peer_tracker.clone();
550            Box::pin(async move {
551                let pt = peer_tracker
552                    .upgrade()
553                    .ok_or(Error::Internal(InternalErrorKind::Actor))?;
554                let peer = pt
555                    .ask(ts_runtime::peer_tracker::PeerByName { name: name.clone() })
556                    .await
557                    .map_err(ts_runtime::Error::from)?;
558                if let Some(peer) = peer {
559                    return Ok(Some(peer.tailnet_address.ipv4.addr()));
560                }
561                // tsnet's dnsMap also resolves our own name; fall back to self.
562                let me = control
563                    .ask(ts_runtime::control_runner::SelfNode)
564                    .await
565                    .map_err(ts_runtime::Error::from)?
566                    .ok_or(Error::Internal(InternalErrorKind::Actor))?;
567                if me.matches_name(&name) {
568                    Ok(Some(me.tailnet_address.ipv4.addr()))
569                } else {
570                    Ok(None)
571                }
572            }) as std::pin::Pin<Box<dyn std::future::Future<Output = _> + Send>>
573        });
574
575        let dialer = loopback::OverlayDialer::new(channel, self_ipv4, resolve);
576        loopback::start(dialer).await
577    }
578
579    /// Get our node info.
580    pub async fn self_node(&self) -> Result<NodeInfo, Error> {
581        self.runtime
582            .control
583            .ask(ts_runtime::control_runner::SelfNode)
584            .await
585            .map_err(ts_runtime::Error::from)?
586            .ok_or(Error::Internal(InternalErrorKind::Actor))
587    }
588
589    /// This node's key-expiry instant as Unix seconds (`Node.KeyExpiry` in Go), or `Ok(None)` if
590    /// the key never expires.
591    ///
592    /// Like Go, this fork is **reactive** about key expiry — it reports it rather than rotating the
593    /// node key in the background. A caller can schedule re-authentication around this time; on
594    /// expiry, re-create the [`Device`] (which re-registers), supplying a fresh node key + the prior
595    /// `old_node_key` to rotate, or the same key to refresh.
596    pub async fn self_key_expiry_unix(&self) -> Result<Option<i64>, Error> {
597        Ok(self.self_node().await?.key_expiry_unix())
598    }
599
600    /// Whether this node's key has expired as of now (`!KeyExpiry.IsZero() && KeyExpiry.Before(now)`
601    /// in Go). A key with no expiry is never expired. See [`Device::self_key_expiry_unix`] for the
602    /// reactive-rotation note.
603    pub async fn self_key_expired(&self) -> Result<bool, Error> {
604        let now = std::time::SystemTime::now()
605            .duration_since(std::time::UNIX_EPOCH)
606            .map(|d| d.as_secs() as i64)
607            // An unreadable clock (pre-epoch) is treated as the far future so a time-limited key
608            // looks expired — fail-safe toward prompting re-auth rather than trusting a stale key.
609            .unwrap_or(i64::MAX);
610        Ok(self.self_node().await?.key_expired_at_unix(now))
611    }
612
613    /// Fetch the current Tailscale SSH policy pushed by control, if any.
614    ///
615    /// Returns `Ok(None)` when control has not sent an SSH policy. The SSH server treats an absent
616    /// or empty policy as **deny-all** (fail-closed). Used by the SSH auth path
617    /// ([`SshPolicy::evaluate`][ts_control::SshPolicy::evaluate]) to authorize incoming
618    /// connections.
619    pub async fn ssh_policy(&self) -> Result<Option<ts_control::SshPolicy>, Error> {
620        self.runtime
621            .control
622            .ask(ts_runtime::control_runner::CurrentSshPolicy)
623            .await
624            .map_err(ts_runtime::Error::from)
625            .map_err(Into::into)
626    }
627
628    /// Look up a peer by name.
629    pub async fn peer_by_name(&self, name: &str) -> Result<Option<NodeInfo>, Error> {
630        let pt = self
631            .runtime
632            .peer_tracker
633            .upgrade()
634            .ok_or(Error::Internal(InternalErrorKind::Actor))?;
635
636        pt.ask(ts_runtime::peer_tracker::PeerByName {
637            name: name.to_string(),
638        })
639        .await
640        .map_err(ts_runtime::Error::from)
641        .map_err(Into::into)
642    }
643
644    /// Look up a peer by ip.
645    pub async fn peer_by_tailnet_ip(&self, ip: IpAddr) -> Result<Option<NodeInfo>, Error> {
646        let pt = self
647            .runtime
648            .peer_tracker
649            .upgrade()
650            .ok_or(Error::Internal(InternalErrorKind::Actor))?;
651
652        pt.ask(ts_runtime::peer_tracker::PeerByTailnetIp { ip })
653            .await
654            .map_err(ts_runtime::Error::from)
655            .map_err(Into::into)
656    }
657
658    /// Look up the peer(s) with the most-specific route matches for `ip`.
659    ///
660    /// This reports which peers *advertise* a route covering `ip`, independent of this device's
661    /// `accept_routes` setting — analogous to the Go client's informational `PrimaryRoutes`. It is
662    /// not a reachability oracle: with `accept_routes` off, the dataplane will not actually route
663    /// to (or accept return traffic from) advertised subnet routes even if this returns a peer.
664    pub async fn peers_with_route(&self, ip: IpAddr) -> Result<Vec<NodeInfo>, Error> {
665        let pt = self
666            .runtime
667            .peer_tracker
668            .upgrade()
669            .ok_or(Error::Internal(InternalErrorKind::Actor))?;
670
671        pt.ask(ts_runtime::peer_tracker::PeerByAcceptedRoute { ip })
672            .await
673            .map_err(ts_runtime::Error::from)
674            .map_err(Into::into)
675    }
676
677    /// List the Taildrop files this device has fully received and not yet consumed (Go LocalAPI
678    /// `WaitingFiles`).
679    ///
680    /// Returns the files waiting under the configured `taildrop_dir`, sorted by name. Returns an
681    /// empty list when Taildrop is disabled (`Config::taildrop_dir` unset) — fail-closed, never an
682    /// error for the disabled case. A filesystem error while listing surfaces as
683    /// [`InternalErrorKind::Actor`].
684    pub fn taildrop_waiting_files(&self) -> Result<Vec<WaitingFile>, Error> {
685        let Some(store) = self.runtime.taildrop_store() else {
686            return Ok(Vec::new());
687        };
688        store
689            .waiting_files()
690            .map_err(|_| Error::Internal(InternalErrorKind::Actor))
691    }
692
693    /// Open a received Taildrop file by name for reading, returning the handle and its size (Go
694    /// LocalAPI `OpenFile`).
695    ///
696    /// The `name` is validated (path-traversal-safe) inside the store before any path is built.
697    /// Returns [`InternalErrorKind::BadRequest`] when Taildrop is disabled or the name is invalid,
698    /// and [`InternalErrorKind::Actor`] for a filesystem error (e.g. the file does not exist).
699    pub fn taildrop_open_file(&self, name: &str) -> Result<(std::fs::File, u64), Error> {
700        let store = self
701            .runtime
702            .taildrop_store()
703            .ok_or(Error::Internal(InternalErrorKind::BadRequest))?;
704        store.open_file(name).map_err(taildrop_err)
705    }
706
707    /// Delete a received Taildrop file by name (Go LocalAPI `DeleteFile`).
708    ///
709    /// The `name` is validated (path-traversal-safe) inside the store before any path is built.
710    /// Returns [`InternalErrorKind::BadRequest`] when Taildrop is disabled or the name is invalid,
711    /// and [`InternalErrorKind::Actor`] for a filesystem error (e.g. the file does not exist).
712    pub fn taildrop_delete_file(&self, name: &str) -> Result<(), Error> {
713        let store = self
714            .runtime
715            .taildrop_store()
716            .ok_or(Error::Internal(InternalErrorKind::BadRequest))?;
717        store.delete_file(name).map_err(taildrop_err)
718    }
719
720    /// Send a local file to a tailnet `peer` via Taildrop (Go `PushFile` / `tailscale file cp`).
721    ///
722    /// Pushes `content_length` bytes from `reader` to the peer's peerAPI as
723    /// `PUT /v0/put/<name>` over the overlay netstack — the sending counterpart to the receive store
724    /// surfaced by [`Device::taildrop_waiting_files`]. The transfer rides the encrypted WireGuard
725    /// overlay, never a host socket. The body is streamed from offset 0 (no resume).
726    ///
727    /// The destination is derived **solely from `peer`'s own node record**
728    /// ([`NodeInfo::peerapi_addr`][ts_control::Node::peerapi_addr]): its advertised tailnet IPv4 and
729    /// `peerapi4` port. The caller obtains `peer` from [`Device::peer_by_name`] /
730    /// [`Device::peer_by_tailnet_ip`], so it is always a current netmap peer — a raw control-supplied
731    /// or attacker-chosen address can never be targeted. As defense in depth, the resolved address is
732    /// additionally asserted to be a Tailscale CGNAT IP before dialing.
733    ///
734    /// Returns [`InternalErrorKind::BadRequest`] when the peer advertises no IPv4 peerAPI (so it
735    /// cannot receive files), when the name is invalid, or when the peer refuses the transfer
736    /// (`403`/`409`/unexpected status); [`Error::Timeout`] on a dial failure or timeout; and
737    /// [`InternalErrorKind::Io`] on a mid-transfer stream error.
738    pub async fn send_file<R>(
739        &self,
740        peer: &NodeInfo,
741        name: &str,
742        content_length: u64,
743        reader: R,
744    ) -> Result<(), Error>
745    where
746        R: tokio::io::AsyncRead + Unpin,
747    {
748        let channel = self.channel()?;
749
750        // Destination comes only from the peer's own node record — never an arbitrary address.
751        let dst = peer
752            .peerapi_addr()
753            .ok_or(Error::Internal(InternalErrorKind::BadRequest))?;
754        // Defense in depth: refuse to dial anything outside the Tailscale CGNAT range, so a
755        // malformed node record can't steer the PUT at a non-tailnet host.
756        if !ts_control::is_tailscale_ip(dst.ip()) {
757            return Err(Error::Internal(InternalErrorKind::BadRequest));
758        }
759
760        let self_ipv4 = self.ipv4_addr().await?;
761
762        ts_runtime::taildrop_send::send_file(channel, self_ipv4, dst, name, content_length, reader)
763            .await
764            .map_err(taildrop_send_err)
765    }
766
767    /// Begin a debug packet capture, streaming a pcap of every packet crossing the dataplane to
768    /// `writer` (Go `tsnet.Server.CapturePcap`).
769    ///
770    /// Installs a capture hook on the running dataplane: from now until [`Device::stop_capture`] is
771    /// called (or another capture replaces this one), a copy of every plaintext IP packet on the
772    /// datapath — outbound (pre-encrypt) and inbound (post-decrypt) — is framed and written to
773    /// `writer`. The 24-byte pcap global header is written immediately on success.
774    ///
775    /// The format is byte-faithful classic pcap with Tailscale's `LINKTYPE_USER0` + 4-byte path
776    /// preamble per record (see [`ts_runtime::capture`]); a resulting file opens in Wireshark, and
777    /// with Tailscale's `ts-dissector.lua` the direction/path of each packet decodes.
778    ///
779    /// The hook runs **inline on the single-threaded dataplane step**, so `writer` must not block for
780    /// long — a slow writer back-pressures the datapath. Records are **not** flushed per packet (that
781    /// would be a syscall on every packet on the dataplane thread); buffered bytes are flushed when
782    /// the writer is dropped on [`Device::stop_capture`]. Wrap `writer` in a [`std::io::BufWriter`] if
783    /// you want buffering. A write error is swallowed per-packet (the capture silently drops that
784    /// record) rather than tearing down the datapath; call [`Device::stop_capture`] to end it. Returns
785    /// an error only if the dataplane actor is unreachable or the initial global-header write fails.
786    pub async fn capture_pcap<W>(&self, writer: W) -> Result<(), Error>
787    where
788        W: std::io::Write + Send + 'static,
789    {
790        let sink = std::sync::Arc::new(std::sync::Mutex::new(
791            ts_runtime::capture::PcapSink::new(writer)
792                .map_err(|_| Error::Internal(InternalErrorKind::Io))?,
793        ));
794        let hook: ts_runtime::CaptureHook = std::sync::Arc::new(move |path, pkt: &[u8]| {
795            if let Ok(mut sink) = sink.lock() {
796                // A per-packet write failure (e.g. a closed pipe) silently drops that record rather
797                // than tearing down the datapath; the caller ends capture via `stop_capture`.
798                drop(sink.log_packet(path.code(), pkt));
799            }
800        });
801        self.runtime.install_capture(Some(hook)).await?;
802        Ok(())
803    }
804
805    /// Stop a debug packet capture started by [`Device::capture_pcap`] (Go `ClearCaptureSink`).
806    ///
807    /// Clears the dataplane capture hook; the writer is dropped (its remaining buffered bytes are
808    /// flushed by its own `Drop`). Idempotent — clearing when no capture is installed is a no-op.
809    /// Returns an error only if the dataplane actor is unreachable.
810    pub async fn stop_capture(&self) -> Result<(), Error> {
811        self.runtime.install_capture(None).await?;
812        Ok(())
813    }
814
815    /// Snapshot of this device and its tailnet peers (like `tailscale status`).
816    ///
817    /// Combines this node's self info with the current peer set: each [`StatusNode`] reports the
818    /// stable id, display name, tailnet IPs, advertised routes, and exit-node flag. (Per-peer
819    /// `online`/user/capabilities are honestly `None`/empty in this fork — the domain node model
820    /// does not yet carry the wire-level liveness/login fields; see `ts_runtime::status` docs.)
821    pub async fn status(&self) -> Result<Status, Error> {
822        self.runtime.status().await.map_err(Into::into)
823    }
824
825    /// Fetch the current Tailnet Lock (TKA) status pushed by control, if any.
826    ///
827    /// Returns `Ok(None)` when control has sent no `TKAInfo` (tailnet lock not in use, or no change
828    /// observed yet). The returned [`TkaStatus`][ts_control::TkaStatus] carries the authority head
829    /// (a base32 `AUMHash`, decode with [`tka::AumHash::from_base32`][ts_tka::AumHash::from_base32])
830    /// and the disablement signal. Signature verification of a peer's node-key signature against the
831    /// authority is performed with the [`tka`] module's [`tka::Authority`][ts_tka::Authority].
832    pub async fn tka_status(&self) -> Result<Option<ts_control::TkaStatus>, Error> {
833        self.runtime
834            .control
835            .ask(ts_runtime::control_runner::CurrentTkaStatus)
836            .await
837            .map_err(ts_runtime::Error::from)
838            .map_err(Into::into)
839    }
840
841    /// Request an OIDC **ID token** from control for this node, scoped to `audience` (workload-
842    /// identity federation, like `tailscale`'s `id-token` LocalAPI).
843    ///
844    /// Returns a signed JWT whose `sub` claim is this node's MagicDNS name and whose `aud` claim is
845    /// `audience`, suitable for presenting to a third-party relying party (e.g. AWS/GCP
846    /// workload-identity federation). The node is the token *subject*, not the authenticator — this
847    /// is token issuance over the Noise transport (`POST /machine/id-token`), not a login path.
848    /// Requires the control plane to support capability version ≥ 30.
849    pub async fn fetch_id_token(&self, audience: &str) -> Result<String, ts_control::IdTokenError> {
850        self.runtime.fetch_id_token(audience.to_string()).await
851    }
852
853    /// Log this node out of the tailnet — deregister it from the control plane (the equivalent of
854    /// Go `tsnet`'s `LocalClient.Logout`).
855    ///
856    /// Re-`POST`s `/machine/register` with this node's current node key and a past expiry, which the
857    /// control plane honors by **expiring the node now**: it drops out of every peer's netmap and
858    /// must re-register (re-authenticate) to rejoin.
859    ///
860    /// This is primarily for **non-ephemeral** nodes. An ephemeral node is garbage-collected by
861    /// control shortly after it disconnects, but a persistent node lingers in the tailnet
862    /// (visible to peers, counting against the machine limit) for up to ~24h after the process exits
863    /// unless explicitly logged out. Call this before [`shutdown`](Self::shutdown) to deregister
864    /// immediately. Calling it on an ephemeral node simply brings the GC forward; it is idempotent,
865    /// so logging out an already-gone node is not an error.
866    ///
867    /// This is a **control-plane state change only**: it does not tear down the local datapath (do
868    /// that via [`shutdown`](Self::shutdown)), and it does not delete or rotate the on-disk node key
869    /// — re-registering with the same key (a fresh [`Device::new`]) is the re-login path.
870    pub async fn logout(&self) -> Result<(), ts_control::LogoutError> {
871        self.runtime.logout().await
872    }
873
874    /// Snapshot this node's client metrics in Prometheus text exposition format.
875    ///
876    /// Mirrors Go Tailscale's `clientmetric` registry: process-global counters/gauges incremented
877    /// on the datapath hot loops (e.g. `magicsock_send_udp`, `magicsock_recv_data_bytes_udp`),
878    /// rendered as `# TYPE <name> <kind>\n<name> <value>\n` per metric, sorted by name. (Go `tsnet`
879    /// exposes no metrics method of its own, so this is the fork's clean public surface.) The
880    /// registry is process-global, so the output covers every `Device` in the process.
881    pub fn metrics(&self) -> String {
882        ts_metrics::write_prometheus()
883    }
884
885    /// Map a tailnet source `addr` to the node that owns its IP (like `tsnet`'s `WhoIs`).
886    ///
887    /// Only the IP of `addr` is used; the port is ignored. Returns `Ok(None)` if no tailnet node
888    /// owns that address.
889    pub async fn whois(&self, addr: SocketAddr) -> Result<Option<WhoIs>, Error> {
890        self.runtime.whois(addr).await.map_err(Into::into)
891    }
892
893    /// Change the selected exit node at runtime, without recreating the [`Device`] — the equivalent
894    /// of Go `tsnet`'s `LocalClient.EditPrefs(ExitNodeID/ExitNodeIP)`.
895    ///
896    /// The peer may be named by stable node ID, tailnet IP, or MagicDNS name via
897    /// [`ExitNodeSelector`] (a bare IP or name parses with `selector.parse()`); this is the same
898    /// selector type as [`Config::exit_node`](crate::Config::exit_node), so the construction-time
899    /// and runtime paths are identical. Passing `None` clears the exit node — internet-bound traffic
900    /// is then dropped (fail-closed) unless this node egresses directly.
901    ///
902    /// The change is applied immediately: the new selector is re-resolved against the live peer set
903    /// and the outbound route + inbound source filter are recomputed at once. A selector for a peer
904    /// not yet in the netmap simply takes effect once that peer appears.
905    ///
906    /// Only NEW flows use the changed exit; in-flight connections are not torn down and continue
907    /// egressing via the previously-selected exit until they close.
908    pub async fn set_exit_node(&self, exit_node: Option<ExitNodeSelector>) -> Result<(), Error> {
909        self.runtime
910            .set_exit_node(exit_node)
911            .await
912            .map_err(Into::into)
913    }
914
915    /// The currently-selected exit node, or `None` if none is selected.
916    pub fn exit_node(&self) -> Option<ExitNodeSelector> {
917        self.runtime.exit_node()
918    }
919
920    /// The stable id of the exit node traffic is **currently** egressing through, or `None` if none
921    /// is engaged (the equivalent of Go `tsnet`'s `Status.ExitNodeStatus.ID`).
922    ///
923    /// This differs from [`exit_node`](Self::exit_node), which returns the *configured* selector:
924    /// the active exit node is the route updater's resolved, fail-closed answer. It is `None` when
925    /// no exit node is configured, the configured selector matches no current peer, or the matched
926    /// peer no longer advertises a default route (egress is then dropped, fail-closed). Match the id
927    /// against [`Status::peers`](crate::Status::peers) (via [`status`](Self::status)) for details.
928    pub fn active_exit_node(&self) -> Option<ts_control::StableNodeId> {
929        self.runtime.active_exit_node()
930    }
931
932    /// Watch for netmap changes: the returned receiver's value is the current set of peer
933    /// [`StatusNode`]s and updates on every netmap change (like subscribing to `ipn` notifications).
934    pub async fn watch_netmap(
935        &self,
936    ) -> Result<tokio::sync::watch::Receiver<Vec<StatusNode>>, Error> {
937        self.runtime.watch_netmap().await.map_err(Into::into)
938    }
939
940    /// The current device connection-[`DeviceState`] (`Connecting` / `Running` / `NeedsLogin` /
941    /// `Expired` / `Failed`).
942    pub fn device_state(&self) -> DeviceState {
943        self.runtime.device_state()
944    }
945
946    /// Watch the device connection-[`DeviceState`], reacting push-style to control connection
947    /// transitions instead of polling [`status`](Self::status).
948    ///
949    /// Returns a [`tokio::sync::watch::Receiver`]; await its
950    /// [`changed`](tokio::sync::watch::Receiver::changed) to be woken on each transition. The
951    /// initial value is the current state.
952    pub fn watch_state(&self) -> tokio::sync::watch::Receiver<DeviceState> {
953        self.runtime.watch_state()
954    }
955
956    /// Wait until the device finishes registering, returning a typed outcome — the clean
957    /// replacement for polling [`ipv4_addr`](Self::ipv4_addr) in a loop.
958    ///
959    /// Resolves `Ok(())` once the device is [`DeviceState::Running`]. On a non-running outcome it
960    /// returns a typed [`RegistrationError`]:
961    /// - [`AuthRejected`](RegistrationError::AuthRejected) — bad/expired/unknown auth key;
962    ///   **permanent** (re-pair).
963    /// - [`NeedsLogin`](RegistrationError::NeedsLogin) — interactive authorization required;
964    ///   **not permanent** (the runtime keeps retrying and reaches `Running` once the user
965    ///   authorizes). Auth-key callers treat this as failure; interactive callers should ignore it
966    ///   and drive the flow via [`watch_state`](Self::watch_state).
967    /// - [`NetworkUnreachable`](RegistrationError::NetworkUnreachable) — **transient** (retry).
968    /// - [`Timeout`](RegistrationError::Timeout) — no settled state within `timeout` (`None` waits
969    ///   indefinitely).
970    ///
971    /// [`KeyExpired`](RegistrationError::KeyExpired) is not produced here (a key expires only after
972    /// the node is up); observe it via [`watch_state`](Self::watch_state). Use
973    /// [`RegistrationError::is_permanent`] to branch "re-pair" vs. "retry / drive login".
974    pub async fn wait_until_running(
975        &self,
976        timeout: Option<Duration>,
977    ) -> Result<(), RegistrationError> {
978        self.runtime.wait_until_running(timeout).await
979    }
980
981    /// Ping a tailnet peer over the overlay with an ICMPv4 echo, returning the round-trip time
982    /// (like `tailscale ping`).
983    ///
984    /// The echo is sent from this device's own tailnet IPv4 over the overlay netstack — never a
985    /// host socket. IPv6 destinations return [`PingError::Ipv6Unsupported`] (this fork is
986    /// IPv4-only on the tailnet). A peer answers from its own OS stack; this netstack does not
987    /// auto-reply to echo requests.
988    ///
989    /// In TUN transport mode there is no application netstack to ping from; this surfaces as
990    /// [`PingError::Timeout`] (the same error this method already uses for an unavailable source
991    /// address — `PingError` carries no dedicated "unsupported" variant).
992    pub async fn ping(&self, dst: IpAddr, timeout: Duration) -> Result<Duration, PingError> {
993        let channel = self.channel().map_err(|_| PingError::Timeout)?;
994        let src = self.ipv4_addr().await.map_err(|_| PingError::Timeout)?;
995        ts_netstack_smoltcp::ping(channel, src, dst, timeout).await
996    }
997
998    /// Obtain a TLS certificate for a node's MagicDNS `name` (like `tsnet`'s `GetCertificate`).
999    ///
1000    /// **Fail-closed without the `acme` feature.** By default this fork has no client-side ACME
1001    /// engine wired in, so this returns [`ts_control::CertError::Unimplemented`] (after a
1002    /// tailnet-name check) — it NEVER self-signs and NEVER returns a placeholder certificate
1003    /// ([`ts_control::MISSING_CERT_RPC`] names what is missing).
1004    ///
1005    /// **With the `acme` feature** this instead drives the client-side ACME DNS-01 engine to issue a
1006    /// real Let's Encrypt certificate for `name`, publishing the challenge TXT via the node's
1007    /// `POST /machine/set-dns` RPC (routed through the control runner). SaaS-only: a self-hosted
1008    /// control plane may 501 on set-dns, surfaced as [`ts_control::CertError::Acme`].
1009    #[cfg(not(feature = "acme"))]
1010    pub async fn get_certificate(&self, name: &str) -> Result<CertifiedKey, ts_control::CertError> {
1011        ts_control::get_certificate(name).await
1012    }
1013
1014    /// See the no-`acme` variant for the contract; with `acme` this issues a real cert via the
1015    /// runtime's ACME engine (`Device → Runtime → ControlRunner → issue_certificate_via_setdns`).
1016    #[cfg(feature = "acme")]
1017    pub async fn get_certificate(&self, name: &str) -> Result<CertifiedKey, ts_control::CertError> {
1018        self.runtime.get_certificate(name.to_string()).await
1019    }
1020
1021    /// Build a [`TlsAcceptor`] terminating TLS for `cfg.name` on the overlay (like `tsnet`'s
1022    /// `ListenTLS`).
1023    ///
1024    /// Obtains the certificate via [`Device::get_certificate`] — so with the `acme` feature this
1025    /// issues a real Let's Encrypt cert (when the control plane answers `set-dns`), and without it
1026    /// (or when issuance is unavailable) it surfaces the same fail-closed
1027    /// [`ts_control::CertError`] rather than ever serving a self-signed cert or downgrading to
1028    /// plaintext. Terminate accepted overlay streams with [`ts_control::accept_tls`].
1029    pub async fn listen_tls(
1030        &self,
1031        cfg: &ts_control::ServeConfig,
1032    ) -> Result<TlsAcceptor, ts_control::CertError> {
1033        // Route through Device::get_certificate (the acme-aware issuance path) rather than
1034        // ts_control::listen_tls, which only knows the non-acme stub. Validate the serve config
1035        // first (same fail-closed checks ts_control::listen_tls applies), then assemble the acceptor.
1036        cfg.validate()?;
1037        let cert = self.get_certificate(&cfg.name).await?;
1038        ts_control::tls_acceptor(cert)
1039    }
1040
1041    /// The currently-stored Serve config (like `tsnet`'s `GetServeConfig`).
1042    ///
1043    /// Returns the config last passed to [`Device::set_serve_config`], or an empty
1044    /// [`ts_control::ServeState`] (no ports) if none was ever set. Pure read — does not touch the
1045    /// network.
1046    pub fn get_serve_config(&self) -> ts_control::ServeState {
1047        match &*self.serve.lock().unwrap_or_else(|e| e.into_inner()) {
1048            Some(mgr) => mgr.get(),
1049            None => ts_control::ServeState::default(),
1050        }
1051    }
1052
1053    /// Replace this node's Serve config and (re)bind its tailnet ports (like `tsnet`'s
1054    /// `SetServeConfig`, REPLACE semantics).
1055    ///
1056    /// `state` becomes the **whole** config (full-replace reconcile: every previously-bound serve
1057    /// port's accept loop is torn down and the new config's ports are bound from scratch). For each
1058    /// configured port the manager binds an overlay listener on this node's tailnet IPv4 and
1059    /// dispatches per [`ts_control::ServeTarget`]:
1060    /// - [`Accept`](ts_control::ServeTarget::Accept) — the TLS-terminated stream is handed back over
1061    ///   the returned [`ServeAcceptedReceiver`](ts_runtime::serve::ServeAcceptedReceiver) (the
1062    ///   in-process stand-in for `ListenTLS`'s `net.Listener`).
1063    /// - [`Proxy`](ts_control::ServeTarget::Proxy) — reverse-proxy the decrypted stream to a local
1064    ///   host backend.
1065    /// - [`Text`](ts_control::ServeTarget::Text) — write a fixed body and close.
1066    /// - [`TcpForward`](ts_control::ServeTarget::TcpForward) — forward the **raw** (non-TLS) stream
1067    ///   to a local host backend.
1068    ///
1069    /// **Fail-closed.** `state.validate()` runs first. Every TLS-terminating port's acceptor is
1070    /// obtained up-front via [`Device::listen_tls`] (the ACME-aware cert path); if any cert cannot be
1071    /// issued the whole call fails with that [`ts_control::CertError`] and **nothing is bound** — a
1072    /// TLS port never downgrades to plaintext.
1073    ///
1074    /// **Anti-leak.** Listeners bind the overlay netstack only (never a host socket). The
1075    /// `Proxy`/`TcpForward` backend dial is a local host socket to the embedder's own backend (like
1076    /// Go's reverse-proxy to `127.0.0.1`), intentionally NOT routed through the exit-egress
1077    /// forwarder. A backend dial failure drops that connection; it never falls back.
1078    ///
1079    /// Returns an error in TUN transport mode (there is no application netstack to bind on). The
1080    /// previous config's accept loops (and any earlier `ServeAcceptedReceiver`) stop when this
1081    /// returns; the new receiver delivers every `Accept`-port connection.
1082    pub async fn set_serve_config(
1083        &self,
1084        state: ts_control::ServeState,
1085    ) -> Result<ts_runtime::serve::ServeAcceptedReceiver, Error> {
1086        state
1087            .validate()
1088            .map_err(|_| Error::Internal(InternalErrorKind::BadRequest))?;
1089
1090        // Fail-closed: build every TLS-terminating port's acceptor up-front via the ACME-aware cert
1091        // path. If any cert can't be issued, return before binding anything (no plaintext downgrade).
1092        let mut resolved = std::collections::BTreeMap::new();
1093        for (port, target) in &state.ports {
1094            let acceptor = if target.terminates_tls() {
1095                let cfg = ts_control::ServeConfig {
1096                    name: state.name.clone(),
1097                    port: *port,
1098                    target: target.clone(),
1099                };
1100                Some(self.listen_tls(&cfg).await.map_err(|_| {
1101                    // Cert issuance is fail-closed in this fork; surface as a request error rather
1102                    // than ever binding a plaintext TLS port.
1103                    Error::Internal(InternalErrorKind::BadRequest)
1104                })?)
1105            } else {
1106                None
1107            };
1108            resolved.insert(
1109                *port,
1110                ts_runtime::serve::ResolvedPort {
1111                    target: target.clone(),
1112                    acceptor,
1113                },
1114            );
1115        }
1116
1117        // The manager binds the OVERLAY netstack on this node's own tailnet IPv4.
1118        let self_ipv4 = self.ipv4_addr().await?;
1119        let channel = self.channel()?.clone();
1120
1121        let mut slot = self.serve.lock().unwrap_or_else(|e| e.into_inner());
1122        let mgr =
1123            slot.get_or_insert_with(|| ts_runtime::serve::ServeManager::new(channel, self_ipv4));
1124        Ok(mgr.set(state, resolved))
1125    }
1126
1127    /// Expose a tailnet TLS service to the public internet via Tailscale Funnel (like `tsnet`'s
1128    /// `ListenFunnel`), returning a [`FunnelAcceptedReceiver`](ts_runtime::funnel::FunnelAcceptedReceiver)
1129    /// that delivers each TLS-terminated public connection.
1130    ///
1131    /// **Two fail-closed gates, then the live ingress listener.** First the node-attribute gate is
1132    /// fully enforced from this node's own capability map (mirroring Go `ipn.NodeCanFunnel` +
1133    /// `ipn.CheckFunnelPort`): the tailnet admin must have enabled HTTPS and granted the `funnel`
1134    /// node attribute, and `cfg.port` must be in the set the `funnel-ports` capability allows —
1135    /// otherwise this returns [`ts_control::FunnelError::NotAllowed`] /
1136    /// [`ts_control::FunnelError::PortNotAllowed`] before touching any cert or network. Then the
1137    /// node's `*.ts.net` certificate is obtained via the ACME-aware [`Device::get_certificate`] (the
1138    /// Funnel hostname *is* the node's MagicDNS name, so its DNS-01 cert matches); fail-closed on
1139    /// [`ts_control::FunnelError::Cert`] — no self-signed or plaintext fallback.
1140    ///
1141    /// On success a [`FunnelManager`](ts_runtime::funnel::FunnelManager) is registered: its ingress
1142    /// sink is installed into the runtime's peerAPI `/v0/ingress` slot (making that route live without
1143    /// restarting the peerAPI server), and the `HostInfo.IngressEnabled` map-request signal is set so
1144    /// control routes Funnel traffic to this node. Public Funnel bytes arrive as a relay POST to
1145    /// `/v0/ingress`, are membership-gated + `101`-hijacked into a raw stream, TLS-terminated by the
1146    /// manager, and delivered over the returned receiver.
1147    ///
1148    /// **Where the relay comes from.** The public ingress **relay + DNS mapping** that feed
1149    /// `/v0/ingress` are Tailscale infrastructure ([`ts_control::MISSING_FUNNEL_RELAY`]), provisioned
1150    /// automatically against real Tailscale SaaS with a Funnel-enabled ACL; against a self-hosted
1151    /// control plane no relay exists, so the listener is correct but never fed.
1152    ///
1153    /// Anti-leak: Funnel TLS terminates only on the overlay netstack (the hijacked ingress stream
1154    /// arrives on the overlay peerAPI listener), never a host socket; there is no self-signed or
1155    /// plaintext fallback. A new `listen_funnel` replaces the previous manager (its pump + sink tear
1156    /// down); dropping the `Device` tears it down too.
1157    pub async fn listen_funnel(
1158        &self,
1159        cfg: &ts_control::ServeConfig,
1160        opts: ts_control::FunnelOptions,
1161    ) -> Result<ts_runtime::funnel::FunnelAcceptedReceiver, ts_control::FunnelError> {
1162        // Gate 1 (fail-closed, no network): node-attribute + funnel-port access from our cap map.
1163        let me = self
1164            .self_node()
1165            .await
1166            .map_err(|_| ts_control::FunnelError::NotAllowed)?;
1167        cfg.validate()?;
1168        ts_control::funnel_access(&me, cfg.port)?;
1169
1170        // Gate 2 (fail-closed): obtain the node's `*.ts.net` cert via the ACME-aware path and build
1171        // the TLS acceptor. A cert failure surfaces as FunnelError::Cert — never a plaintext listener.
1172        let cert = self
1173            .get_certificate(&cfg.name)
1174            .await
1175            .map_err(ts_control::FunnelError::Cert)?;
1176        let acceptor = ts_control::tls_acceptor(cert).map_err(ts_control::FunnelError::Cert)?;
1177
1178        // `opts.funnel_only` (reject tailnet-internal connections) is accepted for surface stability;
1179        // the ingress data path only ever carries relay-delivered public traffic, so there is no
1180        // tailnet-internal leg on this listener to reject. Documented as a no-op here for now.
1181        let _ = opts;
1182
1183        // Build the funnel manager + its ingress sink + the hand-back receiver, install the sink into
1184        // the runtime's shared peerAPI `/v0/ingress` slot (making the route live), and flip the
1185        // IngressEnabled map signal. Hold the manager on the device so its pump/sink live as long as
1186        // the listener; replacing a prior manager tears the old one down on drop at end of scope.
1187        let (manager, sink, receiver) = ts_runtime::funnel::FunnelManager::new(acceptor);
1188        {
1189            let slot = self.runtime.funnel_ingress_slot();
1190            *slot.lock().unwrap_or_else(|e| e.into_inner()) = Some(sink);
1191        }
1192        self.runtime
1193            .ingress_active_flag()
1194            .store(true, std::sync::atomic::Ordering::Relaxed);
1195
1196        let old = {
1197            let mut held = self.funnel.lock().unwrap_or_else(|e| e.into_inner());
1198            held.replace(manager)
1199        };
1200        drop(old);
1201
1202        Ok(receiver)
1203    }
1204
1205    /// Host a Tailscale **VIP service** (`svc:<label>`) by binding an overlay listener on the
1206    /// service's control-assigned virtual IP (like `tsnet`'s `ListenService`).
1207    ///
1208    /// **Fail-closed.** Mirrors Go `tsnet.Server.ListenService`'s preconditions, enforced from this
1209    /// node's own netmap state ([`ts_control::resolve_service_listen`]): the `name` must be a valid
1210    /// `svc:<dns-label>`, this node must be **tagged** (Go `ErrUntaggedServiceHost`), and control
1211    /// must have assigned the service a VIP address on this node (delivered via the `service-host`
1212    /// node-capability — see [`ts_control::Node::service_addresses`]). Any unmet precondition
1213    /// returns a typed [`ts_control::ServiceError`] before binding anything.
1214    ///
1215    /// When all hold, this binds a [`tcp_listen`][Device::tcp_listen] on the service VIP and the
1216    /// configured `mode` port over the **overlay netstack** (never a host socket) and returns the
1217    /// listener. The netstack already accepts packets for control-assigned VIPs (they are injected
1218    /// alongside the node's own tailnet address), so the listener is reachable by tailnet peers.
1219    ///
1220    /// The `Tun`/L3 service mode is unsupported (a TODO in upstream tsnet); only TCP/HTTP modes
1221    /// (which bind the same VIP:port at the listen layer) are offered. Returns an error in TUN
1222    /// transport mode (there is no application netstack to bind on).
1223    pub async fn listen_service(
1224        &self,
1225        name: &str,
1226        mode: ts_control::ServiceMode,
1227    ) -> Result<netstack::TcpListener, ts_control::ServiceError> {
1228        let me = self
1229            .self_node()
1230            .await
1231            .map_err(|e| ts_control::ServiceError::Listen(e.to_string()))?;
1232        let listen_addr = ts_control::resolve_service_listen(&me, name, mode, self.enable_ipv6)?;
1233        self.tcp_listen(listen_addr)
1234            .await
1235            .map_err(|e| ts_control::ServiceError::Listen(e.to_string()))
1236    }
1237
1238    /// Attempt to gracefully shut down this device's runtime.
1239    ///
1240    /// Reports whether the device was fully shut down before the timeout. It is still shut
1241    /// down if it timed out, just more violently and with potential resource leaks.
1242    ///
1243    /// If `timeout` is `None`, then shutdown will never time-out.
1244    pub async fn shutdown(self, timeout: Option<Duration>) -> bool {
1245        self.runtime.graceful_shutdown(timeout).await
1246    }
1247}
1248
1249/// Command-channel-driven userspace network stack.
1250///
1251/// This is an opinionated wrapper around [smoltcp](https://docs.rs/smoltcp) that provides an
1252/// easier-to-integrate, more-portable API.
1253pub mod netstack {
1254    #[doc(inline)]
1255    pub use ts_netstack_smoltcp::netcore::Error;
1256    #[doc(inline)]
1257    pub use ts_netstack_smoltcp::netcore::InternalErrorKind;
1258    #[doc(inline)]
1259    pub use ts_netstack_smoltcp::netsock::{TcpListener, TcpStream, UdpSocket};
1260}
1261
1262/// Geneve (RFC 8926) framing for Tailscale **peer-relay** traffic. A peer that advertises
1263/// [`NodeInfo::is_peer_relay`] runs a UDP relay server; relayed disco + WireGuard frames are
1264/// Geneve-encapsulated with a VNI. This module exposes the header codec so the framing is
1265/// recognizable. NOTE: the active relay *data path* (the relay-allocation handshake +
1266/// magicsock integration) is **not yet implemented** in this fork — this is the wire-aware slice.
1267pub mod geneve {
1268    #[doc(inline)]
1269    pub use ts_packet::geneve::{
1270        GENEVE_FIXED_HEADER_LEN, GENEVE_PROTOCOL_DISCO, GENEVE_PROTOCOL_WIREGUARD, GeneveError,
1271        GeneveHeader,
1272    };
1273}
1274
1275/// Tailnet Lock (TKA) verification: the [`tka::Authority`] checks a peer's node-key signature
1276/// against the trusted-key state, mirroring Go's `tka` package. Pair with [`Device::tka_status`]
1277/// (the control-pushed head/disablement signal).
1278pub mod tka {
1279    #[doc(inline)]
1280    pub use ts_tka::{
1281        AumHash, AumKind, Authority, Key, KeyKind, NodeKeySignature, SigKind, State, TkaError,
1282        aum_hash,
1283    };
1284}
1285
1286/// Tailscale cryptographic key types.
1287pub mod keys {
1288    #[doc(inline)]
1289    pub use ts_keys::{
1290        DiscoKeyPair, DiscoPrivateKey, DiscoPublicKey, MachineKeyPair, MachinePrivateKey,
1291        MachinePublicKey, NetworkLockKeyPair, NetworkLockPrivateKey, NetworkLockPublicKey,
1292        NodeKeyPair, NodePrivateKey, NodePublicKey, NodeState, PersistState,
1293    };
1294}
1295
1296const ENV_MAGIC_VAR: &str = "TS_RS_EXPERIMENT";
1297const ENV_MAGIC_VALUE: &str = "this_is_unstable_software";
1298
1299fn check_magic_env() -> Result<(), Error> {
1300    if std::env::var(ENV_MAGIC_VAR).as_deref() != Ok(ENV_MAGIC_VALUE) {
1301        let warning = format!(
1302            "
1303check failed: set {ENV_MAGIC_VAR}={ENV_MAGIC_VALUE} to acknowledge that tailscale-rs is early-days
1304experimental software containing bugs, unvalidated cryptography, and no stability or compatibility
1305guarantees.
1306            "
1307        );
1308
1309        eprintln!("{}", warning.trim());
1310
1311        return Err(Error::UnstableEnvVar);
1312    };
1313
1314    Ok(())
1315}
1316
1317#[cfg(test)]
1318mod tests {
1319    use secrecy::ExposeSecret as _;
1320
1321    use super::*;
1322
1323    // `Device::new`/`new_with_secret` cannot be unit-tested end-to-end without a live control
1324    // server (registration). The only behavioral difference `new_with_secret` introduces over `new`
1325    // is exposing the `SecretString` to a plain `String` on the last inch; everything after is the
1326    // shared `new` path. So we assert that equivalence at the auth-key-resolution level: the secret
1327    // path must resolve to the exact same key the plain path feeds into `resolve_auth_key`.
1328    const SAMPLE_KEY: &str = "tskey-auth-koCgSLP5R811CNTRL-EXAMPLEEXAMPLEEXAMPLEEXAMPLE";
1329
1330    // The mapping `new_with_secret` applies (`Option<SecretString>` -> `Option<String>`) must be a
1331    // byte-for-byte round-trip, so the spawn arg is identical to a direct `new(config, Some(..))`.
1332    #[test]
1333    fn secret_exposes_to_identical_string() {
1334        let plain = Some(SAMPLE_KEY.to_string());
1335        let from_secret =
1336            Some(SecretString::from(SAMPLE_KEY)).map(|s| s.expose_secret().to_string());
1337        assert_eq!(from_secret, plain);
1338
1339        // `None` must pass through unchanged (so it falls back to `config.auth_key` exactly as `new`).
1340        let none_secret: Option<SecretString> = None;
1341        assert_eq!(
1342            none_secret.map(|s| s.expose_secret().to_string()),
1343            None::<String>
1344        );
1345    }
1346
1347    // End-to-end equivalence at the resolve layer: feeding the exposed secret through
1348    // `resolve_auth_key` yields the same `Option<String>` as feeding the plain string — i.e. both
1349    // constructors reach the same spawn argument, without registering against a control server.
1350    #[tokio::test]
1351    async fn new_with_secret_resolves_same_as_new() {
1352        let config = Config::default();
1353
1354        let via_plain = resolve_auth_key(&config, Some(SAMPLE_KEY.to_string()))
1355            .await
1356            .expect("plain auth key resolves");
1357
1358        let exposed = Some(SecretString::from(SAMPLE_KEY)).map(|s| s.expose_secret().to_string());
1359        let via_secret = resolve_auth_key(&config, exposed)
1360            .await
1361            .expect("secret-derived auth key resolves");
1362
1363        assert_eq!(via_plain, via_secret);
1364        // Without the `identity-federation` feature `resolve_auth_key` is a pass-through, so the
1365        // resolved key is the input verbatim; assert that too to pin the default-build behavior.
1366        #[cfg(not(feature = "identity-federation"))]
1367        assert_eq!(via_secret, Some(SAMPLE_KEY.to_string()));
1368    }
1369}