mirror of
				https://github.com/MHSanaei/3x-ui.git
				synced 2025-10-30 20:02:51 +00:00 
			
		
		
		
	DockerInit rewritten, bash added to dockerfile at build stage
This commit is contained in:
		
							parent
							
								
									187084569f
								
							
						
					
					
						commit
						ef7a68abea
					
				
					 2 changed files with 49 additions and 148 deletions
				
			
		
							
								
								
									
										180
									
								
								DockerInit.sh
									
									
									
									
									
								
							
							
						
						
									
										180
									
								
								DockerInit.sh
									
									
									
									
									
								
							|  | @ -1,155 +1,55 @@ | ||||||
| #!/usr/bin/env bash | #!/bin/sh | ||||||
| set -euo pipefail | # POSIX-compatible script to download Xray binary and GeoIP/GeoSite databases | ||||||
| IFS=$'\n\t' |  | ||||||
| 
 | 
 | ||||||
| # ------------------------------------------------------------------------------ | set -eu | ||||||
| # DockerInit.sh — download and prepare Xray binaries and geolocation databases |  | ||||||
| # ------------------------------------------------------------------------------ |  | ||||||
| 
 | 
 | ||||||
| # Xray version | XRAY_VERSION="v25.6.8" | ||||||
| readonly XRAY_VERSION="v25.5.16" | BASE_URL="https://github.com/XTLS/Xray-core/releases/download/${XRAY_VERSION}" | ||||||
|  | DAT_DIR="build/bin" | ||||||
|  | ARCH="${1:-amd64}"  # Default to amd64 if not provided | ||||||
| 
 | 
 | ||||||
| # URL template for downloading Xray | # Map architecture | ||||||
| readonly XRAY_URL_TEMPLATE="https://github.com/XTLS/Xray-core/releases/download/${XRAY_VERSION}/Xray-linux-%s.zip" | case "$ARCH" in | ||||||
| 
 |     amd64)    ARCH_SUFFIX="64";        FNAME="amd64"     ;; | ||||||
| # Directories |     i386)     ARCH_SUFFIX="32";        FNAME="i386"      ;; | ||||||
| readonly BUILD_DIR="build/bin" |     armv8|arm64|aarch64) ARCH_SUFFIX="arm64-v8a"; FNAME="arm64" ;; | ||||||
| readonly ROOT_DIR="$(pwd)" |     armv7|arm|arm32)     ARCH_SUFFIX="arm32-v7a"; FNAME="arm32" ;; | ||||||
| 
 |     armv6)    ARCH_SUFFIX="arm32-v6";  FNAME="armv6"     ;; | ||||||
