P2P 연결의 새로운 표준: Iroh API를 활용한 분산 통신 가이드
요약
Iroh는 공용 키(public key) 기반으로 가장 빠르고 안정적인 P2P 연결을 제공하는 API입니다. 지리적 위치에 관계없이 직접 연결(direct connection)을 시도하고, 실패할 경우 공개 릴레이 서버(public relay servers)를 통해 통신합니다. 내부적으로 noq 라이브러리를 사용하여 QUIC 연결을 구축함으로써, 인증된 암호화, 스트림 우선순위가 적용된 동시 스트리밍, 데이터그램 전송 등의 이점을 기본 제공합니다. 개발자는 iroh-blobs (BLAKE3 기반), iroh-gossip 등 이미 잘
핵심 포인트
- Iroh는 공용 키를 이용해 최적의 P2P 연결을 찾아주며, 직접 연결(hole-punching)과 공개 릴레이 서버 폴백(fallback) 메커니즘을 지원합니다.
- QUIC 프로토콜 기반으로 noq를 사용하여 구현되어, Head-of-Line Blocking 방지 및 인증된 암호화 기능을 기본 제공합니다.
- 개발자는 iroh-blobs (BLAKE3 기반 대용량 콘텐츠 주소 지정), iroh-gossip (확장 가능한 Pub/Sub 오버레이 네트워크) 등 고수준의 전용 프로토콜을 활용할 수 있습니다.
- Rust 언어에서 가장 쉽게 사용 가능하며, `cargo add iroh`를 통해 핵심 라이브러리를 설치하고 구현할 수 있습니다.
Iroh API Overview
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.
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.
Iroh uses noq 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.
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
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.
If you want to use iroh from other languages, make sure to check out iroh-ffi, the repository for FFI bindings.
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 then0_discovery for EndpointIds, running at dns.iroh.link.iroh-net-report: Analyzes your host's networking ability & NAT.
Copyright 2025 N0, INC.
AI 자동 생성 콘텐츠
본 콘텐츠는 GitHub Trending Rust (weekly)의 원문을 AI가 자동으로 요약·번역·분석한 것입니다. 원 저작권은 원저작자에게 있으며, 정확한 내용은 반드시 원문을 확인해 주세요.
원문 바로가기