| | #!/bin/bash |
| |
|
| | set -e |
| |
|
| | |
| | log() { |
| | echo "[$(date '+%Y-%m-%d %H:%M:%S')] [ARIA2] $*" |
| | } |
| |
|
| | |
| | check_port() { |
| | local port=$1 |
| | if nc -z localhost "$port" 2>/dev/null; then |
| | return 1 |
| | else |
| | return 0 |
| | fi |
| | } |
| |
|
| | |
| | wait_for_aria2() { |
| | local port=$1 |
| | local max_attempts=30 |
| | local attempt=1 |
| | |
| | log "Waiting for aria2 to start on port $port..." |
| | |
| | while [ $attempt -le $max_attempts ]; do |
| | if nc -z localhost "$port" 2>/dev/null; then |
| | log "aria2 is running on port $port" |
| | return 0 |
| | fi |
| | |
| | sleep 1 |
| | attempt=$((attempt + 1)) |
| | done |
| | |
| | log "ERROR: aria2 failed to start after $max_attempts seconds" |
| | return 1 |
| | } |
| |
|
| | log "Starting aria2 service..." |
| |
|
| | |
| | ARIA2_PORT="${ARIA2_PORT:-6800}" |
| | ARIA2_WEBUI_PORT="${ARIA2_WEBUI_PORT:-6880}" |
| | ARIA2_SECRET="${ARIA2_SECRET:-$(openssl rand -base64 32)}" |
| | ARIA2_DOWNLOAD_DIR="${ARIA2_DOWNLOAD_DIR:-/home/user/download}" |
| | ARIA2_SESSION_FILE="${ARIA2_SESSION_FILE:-/home/user/config/aria2.session}" |
| | ARIA2_LOG_FILE="${ARIA2_LOG_FILE:-/home/user/log/aria2.log}" |
| |
|
| | |
| | ARIA2_MAX_CONCURRENT="${ARIA2_MAX_CONCURRENT:-5}" |
| | ARIA2_MAX_CONN_PER_SERVER="${ARIA2_MAX_CONN_PER_SERVER:-16}" |
| | ARIA2_SPLIT="${ARIA2_SPLIT:-16}" |
| | ARIA2_DISK_CACHE="${ARIA2_DISK_CACHE:-64M}" |
| | ARIA2_BT_MAX_PEERS="${ARIA2_BT_MAX_PEERS:-55}" |
| | ARIA2_MAX_OVERALL_DOWNLOAD_LIMIT="${ARIA2_MAX_OVERALL_DOWNLOAD_LIMIT:-0}" |
| | ARIA2_MAX_DOWNLOAD_LIMIT="${ARIA2_MAX_DOWNLOAD_LIMIT:-0}" |
| | ARIA2_MAX_OVERALL_UPLOAD_LIMIT="${ARIA2_MAX_OVERALL_UPLOAD_LIMIT:-0}" |
| | ARIA2_MAX_UPLOAD_LIMIT="${ARIA2_MAX_UPLOAD_LIMIT:-0}" |
| |
|
| | |
| | mkdir -p "$(dirname "$ARIA2_SESSION_FILE")" \ |
| | "$(dirname "$ARIA2_LOG_FILE")" \ |
| | "$ARIA2_DOWNLOAD_DIR" |
| |
|
| | |
| | touch "$ARIA2_SESSION_FILE" |
| |
|
| | |
| | if ! check_port "$ARIA2_PORT"; then |
| | log "ERROR: Port $ARIA2_PORT is already in use" |
| | exit 1 |
| | fi |
| |
|
| | |
| | log "Generating aria2 configuration..." |
| | cat > /home/user/config/aria2.conf << EOF |
| | # aria2 configuration file |
| | # Basic Options |
| | dir=$ARIA2_DOWNLOAD_DIR |
| | input-file=$ARIA2_SESSION_FILE |
| | save-session=$ARIA2_SESSION_FILE |
| | save-session-interval=60 |
| | force-save=true |
| | log=$ARIA2_LOG_FILE |
| | log-level=info |
| | |
| | # RPC Options |
| | enable-rpc=true |
| | rpc-listen-all=true |
| | rpc-listen-port=$ARIA2_PORT |
| | rpc-secret=$ARIA2_SECRET |
| | rpc-allow-origin-all=true |
| | |
| | # Download Options |
| | max-concurrent-downloads=$ARIA2_MAX_CONCURRENT |
| | max-connection-per-server=$ARIA2_MAX_CONN_PER_SERVER |
| | split=$ARIA2_SPLIT |
| | min-split-size=1M |
| | continue=true |
| | max-overall-download-limit=$ARIA2_MAX_OVERALL_DOWNLOAD_LIMIT |
| | max-download-limit=$ARIA2_MAX_DOWNLOAD_LIMIT |
| | max-overall-upload-limit=$ARIA2_MAX_OVERALL_UPLOAD_LIMIT |
| | max-upload-limit=$ARIA2_MAX_UPLOAD_LIMIT |
| | |
| | # HTTP/HTTPS/FTP Options |
| | connect-timeout=60 |
| | timeout=60 |
| | max-tries=5 |
| | retry-wait=0 |
| | |
| | # BitTorrent Options |
| | bt-enable-lpd=true |
| | bt-lpd-interface=0.0.0.0 |
| | follow-torrent=true |
| | bt-max-open-files=16 |
| | bt-max-peers=$ARIA2_BT_MAX_PEERS |
| | bt-request-peer-speed-limit=50K |
| | bt-stop-timeout=0 |
| | bt-tracker-connect-timeout=10 |
| | bt-tracker-timeout=10 |
| | seed-time=0 |
| | |
| | # DHT Options |
| | enable-dht=true |
| | enable-peer-exchange=true |
| | |
| | # Metalink Options |
| | follow-metalink=true |
| | metalink-preferred-protocol=https |
| | |
| | # Advanced Options |
| | disable-ipv6=false |
| | disk-cache=$ARIA2_DISK_CACHE |
| | file-allocation=prealloc |
| | no-file-allocation-limit=32M |
| | |
| | # Error Handling |
| | max-file-not-found=0 |
| | check-integrity=true |
| | EOF |
| | EOF |
| |
|
| | log "Starting aria2 daemon..." |
| | aria2c --conf-path=/home/user/config/aria2.conf --daemon=true |
| |
|
| | |
| | if wait_for_aria2 "$ARIA2_PORT"; then |
| | log "aria2 daemon started successfully" |
| | log "RPC endpoint: http://localhost:$ARIA2_PORT/jsonrpc" |
| | log "RPC secret: $ARIA2_SECRET" |
| | log "Download directory: $ARIA2_DOWNLOAD_DIR" |
| | |
| | |
| | if [ -d "/usr/local/share/aria2" ]; then |
| | if check_port "$ARIA2_WEBUI_PORT"; then |
| | log "Starting aria2 web UI on port $ARIA2_WEBUI_PORT..." |
| | cd /usr/local/share/aria2 |
| | python3 -m http.server "$ARIA2_WEBUI_PORT" --bind 0.0.0.0 & |
| | WEBUI_PID=$! |
| | log "Web UI started with PID: $WEBUI_PID" |
| | log "Web UI URL: http://localhost:$ARIA2_WEBUI_PORT" |
| | else |
| | log "WARNING: Port $ARIA2_WEBUI_PORT is already in use, skipping web UI startup" |
| | fi |
| | else |
| | log "WARNING: AriaNg web UI not found in /usr/local/share/aria2" |
| | fi |
| | |
| | |
| | while true; do |
| | if ! nc -z localhost "$ARIA2_PORT" 2>/dev/null; then |
| | log "ERROR: aria2 daemon stopped unexpectedly" |
| | exit 1 |
| | fi |
| | sleep 30 |
| | done |
| | else |
| | log "ERROR: Failed to start aria2 daemon" |
| | exit 1 |
| | fi |