File size: 4,219 Bytes
39ac2b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92f5884
 
 
 
 
47e088b
 
 
 
92f5884
 
 
 
39ac2b5
92f5884
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39ac2b5
 
 
 
 
 
 
 
92f5884
 
 
 
 
 
 
 
 
 
39ac2b5
5df8575
47e088b
 
 
5df8575
 
 
 
 
 
39ac2b5
 
 
 
 
 
 
 
47e088b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39ac2b5
 
 
 
 
 
 
 
 
5df8575
 
39ac2b5
5df8575
47e088b
 
39ac2b5
 
 
 
5df8575
39ac2b5
5df8575
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env bash
set -euo pipefail

if [[ -n "${QBT_PROFILE:-}" ]]; then
  CONFIG_DIR="$QBT_PROFILE"
elif [[ -d /data ]]; then
  CONFIG_DIR="/data/qbittorrent"
else
  CONFIG_DIR="/config"
fi

if [[ -n "${QBITTORRENT_SAVE_PATH:-}" ]]; then
  DOWNLOAD_DIR="$QBITTORRENT_SAVE_PATH"
elif [[ -d /data ]]; then
  DOWNLOAD_DIR="/data/downloads"
else
  DOWNLOAD_DIR="/downloads"
fi

DEFAULT_CONFIG="/defaults/qBittorrent.conf"
ACTIVE_CONFIG_DIR="$CONFIG_DIR/qBittorrent/config"
ACTIVE_CONFIG="$ACTIVE_CONFIG_DIR/qBittorrent.conf"
LEGACY_CONFIG="$CONFIG_DIR/qBittorrent.conf"
WEB_PORT="${PORT:-5000}"
TORRENT_PORT="${QBITTORRENT_TORRENT_PORT:-6881}"
TORRENT_SSL_PORT="${QBITTORRENT_TORRENT_SSL_PORT:-14906}"
KEEPALIVE_ENABLED="${KEEPALIVE_ENABLED:-true}"
KEEPALIVE_INTERVAL_SECONDS="${KEEPALIVE_INTERVAL_SECONDS:-900}"
KEEPALIVE_INITIAL_DELAY_SECONDS="${KEEPALIVE_INITIAL_DELAY_SECONDS:-60}"
KEEPALIVE_TIMEOUT_SECONDS="${KEEPALIVE_TIMEOUT_SECONDS:-20}"

mkdir -p "$CONFIG_DIR" "$ACTIVE_CONFIG_DIR" "$DOWNLOAD_DIR" "$DOWNLOAD_DIR/incomplete"

has_webui_config() {
  local config_file="$1"
  [[ -f "$config_file" ]] \
    && grep -q '^WebUI\\Username=' "$config_file" \
    && grep -q '^WebUI\\Password_PBKDF2=' "$config_file"
}

ensure_config_value() {
  local config_file="$1"
  local key="$2"
  local value="$3"

  if grep -q "^${key}=" "$config_file"; then
    sed -i "s|^${key}=.*|${key}=${value}|" "$config_file"
  else
    printf '%s=%s\n' "$key" "$value" >> "$config_file"
  fi
}

SOURCE_CONFIG="$DEFAULT_CONFIG"
if has_webui_config "$LEGACY_CONFIG"; then
  SOURCE_CONFIG="$LEGACY_CONFIG"
fi

# qBittorrent stores its real profile under <profile>/qBittorrent/config.
# Keep older /config/qBittorrent.conf mounts working by migrating them forward.
if ! has_webui_config "$ACTIVE_CONFIG"; then
  cp "$SOURCE_CONFIG" "$ACTIVE_CONFIG"
fi

ensure_config_value "$ACTIVE_CONFIG" 'Session\\Port' "$TORRENT_PORT"
ensure_config_value "$ACTIVE_CONFIG" 'Session\\SSL\\Port' "$TORRENT_SSL_PORT"
ensure_config_value "$ACTIVE_CONFIG" 'Session\\AddTorrentStopped' "false"
ensure_config_value "$ACTIVE_CONFIG" 'Session\\ShareLimitAction' "Stop"

cp "$ACTIVE_CONFIG" "$LEGACY_CONFIG"

shutdown() {
  if [[ -n "${KEEPALIVE_PID:-}" ]] && kill -0 "$KEEPALIVE_PID" 2>/dev/null; then
    kill "$KEEPALIVE_PID" 2>/dev/null || true
  fi
  if [[ -n "${BOT_PID:-}" ]] && kill -0 "$BOT_PID" 2>/dev/null; then
    kill "$BOT_PID" 2>/dev/null || true
  fi
  if [[ -n "${WEB_PID:-}" ]] && kill -0 "$WEB_PID" 2>/dev/null; then
    kill "$WEB_PID" 2>/dev/null || true
  fi
  if [[ -n "${QBT_PID:-}" ]] && kill -0 "$QBT_PID" 2>/dev/null; then
    kill "$QBT_PID" 2>/dev/null || true
  fi
  wait || true
}

trap shutdown SIGINT SIGTERM

resolve_keepalive_url() {
  if [[ -n "${KEEPALIVE_URL:-}" ]]; then
    printf '%s\n' "$KEEPALIVE_URL"
  elif [[ -n "${SPACE_HOST:-}" ]]; then
    printf 'https://%s/healthz\n' "$SPACE_HOST"
  else
    printf 'http://127.0.0.1:%s/healthz\n' "$WEB_PORT"
  fi
}

start_keepalive() {
  case "$KEEPALIVE_ENABLED" in
    true|TRUE|1|yes|YES|on|ON) ;;
    *)
      echo "[keepalive] Disabled"
      return
      ;;
  esac

  local keepalive_url
  keepalive_url="$(resolve_keepalive_url)"

  (
    sleep "$KEEPALIVE_INITIAL_DELAY_SECONDS" || true
    while true; do
      if curl -fsS --max-time "$KEEPALIVE_TIMEOUT_SECONDS" -A "torrent-web-keepalive/1.0" "$keepalive_url" >/dev/null; then
        echo "[keepalive] Ping OK: $keepalive_url"
      else
        echo "[keepalive] Ping failed: $keepalive_url"
      fi
      sleep "$KEEPALIVE_INTERVAL_SECONDS"
    done
  ) &
  KEEPALIVE_PID=$!
  echo "[entrypoint] Keepalive PID: $KEEPALIVE_PID"
}

qbittorrent-nox --profile="$CONFIG_DIR" &
QBT_PID=$!
echo "[entrypoint] qBittorrent PID: $QBT_PID"

gunicorn \
  --bind "0.0.0.0:${WEB_PORT}" \
  --workers "${GUNICORN_WORKERS:-2}" \
  --threads "${GUNICORN_THREADS:-4}" \
  --timeout "${GUNICORN_TIMEOUT:-120}" \
  app:app &
WEB_PID=$!
echo "[entrypoint] Web PID: $WEB_PID"

start_keepalive

echo "[entrypoint] Starting Telegram bot..."
python -m bot.bot &
BOT_PID=$!
echo "[entrypoint] Telegram bot PID: $BOT_PID"

WAIT_PIDS=("$QBT_PID" "$WEB_PID" "$BOT_PID")

wait -n "${WAIT_PIDS[@]}"
EXIT_CODE=$?
shutdown
exit "$EXIT_CODE"