A high-performance, BitTorrent-like peer-to-peer file sharing system implemented in Rust with distributed architecture, automatic peer discovery, and chunked file transfers.
- Distributed Architecture: No central server required - fully decentralized P2P network
- Automatic Peer Discovery: UDP-based discovery service for finding peers on the local network
- DHT (Distributed Hash Table): Kademlia-inspired DHT for decentralized peer discovery
- Chunked File Transfer: Files are split into verified chunks for efficient and reliable transfer
- Data Integrity: SHA-256 hash verification ensures file integrity during transfers
- Bandwidth Throttling: Token bucket rate limiting for upload/download speed control
- NAT Traversal: Automatic UPnP port mapping and STUN for public IP discovery
- High Performance: Optimized for low latency (~1.7s) with 100% reliability
- Command Line Interface: Easy-to-use CLI for node management and file operations
- Cross-Platform: Built with Rust and Tokio for excellent cross-platform support
- Benchmarking Tools: Built-in performance testing and metrics collection
Recent benchmark results demonstrate excellent performance:
| File Size | Transfer Time | Success Rate | Throughput |
|---|---|---|---|
| 1KB | 1.7s | 100% | 0.0006 MB/s |
| 10KB | 1.7s | 100% | 0.0057 MB/s |
| 100KB | 1.8s | 100% | 0.056 MB/s |
| 50KB+ | 1.2s | 100% | 0.039 MB/s |
Note: Performance scales significantly with larger files as connection overhead is amortized.
- Rust 1.70+ (2024 edition)
- Cargo package manager
# Clone the repository
git clone https://github.com/AarambhDevHub/mini-p2p.git
cd mini-p2p
# Build the project
cargo build --release
# Run tests
cargo test# Start the first node with discovery service enabled
cargo run -- start --port 8080 --dir ./shared --discovery --name "SeedNode"# Start the first node with DHT enabled
cargo run -- start --port 8080 --dir ./shared --name "SeedNode" --dht
# Start a second node and bootstrap via the first node
cargo run -- start --port 8081 --dir ./shared2 --name "PeerNode" --dht --dht-bootstrap "127.0.0.1:6881"# Start a node with NAT traversal enabled (default)
# This will attempt to map ports on your router for external access
cargo run -- start --port 8080 --dir ./shared --name "PublicNode"
# To disable NAT traversal (e.g., for local testing only)
cargo run -- start --port 8080 --dir ./shared --name "LocalNode" --no-nat# Start peer nodes that connect to the seed
cargo run -- start --port 8081 --dir ./shared2 --bootstrap 127.0.0.1:8080 --name "PeerNode1"
cargo run -- start --port 8082 --dir ./shared3 --bootstrap 127.0.0.1:8080 --name "PeerNode2"# Query a peer for available files
cargo run -- list --peer 127.0.0.1:8080# Download a file by hash
cargo run -- download --hash <file_hash> --output downloaded_file.txt --peer 127.0.0.1:8080cargo run -- start [OPTIONS]
Options:
-p, --port <PORT> Port to listen on [default: 8080]
-d, --dir <DIRECTORY> Directory to share files from [default: ./shared]
-b, --bootstrap <ADDRESS> Bootstrap peer address (host:port)
-n, --name <NAME> Node name for identification
--discovery Enable discovery service (only one per network)
--dht Enable DHT for decentralized peer discovery
--dht-port <PORT> DHT UDP port [default: 6881]
--dht-bootstrap <ADDRESS> DHT bootstrap node address (host:port)
--no-nat Disable NAT traversal (UPnP/STUN)cargo run -- download [OPTIONS]
Options:
-h, --hash <HASH> File hash to download
-o, --output <PATH> Output file path
-p, --peer <ADDRESS> Peer address to connect tocargo run -- list [OPTIONS]
Options:
-p, --peer <ADDRESS> Peer address to queryโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Seed Node โ โ Peer Node 1 โ โ Peer Node 2 โ
โ Port: 8080 โโโโโบโ Port: 8081 โโโโโบโ Port: 8082 โ
โ Discovery: โ โ Bootstrap: โ โ Bootstrap: โ
โ 9999 โ โ 127.0.0.1:8080โ โ 127.0.0.1:8080โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
- Node: Main P2P node managing connections and file sharing
- Discovery Service: UDP-based peer discovery on the local network
- File Manager: Handles file scanning, chunking, and reconstruction
- Transfer Engine: Manages upload/download operations
- Peer Manager: Maintains connections and peer state
- Storage Engine: File and chunk storage with integrity verification
- Peer Discovery: UDP broadcast/multicast for finding peers
- Connection: TCP handshake with protocol negotiation
- File Listing: Request/response for available files
- Chunk Transfer: Parallel download of file chunks with verification
- File Reconstruction: Reassembly of chunks into complete files
# Set log level
export RUST_LOG=info
# Custom configuration
export MINI_P2P_MAX_PEERS=50
export MINI_P2P_CHUNK_SIZE=65536Create config/default.toml:
[network]
max_peers = 50
connection_timeout = 30
discovery_interval = 30
[storage]
chunk_size = 65536
max_file_size = 1073741824 # 1GB
[bandwidth]
max_upload_speed = 1048576 # 1 MB/s (optional, omit for unlimited)
max_download_speed = 2097152 # 2 MB/s (optional, omit for unlimited)
[logging]
level = "info"Control upload and download speeds programmatically:
use mini_p2p::Config;
use std::path::PathBuf;
let config = Config {
port: 8080,
shared_dir: PathBuf::from("./shared"),
bootstrap_peer: None,
node_name: "MyNode".to_string(),
discovery_port: Some(9999),
max_upload_speed: Some(10_000), // 10 KB/s upload limit
max_download_speed: Some(50_000), // 50 KB/s download limit
};Features:
- Token bucket algorithm for smooth rate limiting
- Configurable burst capacity (2x the rate limit)
- Per-connection throttling
- Zero overhead when disabled (None = unlimited)
The system automatically handles NAT traversal to allow nodes behind routers to communicate:
- UPnP (Universal Plug and Play): Automatically maps TCP (file transfer) and UDP (DHT) ports on supported routers.
- STUN (Session Traversal Utilities for NAT): Discovers the node's public IP address using Google's public STUN servers.
This is enabled by default. To disable it (e.g., for local-only testing), use the --no-nat flag.
cargo run --example basic_usagecargo run --example file_sharing_democargo run --example network_simulationcargo run --example benchmarkRun comprehensive performance tests:
# Run full benchmark suite
cargo run --example benchmark
# View results
cat benchmark_summary.txt
cat benchmark_report.jsonBenchmark tests include:
- Single file transfer performance
- Multiple file size comparisons
- Peer scalability testing
- Concurrent download performance
- Network resilience testing
Enable debug logging for troubleshooting:
# Verbose logging
RUST_LOG=debug cargo run -- start --port 8080 --dir ./shared --discovery
# Trace network operations
RUST_LOG=mini_p2p::network=trace cargo run -- start --port 8080- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
# Install development dependencies
cargo install cargo-watch
cargo install cargo-tarpaulin
# Run tests with file watching
cargo watch -x test
# Generate coverage report
cargo tarpaulin --out htmlGenerate and view documentation:
cargo doc --open- Small File Overhead: ~1.7s connection overhead affects small files
- Discovery Scope: UDP discovery limited to local network segment
- DHT (Distributed Hash Table) implementation
- NAT traversal and hole punching
- Bandwidth throttling and QoS
- Web interface for node management
- Torrent file format support
- Encryption and authentication
- Mobile platform support
This project is licensed under the MIT License - see the LICENSE file for details.
If you find Ignitia helpful, consider supporting the project:
- Built with Tokio async runtime
- Inspired by BitTorrent protocol design
- Uses serde for serialization
- Networking powered by socket2
- Create an issue for bug reports
- Join discussions for questions
- Check the Wiki for detailed guides
Made with โค๏ธ in Rust | โญ Star this repo if you find it useful!