A production-ready, BitTorrent-inspired peer-to-peer file sharing system built with modern web technologies. This system implements custom signaling, STUN/TURN servers, and torrent-like protocols for secure, decentralized file distribution.
- Real P2P Connections: Direct browser-to-browser file sharing using WebRTC
- Custom Signaling Server: WebSocket-based peer discovery and connection establishment
- In-House STUN/TURN Services: Complete NAT traversal solution without external dependencies
- BitTorrent-Like Protocol: Chunked file transfer with hash verification and piece exchange
- Secure Communication: TLS encryption and peer verification throughout the system
- Distributed Hash Table (DHT): Decentralized peer and file discovery
- Piece-Based Downloads: Efficient parallel downloading from multiple peers
- Hash Verification: SHA-256 integrity checking for all file pieces
- Connection Recovery: Automatic reconnection and error handling
- Rate Limiting: Built-in protection against abuse and DoS attacks
- Docker Containerization: Complete deployment solution with docker-compose
- Load Balancing: Nginx reverse proxy with upstream server configuration
- Monitoring: Prometheus metrics and Grafana dashboards
- Health Checks: Comprehensive system health monitoring
- Security: CORS, rate limiting, and secure headers implementation
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Web Client │ │ Web Client │ │ Web Client │
│ (Browser) │ │ (Browser) │ │ (Browser) │
└─────────┬───────┘ └─────────┬───────┘ └─────────┬───────┘
│ │ │
└──────────────────────┼──────────────────────┘
│
┌─────────────┴─────────────┐
│ Signaling Server │
│ (WebSocket) │
└─────────────┬─────────────┘
│
┌──────────────────────┼──────────────────────┐
│ │ │
┌─────────┴───────┐ ┌─────────┴───────┐ ┌─────────┴───────┐
│ STUN Server │ │ TURN Server │ │ Torrent Tracker │
│ (UDP 3478) │ │ (UDP 3479) │ │ (HTTP 8000) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
-
Signaling Server (
backend/signaling-server.js)- WebSocket-based peer discovery
- Connection establishment coordination
- Room management for file sharing
- Authentication and session management
-
STUN Server (
backend/stun-server.js)- RFC 5389 compliant STUN implementation
- NAT type discovery and public IP resolution
- UDP hole punching coordination
-
TURN Server (
backend/turn-server.js)- RFC 5766 compliant TURN implementation
- Media relay for restrictive NATs
- Allocation and permission management
-
Torrent Tracker (
backend/torrent-tracker.js)- BitTorrent-compatible tracker protocol
- Peer list management and statistics
- File announce and scrape operations
-
Web Frontend (
src/)- Modern React-based user interface
- WebRTC peer connection management
- File chunking and piece verification
- Real-time progress tracking
- Node.js 18+
- Docker & Docker Compose
- Modern web browser with WebRTC support
-
Clone the repository
git clone <repository-url> cd p2p-distributed-system
-
Build and start all services
docker-compose up --build
-
Access the application
- Web Interface: http://localhost
- Signaling Server: ws://localhost:8080
- Torrent Tracker: http://localhost:8000
- Monitoring: http://localhost:3000 (Grafana)
-
Install dependencies
npm install
-
Start individual services
# Terminal 1 - Signaling Server npm run start:signaling # Terminal 2 - STUN Server npm run start:stun # Terminal 3 - TURN Server npm run start:turn # Terminal 4 - Torrent Tracker npm run start:tracker # Terminal 5 - Frontend Development npm run dev
- Click "Add Files" button in the web interface
- Select files from your local system
- Files are automatically chunked and hashed
- File information is announced to connected peers
- Browse available files from connected peers
- Click download to start piece-based transfer
- Monitor progress in the Downloads tab
- Completed files are automatically verified and saved
- View connected peers in the Network tab
- Monitor connection status and statistics
- Check system logs for debugging information
# Signaling Server
PORT=8080
NODE_ENV=production
# STUN Server
STUN_PORT=3478
# TURN Server
TURN_PORT=3479
# Torrent Tracker
TRACKER_PORT=8000Create docker-compose.override.yml for custom configurations:
version: '3.8'
services:
signaling-server:
environment:
- DEBUG=true
- MAX_PEERS=1000
ports:
- "8080:8080"- TLS Encryption: All WebRTC connections use DTLS
- Peer Verification: Challenge-response authentication
- Rate Limiting: Protection against DoS attacks
- CORS Protection: Configurable cross-origin policies
- SHA-256 Hashing: All file pieces are cryptographically verified
- Merkle Trees: Efficient integrity verification for large files
- Piece Validation: Automatic retry for corrupted pieces
- Room-Based Sharing: Files shared within specific rooms
- Peer Permissions: Granular control over peer interactions
- Session Management: Secure peer session handling
/health- Service health status/stats- Real-time statistics- Connection state monitoring
- Automatic service recovery
- Peer connection counts
- File transfer statistics
- Error rates and latencies
- Resource utilization
- Real-time system overview
- Network topology visualization
- Performance analytics
- Alert management
npm testnpm run test:integrationnpm run test:load- Configure SSL certificates in
nginx.conf - Set production environment variables
- Deploy with Docker Compose
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
- Horizontal Scaling: Multiple signaling server instances
- Load Balancing: Nginx upstream configuration
- Database: Redis for session persistence
- CDN: Static asset distribution
- 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
- Follow SOLID principles and clean code practices
- Maintain comprehensive test coverage
- Document all public APIs
- Use TypeScript for type safety
- Follow semantic versioning
// Authentication
{
"type": "authenticate",
"challenge": "random-string",
"response": "hashed-response"
}
// File Announcement
{
"type": "file-announce",
"fileName": "example.pdf",
"fileSize": 1048576,
"fileHash": "sha256-hash",
"pieceHashes": ["piece1-hash", "piece2-hash"]
}// Piece Request
{
"type": "piece-request",
"fileHash": "file-hash",
"pieceIndex": 0,
"blockOffset": 0,
"blockSize": 16384
}
// Piece Response
{
"type": "piece-response",
"fileHash": "file-hash",
"pieceIndex": 0,
"blockOffset": 0,
"blockData": ArrayBuffer
}Connection Failed
- Check firewall settings
- Verify STUN/TURN server accessibility
- Ensure WebRTC support in browser
File Transfer Slow
- Check network bandwidth
- Verify peer connectivity
- Monitor piece availability
Authentication Errors
- Verify signaling server connection
- Check authentication credentials
- Review server logs
Enable debug logging:
DEBUG=p2p:* npm run devThis project is licensed under the MIT License - see the LICENSE file for details.
- WebRTC specification and implementations
- BitTorrent protocol design principles
- Open source P2P networking libraries
- Modern web development community
For support and questions:
- Create an issue on GitHub
- Check the documentation wiki
- Join our community discussions
Built with ❤️ for the decentralized web