P2P 网络通信栈,自动 hole-punching + relay fallback,基于 QUIC 协议,连接稳定且快。Rust 实现,9.7k stars。

概述

iroh 提供基于公钥的连接 API —— dial by public key,无需 IP 地址,iroh 自动寻找并维护最快路径(直连 hole-punching 或 relay 中继)。基于 QUIC 协议(自研 noq 库),提供认证加密、并发流、流优先级、数据报传输,避免队头阻塞。

核心指标:

  • 自动 hole-punching(NAT 穿透)
  • relay fallback(穿透失败时 fallback)
  • QUIC 协议( authenticated encryption + multiplexed streams)
  • 性能基准持续运行:https://perf.iroh.computer
  • 9,681 stars,MIT / Apache-2.0 双许可

核心特性

连接管理

  • 公钥 dial:Endpoint::connect(node_id),iroh 自动寻路
  • 自动 hole-punching:NAT 环境下优先直连
  • relay fallback:穿透失败时自动切换 relay
  • 连接持久:网络抖动自动重连
  • 多路径传输:同时用多路径提升吞吐

QUIC 协议(noq 库)

  • 认证加密:ed25519 + TLS 1.3
  • 并发流:单连接多流,无队头阻塞
  • 流优先级:优先级调度(critical / bulk)
  • 数据报:datagram transport(小消息场景)

内置协议栈

协议功能
iroh-blobsBLAKE3 内容寻址 blob 传输(KB → TB)
iroh-gossipPub-sub overlay 网络(移动端省资源)
iroh-docs最终一致 KV store(基于 iroh-blobs)

DNS 服务

  • iroh-dns-server:自建 DNS + Pkarr 地址查询(运行在 dns.iroh.link)
  • iroh-dns:DNS 客户端组件

安装

Cargo:

[dependencies]
iroh = "0.34"

最小 Rust 版本:1.91(edition 2024)

FFI bindings:https://github.com/n0-computer/iroh-ffi(其他语言)

示例代码

use iroh::{Endpoint, NodeId};
 
let endpoint = Endpoint::builder()
    .relay_mode(RelayMode::Default)
    .bind()
    .await?;
 
let node_id: NodeId = "public-key-here".parse()?;
let conn = endpoint.connect(node_id, "example-alpn").await?;
 
// 发送数据
let stream = conn.open_bi().await?;
stream.write_all(b"hello").await?;

完整示例(仓库 examples/):

  • echo.rs:echo 协议
  • connect.rs / listen.rs:连接建立
  • transfer.rs:文件传输
  • search.rs:搜索功能
  • monitor-connections.rs:连接监控
  • custom-transport.rs:自定义传输层
  • auth-hook.rs:认证钩子

性能基准

持续运行:https://perf.iroh.computer

实测指标(官方数据):

  • hole-punching 成功率:90%+(常见 NAT 环境)
  • relay latency:额外 ~50ms(fallback)
  • 多路径吞吐:+30%(双路径)

与同类工具对比

工具协议hole-punchingrelay传输层
irohQUIC自动 + fallback内置noq(自研)
libp2p多协议手动配置可选TCP/WebRTC
WireGuardUDP + IP需公网 IP无Kernel
TailscaleWireGuard自动DERP relayKernel

应用场景

  • Agent 通信:Agent-to-Agent P2P 数据交换(无中心服务器)
  • 分布式系统:去中心化存储、计算、共识
  • 实时协作:多人协作工具(白板、文档、代码)
  • IoT 设备:设备间直连(手机 ↔ 传感器)
  • 文件同步:跨设备文件传输(类似 Syncthing)

生态

  • noq:QUIC 实现(iroh 底层)
  • iroh-blobs:blob 传输协议
  • iroh-gossip:gossip 协议
  • iroh-docs:文档同步
  • iroh-doctor:诊断工具
  • iroh-ffi:FFI bindings(C / Python / Go)
  • iroh-examples:示例代码仓库
  • iroh-experiments:实验性功能

资源