autoseed.sh
                        
                             · 2.5 KiB · Bash
                        
                    
                    
                      
                        Sin formato
                      
                    
                      
                    
                        
                          
                        
                    
                    
                
                
                
            #!/usr/bin/env bash
set -euo pipefail
# -----------------------------------------------------------------------------
# 依赖检查:aria2c、tmux 和 ufw(如果存在)
# -----------------------------------------------------------------------------
if ! command -v aria2c &> /dev/null; then
  echo "[INFO] Installing aria2..."
  sudo apt-get update && sudo apt-get install -y aria2
fi
if ! command -v tmux &> /dev/null; then
  echo "[INFO] Installing tmux..."
  sudo apt-get update && sudo apt-get install -y tmux
fi
# (可选)如果你没有 ufw,也可以忽略此依赖检查
if command -v ufw &> /dev/null; then
  echo "[INFO] ufw detected."
else
  echo "[INFO] ufw not installed or not in PATH; skipping firewall rules."
fi
# -----------------------------------------------------------------------------
# 查找 .torrent 文件
# -----------------------------------------------------------------------------
shopt -s nullglob
torrent_files=( *.torrent )
if [[ ${#torrent_files[@]} -eq 0 ]]; then
  echo "[INFO] 当前目录下没有找到 .torrent 文件,退出。"
  exit 0
fi
# -----------------------------------------------------------------------------
# 针对每个 .torrent,创建 tmux 会话并启动做种(随机高端口 + ufw 放行)
# -----------------------------------------------------------------------------
for torrent in "${torrent_files[@]}"; do
  # 会话名:seed-<basename>
  base="$(basename "$torrent" .torrent)"
  session_name="seed-$base"
  # 如果会话已存在则跳过
  if tmux has-session -t "$session_name" 2> /dev/null; then
    echo "[INFO] 会话 $session_name 已存在,跳过。"
    continue
  fi
  # 随机选一个高端口(49152–65535)
  if command -v shuf &> /dev/null; then
    port=$(shuf -i 49152-65535 -n 1)
  else
    port=$(( RANDOM % 16384 + 49152 ))
  fi
  # 如果 ufw 已启用,则添加放行规则;若已放行则输出提示
  if command -v ufw &> /dev/null && sudo ufw status | grep -q "Status: active"; then
    echo "[INFO] 放行端口 $port/tcp 到 ufw..."
    sudo ufw allow "${port}/tcp" || echo "[INFO] 端口 $port 已经放行"
  fi
  echo "[INFO] 启动会话 $session_name,做种文件:$torrent (端口:$port)"
  tmux new-session -d -s "$session_name" -- \
    aria2c \
      --enable-dht=true \
      --enable-dht6=true \
      --enable-peer-exchange=true \
      --bt-seed-unverified=true \
      --bt-save-metadata=true \
      --dir="$(pwd)" \
      --listen-port="$port" \
      "$torrent"
done
echo "[INFO] 所有做种会话已启动。"
                | 1 | #!/usr/bin/env bash | 
| 2 | set -euo pipefail | 
| 3 | |
| 4 | # ----------------------------------------------------------------------------- | 
| 5 | # 依赖检查:aria2c、tmux 和 ufw(如果存在) | 
| 6 | # ----------------------------------------------------------------------------- | 
| 7 | if ! command -v aria2c &> /dev/null; then | 
| 8 | echo "[INFO] Installing aria2..." | 
| 9 | sudo apt-get update && sudo apt-get install -y aria2 | 
| 10 | fi | 
| 11 | |
| 12 | if ! command -v tmux &> /dev/null; then | 
| 13 | echo "[INFO] Installing tmux..." | 
| 14 | sudo apt-get update && sudo apt-get install -y tmux | 
| 15 | fi | 
| 16 | |
| 17 | # (可选)如果你没有 ufw,也可以忽略此依赖检查 | 
| 18 | if command -v ufw &> /dev/null; then | 
| 19 | echo "[INFO] ufw detected." | 
| 20 | else | 
| 21 | echo "[INFO] ufw not installed or not in PATH; skipping firewall rules." | 
| 22 | fi | 
| 23 | |
| 24 | # ----------------------------------------------------------------------------- | 
| 25 | # 查找 .torrent 文件 | 
| 26 | # ----------------------------------------------------------------------------- | 
| 27 | shopt -s nullglob | 
| 28 | torrent_files=( *.torrent ) | 
| 29 | |
| 30 | if [[ ${#torrent_files[@]} -eq 0 ]]; then | 
| 31 | echo "[INFO] 当前目录下没有找到 .torrent 文件,退出。" | 
| 32 | exit 0 | 
| 33 | fi | 
| 34 | |
| 35 | # ----------------------------------------------------------------------------- | 
| 36 | # 针对每个 .torrent,创建 tmux 会话并启动做种(随机高端口 + ufw 放行) | 
| 37 | # ----------------------------------------------------------------------------- | 
| 38 | for torrent in "${torrent_files[@]}"; do | 
| 39 | # 会话名:seed-<basename> | 
| 40 | base="$(basename "$torrent" .torrent)" | 
| 41 | session_name="seed-$base" | 
| 42 | |
| 43 | # 如果会话已存在则跳过 | 
| 44 | if tmux has-session -t "$session_name" 2> /dev/null; then | 
| 45 | echo "[INFO] 会话 $session_name 已存在,跳过。" | 
| 46 | continue | 
| 47 | fi | 
| 48 | |
| 49 | # 随机选一个高端口(49152–65535) | 
| 50 | if command -v shuf &> /dev/null; then | 
| 51 | port=$(shuf -i 49152-65535 -n 1) | 
| 52 | else | 
| 53 | port=$(( RANDOM % 16384 + 49152 )) | 
| 54 | fi | 
| 55 | |
| 56 | # 如果 ufw 已启用,则添加放行规则;若已放行则输出提示 | 
| 57 | if command -v ufw &> /dev/null && sudo ufw status | grep -q "Status: active"; then | 
| 58 | echo "[INFO] 放行端口 $port/tcp 到 ufw..." | 
| 59 | sudo ufw allow "${port}/tcp" || echo "[INFO] 端口 $port 已经放行" | 
| 60 | fi | 
| 61 | |
| 62 | echo "[INFO] 启动会话 $session_name,做种文件:$torrent (端口:$port)" | 
| 63 | tmux new-session -d -s "$session_name" -- \ | 
| 64 | aria2c \ | 
| 65 | --enable-dht=true \ | 
| 66 | --enable-dht6=true \ | 
| 67 | --enable-peer-exchange=true \ | 
| 68 | --bt-seed-unverified=true \ | 
| 69 | --bt-save-metadata=true \ | 
| 70 | --dir="$(pwd)" \ | 
| 71 | --listen-port="$port" \ | 
| 72 | "$torrent" | 
| 73 | done | 
| 74 | |
| 75 | echo "[INFO] 所有做种会话已启动。" | 
| 76 |