| # Check for required utilities |     *)        ARCH_SUFFIX="64";        FNAME="amd64"     ;; | ||||||
| check_dependencies() { |  | ||||||
|     local deps=(wget unzip) |  | ||||||
|     for cmd in "${deps[@]}"; do |  | ||||||
|         if ! command -v "$cmd" &> /dev/null; then |  | ||||||
|             echo "Error: Required utility '$cmd' is not installed. Please install it and try again." >&2 |  | ||||||
|             exit 1 |  | ||||||
|         fi |  | ||||||
|     done |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| # Print usage help |  | ||||||
| usage() { |  | ||||||
|     cat <<EOF |  | ||||||
| Usage: $0 <architecture> |  | ||||||
| 
 |  | ||||||
| Supported architectures: |  | ||||||
|   amd64    ? 64-bit x86 |  | ||||||
|   i386     ? 32-bit x86 |  | ||||||
|   armv8    ? ARM64-v8a (also accepts arm64, aarch64) |  | ||||||
|   armv7    ? ARM32-v7a (also accepts arm, arm32) |  | ||||||
|   armv6    ? ARM32-v6 |  | ||||||
| 
 |  | ||||||
| If no argument is provided or the argument is not recognized, 'amd64' will be used by default. |  | ||||||
| 
 |  | ||||||
| Example: |  | ||||||
|   $0 armv7 |  | ||||||
| EOF |  | ||||||
|     exit 1 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| # Determine ARCH and FNAME based on input argument |  | ||||||
| detect_arch() { |  | ||||||
|     local input="$1" |  | ||||||
| 
 |  | ||||||
|     case "$input" 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" |  | ||||||
|             ;; |  | ||||||
|         "") |  | ||||||
|             # If argument is empty, default to amd64 |  | ||||||
|             ARCH="64" |  | ||||||
|             FNAME="amd64" |  | ||||||
|             ;; |  | ||||||
|         *) |  | ||||||
|             echo "Warning: Architecture '$input' not recognized. Defaulting to 'amd64'." >&2 |  | ||||||
|             ARCH="64" |  | ||||||
|             FNAME="amd64" |  | ||||||
|             ;; |  | ||||||
| esac | esac | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| # Generic function to download a file by URL (with error handling) | echo "Selected architecture: $ARCH → $ARCH_SUFFIX → $FNAME" | ||||||
| download_file() { |  | ||||||
|     local url="$1" |  | ||||||
|     local output="$2" |  | ||||||
| 
 | 
 | ||||||
|     echo "Downloading: $url" | # Create directory | ||||||
|     if ! wget -q -O "$output" "$url"; then | mkdir -p "$DAT_DIR" | ||||||
|         echo "Error: Failed to download '$url'" >&2 | cd "$DAT_DIR" | ||||||
|         exit 1 |  | ||||||
|     fi |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| # Main function: create directory, download and unpack Xray, then geolocation databases |  | ||||||
| main() { |  | ||||||
|     # Check dependencies |  | ||||||
|     check_dependencies |  | ||||||
| 
 |  | ||||||
|     # Get architecture from argument |  | ||||||
|     local ARCH_ARG="${1-}" |  | ||||||
|     detect_arch "$ARCH_ARG" |  | ||||||
| 
 |  | ||||||
|     # Construct URL for Xray download |  | ||||||
|     local xray_url |  | ||||||
|     printf -v xray_url "$XRAY_URL_TEMPLATE" "$ARCH" |  | ||||||
| 
 |  | ||||||
|     # Create build directory |  | ||||||
|     echo "Creating directory: $BUILD_DIR" |  | ||||||
|     mkdir -p "$BUILD_DIR" |  | ||||||
|     cd "$BUILD_DIR" || exit 1 |  | ||||||
| 
 | 
 | ||||||
| # Download and unpack Xray | # Download and unpack Xray | ||||||
|     local xray_zip="Xray-linux-${ARCH}.zip" | XRAY_ZIP="Xray-linux-${ARCH_SUFFIX}.zip" | ||||||
|     download_file "$xray_url" "$xray_zip" | echo "Downloading Xray: $XRAY_ZIP" | ||||||
|     echo "Unpacking $xray_zip" | wget -q "${BASE_URL}/${XRAY_ZIP}" | ||||||
|     unzip -q "$xray_zip" | unzip -q "$XRAY_ZIP" | ||||||
|     rm -f "$xray_zip" geoip.dat geosite.dat | rm -f "$XRAY_ZIP" geoip.dat geosite.dat | ||||||
| 
 |  | ||||||
|     # Rename binary according to target architecture |  | ||||||
| mv xray "xray-linux-${FNAME}" | mv xray "xray-linux-${FNAME}" | ||||||
| chmod +x "xray-linux-${FNAME}" | chmod +x "xray-linux-${FNAME}" | ||||||
|  | echo "Xray extracted and renamed" | ||||||
| 
 | 
 | ||||||
|     # Return to project root | # Download primary databases | ||||||
|     cd "$ROOT_DIR" || exit 1 | echo "Downloading official geoip/geosite..." | ||||||
|  | wget -q https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat | ||||||
|  | wget -q https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat | ||||||
| 
 | 
 | ||||||
|     # Download standard GeoIP and GeoSite databases | # Download region-specific datasets | ||||||
|     echo "Downloading default GeoIP and GeoSite databases" | download_variant() { | ||||||
|     download_file "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat" "geoip.dat" |     REGION="$1" | ||||||
|     download_file "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat" "geosite.dat" |     BASE_URL="$2" | ||||||
| 
 |     echo "Downloading $REGION GeoIP/GeoSite..." | ||||||
|     # Download alternative GeoIP/GeoSite for Iran (IR) and Russia (RU) |     wget -q -O "geoip_${REGION}.dat"    "${BASE_URL}/geoip.dat" | ||||||
|     echo "Downloading alternative GeoIP/GeoSite for Iran" |     wget -q -O "geosite_${REGION}.dat"  "${BASE_URL}/geosite.dat" | ||||||
|     download_file "https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat" "geoip_IR.dat" |  | ||||||
|     download_file "https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat" "geosite_IR.dat" |  | ||||||
| 
 |  | ||||||
|     echo "Downloading alternative GeoIP/GeoSite for Russia" |  | ||||||
|     download_file "https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat" "geoip_RU.dat" |  | ||||||
|     download_file "https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat" "geosite_RU.dat" |  | ||||||
| 
 |  | ||||||
|     echo "Done." |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| # If -h or --help is passed, show usage | download_variant "IR" "https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download" | ||||||
| if [[ "${1-}" == "-h" || "${1-}" == "--help" ]]; then | download_variant "RU" "https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download" | ||||||
|     usage |  | ||||||
| fi |  | ||||||
| 
 | 
 | ||||||
| # Run main with the provided argument (if any) | echo "All files downloaded successfully." | ||||||
| main "${1-}" | cd ../../ | ||||||
|  |  | ||||||
|  | @ -9,7 +9,8 @@ RUN apk --no-cache --update add \ | ||||||
|   build-base \ |   build-base \ | ||||||
|   gcc \ |   gcc \ | ||||||
|   wget \ |   wget \ | ||||||
|   unzip |   unzip \ | ||||||
|  |   bash | ||||||
| 
 | 
 | ||||||
| COPY . . | COPY . . | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue
	
	 TorotinJenkins
						TorotinJenkins