Iroh: A library to establish direct connection between peers

原始链接: https://github.com/n0-computer/iroh

Iroh simplifies network connectivity by providing an API for dialing by public key. It establishes and maintains the fastest connection between nodes, prioritizing direct connections via hole-punching and falling back on public relays if necessary. Iroh utilizes QUIC for authenticated encryption, concurrent streams, and efficient data transfer. Developers can leverage pre-built protocols like iroh-blobs (BLAKE3 content-addressed storage), iroh-gossip (scalable publish-subscribe networks), and iroh-docs (eventually-consistent key-value store). Rust developers can easily integrate iroh using `cargo add iroh`. The example code demonstrates a basic echo server using Iroh's API. For other languages, iroh-ffi provides FFI bindings. The iroh ecosystem includes core components such as the iroh library, the iroh-relay server, iroh-base for common types, iroh-dns-server for NodeID discovery, and iroh-net-report for network analysis. This architecture delivers efficient and reliable communication with minimal developer effort.

Iroh是一个Rust库,用于建立直接的点对点连接。Hacker News上的一篇帖子讨论了它的特性和潜在用途。它使用中继器进行连接发现和中继,采用TCP进行中继连接,但在可能的情况下升级到直接连接。它使用QUIC进行连接。 评论者强调Iroh专注于成为一个高质量的P2P应用库,这与试图解决所有问题的项目形成对比。像Dumb Pipe和SendMe这样的演示展示了Iroh的功能,包括视频游戏流媒体。该库正在准备发布1.0版本。 Iroh利用Bittorrent DHT Mainline进行发现,并提供自定义DNS或本地mDNS选项。开发者正在帖子中回答问题,澄清其功能,尤其是在流媒体和UDP转发方面。虽然由于QUIC的要求,目前还没有针对嵌入式系统的no_std实现,但团队建议可以将Iroh与gRPC集成,同时使用Bittorrent DHT进行对等发现。与libp2p相比,Iroh的配置更少,并且通过使用中继器提高了可靠性。
相关文章

原文

less net work for networks

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

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 Quinn to establish QUIC connections between nodes. 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::builder().discovery_n0().bind().await?;

// Open a connection to the accepting node
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::builder().discovery_n0().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 the n0_discovery for NodeIds, running at dns.iroh.link.
  • iroh-net-report: Analyzes your host's networking ability & NAT.

Copyright 2024 N0, INC.

This project is licensed under either of

at your option.

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.

联系我们 contact @ memedata.com