rbit
A comprehensive BitTorrent library implementing core BEP (BitTorrent Enhancement Proposals) specifications in pure Rust.
Features
- Torrent parsing - Read
.torrentfiles and magnet links - Peer communication - Connect to and exchange data with peers using the wire protocol
- Tracker protocols - Discover peers via HTTP and UDP trackers
- DHT - Trackerless peer discovery using a Kademlia-based distributed hash table
- Storage management - Efficient disk I/O with piece verification
- Caching - Memory-efficient piece and block caching
Highlights
- Async/await - Built on tokio for efficient async I/O
- Zero-copy - Uses
bytes::Bytesfor efficient buffer handling - Memory-safe - Pure Rust with no unsafe code in the public API
- Concurrent - Thread-safe primitives from
parking_lotanddashmap
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Quick Start
Parsing a torrent file
use Metainfo;
let torrent_data = read?;
let metainfo = from_bytes?;
println!;
println!;
println!;
println!;
for tracker in metainfo.trackers
Parsing a magnet link
use MagnetLink;
let magnet = parse?;
println!;
println!;
Connecting to a peer
use ;
use SocketAddr;
let peer_addr: SocketAddr = "192.168.1.100:6881".parse?;
let info_hash = ; // Your torrent's info hash
let our_peer_id = generate;
let mut conn = connect.await?;
// Express interest in downloading
conn.send.await?;
// Wait for unchoke before requesting pieces
loop
Announcing to an HTTP tracker
use ;
let tracker = new?;
let response = tracker.announce.await?;
println!;
println!;
Using the DHT
use DhtServer;
let dht = bind.await?;
// Bootstrap from well-known nodes
dht.bootstrap.await?;
// Find peers for a specific info hash
let info_hash = ;
let peers = dht.get_peers.await?;
for peer in peers
Supported BEPs
| BEP | Description | Module |
|---|---|---|
| BEP-3 | BitTorrent Protocol | peer, metainfo, tracker |
| BEP-5 | DHT Protocol | dht |
| BEP-6 | Fast Extension | peer |
| BEP-9 | Magnet Links | metainfo |
| BEP-10 | Extension Protocol | peer |
| BEP-11 | Peer Exchange (PEX) | pex |
| BEP-14 | Local Service Discovery | lsd |
| BEP-15 | UDP Tracker Protocol | tracker |
| BEP-23 | Compact Peer Lists | tracker |
| BEP-52 | BitTorrent v2 (partial) | metainfo |
Modules
- bencode - Bencode serialization format used throughout BitTorrent
- metainfo - Torrent file parsing, magnet links, and info hashes
- peer - Peer wire protocol for data exchange between clients
- tracker - HTTP and UDP tracker clients for peer discovery
- dht - Kademlia-based distributed hash table for trackerless operation
- pex - Peer Exchange for sharing peer lists between connected peers
- lsd - Local Service Discovery via multicast for LAN peers
- storage - Disk I/O management with piece verification
- cache - Memory caching for pieces and blocks
Architecture Notes
This library provides low-level building blocks rather than a complete BitTorrent client. You are responsible for:
- Coordinating peer connections and piece selection
- Managing download/upload state across peers
- Implementing rate limiting and choking algorithms
- Handling torrent lifecycle (start, pause, resume, remove)
For a complete client implementation, combine these modules with your own orchestration logic.
Minimum Supported Rust Version
This crate requires Rust 1.85 or later.
License
Licensed under the MIT license. See LICENSE for details.