1#![doc = include_str!("../README.md")]
2
3extern crate alloc;
4
5const PKG_VERSION: &str = if let Some(version) = option_env!("CARGO_PKG_VERSION") {
9 version
10} else {
11 ""
12};
13
14pub(crate) const MAX_CONTROL_RESPONSE: usize = 1024 * 1024;
24
25#[cfg(feature = "acme")]
27pub mod acme;
28#[cfg(feature = "async_tokio")]
29mod cert;
30mod config;
31mod control_dialer;
32mod derp;
33mod dial_plan;
34mod dns;
35mod hostinfo;
36#[cfg_attr(not(feature = "async_tokio"), expect(dead_code))]
37mod map_request_builder;
38mod node;
39#[cfg(feature = "async_tokio")]
40mod serve;
41mod service;
42mod ssh_policy;
43mod tka;
44#[cfg(feature = "async_tokio")]
45mod tokio;
46#[cfg(feature = "identity-federation")]
47pub mod wif;
48
49use std::fmt;
50
51#[cfg(feature = "async_tokio")]
52pub use cert::{
53 CertError, MISSING_CERT_RPC, certified_key_from_pem, get_certificate, is_tailnet_name,
54};
55#[cfg(feature = "acme")]
56pub use cert::{
57 PublishTxt, SetDnsPublisher, issue_cert_pair_via_setdns, issue_certificate_via_setdns,
58};
59#[doc(inline)]
60pub use config::{
61 Config, DEFAULT_CONTROL_SERVER, DEFAULT_PERSISTENT_KEEPALIVE, ExitProxyConfig, ExitProxyScheme,
62 TransportMode, TunConfig, services_hash,
63};
64pub use control_dialer::{ControlDialer, TcpDialer, complete_connection};
65pub use derp::{Map as DerpMap, Region as DerpRegion, convert_derp_map};
66pub use dial_plan::{DialCandidate, DialMode, DialPlan};
67pub use dns::{DnsConfig, ExtraRecord, Resolver as DnsResolver, ResolverTransport};
68pub use node::{
69 ExitNodeSelector, Id as NodeId, Node, NodeCapMap, PeerChange, StableId as StableNodeId,
70 TailnetAddress, UserProfile, is_tailscale_ip, validate_service_name,
71};
72#[cfg(feature = "async_tokio")]
73pub use serve::{
74 FunnelError, FunnelOptions, MISSING_FUNNEL_RELAY, ServeConfig, ServeState, ServeTarget,
75 accept_tls, funnel_access, listen_funnel, listen_tls, tls_acceptor,
76};
77pub use service::{ServiceError, ServiceMode, resolve_service_listen};
78pub use ssh_policy::{
79 SshAccept, SshAction, SshConnIdentity, SshDecision, SshDenyReason, SshPolicy, SshPrincipal,
80 SshRule,
81};
82pub use tka::TkaStatus;
83pub use ts_control_serde::{
84 Endpoint, EndpointType, TkaBootstrapRequest, TkaBootstrapResponse, TkaDisableRequest,
85 TkaDisableResponse, TkaInitBeginRequest, TkaInitBeginResponse, TkaInitFinishRequest,
86 TkaInitFinishResponse, TkaSignInfo, TkaSubmitSignatureRequest, TkaSubmitSignatureResponse,
87 TkaSyncOfferRequest, TkaSyncOfferResponse, TkaSyncSendRequest, TkaSyncSendResponse, UserId,
88};
89#[cfg(feature = "identity-federation")]
90pub use wif::{WifConfig, WifError, resolve_auth_key};
91
92#[cfg(feature = "async_tokio")]
96pub mod tls {
97 pub use tokio_rustls::{TlsAcceptor, rustls::sign::CertifiedKey, server::TlsStream};
98}
99
100#[cfg(feature = "async_tokio")]
101pub use crate::tokio::{
102 AsyncControlClient, FilterUpdate, IdTokenError, LogoutError, LogoutInternalErrorKind,
103 PeerUpdate, SetDnsError, SetDnsInternalErrorKind, StateUpdate, TkaSyncError,
104 TkaSyncInternalErrorKind, fetch_id_token, logout, set_dns, tka_bootstrap, tka_disable,
105 tka_init_begin, tka_init_finish, tka_submit_signature, tka_sync_offer, tka_sync_send,
106};
107
108#[derive(Debug, thiserror::Error, Clone, Eq, PartialEq)]
110pub enum Error {
111 #[error("machine was not authorized by control to join tailnet, authorize at {0}")]
113 MachineNotAuthorized(url::Url),
114
115 #[error("machine awaiting admin approval to join tailnet (no interactive auth URL)")]
123 NeedsMachineAuth,
124
125 #[error("invalid URL: {0}")]
127 InvalidUrl(url::Url),
128
129 #[error("control rejected registration: {0}")]
132 Registration(String),
133
134 #[error("control rate limited the request; retry after {0:?}")]
138 RateLimited(core::time::Duration),
139
140 #[error("a networking error occurred in {0}")]
146 NetworkError(Operation),
147
148 #[error("{0} error in {1}")]
153 Internal(InternalErrorKind, Operation),
154}
155
156impl Error {
157 fn io_error(err: std::io::Error, op: Operation) -> Self {
158 if crate::is_network_error(&err) {
159 Error::NetworkError(op)
160 } else {
161 Error::Internal(InternalErrorKind::Io, op)
162 }
163 }
164}
165
166#[non_exhaustive]
170#[derive(Debug, Clone, Copy, Eq, PartialEq)]
171pub enum InternalErrorKind {
172 Url,
174 Http,
176 SerDe,
178 Io,
180 MessageFormat,
182 Utf8,
184 NoiseHandshake,
186 Challenge,
188 MachineAuthorization,
191}
192
193impl fmt::Display for InternalErrorKind {
194 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
195 match self {
196 InternalErrorKind::Url => write!(f, "URL parsing error"),
197 InternalErrorKind::Http => write!(f, "unsuccessful HTTP request or upgrade"),
198 InternalErrorKind::SerDe => write!(f, "serialization/deserialization error"),
199 InternalErrorKind::Io => write!(f, "I/O error"),
200 InternalErrorKind::MessageFormat => write!(f, "message format error"),
201 InternalErrorKind::Utf8 => write!(f, "invalid UTF8"),
202 InternalErrorKind::NoiseHandshake => write!(f, "error in Noise handshake"),
203 InternalErrorKind::Challenge => write!(f, "error with Tailscale challenge packet"),
204 InternalErrorKind::MachineAuthorization => {
205 write!(f, "machine not authorized to register with Tailnet")
206 }
207 }
208 }
209}
210
211#[derive(Debug, Clone, Copy, Eq, PartialEq)]
213pub enum Operation {
214 MapRequest,
216 ConnectToControlServer,
218 Registration,
220}
221
222impl fmt::Display for Operation {
223 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
224 match self {
225 Operation::MapRequest => write!(f, "net map request"),
226 Operation::ConnectToControlServer => write!(f, "connection to control server"),
227 Operation::Registration => write!(f, "registration"),
228 }
229 }
230}
231
232impl From<ts_http_util::Error> for Error {
233 fn from(error: ts_http_util::Error) -> Self {
234 tracing::error!(%error, "http error");
235
236 if http_error_is_recoverable(error) {
237 Error::NetworkError(Operation::ConnectToControlServer)
238 } else {
239 Error::Internal(InternalErrorKind::Http, Operation::ConnectToControlServer)
240 }
241 }
242}
243
244fn is_network_error(err: &std::io::Error) -> bool {
246 use std::io::ErrorKind::*;
247 matches!(
248 err.kind(),
249 ConnectionRefused
250 | ConnectionReset
251 | HostUnreachable
252 | NetworkUnreachable
253 | ConnectionAborted
254 | NotConnected
255 | TimedOut
256 | AddrNotAvailable
257 | Interrupted
258 | NetworkDown
259 )
260}
261
262fn http_error_is_recoverable(error: ts_http_util::Error) -> bool {
264 match error {
265 ts_http_util::Error::Io => true,
266 ts_http_util::Error::InvalidInput
267 | ts_http_util::Error::Timeout
270 | ts_http_util::Error::InvalidResponse
271 | ts_http_util::Error::BodyTooLarge => false,
274 ts_http_util::Error::ConnectionClosed => false,
276 }
277}