Skip to content

AarambhDevHub/mini-p2p

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Mini P2P File Sharing System

A high-performance, BitTorrent-like peer-to-peer file sharing system implemented in Rust with distributed architecture, automatic peer discovery, and chunked file transfers.

๐Ÿš€ Features

  • 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

๐Ÿ“Š Performance Metrics

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.

๐Ÿ›  Installation

Prerequisites

  • Rust 1.70+ (2024 edition)
  • Cargo package manager

Build from Source

# 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

๐ŸŽฏ Quick Start

Option 1: Start with Local Discovery (UDP Broadcast)

# Start the first node with discovery service enabled
cargo run -- start --port 8080 --dir ./shared --discovery --name "SeedNode"

Option 2: Start with DHT (Decentralized Discovery)

# 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"

Option 3: Start with NAT Traversal (Public Network)

# 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

2. Start Additional Peer Nodes

# 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"

3. List Available Files

# Query a peer for available files
cargo run -- list --peer 127.0.0.1:8080

4. Download Files

# Download a file by hash
cargo run -- download --hash <file_hash> --output downloaded_file.txt --peer 127.0.0.1:8080

๐Ÿ“‹ Command Reference

Start Node

cargo 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)

Download File

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 to

List Files

cargo run -- list [OPTIONS]

Options:
  -p, --peer <ADDRESS>        Peer address to query

๐Ÿ— Architecture Overview

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   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โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Core Components

  • 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

Network Protocol

  1. Peer Discovery: UDP broadcast/multicast for finding peers
  2. Connection: TCP handshake with protocol negotiation
  3. File Listing: Request/response for available files
  4. Chunk Transfer: Parallel download of file chunks with verification
  5. File Reconstruction: Reassembly of chunks into complete files

๐Ÿ”ง Configuration

Environment Variables

# Set log level
export RUST_LOG=info

# Custom configuration
export MINI_P2P_MAX_PEERS=50
export MINI_P2P_CHUNK_SIZE=65536

Configuration Files

Create 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"

Bandwidth Throttling

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)

NAT Traversal

The system automatically handles NAT traversal to allow nodes behind routers to communicate:

  1. UPnP (Universal Plug and Play): Automatically maps TCP (file transfer) and UDP (DHT) ports on supported routers.
  2. 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.

๐Ÿงช Running Examples

Basic Usage

cargo run --example basic_usage

File Sharing Demo

cargo run --example file_sharing_demo

Network Simulation (Multiple Nodes)

cargo run --example network_simulation

Performance Benchmark

cargo run --example benchmark

๐Ÿ“ˆ Benchmarking

Run comprehensive performance tests:

# Run full benchmark suite
cargo run --example benchmark

# View results
cat benchmark_summary.txt
cat benchmark_report.json

Benchmark tests include:

  • Single file transfer performance
  • Multiple file size comparisons
  • Peer scalability testing
  • Concurrent download performance
  • Network resilience testing

๐Ÿ” Debugging

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

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Setup

# 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 html

๐Ÿ“ API Documentation

Generate and view documentation:

cargo doc --open

๐Ÿ› Known Issues

  • Small File Overhead: ~1.7s connection overhead affects small files
  • Discovery Scope: UDP discovery limited to local network segment

๐Ÿ”ฎ Roadmap

  • 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

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

โ˜• Support & Community

If you find Ignitia helpful, consider supporting the project:

Buy Me A Coffee

๐Ÿ™ Acknowledgments

  • Built with Tokio async runtime
  • Inspired by BitTorrent protocol design
  • Uses serde for serialization
  • Networking powered by socket2

๐Ÿ“ง Support

  • 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!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

  •  

Packages

No packages published

Languages