From 3a2988c06812605fda074f240a284ee6c7202c8d Mon Sep 17 00:00:00 2001 From: Michael S2pac Date: Sun, 23 Nov 2025 16:43:17 +0300 Subject: [PATCH 1/3] DRY: unified sh script for geodata update --- x-ui.sh | 42 +++++++++--------- xray-tools.sh | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 20 deletions(-) create mode 100644 xray-tools.sh diff --git a/x-ui.sh b/x-ui.sh index e1648e51..d9112b50 100644 --- a/x-ui.sh +++ b/x-ui.sh @@ -6,6 +6,8 @@ blue='\033[0;34m' yellow='\033[0;33m' plain='\033[0m' +source xray-tools.sh + #Add some basic function here function LOGD() { echo -e "${yellow}[DEG] $* ${plain}" @@ -863,26 +865,26 @@ delete_ports() { fi } -update_all_geofiles() { - update_main_geofiles - update_ir_geofiles - update_ru_geofiles -} - -update_main_geofiles() { - wget -O geoip.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat - wget -O geosite.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat -} - -update_ir_geofiles() { - wget -O geoip_IR.dat https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat - wget -O geosite_IR.dat https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat -} - -update_ru_geofiles() { - wget -O geoip_RU.dat https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat - wget -O geosite_RU.dat https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat -} +#update_all_geofiles() { +# update_main_geofiles +# update_ir_geofiles +# update_ru_geofiles +#} +# +#update_main_geofiles() { +# wget -O geoip.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat +# wget -O geosite.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat +#} +# +#update_ir_geofiles() { +# wget -O geoip_IR.dat https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat +# wget -O geosite_IR.dat https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat +#} +# +#update_ru_geofiles() { +# wget -O geoip_RU.dat https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat +# wget -O geosite_RU.dat https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat +#} update_geo() { echo -e "${green}\t1.${plain} Loyalsoldier (geoip.dat, geosite.dat)" diff --git a/xray-tools.sh b/xray-tools.sh new file mode 100644 index 00000000..c28c8537 --- /dev/null +++ b/xray-tools.sh @@ -0,0 +1,117 @@ +#!/bin/sh + +update_all_geofiles() { + update_main_geofiles + update_ir_geofiles + update_ru_geofiles +} + +update_main_geofiles() { + wget -O geoip.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat + wget -O geosite.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat +} + +update_ir_geofiles() { + wget -O geoip_IR.dat https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat + wget -O geosite_IR.dat https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat +} + +update_ru_geofiles() { + wget -O geoip_RU.dat https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat + wget -O geosite_RU.dat https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat +} + +update_geodata_in_docker() { + WORKDIR="$1" + OLD_DIR=$(pwd) + trap 'cd "$OLD_DIR"' EXIT + + echo "[$(date)] Running update_geodata" + + if [ ! -d "$WORKDIR" ]; then + mkdir -p "$WORKDIR" + fi + cd "$WORKDIR" + + update_all_geofiles + echo "[$(date)] All geo files have been updated successfully!" +} + + +update_xray_core() { + TARGETARCH="$1" + WORKDIR="$2" + XRAY_VERSION="$3" + + OLD_DIR=$(pwd) + trap 'cd "$OLD_DIR"' EXIT + + echo "[$(date)] Running update_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 "$WORKDIR" ]; then + mkdir -p "$WORKDIR" + fi + cd "$WORKDIR" + + wget -q "https://github.com/XTLS/Xray-core/releases/download/${XRAY_VERSION}/Xray-linux-${ARCH}.zip" + unzip "Xray-linux-${ARCH}.zip" -d ./xray-unzip + cp ./xray-unzip/xray ./"xray-linux-${FNAME}" + rm -r xray-unzip + rm "Xray-linux-${ARCH}.zip" + } + +# --- dispatcher: вызываем функции по имени ТОЛЬКО если скрипт запущен как файл --- +# Предполагаем, что файл называется xray-updates.sh +if [ "${0##*/}" = "xray-tools.sh" ]; then + cmd="$1" + shift || true + + case "$cmd" in + update_xray_core) + # args: TARGETARCH WORKDIR XRAY_VERSION + update_xray_core "$@" + ;; + update_geodata_in_docker) + # args: WORKDIR + update_geodata_in_docker "$@" + ;; + ""|help|-h|--help) + echo "Usage:" + echo " $0 update_xray_core TARGETARCH WORKDIR XRAY_VERSION" + echo " $0 update_geodata_in_docker WORKDIR" + exit 1 + ;; + *) + echo "Unknown command: $cmd" >&2 + echo "Try: $0 help" >&2 + exit 1 + ;; + esac +fi \ No newline at end of file From e1058b1eafc413b4a953bd6a5c34e0a39c8051d5 Mon Sep 17 00:00:00 2001 From: Michael S2pac Date: Sun, 23 Nov 2025 17:16:16 +0300 Subject: [PATCH 2/3] Integrated unified sh scripts into docker build system + some cache updates --- .dockerignore | 4 +++- DockerEntrypoint.sh | 2 +- Dockerfile | 35 +++++++++++++++++++++++++++-------- docker-compose.yml | 8 +++++--- xray-tools.sh | 10 +++++----- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/.dockerignore b/.dockerignore index 096960c3..99bb78cc 100644 --- a/.dockerignore +++ b/.dockerignore @@ -6,4 +6,6 @@ Dockerfile docker-compose.yml .tmp .idea -.vscode \ No newline at end of file +.vscode +LICENSE +README.* \ No newline at end of file diff --git a/DockerEntrypoint.sh b/DockerEntrypoint.sh index 7511d2ea..277026b8 100644 --- a/DockerEntrypoint.sh +++ b/DockerEntrypoint.sh @@ -1,7 +1,7 @@ #!/bin/sh # Start fail2ban -[ $XUI_ENABLE_FAIL2BAN == "true" ] && fail2ban-client -x start +[ "$XUI_ENABLE_FAIL2BAN" = "true" ] && fail2ban-client -x start # Run x-ui exec /app/x-ui diff --git a/Dockerfile b/Dockerfile index cddc945c..e3d04a17 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,27 +2,44 @@ # Stage: Builder # ======================================================== FROM golang:1.25-alpine AS builder + WORKDIR /app -ARG TARGETARCH RUN apk --no-cache --update add \ build-base \ - gcc \ - wget \ - unzip + gcc + +# docker CACHE +COPY go.mod go.sum ./ +RUN go mod download COPY . . ENV CGO_ENABLED=1 ENV CGO_CFLAGS="-D_LARGEFILE64_SOURCE" RUN go build -ldflags "-w -s" -o build/x-ui main.go -RUN ./DockerInit.sh "$TARGETARCH" + +# ======================================================== +# Stage: Xray downloader +# ======================================================== +FROM alpine AS xray-downloader + +ARG TARGETARCH +ARG XRAY_VERSION + +WORKDIR /app +RUN apk add --no-cache wget unzip + +COPY xray-tools.sh . +RUN chmod +x /app/xray-tools.sh +RUN ./xray-tools.sh install_xray_core "$TARGETARCH" "/app/bin" "$XRAY_VERSION" +RUN ./xray-tools.sh update_geodata_in_docker "/app/bin" # ======================================================== # Stage: Final Image of 3x-ui # ======================================================== FROM alpine -ENV TZ=Asia/Tehran + WORKDIR /app RUN apk add --no-cache --update \ @@ -31,9 +48,11 @@ RUN apk add --no-cache --update \ fail2ban \ bash +COPY DockerEntrypoint.sh /app/ COPY --from=builder /app/build/ /app/ -COPY --from=builder /app/DockerEntrypoint.sh /app/ +#COPY --from=builder /app/DockerEntrypoint.sh /app/ COPY --from=builder /app/x-ui.sh /usr/bin/x-ui +COPY --from=xray-downloader /app/bin /app/bin # Configure fail2ban @@ -51,5 +70,5 @@ RUN chmod +x \ ENV XUI_ENABLE_FAIL2BAN="true" EXPOSE 2053 VOLUME [ "/etc/x-ui" ] -CMD [ "./x-ui" ] +#CMD [ "./x-ui" ] ENTRYPOINT [ "/app/DockerEntrypoint.sh" ] diff --git a/docker-compose.yml b/docker-compose.yml index 198df198..53f96a97 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,15 +2,17 @@ services: 3xui: build: context: . - dockerfile: ./Dockerfile + args: + XRAY_VERSION: "v25.10.15" + GEOUPDATE_CRON_SCHEDULE: "0 */6 * * *" container_name: 3xui_app - # hostname: yourhostname <- optional volumes: - $PWD/db/:/etc/x-ui/ - $PWD/cert/:/root/cert/ environment: + TZ: "Asia/Tehran" XRAY_VMESS_AEAD_FORCED: "false" XUI_ENABLE_FAIL2BAN: "true" tty: true network_mode: host - restart: unless-stopped + restart: unless-stopped \ No newline at end of file diff --git a/xray-tools.sh b/xray-tools.sh index c28c8537..a2ddd682 100644 --- a/xray-tools.sh +++ b/xray-tools.sh @@ -38,7 +38,7 @@ update_geodata_in_docker() { } -update_xray_core() { +install_xray_core() { TARGETARCH="$1" WORKDIR="$2" XRAY_VERSION="$3" @@ -46,7 +46,7 @@ update_xray_core() { OLD_DIR=$(pwd) trap 'cd "$OLD_DIR"' EXIT - echo "[$(date)] Running update_xray_core" + echo "[$(date)] Running install_xray_core" case $1 in amd64) @@ -94,9 +94,9 @@ if [ "${0##*/}" = "xray-tools.sh" ]; then shift || true case "$cmd" in - update_xray_core) + install_xray_core) # args: TARGETARCH WORKDIR XRAY_VERSION - update_xray_core "$@" + install_xray_core "$@" ;; update_geodata_in_docker) # args: WORKDIR @@ -104,7 +104,7 @@ if [ "${0##*/}" = "xray-tools.sh" ]; then ;; ""|help|-h|--help) echo "Usage:" - echo " $0 update_xray_core TARGETARCH WORKDIR XRAY_VERSION" + echo " $0 install_xray_core TARGETARCH WORKDIR XRAY_VERSION" echo " $0 update_geodata_in_docker WORKDIR" exit 1 ;; From 1012636d35f193732d0bb2fa38574cd24dfc0973 Mon Sep 17 00:00:00 2001 From: Michael S2pac Date: Sun, 23 Nov 2025 17:30:52 +0300 Subject: [PATCH 3/3] Quick docker refactoring --- Dockerfile | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index e3d04a17..56e6a3d2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ FROM golang:1.25-alpine AS builder WORKDIR /app -RUN apk --no-cache --update add \ +RUN apk add --no-cache \ build-base \ gcc @@ -28,12 +28,12 @@ ARG TARGETARCH ARG XRAY_VERSION WORKDIR /app -RUN apk add --no-cache wget unzip +RUN apk add --no-cache wget unzip && mkdir -p /app/bin COPY xray-tools.sh . -RUN chmod +x /app/xray-tools.sh -RUN ./xray-tools.sh install_xray_core "$TARGETARCH" "/app/bin" "$XRAY_VERSION" -RUN ./xray-tools.sh update_geodata_in_docker "/app/bin" +RUN chmod +x /app/xray-tools.sh && \ + ./xray-tools.sh install_xray_core "$TARGETARCH" "/app/bin" "$XRAY_VERSION" && \ + ./xray-tools.sh update_geodata_in_docker "/app/bin" # ======================================================== # Stage: Final Image of 3x-ui @@ -42,7 +42,7 @@ FROM alpine WORKDIR /app -RUN apk add --no-cache --update \ +RUN apk add --no-cache \ ca-certificates \ tzdata \ fail2ban \ @@ -50,7 +50,6 @@ RUN apk add --no-cache --update \ COPY DockerEntrypoint.sh /app/ COPY --from=builder /app/build/ /app/ -#COPY --from=builder /app/DockerEntrypoint.sh /app/ COPY --from=builder /app/x-ui.sh /usr/bin/x-ui COPY --from=xray-downloader /app/bin /app/bin @@ -70,5 +69,5 @@ RUN chmod +x \ ENV XUI_ENABLE_FAIL2BAN="true" EXPOSE 2053 VOLUME [ "/etc/x-ui" ] -#CMD [ "./x-ui" ] + ENTRYPOINT [ "/app/DockerEntrypoint.sh" ]