1use std::io;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use iroh::endpoint::{RecvStream, SendStream};
6use iroh::{Endpoint, RelayMode, SecretKey, Watcher};
7use iroh_tickets::endpoint::EndpointTicket;
8use tokio::io::{AsyncRead, AsyncWrite};
9use tokio::net::TcpListener;
10
11#[cfg(unix)]
12use tokio::net::UnixListener;
13#[cfg(unix)]
14#[cfg(test)]
15use tokio::net::UnixStream;
16
17#[cfg(windows)]
18mod win_uds_compat {
19 use std::io;
20 use std::pin::Pin;
21 use std::task::{Context, Poll};
22 use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
23 use win_uds::net::{AsyncListener, AsyncStream};
24
25 pub struct WinUnixStream(tokio_util::compat::Compat<AsyncStream>);
27
28 impl WinUnixStream {
29 pub async fn connect<P: AsRef<std::path::Path>>(path: P) -> io::Result<Self> {
30 use tokio_util::compat::FuturesAsyncReadCompatExt;
31 let stream: AsyncStream = AsyncStream::connect(path).await?;
32 Ok(Self(stream.compat()))
33 }
34 }
35
36 impl AsyncRead for WinUnixStream {
37 fn poll_read(
38 mut self: Pin<&mut Self>,
39 cx: &mut Context<'_>,
40 buf: &mut ReadBuf<'_>,
41 ) -> Poll<io::Result<()>> {
42 Pin::new(&mut self.0).poll_read(cx, buf)
43 }
44 }
45
46 impl AsyncWrite for WinUnixStream {
47 fn poll_write(
48 mut self: Pin<&mut Self>,
49 cx: &mut Context<'_>,
50 buf: &[u8],
51 ) -> Poll<io::Result<usize>> {
52 Pin::new(&mut self.0).poll_write(cx, buf)
53 }
54
55 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
56 Pin::new(&mut self.0).poll_flush(cx)
57 }
58
59 fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
60 Pin::new(&mut self.0).poll_shutdown(cx)
61 }
62 }
63
64 pub struct WinUnixListener {
66 inner: AsyncListener,
67 path: std::path::PathBuf,
68 }
69
70 impl WinUnixListener {
71 pub fn bind<P: AsRef<std::path::Path>>(path: P) -> io::Result<Self> {
72 let path_buf = path.as_ref().to_path_buf();
73 Ok(Self {
74 inner: AsyncListener::bind(path)?,
75 path: path_buf,
76 })
77 }
78
79 pub async fn accept(&self) -> io::Result<(WinUnixStream, ())> {
80 use tokio_util::compat::FuturesAsyncReadCompatExt;
81 let (stream, _addr): (AsyncStream, _) = self.inner.accept().await?;
82 Ok((WinUnixStream(stream.compat()), ()))
83 }
84
85 pub fn local_addr(&self) -> io::Result<std::path::PathBuf> {
86 Ok(self.path.clone())
87 }
88 }
89}
90
91#[cfg(windows)]
92use win_uds_compat::WinUnixListener as UnixListener;
93#[cfg(windows)]
94pub use win_uds_compat::WinUnixStream;
95
96#[cfg(test)]
97use tokio::net::TcpStream;
98
99pub const ALPN: &[u8] = b"XS/1.0";
101
102pub const HANDSHAKE: [u8; 5] = *b"xs..!";
105
106fn is_windows_path(s: &str) -> bool {
108 let bytes = s.as_bytes();
109 bytes.len() >= 3
110 && bytes[0].is_ascii_alphabetic()
111 && bytes[1] == b':'
112 && (bytes[2] == b'\\' || bytes[2] == b'/')
113}
114
115fn get_or_create_secret() -> io::Result<SecretKey> {
118 match std::env::var("IROH_SECRET") {
119 Ok(secret) => {
120 use std::str::FromStr;
121 SecretKey::from_str(&secret).map_err(|e| {
122 io::Error::new(
123 io::ErrorKind::InvalidData,
124 format!("Invalid secret key: {e}"),
125 )
126 })
127 }
128 Err(_) => {
129 let key = SecretKey::generate();
130 tracing::info!(
131 "Generated new secret key: {}",
132 data_encoding::HEXLOWER.encode(&key.to_bytes())
133 );
134 Ok(key)
135 }
136 }
137}
138
139fn iroh_relay_disabled() -> bool {
143 std::env::var("IROH_RELAY_MODE").as_deref() == Ok("disabled")
144}
145
146pub(crate) async fn bind_iroh_endpoint(
147 alpns: Vec<Vec<u8>>,
148 secret_key: SecretKey,
149) -> Result<Endpoint, Box<dyn std::error::Error + Send + Sync>> {
150 if iroh_relay_disabled() {
151 Endpoint::builder(iroh::endpoint::presets::Minimal)
152 .alpns(alpns)
153 .relay_mode(RelayMode::Disabled)
154 .secret_key(secret_key)
155 .bind()
156 .await
157 .map_err(|e| e.into())
158 } else {
159 Endpoint::builder(iroh::endpoint::presets::N0)
160 .alpns(alpns)
161 .relay_mode(RelayMode::Default)
162 .secret_key(secret_key)
163 .bind()
164 .await
165 .map_err(|e| e.into())
166 }
167}
168
169pub trait AsyncReadWrite: AsyncRead + AsyncWrite {}
170
171impl<T: AsyncRead + AsyncWrite> AsyncReadWrite for T {}
172
173pub type AsyncReadWriteBox = Box<dyn AsyncReadWrite + Unpin + Send>;
174
175pub struct IrohStream {
176 send_stream: SendStream,
177 recv_stream: RecvStream,
178 _conn: iroh::endpoint::Connection,
182 _endpoint: Option<Endpoint>,
183}
184
185impl IrohStream {
186 pub fn new(
187 send_stream: SendStream,
188 recv_stream: RecvStream,
189 conn: iroh::endpoint::Connection,
190 endpoint: Option<Endpoint>,
191 ) -> Self {
192 Self {
193 send_stream,
194 recv_stream,
195 _conn: conn,
196 _endpoint: endpoint,
197 }
198 }
199}
200
201impl Drop for IrohStream {
202 fn drop(&mut self) {
203 self.send_stream.reset(0u8.into()).ok();
205 self.recv_stream.stop(0u8.into()).ok();
206
207 tracing::debug!("IrohStream dropped with cleanup");
208 }
209}
210
211impl AsyncRead for IrohStream {
212 fn poll_read(
213 self: Pin<&mut Self>,
214 cx: &mut Context<'_>,
215 buf: &mut tokio::io::ReadBuf<'_>,
216 ) -> Poll<io::Result<()>> {
217 let this = self.get_mut();
218 match Pin::new(&mut this.recv_stream).poll_read(cx, buf) {
219 Poll::Ready(Ok(())) => Poll::Ready(Ok(())),
220 Poll::Ready(Err(e)) => Poll::Ready(Err(io::Error::other(e))),
221 Poll::Pending => Poll::Pending,
222 }
223 }
224}
225
226impl AsyncWrite for IrohStream {
227 fn poll_write(
228 self: Pin<&mut Self>,
229 cx: &mut Context<'_>,
230 buf: &[u8],
231 ) -> Poll<io::Result<usize>> {
232 let this = self.get_mut();
233 match Pin::new(&mut this.send_stream).poll_write(cx, buf) {
234 Poll::Ready(Ok(n)) => Poll::Ready(Ok(n)),
235 Poll::Ready(Err(e)) => Poll::Ready(Err(io::Error::other(e))),
236 Poll::Pending => Poll::Pending,
237 }
238 }
239
240 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
241 let this = self.get_mut();
242 match Pin::new(&mut this.send_stream).poll_flush(cx) {
243 Poll::Ready(Ok(())) => Poll::Ready(Ok(())),
244 Poll::Ready(Err(e)) => Poll::Ready(Err(io::Error::other(e))),
245 Poll::Pending => Poll::Pending,
246 }
247 }
248
249 fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
250 let this = self.get_mut();
251 match Pin::new(&mut this.send_stream).poll_shutdown(cx) {
252 Poll::Ready(Ok(())) => Poll::Ready(Ok(())),
253 Poll::Ready(Err(e)) => Poll::Ready(Err(io::Error::other(e))),
254 Poll::Pending => Poll::Pending,
255 }
256 }
257}
258
259pub enum Listener {
260 Tcp(TcpListener),
261 Unix(UnixListener),
262 Iroh(Endpoint, String), }
264
265impl Listener {
266 pub async fn accept(
267 &mut self,
268 ) -> io::Result<(AsyncReadWriteBox, Option<std::net::SocketAddr>)> {
269 match self {
270 Listener::Tcp(listener) => {
271 let (stream, addr) = listener.accept().await?;
272 Ok((Box::new(stream), Some(addr)))
273 }
274 Listener::Unix(listener) => {
275 let (stream, _) = listener.accept().await?;
276 Ok((Box::new(stream), None))
277 }
278 Listener::Iroh(endpoint, _) => {
279 let incoming = endpoint.accept().await.ok_or_else(|| {
281 tracing::error!("No incoming iroh connection available");
282 io::Error::other("No incoming connection")
283 })?;
284
285 let conn = incoming.await.map_err(|e| {
286 tracing::error!("Failed to accept iroh connection: {}", e);
287 io::Error::other(format!("Connection failed: {e}"))
288 })?;
289
290 let remote_node_id = "unknown"; tracing::info!("Got iroh connection from {}", remote_node_id);
292
293 let (send_stream, mut recv_stream) = conn.accept_bi().await.map_err(|e| {
295 tracing::error!(
296 "Failed to accept bidirectional stream from {}: {}",
297 remote_node_id,
298 e
299 );
300 io::Error::other(format!("Failed to accept stream: {e}"))
301 })?;
302
303 tracing::debug!("Accepted bidirectional stream from {}", remote_node_id);
304
305 let mut handshake_buf = [0u8; HANDSHAKE.len()];
307 #[allow(unused_imports)]
308 use tokio::io::AsyncReadExt;
309 recv_stream
310 .read_exact(&mut handshake_buf)
311 .await
312 .map_err(|e| {
313 tracing::error!("Failed to read handshake from {}: {}", remote_node_id, e);
314 io::Error::other(format!("Failed to read handshake: {e}"))
315 })?;
316
317 if handshake_buf != HANDSHAKE {
318 tracing::error!(
319 "Invalid handshake received from {}: expected {:?}, got {:?}",
320 remote_node_id,
321 HANDSHAKE,
322 handshake_buf
323 );
324 return Err(io::Error::new(
325 io::ErrorKind::InvalidData,
326 format!("Invalid handshake from {remote_node_id}"),
327 ));
328 }
329
330 tracing::info!("Handshake verified successfully from {}", remote_node_id);
331
332 let stream = IrohStream::new(send_stream, recv_stream, conn, None);
333 Ok((Box::new(stream), None))
334 }
335 }
336 }
337
338 pub async fn bind(addr: &str) -> io::Result<Self> {
339 if addr.starts_with("iroh://") {
340 tracing::info!("Binding iroh endpoint");
341
342 let secret_key = get_or_create_secret()?;
343 let endpoint = bind_iroh_endpoint(vec![ALPN.to_vec()], secret_key)
344 .await
345 .map_err(|e| {
346 tracing::error!("Failed to bind iroh endpoint: {}", e);
347 io::Error::other(format!("Failed to bind endpoint: {e}"))
348 })?;
349
350 tracing::debug!("Iroh endpoint bound successfully");
351
352 let relay_wait = std::time::Duration::from_secs(5);
359 let got_relay = if iroh_relay_disabled() {
360 false
361 } else {
362 let mut relay_status = endpoint.home_relay_status();
363 tokio::time::timeout(relay_wait, async {
364 while relay_status.get().is_empty() {
365 if relay_status.updated().await.is_err() {
366 break;
367 }
368 }
369 })
370 .await
371 .is_ok()
372 };
373 if !got_relay && !iroh_relay_disabled() {
374 tracing::warn!(
375 "No iroh home relay after {relay_wait:?}; \
376 issuing a direct-addresses-only ticket"
377 );
378 }
379
380 let mut addr_watcher = endpoint.watch_addr();
385 let node_addr = tokio::time::timeout(std::time::Duration::from_secs(5), async {
386 loop {
387 let addr = addr_watcher.get();
388 if !addr.addrs.is_empty() {
389 return addr;
390 }
391 if addr_watcher.updated().await.is_err() {
392 return addr_watcher.peek().clone();
393 }
394 }
395 })
396 .await
397 .unwrap_or_else(|_| addr_watcher.get());
398
399 let ticket = EndpointTicket::new(node_addr.clone()).to_string();
401
402 tracing::info!("Iroh endpoint ready with node ID: {}", node_addr.id);
403 tracing::info!("Iroh ticket: {}", ticket);
404
405 Ok(Listener::Iroh(endpoint, ticket))
406 } else if addr.starts_with('/') || addr.starts_with('.') || is_windows_path(addr) {
407 let _ = std::fs::remove_file(addr);
409 let listener = UnixListener::bind(addr)?;
410 Ok(Listener::Unix(listener))
411 } else {
412 let mut addr = addr.to_owned();
413 if addr.starts_with(':') {
414 addr = format!("127.0.0.1{addr}");
415 };
416 let listener = TcpListener::bind(addr).await?;
417 Ok(Listener::Tcp(listener))
418 }
419 }
420
421 pub fn get_ticket(&self) -> Option<&str> {
422 match self {
423 Listener::Iroh(_, ticket) => Some(ticket),
424 _ => None,
425 }
426 }
427
428 #[cfg(test)]
429 pub async fn connect(&self) -> io::Result<AsyncReadWriteBox> {
430 match self {
431 Listener::Tcp(listener) => {
432 let stream = TcpStream::connect(listener.local_addr()?).await?;
433 Ok(Box::new(stream))
434 }
435 Listener::Unix(listener) => {
436 #[cfg(unix)]
437 {
438 let stream =
439 UnixStream::connect(listener.local_addr()?.as_pathname().unwrap()).await?;
440 Ok(Box::new(stream))
441 }
442 #[cfg(windows)]
443 {
444 let path = listener.local_addr()?;
445 let stream = WinUnixStream::connect(&path).await?;
446 Ok(Box::new(stream))
447 }
448 }
449 Listener::Iroh(_, ticket) => {
450 let secret_key = get_or_create_secret()?;
451
452 let client_endpoint = bind_iroh_endpoint(vec![], secret_key)
454 .await
455 .map_err(io::Error::other)?;
456
457 let node_ticket: EndpointTicket = ticket
459 .parse()
460 .map_err(|e| io::Error::other(format!("Invalid ticket: {}", e)))?;
461 let node_addr = node_ticket.endpoint_addr().clone();
462
463 let conn = client_endpoint
465 .connect(node_addr, ALPN)
466 .await
467 .map_err(io::Error::other)?;
468
469 let (mut send_stream, recv_stream) =
471 conn.open_bi().await.map_err(io::Error::other)?;
472
473 #[allow(unused_imports)]
475 use tokio::io::AsyncWriteExt;
476 send_stream
477 .write_all(&HANDSHAKE)
478 .await
479 .map_err(io::Error::other)?;
480
481 let stream = IrohStream::new(send_stream, recv_stream, conn, Some(client_endpoint));
482 Ok(Box::new(stream))
483 }
484 }
485 }
486}
487
488impl std::fmt::Display for Listener {
489 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
490 match self {
491 Listener::Tcp(listener) => {
492 let addr = listener.local_addr().unwrap();
493 write!(f, "{}:{}", addr.ip(), addr.port())
494 }
495 Listener::Unix(listener) => {
496 #[cfg(unix)]
497 {
498 let addr = listener.local_addr().unwrap();
499 let path = addr.as_pathname().unwrap();
500 write!(f, "{}", path.display())
501 }
502 #[cfg(windows)]
503 {
504 let path = listener.local_addr().unwrap();
505 write!(f, "{}", path.display())
506 }
507 }
508 Listener::Iroh(_, ticket) => {
509 write!(f, "iroh://{ticket}")
510 }
511 }
512 }
513}
514
515#[cfg(test)]
516mod tests {
517 use super::*;
518
519 use tokio::io::AsyncReadExt;
520 use tokio::io::AsyncWriteExt;
521
522 async fn exercise_listener(addr: &str) {
523 let mut listener = Listener::bind(addr).await.unwrap();
524 let mut client = listener.connect().await.unwrap();
525
526 let (mut serve, _) = listener.accept().await.unwrap();
527 let want = b"Hello from server!";
528 serve.write_all(want).await.unwrap();
529 drop(serve);
530
531 let mut got = Vec::new();
532 client.read_to_end(&mut got).await.unwrap();
533 assert_eq!(want.to_vec(), got);
534 }
535
536 #[tokio::test]
537 async fn test_bind_tcp() {
538 exercise_listener(":0").await;
539 }
540
541 #[tokio::test]
542 async fn test_bind_unix() {
543 let temp_dir = tempfile::tempdir().unwrap();
544 let path = temp_dir.path().join("test.sock");
545 let path = path.to_str().unwrap();
546 exercise_listener(path).await;
547 }
548
549 #[tokio::test]
550 #[ignore] async fn test_bind_iroh() {
552 exercise_listener("iroh://").await;
554 }
555}