A fully functional BitTorrent client built from scratch in Rust that implements the core BitTorrent protocol. This project demonstrates a complete understanding of peer-to-peer file sharing, including torrent file parsing, peer discovery, the BitTorrent wire protocol, and efficient piece-based downloading with integrity verification.
- 🔧 Complete Bencoding Support: Encode and decode all bencoded data types (strings, integers, lists, dictionaries) per BEP-3 specification
- 📄 Torrent File Parsing: Extract and process complete torrent metadata including announce URLs, file structure, and piece information
- 🔐 Info Hash Calculation: Generate SHA-1 hashes for torrent identification and swarm participation
- 🌐 Peer Discovery: Connect to HTTP/HTTPS trackers and parse compact peer lists
- 🤝 BitTorrent Wire Protocol: Full implementation of peer messaging including handshake, bitfield, interested/uninterested, choke/unchoke, request, and piece messages
- ⚡ Concurrent Downloads: Parallel piece downloading with configurable connection limits and round-robin peer selection
- 🧩 Piece Management: Intelligent blockwise downloading (16KiB blocks) with request pipelining for optimal performance
- ✅ Integrity Verification: SHA-1 hash validation for every downloaded piece ensuring data authenticity
- 💾 File Assembly: Seamless reconstruction and disk writing of complete files from downloaded pieces
- 🎯 Robust Architecture: Clean, modular design built for performance and extensibility
- Rust 1.87+ with Cargo
- Active internet connection for tracker communication
git clone <repository-url>
cd bittorrent-rust
cargo build --release./your_program.sh decode "d8:announce9:localhost4:spam4:eggse"./your_program.sh info sample.torrent./your_program.sh peers sample.torrent./your_program.sh handshake sample.torrent <peer_ip:port>./your_program.sh download_piece -o /tmp/piece0 sample.torrent 0./your_program.sh download -o /tmp/downloaded_file sample.torrent- Purpose: Serialization format used throughout BitTorrent protocol
- Functions:
decode_bencoded_value()with recursive parsing for nested structures - Supports: Strings, integers, lists, dictionaries with proper error handling
- Purpose: Parse .torrent files and extract metadata
- Key Structs:
TorrentFile,TorrentInfo - Features:
- Announce URL extraction
- File length and piece information
- Piece hash validation data
- Async peer discovery via tracker communication
- Purpose: Implement BitTorrent wire protocol for peer communication
- Key Features:
- Asynchronous TCP connection management
- Protocol message handling (handshake, bitfield, interested, unchoke, request, piece)
- Message parsing and state management
- Purpose: User-friendly command-line interface
- Commands: decode, info, peers, handshake, download_piece, download
- Features: Parallel downloading with semaphore-based concurrency control
- Purpose: SHA-1 hash calculation for info hashes and piece verification
- Functions:
hash_bytes(),bytes_to_hex(), URL encoding utilities
- Connection Setup: Establish TCP connection and perform BitTorrent handshake
- State Exchange: Wait for bitfield, send interested, wait for unchoke
- Block Requests: Download pieces in 16KiB blocks with request pipelining
- Verification: Validate each piece against stored SHA-1 hash
- Assembly: Reconstruct complete file from verified pieces
- Parallel Downloads: Up to 5 concurrent peer connections (configurable)
- Round-Robin Peer Selection: Distribute load across available peers
- Async/Await: Built on Tokio for efficient I/O operations
- Comprehensive error handling with
anyhowfor user-friendly messages - Graceful connection failures with peer fallback
- Data integrity checks prevent corrupted downloads
- Language: Rust 2021 Edition
- Async Runtime: Tokio for concurrent operations
- HTTP Client: Reqwest for tracker communication
- CLI Framework: Clap with derive macros
- Serialization: Serde with Bencoding support
- Cryptography: SHA-1 hashing
- Error Handling: Anyhow for ergonomic error management
- 🧲 Magnet Link Support: Parse and download files using magnet URIs without .torrent files (coming soon)
- 📊 Advanced Peer Selection: Implement more sophisticated peer choosing algorithms
- 🏃 Performance Optimizations: Additional request pipelining and caching strategies
- 🔧 Extended Protocol Support: DHT, peer exchange, and other BEP implementations
- Bencoding: Compact binary serialization format for metadata exchange
- Info Hash: SHA-1 identifier calculated from torrent's info dictionary
- Tracker: Centralized service providing peer lists for content discovery
- Piece: File chunks (typically 256KB-1MB) that can be downloaded independently
- Block: Smaller chunks within pieces (16KiB) for network transfer efficiency
- Wire Protocol: Standardized message format for peer-to-peer communication
This BitTorrent client was built to demonstrate a deep understanding of peer-to-peer networking and protocol implementation. Feel free to explore the code, suggest improvements, or use it as a learning resource for understanding BitTorrent protocol implementation.
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
This project is open source. Feel free to use, modify, and distribute according to your needs.
Happy torrenting! 🚀 Explore the fascinating world of peer-to-peer file sharing with this comprehensive BitTorrent implementation.