Nicolas LuckandFriedel Ziegelmayer fe7f04f63e feat(iroh-relay): Embeddable relay server (#3832)
## Description
This PR renders the iroh-relay server embeddable as a library by making
`RelayService` public ~and adding a native axum integration around it,
enabling the relay service to be integrated directly into axum
applications as a standard route handler without requiring
connection-level routing, separate ports, or reverse proxies.~

Adds docs, examples and tests for all new and/or now public
functions/structs.

## Key changes:
- Refactored relay internals (RelayedStream, Client, Actor, Clients) to
be generic over stream types while maintaining backward compatibility
  - ~Added new axum feature and server::axum_integration module~
- ~Implemented AxumWebSocketAdapter that bridges axum's WebSocket to the
relay's Stream/Sink traits~
- ~Created RelayState and relay_handler for easy integration into axum
routers~
- ~Full relay protocol support including handshake, authentication,
authorization, and client registration~
- Made all types public that are needed to have the Axum integration be
done in an external crate

## Breaking Changes

None. All changes are internal refactoring or new additions. The generic
implementations are backward compatible with existing relay server usage
through type aliases and preserved public APIs.

## Notes & open questions

- ~Rate limiting: Client-side rate limiting configuration is accepted
but not fully enforced yet in the axum integration (similar limitation
exists in the existing HTTP server integration)~
- ~TLS key export: AxumWebSocketAdapter implements ExportKeyingMaterial
but returns None since axum's WebSocket doesn't support TLS key material
export. This matches the behavior of non-TLS connections in the existing
relay server.~
- ~Error handling: Axum WebSocket errors are wrapped in
tokio_websockets::Error::Io variant for consistency with the relay's
error handling~
- ~Testing: Integration tests verify both non-TLS and TLS scenarios with
full end-to-end relay communication~

### Could We Reduce This?

  We could potentially create a single public "escape hatch" function:
```Rust
  pub async fn handle_custom_websocket<S>(
      stream: S,
      access_config: &AccessConfig,
      clients: &Clients,
  ) -> Result<()>
  where S: Stream + Sink + ExportKeyingMaterial
```
This would hide all the intermediate types and just expose one function.
However, this would:
- Remove flexibility (consuming crates couldn't customize the handshake
or authorization logic)
- Still require AccessConfig, Clients, and the trait bounds to be public
## Change checklist

  - [x] Self-review.
  - [x] AI review
- [x] Documentation updates following the
https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text,
if relevant.
  - [x] Tests added.
  - All breaking changes documented.
    - N/A - No breaking changes to public API

---------

Co-authored-by: Friedel Ziegelmayer <me@dignifiedquire.com>
2026-02-09 13:22:35 +01:00
2026-02-06 12:43:42 -05:00
2026-02-06 12:43:42 -05:00
2026-02-06 12:43:42 -05:00
2024-04-29 10:08:12 +00:00
2026-02-06 12:43:42 -05:00
2026-01-28 13:20:15 -05:00
2024-01-23 14:36:54 +00:00
2026-02-06 12:43:42 -05:00
2023-11-08 11:47:24 +01:00
2024-01-23 14:36:54 +00:00
2026-01-28 13:20:15 -05:00

iroh

less net work for networks

Documentation Crates.io downloads Chat Youtube License: MIT License: Apache 2.0 CI


What is iroh?

Iroh gives you an API for dialing by public key. You say “connect to that phone”, iroh will find & maintain the fastest connection for you, regardless of where it is.

Hole-punching

The fastest route is a direct connection, so if necessary, iroh tries to hole-punch. Should this fail, it can fall back to an open ecosystem of public relay servers. To ensure these connections are as fast as possible, we continuously measure iroh.

Built on QUIC

Iroh uses Quinn to establish QUIC connections between endpoints. This way you get authenticated encryption, concurrent streams with stream priorities, a datagram transport and avoid head-of-line-blocking out of the box.

Compose Protocols

Use pre-existing protocols built on iroh instead of writing your own:

  • iroh-blobs for BLAKE3-based content-addressed blob transfer scaling from kilobytes to terabytes
  • iroh-gossip for establishing publish-subscribe overlay networks that scale, requiring only resources that your average phone can handle
  • iroh-docs for an eventually-consistent key-value store of iroh-blobs blobs
  • iroh-willow for an (in-construction) implementation of the willow protocol

Getting Started

Rust Library

It's easiest to use iroh from rust. Install it using cargo add iroh, then on the connecting side:

const ALPN: &[u8] = b"iroh-example/echo/0";

let endpoint = Endpoint::bind().await?;

// Open a connection to the accepting endpoint
let conn = endpoint.connect(addr, ALPN).await?;

// Open a bidirectional QUIC stream
let (mut send, mut recv) = conn.open_bi().await?;

// Send some data to be echoed
send.write_all(b"Hello, world!").await?;
send.finish()?;

// Receive the echo
let response = recv.read_to_end(1000).await?;
assert_eq!(&response, b"Hello, world!");

// As the side receiving the last application data - say goodbye
conn.close(0u32.into(), b"bye!");

// Close the endpoint and all its connections
endpoint.close().await;

And on the accepting side:

let endpoint = Endpoint::bind().await?;

let router = Router::builder(endpoint)
    .accept(ALPN.to_vec(), Arc::new(Echo))
    .spawn()
    .await?;

// The protocol definition:
#[derive(Debug, Clone)]
struct Echo;

impl ProtocolHandler for Echo {
    async fn accept(&self, connection: Connection) -> Result<()> {
        let (mut send, mut recv) = connection.accept_bi().await?;

        // Echo any bytes received back directly.
        let bytes_sent = tokio::io::copy(&mut recv, &mut send).await?;

        send.finish()?;
        connection.closed().await;

        Ok(())
    }
}

The full example code with more comments can be found at echo.rs.

Or use one of the pre-existing protocols, e.g. iroh-blobs or iroh-gossip.

Other Languages

If you want to use iroh from other languages, make sure to check out iroh-ffi, the repository for FFI bindings.

Repository Structure

This repository contains a workspace of crates:

  • iroh: The core library for hole-punching & communicating with relays.
  • iroh-relay: The relay server implementation. This is the code we run in production (and you can, too!).
  • iroh-base: Common types like Hash, key types or RelayUrl.
  • iroh-dns-server: DNS server implementation powering the n0_discovery for EndpointIds, running at dns.iroh.link.
  • iroh-net-report: Analyzes your host's networking ability & NAT.

License

Copyright 2025 N0, INC.

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

S
Description
IP addresses break, dial keys instead. A library that adds QUIC + NAT Traversal to your apps.
Readme
435 MiB
Languages
Rust 99.6%
Shell 0.3%