Endpoint.They are a lightweight, flexible mechanism for observing connection events or rejecting connections based on conditions. The latter can be used to implement custom authentication schemes. Hooks run at two points:
- Before an outgoing connection starts. No packets have been sent yet.
- After the QUIC/TLS handshake completes for both incoming and outgoing connections. The remote endpoint ID, ALPN, and other metadata are available, but no application data has been sent or received yet.
Endpoint::builder().hooks(...). If multiple hooks are installed, they run in the order they were added, and a rejection from any hook short-circuits the rest.
Note that hooks cannot use connections, they can only observe or reject them. This is an important separation of concerns: If hooks were allowed to use the connections in any way, they could interfer with the actual protocols running within these connections. Hooks can, however, reject connections before they are passed on to protocol handlers. This makes it possible to implement custom authentication schemes with hooks that work without any support from the protocols running in these connections.
Note: Hooks live on theEndpointinstance. Never store anEndpointinside your hook type (even indirectly), or it may cause reference-counting cycles and prevent clean shutdown.
Example: Observing connection events
This example shows a minimal hook implementation that logs the context available at each stage. It does not alter behavior, only observes.Example: Rejecting connections
Hooks can be used to enforce policy. If a hook returns a rejection result, the connection is immediately aborted. The example below rejects all incoming connections after the handshake. Outgoing connections will still dial. In real applications, you would inspectConnectionInfo and reject connections by checking the connection’s remote id or aLPN against authentication state in your app.
More examples
There are a few fully-featured examples for using hooks in the iroh repository.Authentication layer
Demonstrates how to build an authentication flow on top of hooks. This pattern keeps authentication separate from your application protocols while still integrating cleanly with iroh’s connection lifecycle.- We implement a dedicated “auth” protocol (with its own ALPN) for performing a pre-authentication handshake.
- Outgoing connections run the pre-auth step before starting other connections.
- Incoming connections are checked against a set of authorized remote ids.
- If an incoming connection comes from a peer that hasn’t successfully performed pre-auth, the connection is rejected.
Monitoring connection and path events
This example demonstrates how hooks can feed information to external tasks, giving you flexible observability.- A hook sends each
ConnectionInfoto a monitoring task. - The monitor can record events and stats.
Aggregating information about remote endpoints
This example implements aRemoteMap that tracks and aggregates information about all remotes our endpoint knows about.
This can be useful if your app needs to chose between remotes, or for building diagnostic tools.
- A hook forwards
ConnectionInfoupdates into a worker task. - The worker maintains a map of all remotes with counts of active connections and observed statistics (e.g. latency/RTT, whether relay/IP paths were used, etc).
- The
RemoteMapexposes a simple API to query all known remotes and their aggregate metrics.