github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/release/user-package.sh (about) 1 #!/usr/bin/env bash 2 3 set -o errexit 4 set -o pipefail 5 set -o nounset 6 # set -o xtrace 7 8 trap 'echo -e "Aborted, error $? in command: $BASH_COMMAND"; trap ERR; exit 1' ERR 9 10 NOW=$(date '+%Y%m%d-%H%M%S') 11 TMP=$(mktemp -d) 12 SRCDIR=$(pwd) 13 14 CODENAME="user" 15 BUILDNAME=$NOW 16 17 cleanup() { rm -rf "$TMP"; } 18 trap cleanup INT TERM ERR 19 20 get_source() { 21 echo ">>> Clone v2fly/v2ray-core repo..." 22 git clone https://github.com/v2fly/v2ray-core.git 23 cd v2ray-core 24 go mod download 25 } 26 27 build_v2() { 28 if [[ $nosource != 1 ]]; then 29 cd ${SRCDIR}/v2ray-core 30 local VERSIONTAG=$(git describe --abbrev=0 --tags) 31 else 32 echo ">>> Use current directory as WORKDIR" 33 local VERSIONTAG=$(git describe --abbrev=0 --tags) 34 fi 35 36 LDFLAGS="-s -w -buildid= -X github.com/v2fly/v2ray-core/v5.codename=${CODENAME} -X github.com/v2fly/v2ray-core/v5.build=${BUILDNAME} -X github.com/v2fly/v2ray-core/v5.version=${VERSIONTAG}" 37 38 echo ">>> Compile v2ray ..." 39 env CGO_ENABLED=0 go build -o "$TMP"/v2ray"${EXESUFFIX}" -ldflags "$LDFLAGS" ./main 40 } 41 42 build_dat() { 43 echo ">>> Download latest geoip.dat" 44 curl -s -L -o "$TMP"/geoip.dat "https://github.com/v2fly/geoip/raw/release/geoip.dat" 45 46 echo ">>> Download latest geoip-only-cn-private.dat" 47 curl -s -L -o "$TMP"/geoip-only-cn-private.dat "https://github.com/v2fly/geoip/raw/release/geoip-only-cn-private.dat" 48 49 echo ">>> Download latest geosite.dat" 50 curl -s -L -o "$TMP"/geosite.dat "https://github.com/v2fly/domain-list-community/raw/release/dlc.dat" 51 } 52 53 copyconf() { 54 echo ">>> Copying config..." 55 cd ./release/config 56 if [[ $GOOS == "linux" ]]; then 57 tar c --exclude "*.dat" . | tar x -C "$TMP" 58 else 59 tar c --exclude "*.dat" --exclude "systemd/**" . | tar x -C "$TMP" 60 fi 61 } 62 63 packzip() { 64 echo ">>> Generating zip package" 65 cd "$TMP" 66 local PKG=${SRCDIR}/v2ray-custom-${GOARCH}-${GOOS}-${PKGSUFFIX}${NOW}.zip 67 zip -r "$PKG" . 68 echo ">>> Generated: $(basename "$PKG") at $(dirname "$PKG")" 69 } 70 71 packtgz() { 72 echo ">>> Generating tgz package" 73 cd "$TMP" 74 local PKG=${SRCDIR}/v2ray-custom-${GOARCH}-${GOOS}-${PKGSUFFIX}${NOW}.tar.gz 75 tar cvfz "$PKG" . 76 echo ">>> Generated: $(basename "$PKG") at $(dirname "$PKG")" 77 } 78 79 packtgzAbPath() { 80 local ABPATH="$1" 81 echo ">>> Generating tgz package at $ABPATH" 82 cd "$TMP" 83 tar cvfz "$ABPATH" . 84 echo ">>> Generated: $ABPATH" 85 } 86 87 pkg=zip 88 nosource=0 89 nodat=0 90 noconf=0 91 GOOS=linux 92 GOARCH=amd64 93 EXESUFFIX= 94 PKGSUFFIX= 95 96 for arg in "$@"; do 97 case $arg in 98 386 | arm* | mips* | ppc64* | riscv64 | loong64 | s390x) 99 GOARCH=$arg 100 ;; 101 windows) 102 GOOS=$arg 103 EXESUFFIX=.exe 104 ;; 105 darwin | dragonfly | freebsd | openbsd) 106 GOOS=$arg 107 ;; 108 nodat) 109 nodat=1 110 PKGSUFFIX=${PKGSUFFIX}nodat- 111 ;; 112 noconf) 113 noconf=1 114 ;; 115 nosource) 116 nosource=1 117 ;; 118 tgz) 119 pkg=tgz 120 ;; 121 abpathtgz=*) 122 pkg=${arg##abpathtgz=} 123 ;; 124 codename=*) 125 CODENAME=${arg##codename=} 126 ;; 127 buildname=*) 128 BUILDNAME=${arg##buildname=} 129 ;; 130 esac 131 done 132 133 if [[ $nosource != 1 ]]; then 134 get_source 135 fi 136 137 export GOOS GOARCH 138 echo "Build ARGS: GOOS=${GOOS} GOARCH=${GOARCH} CODENAME=${CODENAME} BUILDNAME=${BUILDNAME}" 139 echo "PKG ARGS: pkg=${pkg}" 140 build_v2 141 142 if [[ $nodat != 1 ]]; then 143 build_dat 144 fi 145 146 if [[ $noconf != 1 ]]; then 147 copyconf 148 fi 149 150 if [[ $pkg == "zip" ]]; then 151 packzip 152 elif [[ $pkg == "tgz" ]]; then 153 packtgz 154 else 155 packtgzAbPath "$pkg" 156 fi 157 158 cleanup