Skip to main content

moq_native/
server.rs

1use std::net;
2#[cfg(any(test, all(feature = "uds", unix)))]
3use std::path::PathBuf;
4
5#[cfg(feature = "iroh")]
6use crate::iroh;
7use crate::{Error, QuicBackend};
8use moq_net::Session;
9use url::Url;
10
11use futures::FutureExt;
12use futures::future::BoxFuture;
13use futures::stream::FuturesUnordered;
14use futures::stream::StreamExt;
15
16/// Configuration for the MoQ server.
17#[derive(clap::Args, Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
18#[serde(deny_unknown_fields, default)]
19#[non_exhaustive]
20pub struct ServerConfig {
21	/// Listen for QUIC (UDP) on the given address. Defaults to `[::]:443`.
22	///
23	/// Accepts standard socket address syntax (e.g. `[::]:443`) or a DNS
24	/// `host:port` pair (e.g. `fly-global-services:443`), resolved at bind time
25	/// (first address only; Quinn cannot bind multiple). Leave unset while a
26	/// `tcp`/`unix` listener is configured to run a stream-only server with no
27	/// QUIC.
28	#[serde(alias = "listen")]
29	#[arg(id = "server-bind", long = "server-bind", alias = "listen", env = "MOQ_SERVER_BIND")]
30	pub bind: Option<String>,
31
32	/// Plaintext qmux TCP listener (`--server-tcp-bind`, no TLS). Requires the
33	/// `tcp` feature.
34	#[cfg(feature = "tcp")]
35	#[command(flatten)]
36	#[serde(default)]
37	pub tcp: crate::tcp::Config,
38
39	/// Plaintext qmux Unix-socket listener (`--server-unix-bind`) with an optional
40	/// peer-credential allowlist. Requires the `uds` feature; unix-only.
41	#[cfg(all(feature = "uds", unix))]
42	#[command(flatten)]
43	#[serde(default)]
44	pub unix: crate::unix::Config,
45
46	/// The QUIC backend to use.
47	/// Auto-detected from compiled features if not specified.
48	#[arg(id = "server-backend", long = "server-backend", env = "MOQ_SERVER_BACKEND")]
49	pub backend: Option<QuicBackend>,
50
51	/// QUIC transport tuning (`--server-quic-*`): stream limits, GSO, timeouts,
52	/// plus the accept-side knobs (preferred address, QUIC-LB connection IDs).
53	#[command(flatten)]
54	#[serde(default)]
55	pub quic: crate::quic::Server,
56
57	/// Restrict the server to specific MoQ protocol version(s).
58	///
59	/// By default, the server accepts all supported versions.
60	/// Use this to restrict to specific versions, e.g. `--server-version moq-lite-02`.
61	/// Can be specified multiple times to accept a subset of versions.
62	///
63	/// Valid values: moq-lite-01, moq-lite-02, moq-lite-03, moq-transport-14, moq-transport-15, moq-transport-16
64	#[serde(default, skip_serializing_if = "Vec::is_empty")]
65	#[arg(id = "server-version", long = "server-version", env = "MOQ_SERVER_VERSION")]
66	pub version: Vec<moq_net::Version>,
67
68	/// The certificates to serve and the roots that authenticate mTLS clients
69	/// (`--server-tls-*`).
70	#[command(flatten)]
71	#[serde(default)]
72	pub tls: crate::tls::Server,
73}
74
75impl ServerConfig {
76	/// Build the [`Server`] this config describes, binding its listeners.
77	pub fn init(self) -> crate::Result<Server> {
78		Server::new(self)
79	}
80
81	/// Returns the configured versions, defaulting to all if none specified.
82	pub fn versions(&self) -> moq_net::Versions {
83		if self.version.is_empty() {
84			moq_net::Versions::all()
85		} else {
86			moq_net::Versions::from(self.version.clone())
87		}
88	}
89
90	/// Whether a `tcp`/`unix` stream listener is configured.
91	///
92	/// When true and [`bind`](Self::bind) is unset, the server runs stream-only
93	/// (no default QUIC listener).
94	#[allow(unused_mut)]
95	fn has_stream_listener(&self) -> bool {
96		let mut has = false;
97		#[cfg(feature = "tcp")]
98		{
99			has |= self.tcp.bind.is_some();
100		}
101		#[cfg(all(feature = "uds", unix))]
102		{
103			has |= self.unix.bind.is_some();
104		}
105		has
106	}
107}
108
109/// Default bind address used when [`ServerConfig::bind`] is not set.
110pub(crate) const DEFAULT_BIND: &str = "[::]:443";
111
112/// Server for accepting MoQ connections.
113///
114/// Accepts QUIC (and optionally WebSocket), plus plaintext qmux over TCP
115/// (`--server-tcp-bind`) and Unix sockets (`--server-unix-bind`). Create via
116/// [`ServerConfig::init`] or [`Server::new`].
117pub struct Server {
118	moq: moq_net::Server,
119	versions: moq_net::Versions,
120	accept: FuturesUnordered<BoxFuture<'static, crate::Result<Request>>>,
121	#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
122	streams: StreamListeners,
123	#[cfg(feature = "iroh")]
124	iroh: Option<iroh::Endpoint>,
125	#[cfg(feature = "noq")]
126	noq: Option<crate::noq::NoqServer>,
127	#[cfg(feature = "quinn")]
128	quinn: Option<crate::quinn::QuinnServer>,
129	#[cfg(feature = "quiche")]
130	quiche: Option<crate::quiche::QuicheServer>,
131	#[cfg(feature = "websocket")]
132	websocket: Option<crate::websocket::Listener>,
133}
134
135impl Server {
136	/// Build a server from its config, binding the QUIC socket up front.
137	///
138	/// The stream (`tcp`/`unix`) listeners bind lazily on the first
139	/// [`accept`](Self::accept), since they need a runtime.
140	pub fn new(config: ServerConfig) -> crate::Result<Self> {
141		let backend = config.backend.clone().unwrap_or_else(crate::default_quic_backend);
142
143		let versions = config.versions();
144
145		// Build a QUIC backend when `--server-bind` is set, or when nothing else
146		// is (the default). A stream-only server (`--server-unix-bind` with no
147		// `--server-bind`) doesn't also open UDP/443.
148		config.quic.validate()?;
149
150		let build_quic = config.bind.is_some() || !config.has_stream_listener();
151
152		if build_quic && !config.tls.root.is_empty() {
153			let mtls_supported = match backend {
154				#[cfg(feature = "quinn")]
155				QuicBackend::Quinn => true,
156				#[cfg(feature = "noq")]
157				QuicBackend::Noq => true,
158				#[cfg(feature = "quiche")]
159				QuicBackend::Quiche => true,
160				#[allow(unreachable_patterns)]
161				_ => false,
162			};
163			if !mtls_supported {
164				return Err(Error::MtlsUnsupported);
165			}
166		}
167
168		#[cfg(feature = "noq")]
169		#[allow(unreachable_patterns)]
170		let noq = match backend {
171			QuicBackend::Noq if build_quic => Some(crate::noq::NoqServer::new(config.clone())?),
172			_ => None,
173		};
174
175		#[cfg(feature = "quinn")]
176		#[allow(unreachable_patterns)]
177		let quinn = match backend {
178			QuicBackend::Quinn if build_quic => Some(crate::quinn::QuinnServer::new(config.clone())?),
179			_ => None,
180		};
181
182		#[cfg(feature = "quiche")]
183		let quiche = match backend {
184			QuicBackend::Quiche if build_quic => Some(crate::quiche::QuicheServer::new(config.clone())?),
185			_ => None,
186		};
187
188		// Collect the configured stream listeners (at most one TCP, one Unix).
189		#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
190		let mut stream_binds = Vec::new();
191		#[cfg(feature = "tcp")]
192		if let Some(addr) = config.tcp.bind {
193			stream_binds.push(StreamBind::Tcp(addr));
194		}
195		#[cfg(all(feature = "uds", unix))]
196		if let Some(path) = config.unix.bind.clone() {
197			stream_binds.push(StreamBind::Unix(path));
198		}
199		// `None` (or an all-empty allowlist) means the listener enforces nothing.
200		#[cfg(all(feature = "uds", unix))]
201		let unix_allow = config.unix.allow.clone().filter(|allow| !allow.is_empty());
202		#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
203		let streams = StreamListeners::new(
204			stream_binds,
205			stream_versions(&versions),
206			#[cfg(all(feature = "uds", unix))]
207			unix_allow,
208		);
209
210		Ok(Server {
211			accept: Default::default(),
212			moq: moq_net::Server::new().with_versions(versions.clone()),
213			versions,
214			#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
215			streams,
216			#[cfg(feature = "iroh")]
217			iroh: None,
218			#[cfg(feature = "noq")]
219			noq,
220			#[cfg(feature = "quinn")]
221			quinn,
222			#[cfg(feature = "quiche")]
223			quiche,
224			#[cfg(feature = "websocket")]
225			websocket: None,
226		})
227	}
228
229	/// Add a standalone WebSocket listener on a separate TCP port.
230	///
231	/// This is useful for simple applications that want WebSocket on a dedicated port.
232	/// For applications that need WebSocket on the same HTTP port (e.g. moq-relay),
233	/// use `qmux::Session::accept()` with your own HTTP framework instead.
234	#[cfg(feature = "websocket")]
235	pub fn with_websocket(mut self, websocket: crate::websocket::Listener) -> Self {
236		self.websocket = Some(websocket);
237		self
238	}
239
240	/// Also accept sessions over the given Iroh endpoint.
241	#[cfg(feature = "iroh")]
242	pub fn with_iroh(mut self, iroh: iroh::Endpoint) -> Self {
243		self.iroh = Some(iroh);
244		self
245	}
246
247	/// Publish the given origin to every session this server accepts.
248	pub fn with_publisher(mut self, publish: impl moq_net::Consume<moq_net::origin::Consumer>) -> Self {
249		self.moq = self.moq.with_publisher(publish);
250		self
251	}
252
253	/// Subscribe to every session's broadcasts, ingesting them into the given origin.
254	pub fn with_subscriber(mut self, subscribe: moq_net::origin::Producer) -> Self {
255		self.moq = self.moq.with_subscriber(subscribe);
256		self
257	}
258
259	/// Attach a per-connection [`moq_net::stats::Session`] context to all sessions
260	/// accepted by this server.
261	pub fn with_stats(mut self, stats: moq_net::stats::Session) -> Self {
262		self.moq = self.moq.with_stats(stats);
263		self
264	}
265
266	/// Accept sessions until the listener stops, serving `origin` to each subscriber.
267	///
268	/// Spawns a task per session and logs (rather than propagates) per-session
269	/// errors, so one bad peer never tears down the listener. Returns when
270	/// interrupted (Ctrl-C) or on a fatal bind failure. For per-session auth or
271	/// routing, drive [`accept`](Self::accept) yourself instead.
272	pub async fn serve_publish(self, origin: moq_net::origin::Consumer) -> crate::Result<()> {
273		self.with_publisher(origin).serve().await
274	}
275
276	/// Accept sessions until the listener stops, ingesting each publisher into `origin`.
277	///
278	/// The mirror of [`serve_publish`](Self::serve_publish) for the consume direction.
279	pub async fn serve_consume(self, origin: moq_net::origin::Producer) -> crate::Result<()> {
280		self.with_subscriber(origin).serve().await
281	}
282
283	/// Shared accept loop for [`serve_publish`](Self::serve_publish) /
284	/// [`serve_consume`](Self::serve_consume); the origin is already attached.
285	async fn serve(mut self) -> crate::Result<()> {
286		if let Ok(addr) = self.local_addr() {
287			tracing::info!(%addr, "listening");
288		}
289		while let Some(request) = self.accept().await {
290			tokio::spawn(async move {
291				if let Err(err) = serve_session(request).await {
292					tracing::warn!(%err, "session ended with error");
293				}
294			});
295		}
296		Ok(())
297	}
298
299	/// A live handle to the certificates this server is serving.
300	///
301	/// Use it to publish the SHA-256 fingerprints of a generated certificate at
302	/// `/certificate.sha256`, which an `http://` client pins to reach a
303	/// self-signed server. The handle tracks cert hot reloads, so hold it rather
304	/// than the values it returns.
305	///
306	/// Empty when no TLS-bearing backend is configured (e.g. a stream-only server).
307	pub fn certificates(&self) -> crate::tls::Certificates {
308		#[cfg(feature = "noq")]
309		if let Some(noq) = self.noq.as_ref() {
310			return noq.certificates();
311		}
312		#[cfg(feature = "quinn")]
313		if let Some(quinn) = self.quinn.as_ref() {
314			return quinn.certificates();
315		}
316		#[cfg(feature = "quiche")]
317		if let Some(quiche) = self.quiche.as_ref() {
318			return quiche.certificates();
319		}
320		// No QUIC backend (e.g. a stream-only `--server-bind`): no certificates.
321		crate::tls::Certificates::empty()
322	}
323
324	#[cfg(not(any(
325		feature = "noq",
326		feature = "quinn",
327		feature = "quiche",
328		feature = "iroh",
329		feature = "tcp",
330		all(feature = "uds", unix)
331	)))]
332	/// Returns the next partially established session.
333	///
334	/// Panics: no transport feature is compiled in, so nothing can be accepted.
335	pub async fn accept(&mut self) -> Option<Request> {
336		unreachable!("no transport compiled; enable a QUIC backend, tcp, or uds feature");
337	}
338
339	/// The accept-loop health of every listener this server owns that performs a real
340	/// `accept(2)`: the `tcp`/`unix` stream listeners and, if one was set,
341	/// [`with_websocket`](Self::with_websocket).
342	///
343	/// Empty on a QUIC-only server, which is the honest answer rather than a
344	/// convenient one: a QUIC backend multiplexes every session over one UDP socket,
345	/// so it never calls `accept` and has nothing that could fail this way. Publishing
346	/// a zero for it would read as a watch that is passing when it can never fire.
347	///
348	/// Available before [`listen`](Self::listen), so an owner can register these with
349	/// a metrics endpoint at startup even though the sockets bind later.
350	pub fn accept_health(&self) -> Vec<crate::accept::Health> {
351		#[allow(unused_mut)]
352		let mut health = Vec::new();
353		#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
354		health.extend(self.streams.health.iter().cloned());
355		#[cfg(feature = "websocket")]
356		health.extend(self.websocket.as_ref().map(|ws| ws.accept_health()));
357		health
358	}
359
360	/// Bind the listeners that bind lazily, so a bind failure surfaces here.
361	///
362	/// The QUIC socket is bound by [`ServerConfig::init`], but the stream
363	/// (`tcp`/`unix`) listeners need a runtime, so they wait for the first
364	/// [`accept`](Self::accept) instead. That makes a bind failure arrive as a `None`
365	/// from `accept`, which a caller cannot tell apart from an ordinary shutdown.
366	/// Call this first and the two are distinct: the error is yours to handle, and a
367	/// later `None` means the server stopped.
368	///
369	/// Idempotent: a call that fails binds nothing at all (any listener it did bind
370	/// is torn down again), so a retry starts from the same place. Optional, too:
371	/// `accept` still binds them itself, logging the failure, for a caller that
372	/// doesn't call this.
373	///
374	/// Call it after [`with_publisher`](Self::with_publisher) and friends: the stream
375	/// listeners serve what is configured at the moment they bind.
376	pub async fn listen(&mut self) -> crate::Result<()> {
377		#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
378		self.streams.ensure_started(self.moq.clone()).await?;
379		Ok(())
380	}
381
382	/// Returns the next partially established session, across every configured
383	/// transport (QUIC, WebSocket, and plaintext qmux over TCP/Unix).
384	///
385	/// This returns a [Request] instead of a session so the connection can be
386	/// rejected early on an invalid path or missing auth. Call [Request::ok] or
387	/// [Request::close] to complete the handshake.
388	///
389	/// `None` means the server stopped: it was interrupted (Ctrl-C), or a lazy
390	/// listener failed to bind. Call [`listen`](Self::listen) up front to tell those
391	/// two apart.
392	#[cfg(any(
393		feature = "noq",
394		feature = "quinn",
395		feature = "quiche",
396		feature = "iroh",
397		feature = "tcp",
398		all(feature = "uds", unix)
399	))]
400	pub async fn accept(&mut self) -> Option<Request> {
401		// Bind the stream (tcp/unix) listeners on first poll; a bind failure is
402		// fatal, mirroring how a QUIC bind failure aborts startup. They handshake
403		// with the same configured server as the QUIC arms below.
404		#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
405		if let Err(err) = self.streams.ensure_started(self.moq.clone()).await {
406			tracing::error!(%err, "failed to bind stream listener");
407			return None;
408		}
409
410		loop {
411			// tokio::select! does not support cfg directives on arms, so we need to create the futures here.
412			#[cfg(feature = "noq")]
413			let noq_accept = async {
414				#[cfg(feature = "noq")]
415				if let Some(noq) = self.noq.as_mut() {
416					return noq.accept().await;
417				}
418				None
419			};
420			#[cfg(not(feature = "noq"))]
421			let noq_accept = async { None::<()> };
422
423			#[cfg(feature = "iroh")]
424			let iroh_accept = async {
425				#[cfg(feature = "iroh")]
426				if let Some(endpoint) = self.iroh.as_mut() {
427					return endpoint.accept().await;
428				}
429				None
430			};
431			#[cfg(not(feature = "iroh"))]
432			let iroh_accept = async { None::<()> };
433
434			#[cfg(feature = "quinn")]
435			let quinn_accept = async {
436				#[cfg(feature = "quinn")]
437				if let Some(quinn) = self.quinn.as_mut() {
438					return quinn.accept().await;
439				}
440				None
441			};
442			#[cfg(not(feature = "quinn"))]
443			let quinn_accept = async { None::<()> };
444
445			#[cfg(feature = "quiche")]
446			let quiche_accept = async {
447				#[cfg(feature = "quiche")]
448				if let Some(quiche) = self.quiche.as_mut() {
449					return quiche.accept().await;
450				}
451				None
452			};
453			#[cfg(not(feature = "quiche"))]
454			let quiche_accept = async { None::<()> };
455
456			#[cfg(feature = "websocket")]
457			let ws_ref = self.websocket.as_ref();
458			#[cfg(feature = "websocket")]
459			let ws_accept = async {
460				match ws_ref {
461					Some(ws) => ws.accept().await,
462					None => std::future::pending().await,
463				}
464			};
465			#[cfg(not(feature = "websocket"))]
466			let ws_accept = std::future::pending::<Option<crate::Result<()>>>();
467
468			#[allow(unused_variables)]
469			let server = self.moq.clone();
470			#[allow(unused_variables)]
471			let versions = self.versions.clone();
472
473			// No streams configured: never resolves, so it doesn't disturb select!.
474			#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
475			let stream_accept = self.streams.recv();
476			#[cfg(not(any(feature = "tcp", all(feature = "uds", unix))))]
477			let stream_accept = std::future::pending::<Option<Request>>();
478
479			tokio::select! {
480				Some(request) = stream_accept => {
481					return Some(request);
482				}
483				Some(_conn) = noq_accept => {
484					#[cfg(feature = "noq")]
485					{
486						let alpns = versions.alpns();
487						self.accept.push(async move {
488							// Accept the transport (capturing url + mTLS identity) and exchange the
489							// MoQ SETUP up front, so path/role are known before the caller authorizes
490							// (like the stream bindings).
491							let (session, url, identity) = super::noq::accept(_conn, alpns).await?;
492							let request = server.accept_request(session).await?;
493							Ok(Request { transport: Transport::Quic, url, identity, kind: RequestKind::Noq(Box::new(request)) })
494						}.boxed());
495					}
496				}
497				Some(_conn) = quinn_accept => {
498					#[cfg(feature = "quinn")]
499					{
500						let alpns = versions.alpns();
501						self.accept.push(async move {
502							let (session, url, identity) = super::quinn::accept(_conn, alpns).await?;
503							let request = server.accept_request(session).await?;
504							Ok(Request { transport: Transport::Quic, url, identity, kind: RequestKind::Quinn(Box::new(request)) })
505						}.boxed());
506					}
507				}
508				Some(_conn) = quiche_accept => {
509					#[cfg(feature = "quiche")]
510					{
511						let alpns = versions.alpns();
512						self.accept.push(async move {
513							let (session, url, identity) = super::quiche::accept(_conn, alpns).await?;
514							let request = server.accept_request(session).await?;
515							Ok(Request { transport: Transport::Quic, url, identity, kind: RequestKind::Quiche(Box::new(request)) })
516						}.boxed());
517					}
518				}
519				Some(_conn) = iroh_accept => {
520					#[cfg(feature = "iroh")]
521					self.accept.push(async move {
522						let (session, url, identity) = super::iroh::accept(_conn).await?;
523						let request = server.accept_request(session).await?;
524						Ok(Request { transport: Transport::Iroh, url, identity, kind: RequestKind::Iroh(Box::new(request)) })
525					}.boxed());
526				}
527				Some(_res) = ws_accept => {
528					#[cfg(feature = "websocket")]
529					match _res {
530						Ok(session) => {
531							// Read the SETUP off the qmux session before handing it over, so a
532							// slow peer doesn't stall the accept loop (spawned like the others).
533							self.accept.push(async move {
534								let request = server.accept_request(session).await?;
535								Ok(Request { transport: Transport::WebSocket, url: None, identity: None, kind: RequestKind::Qmux(Box::new(request)) })
536							}.boxed());
537						}
538						// One connection's upgrade, not the listener's: a failed
539						// `accept(2)` never reaches here, having been classified,
540						// counted, and warned about by the listener itself.
541						Err(err) => tracing::debug!(%err, "WebSocket upgrade failed"),
542					}
543				}
544				Some(res) = self.accept.next() => {
545					match res {
546						Ok(session) => return Some(session),
547						Err(err) => tracing::debug!(%err, "failed to accept session"),
548					}
549				}
550				_ = tokio::signal::ctrl_c() => {
551					self.close().await;
552					return None;
553				}
554			}
555		}
556	}
557
558	/// The Iroh endpoint from [`with_iroh`](Self::with_iroh), if one was set.
559	#[cfg(feature = "iroh")]
560	pub fn iroh_endpoint(&self) -> Option<&iroh::Endpoint> {
561		self.iroh.as_ref()
562	}
563
564	/// The address the QUIC listener bound to, useful when the config asked for
565	/// port 0.
566	///
567	/// Errors with [`Error::NoBackend`] on a stream-only server, which has no
568	/// QUIC listener.
569	pub fn local_addr(&self) -> crate::Result<net::SocketAddr> {
570		#[cfg(feature = "noq")]
571		if let Some(noq) = self.noq.as_ref() {
572			return Ok(noq.local_addr()?);
573		}
574		#[cfg(feature = "quinn")]
575		if let Some(quinn) = self.quinn.as_ref() {
576			return Ok(quinn.local_addr()?);
577		}
578		#[cfg(feature = "quiche")]
579		if let Some(quiche) = self.quiche.as_ref() {
580			return Ok(quiche.local_addr()?);
581		}
582		// No QUIC backend (e.g. a stream-only `--server-bind`).
583		Err(Error::NoBackend("no QUIC listener configured"))
584	}
585
586	/// The address the WebSocket listener from
587	/// [`with_websocket`](Self::with_websocket) bound to, if one was set.
588	#[cfg(feature = "websocket")]
589	pub fn websocket_local_addr(&self) -> Option<net::SocketAddr> {
590		self.websocket.as_ref().and_then(|ws| ws.local_addr().ok())
591	}
592
593	/// Close every listener, giving in-flight connections a moment to see the
594	/// shutdown.
595	///
596	/// [`accept`](Self::accept) calls this for you on Ctrl-C.
597	pub async fn close(&mut self) {
598		#[cfg(feature = "noq")]
599		if let Some(noq) = self.noq.as_mut() {
600			noq.close();
601			tokio::time::sleep(std::time::Duration::from_millis(100)).await;
602		}
603		#[cfg(feature = "quinn")]
604		if let Some(quinn) = self.quinn.as_mut() {
605			quinn.close();
606			tokio::time::sleep(std::time::Duration::from_millis(100)).await;
607		}
608		#[cfg(feature = "quiche")]
609		if let Some(quiche) = self.quiche.as_mut() {
610			quiche.close();
611			tokio::time::sleep(std::time::Duration::from_millis(100)).await;
612		}
613		#[cfg(feature = "iroh")]
614		if let Some(iroh) = self.iroh.take() {
615			iroh.close().await;
616		}
617		#[cfg(feature = "websocket")]
618		{
619			let _ = self.websocket.take();
620		}
621		#[cfg(not(any(feature = "noq", feature = "quinn", feature = "quiche", feature = "iroh")))]
622		unreachable!("no QUIC backend compiled");
623	}
624}
625
626/// Complete one accepted [`Request`] and wait for the session to close.
627async fn serve_session(request: Request) -> crate::Result<()> {
628	let session = request.ok().await?;
629	Err(session.closed().await.into())
630}
631
632/// The version set offered on stream (`tcp://`/`unix://`) listeners.
633///
634/// A URL-less transport carries the request path in the moq-lite-05 SETUP, so
635/// lite-05 is offered on top of the configured versions even when a custom set
636/// omits it. Older versions still work for clients that need no path.
637#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
638fn stream_versions(base: &moq_net::Versions) -> moq_net::Versions {
639	let mut versions: Vec<moq_net::Version> = base.iter().copied().collect();
640	if let Ok(lite05) = "moq-lite-05".parse::<moq_net::Version>()
641		&& !versions.contains(&lite05)
642	{
643		versions.push(lite05);
644	}
645	moq_net::Versions::from(versions)
646}
647
648/// A configured stream listener (`--server-tcp-bind` / `--server-unix-bind`).
649#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
650#[derive(Clone)]
651enum StreamBind {
652	#[cfg(feature = "tcp")]
653	Tcp(net::SocketAddr),
654	#[cfg(all(feature = "uds", unix))]
655	Unix(PathBuf),
656}
657
658#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
659impl StreamBind {
660	/// The name this listener reports its accept health under.
661	fn name(&self) -> &'static str {
662		match self {
663			#[cfg(feature = "tcp")]
664			Self::Tcp(_) => "tcp",
665			#[cfg(all(feature = "uds", unix))]
666			Self::Unix(_) => "unix",
667		}
668	}
669}
670
671/// The stream (`tcp`/`unix`) listeners owned by a [`Server`].
672///
673/// Bound lazily on the first [`Server::accept`] (they need a runtime), after
674/// which each runs an accept loop in its own task and feeds completed [`Request`]s
675/// back over a channel. The tasks own their listeners and are aborted when the
676/// `Server` (and thus this) is dropped, so bound sockets don't linger.
677#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
678struct StreamListeners {
679	binds: Vec<StreamBind>,
680	/// One per entry in `binds`, in the same order, and created up front rather than
681	/// with the listener: an owner registering these with a metrics endpoint does so
682	/// at startup, long before the first `accept` binds anything.
683	health: Vec<crate::accept::Health>,
684	versions: moq_net::Versions,
685	#[cfg(all(feature = "uds", unix))]
686	unix_allow: Option<crate::unix::Allow>,
687	rx: Option<tokio::sync::mpsc::Receiver<Request>>,
688	tasks: Vec<tokio::task::JoinHandle<()>>,
689}
690
691#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
692impl StreamListeners {
693	fn new(
694		binds: Vec<StreamBind>,
695		versions: moq_net::Versions,
696		#[cfg(all(feature = "uds", unix))] unix_allow: Option<crate::unix::Allow>,
697	) -> Self {
698		let health = binds
699			.iter()
700			.map(|bind| crate::accept::Health::new(bind.name()))
701			.collect();
702		Self {
703			binds,
704			health,
705			versions,
706			#[cfg(all(feature = "uds", unix))]
707			unix_allow,
708			rx: None,
709			tasks: Vec::new(),
710		}
711	}
712
713	/// Bind the configured listeners and spawn their accept loops, once.
714	///
715	/// `server` is the [`Server`]'s configured [`moq_net::Server`], so what
716	/// [`Server::with_publisher`] and friends set applies to stream sessions too.
717	async fn ensure_started(&mut self, server: moq_net::Server) -> crate::Result<()> {
718		if self.rx.is_some() || self.binds.is_empty() {
719			return Ok(());
720		}
721
722		// Stream listeners widen the version set (see `stream_versions`), so the
723		// handshake has to offer that set rather than the server's own.
724		let server = server.with_versions(self.versions.clone());
725
726		let (tx, rx) = tokio::sync::mpsc::channel(16);
727		if let Err(err) = self.start(&server, &tx).await {
728			// All or nothing. A half-bound set would leave the loops we did spawn
729			// feeding the channel this call is about to drop, so a retry would find
730			// listeners that can never deliver a request while `binds` looked done.
731			// Abort them instead and leave the binds untouched, so a retry starts over
732			// and a second `listen` cannot report success over a dead listener.
733			for task in self.tasks.drain(..) {
734				task.abort();
735			}
736			return Err(err);
737		}
738
739		self.rx = Some(rx);
740		Ok(())
741	}
742
743	/// Bind and spawn every configured listener, or return the first failure.
744	async fn start(&mut self, server: &moq_net::Server, tx: &tokio::sync::mpsc::Sender<Request>) -> crate::Result<()> {
745		// Cloned so the loop can push into `self.tasks` while iterating; there are at
746		// most two entries, each an address or a path.
747		let binds = self.binds.clone();
748		let health = self.health.clone();
749		for (bind, health) in binds.into_iter().zip(health) {
750			let alpns = self.versions.alpns();
751			match bind {
752				#[cfg(feature = "tcp")]
753				StreamBind::Tcp(addr) => {
754					if !addr.ip().is_loopback() {
755						tracing::warn!(%addr, "tcp listener bound to a non-loopback address; qmux is UNENCRYPTED, ensure the network is trusted");
756					}
757					let listener = crate::tcp::Listener::bind(addr)
758						.await?
759						.with_protocols(alpns)
760						.with_accept_health(health);
761					tracing::info!(%addr, "listening (tcp)");
762					self.tasks.push(spawn_tcp_loop(listener, server.clone(), tx.clone()));
763				}
764				#[cfg(all(feature = "uds", unix))]
765				StreamBind::Unix(path) => {
766					let listener = crate::unix::Listener::bind(&path)
767						.await?
768						.with_protocols(alpns)
769						.with_accept_health(health);
770					// Loose file perms: the uid/gid/pid allow list is the real gate,
771					// and the worker usually runs as a different user than the server.
772					listener.set_mode(0o666)?;
773					tracing::info!(path = %path.display(), allow = ?self.unix_allow, "listening (unix)");
774					self.tasks.push(spawn_unix_loop(
775						listener,
776						server.clone(),
777						self.unix_allow.clone(),
778						tx.clone(),
779					));
780				}
781			}
782		}
783
784		Ok(())
785	}
786
787	/// Yield the next stream [`Request`], or pend forever if none are running.
788	async fn recv(&mut self) -> Option<Request> {
789		match self.rx.as_mut() {
790			Some(rx) => rx.recv().await,
791			None => std::future::pending().await,
792		}
793	}
794}
795
796#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
797impl Drop for StreamListeners {
798	fn drop(&mut self) {
799		// Stop the accept loops so their listeners (and bound sockets) are freed.
800		for task in &self.tasks {
801			task.abort();
802		}
803	}
804}
805
806#[cfg(feature = "tcp")]
807fn spawn_tcp_loop(
808	listener: crate::tcp::Listener,
809	server: moq_net::Server,
810	tx: tokio::sync::mpsc::Sender<Request>,
811) -> tokio::task::JoinHandle<()> {
812	tokio::spawn(async move {
813		loop {
814			match listener.accept().await {
815				Some(Ok(session)) => spawn_stream_request(session, Transport::Tcp, server.clone(), tx.clone()),
816				// Per-connection: a failed `accept(2)` is the listener's own to
817				// classify and pace, and never surfaces here.
818				Some(Err(err)) => tracing::warn!(%err, "tcp qmux handshake failed"),
819				None => break,
820			}
821		}
822	})
823}
824
825#[cfg(all(feature = "uds", unix))]
826fn spawn_unix_loop(
827	listener: crate::unix::Listener,
828	server: moq_net::Server,
829	allow: Option<crate::unix::Allow>,
830	tx: tokio::sync::mpsc::Sender<Request>,
831) -> tokio::task::JoinHandle<()> {
832	tokio::spawn(async move {
833		loop {
834			match listener.accept().await {
835				Some(Ok((session, cred))) => {
836					// Enforce the allowlist (if any) before reading SETUP bytes from the peer.
837					if let Some(allow) = &allow
838						&& !allow.permits(&cred)
839					{
840						tracing::warn!(uid = cred.uid, gid = cred.gid, pid = ?cred.pid, "unix connection rejected by allow list");
841						continue;
842					}
843					spawn_stream_request(session, Transport::Unix, server.clone(), tx.clone());
844				}
845				// Per-connection, as in `spawn_tcp_loop`.
846				Some(Err(err)) => tracing::warn!(%err, "unix qmux handshake failed"),
847				None => break,
848			}
849		}
850	})
851}
852
853/// Read the SETUP from an accepted stream session (concurrently, so one slow or
854/// malicious peer doesn't stall the listener) and forward the resulting request.
855#[cfg(any(feature = "tcp", all(feature = "uds", unix)))]
856fn spawn_stream_request(
857	session: qmux::Session,
858	transport: Transport,
859	server: moq_net::Server,
860	tx: tokio::sync::mpsc::Sender<Request>,
861) {
862	tokio::spawn(async move {
863		match server.accept_request(session).await {
864			Ok(request) => {
865				let request = Request {
866					transport,
867					url: None,
868					identity: None,
869					kind: RequestKind::Qmux(Box::new(request)),
870				};
871				let _ = tx.send(request).await;
872			}
873			Err(err) => tracing::debug!(%err, "stream SETUP handshake failed"),
874		}
875	});
876}
877
878/// An accepted connection whose MoQ SETUP has already been exchanged.
879///
880/// Every backend drives the transport connect *and* the MoQ handshake up front, so the
881/// [`path`](Request::path)/[`role`](Request::role) a client advertised are available on
882/// every transport before the caller authorizes. The variant only distinguishes the
883/// underlying session type; all of them delegate identically.
884pub(crate) enum RequestKind {
885	#[cfg(feature = "noq")]
886	Noq(Box<moq_net::Request<web_transport_noq::Session>>),
887	#[cfg(feature = "quinn")]
888	Quinn(Box<moq_net::Request<web_transport_quinn::Session>>),
889	#[cfg(feature = "quiche")]
890	Quiche(Box<moq_net::Request<web_transport_quiche::Connection>>),
891	#[cfg(feature = "iroh")]
892	Iroh(Box<moq_net::Request<web_transport_iroh::Session>>),
893	#[cfg(any(feature = "tcp", all(feature = "uds", unix), feature = "websocket"))]
894	Qmux(Box<moq_net::Request<qmux::Session>>),
895}
896
897/// The network transport carrying an incoming MoQ session.
898#[non_exhaustive]
899#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
900pub enum Transport {
901	/// QUIC, either directly or through WebTransport over HTTP/3.
902	Quic,
903	/// An Iroh QUIC connection.
904	Iroh,
905	/// A WebSocket connection using qmux framing.
906	WebSocket,
907	/// A plaintext TCP connection using qmux framing.
908	Tcp,
909	/// A Unix domain socket using qmux framing.
910	Unix,
911}
912
913impl Transport {
914	/// Returns the stable lowercase name used in logs and external metadata.
915	pub const fn as_str(self) -> &'static str {
916		match self {
917			Self::Quic => "quic",
918			Self::Iroh => "iroh",
919			Self::WebSocket => "websocket",
920			Self::Tcp => "tcp",
921			Self::Unix => "unix",
922		}
923	}
924}
925
926impl std::fmt::Display for Transport {
927	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
928		f.write_str(self.as_str())
929	}
930}
931
932/// An incoming MoQ session that can be accepted or rejected.
933///
934/// The transport connection and the MoQ SETUP are already complete, so [`path`](Self::path),
935/// [`role`](Self::role), [`url`](Self::url), and [`peer_identity`](Self::peer_identity) are
936/// all populated consistently regardless of transport. [Self::with_publisher] and
937/// [Self::with_subscriber] configure what is published and subscribed to on the session;
938/// otherwise the Server's configuration is used by default. Call [Self::ok] to start the
939/// session, or [Self::close] to reject it (which closes the just-established session).
940pub struct Request {
941	transport: Transport,
942	/// The dial URL, for transports that carry one (QUIC/WebTransport). `None` for the
943	/// URL-less stream bindings, whose request path rides the SETUP instead.
944	url: Option<Url>,
945	/// The peer's validated mTLS identity, captured at the transport handshake (before
946	/// the MoQ SETUP), when the backend supports it.
947	identity: Option<crate::tls::PeerIdentity>,
948	kind: RequestKind,
949}
950
951/// Delegate a read-only call to the inner [`moq_net::Request`], whatever the transport.
952macro_rules! request_ref {
953	($self:expr, $r:ident => $body:expr) => {
954		match &$self.kind {
955			#[cfg(feature = "noq")]
956			RequestKind::Noq($r) => $body,
957			#[cfg(feature = "quinn")]
958			RequestKind::Quinn($r) => $body,
959			#[cfg(feature = "quiche")]
960			RequestKind::Quiche($r) => $body,
961			#[cfg(feature = "iroh")]
962			RequestKind::Iroh($r) => $body,
963			#[cfg(any(feature = "tcp", all(feature = "uds", unix), feature = "websocket"))]
964			RequestKind::Qmux($r) => $body,
965		}
966	};
967}
968
969/// Delegate a consuming call whose arms all yield the same type (e.g. `ok`, `close`).
970macro_rules! request_into {
971	($kind:expr, $r:ident => $body:expr) => {
972		match $kind {
973			#[cfg(feature = "noq")]
974			RequestKind::Noq($r) => $body,
975			#[cfg(feature = "quinn")]
976			RequestKind::Quinn($r) => $body,
977			#[cfg(feature = "quiche")]
978			RequestKind::Quiche($r) => $body,
979			#[cfg(feature = "iroh")]
980			RequestKind::Iroh($r) => $body,
981			#[cfg(any(feature = "tcp", all(feature = "uds", unix), feature = "websocket"))]
982			RequestKind::Qmux($r) => $body,
983		}
984	};
985}
986
987/// Delegate a consuming builder call, rebuilding the same variant from the returned request.
988macro_rules! request_map {
989	($kind:expr, $r:ident => $body:expr) => {
990		match $kind {
991			#[cfg(feature = "noq")]
992			RequestKind::Noq($r) => RequestKind::Noq(Box::new($body)),
993			#[cfg(feature = "quinn")]
994			RequestKind::Quinn($r) => RequestKind::Quinn(Box::new($body)),
995			#[cfg(feature = "quiche")]
996			RequestKind::Quiche($r) => RequestKind::Quiche(Box::new($body)),
997			#[cfg(feature = "iroh")]
998			RequestKind::Iroh($r) => RequestKind::Iroh(Box::new($body)),
999			#[cfg(any(feature = "tcp", all(feature = "uds", unix), feature = "websocket"))]
1000			RequestKind::Qmux($r) => RequestKind::Qmux(Box::new($body)),
1001		}
1002	};
1003}
1004
1005impl Request {
1006	/// Reject the session. The transport is already accepted, so this closes the
1007	/// just-established MoQ session rather than answering the transport handshake:
1008	/// the `code` (an HTTP-style status the caller passes) maps to a MoQ close reason.
1009	pub async fn close(self, code: u16) -> crate::Result<()> {
1010		let err = match code {
1011			401 | 403 => moq_net::Error::Unauthorized,
1012			other => moq_net::Error::App(other),
1013		};
1014		request_into!(self.kind, request => request.close(err));
1015		Ok(())
1016	}
1017
1018	/// Publish the given origin to the session.
1019	pub fn with_publisher(self, publish: impl moq_net::Consume<moq_net::origin::Consumer>) -> Self {
1020		let Request {
1021			transport,
1022			url,
1023			identity,
1024			kind,
1025		} = self;
1026		let kind = request_map!(kind, request => request.with_publisher(publish));
1027		Request {
1028			transport,
1029			url,
1030			identity,
1031			kind,
1032		}
1033	}
1034
1035	/// Subscribe to the given origin from the session.
1036	pub fn with_subscriber(self, subscribe: moq_net::origin::Producer) -> Self {
1037		let Request {
1038			transport,
1039			url,
1040			identity,
1041			kind,
1042		} = self;
1043		let kind = request_map!(kind, request => request.with_subscriber(subscribe));
1044		Request {
1045			transport,
1046			url,
1047			identity,
1048			kind,
1049		}
1050	}
1051
1052	/// Attach a per-connection [`moq_net::stats::Session`] context to this session.
1053	pub fn with_stats(self, stats: moq_net::stats::Session) -> Self {
1054		let Request {
1055			transport,
1056			url,
1057			identity,
1058			kind,
1059		} = self;
1060		let kind = request_map!(kind, request => request.with_stats(stats));
1061		Request {
1062			transport,
1063			url,
1064			identity,
1065			kind,
1066		}
1067	}
1068
1069	/// Accept the session, starting the MoQ session loops.
1070	pub async fn ok(self) -> crate::Result<Session> {
1071		let pair = request_into!(self.kind, request => request.ok().await?);
1072		Ok(crate::spawn_session(pair))
1073	}
1074
1075	/// Returns the network transport carrying this session.
1076	pub fn transport(&self) -> Transport {
1077		self.transport
1078	}
1079
1080	/// Returns the URL the client dialed, for transports that carry one (QUIC/WebTransport).
1081	///
1082	/// `None` for the URL-less stream bindings (`tcp`/`unix`); use [`Self::path`] for their
1083	/// in-band request path.
1084	pub fn url(&self) -> Option<&Url> {
1085		self.url.as_ref()
1086	}
1087
1088	/// The request path the client advertised, uniform across transports.
1089	///
1090	/// Taken from the SETUP for the URL-less stream bindings (and moq-transport, which
1091	/// carries it in-band), and from the dial [`url`](Self::url) for WebTransport/QUIC.
1092	/// Empty only when neither carries one.
1093	pub fn path(&self) -> &str {
1094		// An empty SETUP path means the client advertised none, so fall back to the
1095		// dial URL. URI-carrying bindings are the ones that must not send a path at
1096		// all, so this never discards a path the client meant us to use.
1097		let setup = request_ref!(self, r => r.path());
1098		if setup.is_empty() {
1099			self.url.as_ref().map(Url::path).unwrap_or("")
1100		} else {
1101			setup
1102		}
1103	}
1104
1105	/// The single direction the client advertised in its SETUP, or `None` for a
1106	/// bidirectional session (it omitted the role, or the version carries none).
1107	/// Available on every transport. Use it to reject a token that lacks the scope for
1108	/// the client's intended direction.
1109	pub fn role(&self) -> Option<moq_net::Role> {
1110		request_ref!(self, r => r.role())
1111	}
1112
1113	/// The origin identity the peer declared in its SETUP (moq-lite-05+).
1114	///
1115	/// A peer declares this when it attaches a publish or subscribe origin.
1116	/// Older versions and peers without one return `None`.
1117	///
1118	/// Self-declared, so treat it as a correlation hint rather than an
1119	/// authenticated identity: authorize on the token or client certificate.
1120	pub fn peer_origin(&self) -> Option<moq_net::Origin> {
1121		request_ref!(self, r => r.peer_origin())
1122	}
1123
1124	/// The client certificate chain the peer presented, if any, validated
1125	/// against a configured [`crate::tls::Server::root`] during the handshake.
1126	///
1127	/// Captured at the transport handshake (before the SETUP). Only the Quinn and noq
1128	/// backends support mTLS; other transports always return `None`. Use it to grant
1129	/// elevated access or to close the session once the certificate expires (see
1130	/// [`crate::tls::PeerIdentity::expiry`]).
1131	pub fn peer_identity(&self) -> Option<crate::tls::PeerIdentity> {
1132		self.identity.clone()
1133	}
1134
1135	#[doc(hidden)]
1136	#[deprecated(note = "use `peer_identity` instead")]
1137	pub fn has_peer_certificate(&self) -> bool {
1138		self.peer_identity().is_some()
1139	}
1140}
1141
1142#[cfg(test)]
1143mod tests {
1144	use super::*;
1145
1146	/// The handles have to exist before anything binds, and cover the stream
1147	/// listeners rather than just the ones an owner happens to construct itself.
1148	///
1149	/// `tcp`/`unix` bind lazily on the first `accept`, so a naive implementation
1150	/// hands out nothing at startup, which is exactly when a metrics endpoint is
1151	/// assembled. A stream-only node would then publish no accept counters for the
1152	/// only sockets on it that can fail.
1153	#[cfg(feature = "tcp")]
1154	#[test]
1155	fn accept_health_covers_stream_listeners_before_they_bind() {
1156		let mut config = ServerConfig::default();
1157		config.tcp.bind = Some("127.0.0.1:0".parse().unwrap());
1158		let server = Server::new(config).expect("stream-only server");
1159
1160		let names: Vec<_> = server.accept_health().iter().map(|h| h.listener()).collect();
1161		assert_eq!(names, vec!["tcp"], "the tcp listener must report before it binds");
1162	}
1163
1164	/// A failed `listen` must leave nothing bound, so a retry starts over.
1165	///
1166	/// The trap is `binds.drain(..)`: consume the list up front and a partial failure
1167	/// leaves it empty, so the *second* `listen` sees nothing left to do and reports
1168	/// success while no stream listener exists and `accept` parks forever.
1169	#[cfg(all(feature = "tcp", feature = "uds", unix))]
1170	#[tokio::test]
1171	async fn a_failed_listen_binds_nothing_and_can_be_retried() {
1172		// A path that cannot be a socket, so the unix bind fails after the tcp one
1173		// has already succeeded.
1174		let dir = tempfile::TempDir::new().unwrap();
1175		let occupied = dir.path().join("not-a-socket");
1176		std::fs::write(&occupied, b"in the way").unwrap();
1177
1178		let mut config = ServerConfig::default();
1179		config.tcp.bind = Some("127.0.0.1:0".parse().unwrap());
1180		config.unix.bind = Some(occupied);
1181		let mut server = Server::new(config).expect("stream-only server");
1182
1183		assert!(server.listen().await.is_err(), "the unix bind must fail");
1184		// Same error the second time, rather than a success over a listener that the
1185		// first call already tore down.
1186		assert!(server.listen().await.is_err(), "a retry must not report success");
1187	}
1188
1189	/// A QUIC-only server reports nothing. It multiplexes over one UDP socket and
1190	/// never calls `accept`, so a zero counter would be a watch that cannot fire.
1191	#[cfg(all(feature = "quinn", not(feature = "tcp")))]
1192	#[test]
1193	fn accept_health_is_empty_without_a_stream_listener() {
1194		let server = ServerConfig::default().init().expect("quic server");
1195		assert!(server.accept_health().is_empty());
1196	}
1197
1198	#[test]
1199	fn transport_names_are_stable() {
1200		assert_eq!(Transport::Quic.as_str(), "quic");
1201		assert_eq!(Transport::Iroh.as_str(), "iroh");
1202		assert_eq!(Transport::WebSocket.as_str(), "websocket");
1203		assert_eq!(Transport::Tcp.as_str(), "tcp");
1204		assert_eq!(Transport::Unix.as_str(), "unix");
1205	}
1206
1207	/// Building the endpoint needs a runtime, and `certificates()` must stay
1208	/// readable without one (no guard escapes to the caller).
1209	#[cfg(feature = "quinn")]
1210	#[tokio::test]
1211	async fn certificates_expose_generated_fingerprints() {
1212		let mut config = ServerConfig {
1213			bind: Some("[::]:0".to_string()),
1214			..Default::default()
1215		};
1216		config.tls.generate = vec!["localhost".into()];
1217
1218		let certs = config.init().expect("server init").certificates();
1219		let fingerprints = certs.fingerprints();
1220		assert_eq!(fingerprints.len(), 1, "one generated certificate");
1221		// Hex-encoded SHA-256.
1222		assert_eq!(fingerprints[0].len(), 64);
1223		assert!(fingerprints[0].chars().all(|c| c.is_ascii_hexdigit()));
1224	}
1225
1226	/// The stream listeners must hand accepted sessions to the *configured*
1227	/// [`moq_net::Server`]. [`Server::serve_publish`] sets the publisher there
1228	/// rather than on the request, so a session that handshakes against any other
1229	/// server accepts and then serves nothing.
1230	#[cfg(all(feature = "uds", unix))]
1231	#[tokio::test]
1232	async fn unix_listener_serves_the_configured_publisher() {
1233		use rand::RngExt;
1234
1235		// macOS caps AF_UNIX paths near 104 bytes and the system temp dir is long,
1236		// so bind under /tmp with a name unique to this process.
1237		let path = PathBuf::from(format!("/tmp/moq-native-publish-{}.sock", std::process::id()));
1238		let _ = std::fs::remove_file(&path);
1239
1240		let origin = moq_net::Origin::random().produce();
1241		let mut broadcast = origin
1242			.create_broadcast("test", moq_net::broadcast::Route::new().with_announce(true))
1243			.expect("create broadcast");
1244		let mut track = broadcast.create_track("video", None).expect("create track");
1245		let mut group = track.append_group().expect("append group");
1246		group
1247			.write_frame(moq_net::Timestamp::ZERO, b"hello".as_ref())
1248			.expect("write frame");
1249		group.finish().expect("finish group");
1250
1251		let mut config = ServerConfig::default();
1252		config.unix.bind = Some(path.clone());
1253		let server = config.init().expect("server init");
1254
1255		// The publisher lives on the server, never on the accepted request.
1256		let serve = tokio::spawn(server.serve_publish(origin.consume()));
1257
1258		// The listener binds on the first accept, so wait for the socket. Keep the
1259		// last error: a bind failure is logged and swallowed, so it's the only clue
1260		// to why the socket never showed up.
1261		const MAX_DELAY: std::time::Duration = std::time::Duration::from_millis(100);
1262		let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(5);
1263		let mut delay = std::time::Duration::from_millis(1);
1264		while let Err(err) = tokio::net::UnixStream::connect(&path).await {
1265			assert!(
1266				tokio::time::Instant::now() < deadline,
1267				"unix listener never bound: {err}"
1268			);
1269			tokio::time::sleep(delay.mul_f64(0.5 + rand::rng().random::<f64>() / 2.0)).await;
1270			delay = (delay * 2).min(MAX_DELAY);
1271		}
1272
1273		const TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
1274
1275		let url: Url = format!("unix://{}", path.display()).parse().expect("parse url");
1276		let subscriber = moq_net::Origin::random().produce();
1277		let mut announced = subscriber.consume().announced();
1278		let client = crate::ClientConfig::default()
1279			.init()
1280			.expect("client init")
1281			.with_subscriber(subscriber);
1282		let session = tokio::time::timeout(TIMEOUT, client.connect(url))
1283			.await
1284			.expect("connect timeout")
1285			.expect("connect");
1286
1287		// Without the server's publisher the session announces nothing, so this is
1288		// where the regression shows up.
1289		let update = tokio::time::timeout(TIMEOUT, announced.next())
1290			.await
1291			.expect("announce timeout")
1292			.expect("origin closed");
1293		assert_eq!(update.path.as_str(), "test");
1294		let broadcast = update.broadcast.expect("expected an announce");
1295
1296		let mut track = broadcast
1297			.track("video")
1298			.expect("track name")
1299			.subscribe(None)
1300			.await
1301			.expect("subscribe");
1302		let mut group = tokio::time::timeout(TIMEOUT, track.recv_group())
1303			.await
1304			.expect("recv group timeout")
1305			.expect("recv group")
1306			.expect("track closed early");
1307		let frame = tokio::time::timeout(TIMEOUT, group.read_frame())
1308			.await
1309			.expect("read frame timeout")
1310			.expect("read frame")
1311			.expect("group closed early");
1312		assert_eq!(&frame.payload[..], b"hello");
1313
1314		drop(session);
1315		serve.abort();
1316		let _ = std::fs::remove_file(&path);
1317	}
1318
1319	/// A stream-only server has no TLS backend, so there's nothing to pin. This
1320	/// must report empty rather than panic.
1321	#[cfg(all(feature = "uds", unix))]
1322	#[tokio::test]
1323	async fn certificates_are_empty_without_a_tls_backend() {
1324		let mut config = ServerConfig::default();
1325		config.unix.bind = Some(PathBuf::from("/tmp/moq-native-certificates-test.sock"));
1326
1327		let server = config.init().expect("server init");
1328		assert!(server.certificates().fingerprints().is_empty());
1329	}
1330
1331	#[test]
1332	fn test_tls_string_or_array() {
1333		// Single string should deserialize into a Vec with one entry.
1334		let single = r#"
1335			cert = "cert.pem"
1336			key = "key.pem"
1337		"#;
1338		let config: crate::tls::Server = toml::from_str(single).unwrap();
1339		assert_eq!(config.cert, vec![PathBuf::from("cert.pem")]);
1340		assert_eq!(config.key, vec![PathBuf::from("key.pem")]);
1341
1342		// TOML arrays should still work.
1343		let array = r#"
1344			cert = ["a.pem", "b.pem"]
1345			key = ["a.key", "b.key"]
1346			generate = ["localhost"]
1347			root = ["ca.pem"]
1348		"#;
1349		let config: crate::tls::Server = toml::from_str(array).unwrap();
1350		assert_eq!(config.cert, vec![PathBuf::from("a.pem"), PathBuf::from("b.pem")]);
1351		assert_eq!(config.key, vec![PathBuf::from("a.key"), PathBuf::from("b.key")]);
1352		assert_eq!(config.generate, vec!["localhost".to_string()]);
1353		assert_eq!(config.root, vec![PathBuf::from("ca.pem")]);
1354	}
1355
1356	#[test]
1357	fn bind_string_or_listen_alias() {
1358		// The QUIC bind is a plain address; the `listen` alias still works.
1359		let bind: ServerConfig = toml::from_str(r#"bind = "[::]:443""#).unwrap();
1360		assert_eq!(bind.bind.as_deref(), Some("[::]:443"));
1361
1362		let alias: ServerConfig = toml::from_str(r#"listen = "0.0.0.0:4443""#).unwrap();
1363		assert_eq!(alias.bind.as_deref(), Some("0.0.0.0:4443"));
1364	}
1365
1366	#[cfg(all(feature = "uds", unix))]
1367	#[test]
1368	fn stream_listener_config_parses() {
1369		let config: ServerConfig = toml::from_str(
1370			r#"
1371bind = "[::]:443"
1372
1373[unix]
1374bind = "/run/moq.sock"
1375
1376[unix.allow]
1377uid = [1001, 1002]
1378"#,
1379		)
1380		.unwrap();
1381		assert_eq!(config.bind.as_deref(), Some("[::]:443"));
1382		assert_eq!(config.unix.bind.as_deref(), Some(std::path::Path::new("/run/moq.sock")));
1383		assert_eq!(config.unix.allow.as_ref().expect("allow").uid, vec![1001, 1002]);
1384		assert!(config.has_stream_listener());
1385	}
1386
1387	#[cfg(all(feature = "uds", unix))]
1388	#[test]
1389	fn stream_only_config_has_no_quic() {
1390		// A unix listener with no `--server-bind` is stream-only.
1391		let mut config = ServerConfig::default();
1392		config.unix.bind = Some(PathBuf::from("/run/moq.sock"));
1393		assert!(config.has_stream_listener());
1394		assert!(config.bind.is_none());
1395
1396		// The default (nothing configured) still runs QUIC.
1397		assert!(!ServerConfig::default().has_stream_listener());
1398	}
1399}