Add geodata-cron-runner

This commit is contained in:
Michael S2pac 2026-01-18 21:15:31 +03:00
parent 70e37e09c7
commit b55fcd130b
5 changed files with 55 additions and 217 deletions

22
Dockerfile-cron-runner Normal file
View file

@ -0,0 +1,22 @@
FROM alpine:3.20
ARG TARGETARCH
ARG XRAY_VERSION
ARG XRAY_BUILD_DIR
WORKDIR /app
RUN apk add --no-cache \
wget \
curl \
bash \
ca-certificates \
tzdata
COPY lib/geo.sh \
docker-cron-runner/entrypoint.sh \
docker-cron-runner/cron-job-script.sh ./
RUN chmod +x geo.sh entrypoint.sh cron-job-script.sh
ENTRYPOINT ["/app/entrypoint.sh"]

View file

@ -14,45 +14,41 @@ services:
TZ: "UTC"
XRAY_VMESS_AEAD_FORCED: "false"
XUI_ENABLE_FAIL2BAN: "true"
XUI_MAIN_FOLDER: "/usr/local/x-ui"
tty: true
network_mode: host
restart: unless-stopped
# If you don't need automatic geodata update, just disable `docker-proxy` and `geodata-cron` containers
# docker-proxy:
# image: tecnativa/docker-socket-proxy
# container_name: docker_proxy
# restart: unless-stopped
# environment:
# - CONTAINERS=1
# - POST=1
# - ALLOW_RESTARTS=1
# volumes:
# - /var/run/docker.sock:/var/run/docker.sock:ro
# networks:
# - docker-internal
#
# geodata-cron:
# build:
# dockerfile: Dockerfile-cron-runner
# args:
# XRAY_VERSION: "${XRAY_VERSION:-v25.10.15}"
# XRAY_BUILD_DIR: "/app/xray"
# container_name: geodata_cron
# restart: unless-stopped
# depends_on:
# - docker-proxy
# environment:
# TZ: "UTC"
# DOCKER_PROXY_URL: "http://docker-proxy:2375"
# TARGET_CONTAINER_NAME: "3xui_app"
# CRON_SCHEDULE: "*/5 * * * *" #"0 */6 * * *"
# SHARED_VOLUME_PATH: "/app/bin"
# volumes:
# - $PWD/geodata/:/app/bin/
# networks:
# - docker-internal
docker-proxy:
image: tecnativa/docker-socket-proxy
container_name: docker_proxy
restart: unless-stopped
environment:
- CONTAINERS=1
- POST=1
- ALLOW_RESTARTS=1
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
networks:
- docker-internal
geodata-cron:
build:
dockerfile: Dockerfile-cron-runner
container_name: geodata_cron
restart: unless-stopped
depends_on:
- docker-proxy
environment:
TZ: "UTC"
DOCKER_PROXY_URL: "http://docker-proxy:2375"
TARGET_CONTAINER_NAME: "3xui_app"
CRON_SCHEDULE: "*/5 * * * *" #"0 */6 * * *"
SHARED_VOLUME_PATH: "/app/bin"
volumes:
- $PWD/geodata/:/app/bin/
networks:
- docker-internal
networks:
docker-internal:

View file

@ -3,14 +3,7 @@ set -eu
echo "[$(date)] Starting geodata update..."
FINISHED_FLAG="${SHARED_VOLUME_PATH}/cron-job-finished.txt"
if [ -f "$FINISHED_FLAG" ]; then
rm -f "$FINISHED_FLAG"
fi
/app/xray-tools.sh update_geodata_in_docker "${SHARED_VOLUME_PATH}"
touch "$FINISHED_FLAG"
/app/geo.sh update_all_geofiles "${SHARED_VOLUME_PATH}"
echo "[$(date)] Geodata update finished, restarting container..."

View file

