| Crates.io | rustp2p |
| lib.rs | rustp2p |
| version | 0.3.2 |
| created_at | 2024-09-14 02:51:26.835886+00 |
| updated_at | 2025-07-23 06:49:56.09721+00 |
| description | A decentralized p2p library powered by Rust, which is devoted to simple use. |
| homepage | |
| repository | https://github.com/rustp2p/rustp2p |
| max_upload_size | |
| id | 1374382 |
| size | 314,491 |
A decentralized p2p library powered by Rust, which is devoted to simple use.
UDP hole punching for both Cone and Symmetric Nat
TCP hole punching for NAT1
Enables reliable transport over KCP
Enables secure encryption with AesGcm or ChaCha20Poly1305.
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).
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;
}