# BitTorrent Gateway A comprehensive unified content distribution system that seamlessly integrates BitTorrent protocol, WebSeed technology, DHT peer discovery, built-in tracker, video transcoding, and Nostr announcements. This gateway provides intelligent content distribution by automatically selecting the optimal delivery method based on file size and network conditions, with automatic video transcoding for web-compatible streaming. ## Architecture Overview The BitTorrent Gateway operates as a unified system with multiple specialized components working together: ### Core Components **1. Gateway HTTP API Server (Port 9877)** - Main web interface and API endpoints - File upload/download management - Smart proxy for reassembling chunked content - WebSeed implementation with advanced LRU caching - Rate limiting and abuse prevention **2. Embedded Blossom Server (Port 8082)** - Nostr-compatible blob storage protocol - Direct blob storage for small files (<100MB) - Integration with gateway for seamless operation **3. DHT Node (Port 6883)** - Distributed peer discovery - BitTorrent DHT protocol implementation - Bootstrap connectivity with major DHT networks - Automatic torrent announcement and peer sharing **4. Built-in BitTorrent Tracker** - Full BitTorrent tracker implementation - Announce/scrape protocol support - P2P coordination and peer ranking - Client compatibility optimizations for qBittorrent, Transmission, WebTorrent, Deluge, uTorrent **5. Video Transcoding Engine** - Automatic H.264/AAC conversion for web compatibility - Smart serving: transcoded versions when ready, originals otherwise - Background processing with priority queuing - FFmpeg integration with progress tracking - Multiple quality profiles and format support ### Smart Storage Strategy The system uses an intelligent dual-storage approach with video optimization: - **Small Files (<100MB)**: Stored directly as blobs using Blossom protocol - **Large Files (≥100MB)**: Automatically chunked into 2MB pieces, stored as torrents with WebSeed fallback - **Video Files**: Automatically queued for H.264/AAC transcoding to web-compatible MP4 format - **Smart Serving**: Transcoded versions served when ready, original chunks as fallback ### P2P Coordination System A sophisticated P2P coordinator manages all networking components: - **Unified Peer Discovery**: Aggregates peers from tracker, DHT, and WebSeed sources - **Smart Peer Ranking**: Geographic proximity and performance-based peer selection - **Load Balancing**: Distributes load across multiple peer sources - **Health Monitoring**: Real-time monitoring of all P2P components with automatic alerting ## Installation ### Prerequisites - Go 1.21 or later - SQLite3 - FFmpeg (for video transcoding, optional) - 10MB+ available storage - Linux/macOS/Windows ### Quick Start (Standalone) ```bash # Clone repository git clone https://git.sovbit.dev/enki/torrentGateway.git cd torrentGateway # Build the gateway go build -o gateway ./cmd/gateway # Run with default configuration ./gateway ``` The web interface will be available at http://localhost:9877 ### Production Deployment (No Docker) For production deployment without Docker, use the native installation script: ```bash # Make installation script executable chmod +x scripts/install_native.sh # Install as system service sudo ./scripts/install_native.sh # Start the service sudo systemctl start torrent-gateway sudo systemctl enable torrent-gateway # Check status sudo systemctl status torrent-gateway ``` ### Nginx Reverse Proxy Configuration For production deployments, use Nginx as a reverse proxy: ```nginx server { listen 80; server_name your-domain.com; # Redirect HTTP to HTTPS return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; server_name your-domain.com; # SSL Configuration ssl_certificate /path/to/your/certificate.pem; ssl_certificate_key /path/to/your/private-key.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers off; # Security Headers add_header X-Frame-Options "SAMEORIGIN" always; add_header X-XSS-Protection "1; mode=block" always; add_header X-Content-Type-Options "nosniff" always; add_header Referrer-Policy "no-referrer-when-downgrade" always; add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always; # Gateway API and Web Interface location / { proxy_pass http://127.0.0.1:9877; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # WebSocket support for real-time features proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; # Increase timeouts for large file uploads proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; } # Blossom Server (optional, if running separately) location /blossom/ { proxy_pass http://127.0.0.1:8082/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } # Increase client max body size for file uploads client_max_body_size 10G; # Enable gzip compression gzip on; gzip_vary on; gzip_min_length 1024; gzip_proxied any; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss application/atom+xml image/svg+xml; } ``` ### SystemD Service Configuration The native installer creates a systemd service. Manual configuration: ```bash # Create service file sudo tee /etc/systemd/system/torrent-gateway.service > /dev/null <