@ -18,8 +18,8 @@ mkdir -p /var/log
touch /var/log/cron.log
mkdir -p "$SHARED_VOLUME_PATH"
cp -r "$XRAY_BUILD_DIR"/* "$SHARED_VOLUME_PATH"/
#cp -r "$XRAY_BUILD_DIR"/* "$SHARED_VOLUME_PATH"/
touch "$SHARED_VOLUME_PATH/cron-job-finished.txt" # cron job execution imitation
#touch "$SHARED_VOLUME_PATH/cron-job-finished.txt" # cron job execution imitation
exec crond -f -l 2

View file

@ -1,173 +0,0 @@
#!/bin/sh
safe_download_and_update() {
url="$1"
dest="$2"
# Create a temporary file
tmp=$(mktemp "${dest}.XXXXXX") || return 1
# Download file into a temporary location
if wget -q -O "$tmp" "$url"; then
# Check that the downloaded file is not empty
if [ -s "$tmp" ]; then
# Atomically replace the destination file
mv "$tmp" "$dest"
echo "[OK] Downloaded: $dest"
else
echo "[ERR] Downloaded file is empty: $url"
rm -f "$tmp"
return 1
fi
else
echo "[ERR] Failed to download: $url"
rm -f "$tmp"
return 1
fi
}
update_all_geofiles() {
update_main_geofiles
update_ir_geofiles
update_ru_geofiles
}
update_main_geofiles() {
safe_download_and_update \
"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat" \
"geoip.dat"
safe_download_and_update \
"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat" \
"geosite.dat"
}
update_ir_geofiles() {
safe_download_and_update \
"https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat" \
"geoip_IR.dat"
safe_download_and_update \
"https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat" \
"geosite_IR.dat"
}
update_ru_geofiles() {
safe_download_and_update \
"https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat" \
"geoip_RU.dat"
safe_download_and_update \
"https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat" \
"geosite_RU.dat"
}
update_geodata_in_docker() {
XRAYDIR="$1"
OLD_DIR=$(pwd)
trap 'cd "$OLD_DIR"' EXIT
echo "[$(date)] Running update_geodata"
if [ ! -d "$XRAYDIR" ]; then
mkdir -p "$XRAYDIR"
fi
cd "$XRAYDIR"
update_all_geofiles
echo "[$(date)] All geo files have been updated successfully!"
}
install_xray_core() {
TARGETARCH="$1"
XRAYDIR="$2"
XRAY_VERSION="$3"
OLD_DIR=$(pwd)
trap 'cd "$OLD_DIR"' EXIT
echo "[$(date)] Running install_xray_core"
case $1 in
amd64)
ARCH="64"
FNAME="amd64"
;;
i386)
ARCH="32"
FNAME="i386"
;;
armv8 | arm64 | aarch64)
ARCH="arm64-v8a"
FNAME="arm64"
;;
armv7 | arm | arm32)
ARCH="arm32-v7a"
FNAME="arm32"
;;
armv6)
ARCH="arm32-v6"
FNAME="armv6"
;;
*)
ARCH="64"
FNAME="amd64"
;;
esac
if [ ! -d "$XRAYDIR" ]; then
mkdir -p "$XRAYDIR"
fi
cd "$XRAYDIR"
wget -q "https://github.com/XTLS/Xray-core/releases/download/${XRAY_VERSION}/Xray-linux-${ARCH}.zip"
# Validate the downloaded zip file
if [ ! -f "Xray-linux-${ARCH}.zip" ] || [ ! -s "Xray-linux-${ARCH}.zip" ]; then
echo "[ERR] Failed to download Xray-core zip or file is empty"
exit 1
fi
unzip -q "Xray-linux-${ARCH}.zip" -d ./xray-unzip
# Validate the extracted xray binary
if [ -f "./xray-unzip/xray" ]; then
cp ./xray-unzip/xray ./"xray-linux-${FNAME}"
rm -r xray-unzip
rm "Xray-linux-${ARCH}.zip"
else
echo "[ERR] Failed to extract xray binary"
exit 1
fi
}
if [ "${0##*/}" = "xray-tools.sh" ]; then
cmd="$1"
shift || true
case "$cmd" in
install_xray_core)
# args: TARGETARCH XRAYDIR XRAY_VERSION
install_xray_core "$@"
;;
update_geodata_in_docker)
# args: XRAYDIR
update_geodata_in_docker "$@"
;;
update_all_geofiles)
update_all_geofiles
;;
""|help|-h|--help)
echo "Usage:"
echo " $0 install_xray_core TARGETARCH XRAYDIR XRAY_VERSION"
echo " $0 update_geodata_in_docker XRAYDIR"
exit 0
;;
*)
echo "Unknown command: $cmd" >&2
echo "Try: $0 help" >&2
exit 1
;;
esac
fi