rustp2p

Crates.iorustp2p
lib.rsrustp2p
version0.3.2
created_at2024-09-14 02:51:26.835886+00
updated_at2025-07-23 06:49:56.09721+00
descriptionA decentralized p2p library powered by Rust, which is devoted to simple use.
homepage
repositoryhttps://github.com/rustp2p/rustp2p
max_upload_size
id1374382
size314,491
XMH (xmh0511)

documentation

README

A decentralized p2p library powered by Rust, which is devoted to simple use.

Crates.io rustp2p

Features

  1. UDP hole punching for both Cone and Symmetric Nat

  2. TCP hole punching for NAT1

  3. Enables reliable transport over KCP

  4. Enables secure encryption with AesGcm or ChaCha20Poly1305.

Description

For connecting two peers, all you need to do is to give the configuration as done in the example. In short, provide a peer named C, peer A and B can directly connect to C, then A and B will find each other by C, A and C can directly connect by hole-punching, the whole process is done by this library. If two peers D and F cannot directly connect via hole-punching, this library can find the best link for indirectly connection(i.e. through some middle nodes).

Example

use rustp2p::Builder;
use rustp2p::protocol::node_id::GroupCode;
use rustp2p::cipher::Algorithm;
use rustp2p::tunnel::PeerNodeAddress;
use std::net::Ipv4Addr;
use std::sync::Arc;

#[tokio::main]
async fn main() {
    let node_id = Ipv4Addr::from([10, 0, 0, 1]);
    let endpoint = Builder::new()
        .node_id(node_id.into())
        .tcp_port(8080)
        .udp_port(8080)
        .peers(vec![PeerNodeAddress::from_str("udp://127.0.0.1:9090").unwrap()])
        .group_code(GroupCode::from(12345))
        .encryption(Algorithm::AesGcm("password".to_string()))
        .build()
        .await?;
    let endpoint = Arc::new(endpoint);
    let receiver = endpoint.clone();
    let h = tokio::spawn(async move {
        while let Ok(peer) = receiver.recv_from().await {}
    });
    let peer_node_id = Ipv4Addr::from([10, 0, 0, 2]);
    endpoint.send_to(b"hello", peer_node_id);
    _ = h.await;
}
Commit count: 395

cargo fmt