#!/bin/sh set -eu CONFIG_DIR="${CONFIG_DIR:-/config}" DOWNLOAD_DIR="${DOWNLOAD_DIR:-/downloads}" DEFAULTS_DIR="${DEFAULTS_DIR:-/defaults}" RUNTIME_DIR="${RUNTIME_DIR:-/run/aria2-rust-pro}" BASE_CONFIG="${CONFIG_DIR}/aria2.conf" SESSION_FILE="${CONFIG_DIR}/aria2.session" RUNTIME_CONFIG="${RUNTIME_DIR}/aria2.generated.conf" ARIA2_BIN="${ARIA2_BIN:-/usr/local/bin/aria2c}" SCRIPT_DIR="${CONFIG_DIR}/script" SCRIPT_CONFIG="${CONFIG_DIR}/script.conf" TRACKER_SNAPSHOT="${DEFAULTS_DIR}/bt-tracker.txt" explicit_aria2_command=0 if [ "$#" -gt 0 ]; then case "$1" in aria2c|aria2-rust-pro) explicit_aria2_command=1 shift ;; -*) ;; *) exec "$@" ;; esac fi if [ "${explicit_aria2_command}" -eq 1 ] && [ "$#" -gt 0 ]; then case "$1" in --version|-v|--help|-h|--help=*) exec "${ARIA2_BIN}" "$@" ;; esac fi mkdir -p "${CONFIG_DIR}" "${DOWNLOAD_DIR}" "${RUNTIME_DIR}" if [ ! -f "${BASE_CONFIG}" ]; then cp "${DEFAULTS_DIR}/aria2.conf" "${BASE_CONFIG}" fi if [ ! -f "${SESSION_FILE}" ]; then : > "${SESSION_FILE}" fi copy_default_if_missing() { src="$1" dest="$2" if [ ! -e "${dest}" ]; then cp "${src}" "${dest}" fi } copy_mode_script() { script_name="$1" mkdir -p "${SCRIPT_DIR}" copy_default_if_missing "${DEFAULTS_DIR}/script/${script_name}" "${SCRIPT_DIR}/${script_name}" chmod 755 "${SCRIPT_DIR}/${script_name}" } apply_bt_tracker_snapshot() { if [ ! -f "${TRACKER_SNAPSHOT}" ]; then return fi bundled_trackers="$( sed 's/\r$//' "${TRACKER_SNAPSHOT}" | awk 'NF {print}' | paste -sd, - )" if [ -z "${bundled_trackers}" ]; then return fi if grep -Eq '^bt-tracker=.+$' "${RUNTIME_CONFIG}"; then return fi if grep -q '^bt-tracker=' "${RUNTIME_CONFIG}"; then tmp_file="$(mktemp)" awk -v value="bt-tracker=${bundled_trackers}" ' BEGIN { replaced = 0 } /^bt-tracker=/ { print value; replaced = 1; next } { print } END { if (!replaced) print value } ' "${RUNTIME_CONFIG}" > "${tmp_file}" cat "${tmp_file}" > "${RUNTIME_CONFIG}" rm -f "${tmp_file}" return fi append_line "bt-tracker=${bundled_trackers}" } configure_special_mode() { mode="${SPECIAL_MODE:-}" case "${mode}" in "") return ;; move) copy_mode_script "move.sh" copy_default_if_missing "${DEFAULTS_DIR}/script.conf" "${SCRIPT_CONFIG}" append_line "on-download-complete=${SCRIPT_DIR}/move.sh" ;; rclone) copy_mode_script "upload.sh" copy_default_if_missing "${DEFAULTS_DIR}/script.conf" "${SCRIPT_CONFIG}" copy_default_if_missing "${DEFAULTS_DIR}/script/rclone.env" "${CONFIG_DIR}/rclone.env" if ! command -v rclone >/dev/null 2>&1; then printf 'warning: SPECIAL_MODE=rclone was requested but the rclone binary is not available\n' >&2 fi append_line "on-download-complete=${SCRIPT_DIR}/upload.sh" ;; *) printf 'warning: unknown SPECIAL_MODE "%s"; expected empty, move, or rclone\n' "${mode}" >&2 ;; esac } append_line() { printf '%s\n' "$1" >> "${RUNTIME_CONFIG}" } cp "${BASE_CONFIG}" "${RUNTIME_CONFIG}" append_line "" append_line "# Generated overrides" append_line "dir=${DOWNLOAD_DIR}" append_line "input-file=${SESSION_FILE}" append_line "save-session=${SESSION_FILE}" append_line "save-session-interval=60" append_line "enable-rpc=true" append_line "rpc-listen-port=${RPC_PORT:-6800}" append_line "listen-port=${LISTEN_PORT:-6888}" append_line "dht-listen-port=${LISTEN_PORT:-6888}" if [ "${IPV6_MODE:-false}" = "true" ] || [ "${IPV6_MODE:-false}" = "TRUE" ] || [ "${IPV6_MODE:-false}" = "1" ]; then append_line "disable-ipv6=false" else append_line "disable-ipv6=true" fi if [ -n "${DISK_CACHE:-}" ]; then append_line "disk-cache=${DISK_CACHE}" fi if [ -n "${RPC_SECRET:-}" ]; then append_line "rpc-secret=${RPC_SECRET}" append_line "rpc-listen-all=true" else printf 'warning: RPC_SECRET is empty; RPC stays loopback-only unless the base config overrides it\n' >&2 fi apply_bt_tracker_snapshot configure_special_mode if [ "${UPDATE_TRACKERS:-false}" = "true" ] || [ "${UPDATE_TRACKERS:-false}" = "TRUE" ] || [ "${UPDATE_TRACKERS:-false}" = "1" ]; then if ! "${DEFAULTS_DIR}/script/tracker.sh" "${RUNTIME_CONFIG}"; then printf 'warning: tracker update failed; continuing with bundled or existing tracker data\n' >&2 fi fi if [ -n "${PUID:-}" ] && [ -n "${PGID:-}" ]; then chown -R "${PUID}:${PGID}" "${CONFIG_DIR}" "${DOWNLOAD_DIR}" "${RUNTIME_DIR}" fi umask "${UMASK_SET:-022}" if [ -n "${PUID:-}" ] && [ -n "${PGID:-}" ]; then exec gosu "${PUID}:${PGID}" "${ARIA2_BIN}" --enable-rpc --conf-path="${RUNTIME_CONFIG}" "$@" fi exec "${ARIA2_BIN}" --enable-rpc --conf-path="${RUNTIME_CONFIG}" "$@"