github.com/v2fly/v2ray-core/v4@v4.45.2/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/v4.codename=${CODENAME} -X github.com/v2fly/v2ray-core/v4.build=${BUILDNAME} -X github.com/v2fly/v2ray-core/v4.version=${VERSIONTAG}" 37 38 echo ">>> Compile v2ray ..." 39 env CGO_ENABLED=0 go build -o "$TMP"/v2ray"${EXESUFFIX}" -ldflags "$LDFLAGS" ./main 40 if [[ $GOOS == "windows" ]]; then 41 env CGO_ENABLED=0 go build -o "$TMP"/wv2ray"${EXESUFFIX}" -ldflags "-H windowsgui $LDFLAGS" ./main 42 fi 43 44 echo ">>> Compile v2ctl ..." 45 env CGO_ENABLED=0 go build -o "$TMP"/v2ctl"${EXESUFFIX}" -tags confonly -ldflags "$LDFLAGS" ./infra/control/main 46 } 47 48 build_dat() { 49 echo ">>> Download latest geoip.dat" 50 curl -s -L -o "$TMP"/geoip.dat "https://github.com/v2fly/geoip/raw/release/geoip.dat" 51 52 echo ">>> Download latest geoip-only-cn-private.dat" 53 curl -s -L -o "$TMP"/geoip-only-cn-private.dat "https://github.com/v2fly/geoip/raw/release/geoip-only-cn-private.dat" 54 55 echo ">>> Download latest geosite.dat" 56 curl -s -L -o "$TMP"/geosite.dat "https://github.com/v2fly/domain-list-community/raw/release/dlc.dat" 57 } 58 59 copyconf() { 60 echo ">>> Copying config..." 61 cd ./release/config 62 if [[ $GOOS == "linux" ]]; then 63 tar c --exclude "*.dat" . | tar x -C "$TMP" 64 else 65 tar c --exclude "*.dat" --exclude "systemd/**" . | tar x -C "$TMP" 66 fi 67 } 68 69 packzip() { 70 echo ">>> Generating zip package" 71 cd "$TMP" 72 local PKG=${SRCDIR}/v2ray-custom-${GOARCH}-${GOOS}-${PKGSUFFIX}${NOW}.zip 73 zip -r "$PKG" . 74 echo ">>> Generated: $(basename "$PKG") at $(dirname "$PKG")" 75 } 76 77 packtgz() { 78 echo ">>> Generating tgz package" 79 cd "$TMP" 80 local PKG=${SRCDIR}/v2ray-custom-${GOARCH}-${GOOS}-${PKGSUFFIX}${NOW}.tar.gz 81 tar cvfz "$PKG" . 82 echo ">>> Generated: $(basename "$PKG") at $(dirname "$PKG")" 83 } 84 85 packtgzAbPath() { 86 local ABPATH="$1" 87 echo ">>> Generating tgz package at $ABPATH" 88 cd "$TMP" 89 tar cvfz "$ABPATH" . 90 echo ">>> Generated: $ABPATH" 91 } 92 93 pkg=zip 94 nosource=0 95 nodat=0 96 noconf=0 97 GOOS=linux 98 GOARCH=amd64 99 EXESUFFIX= 100 PKGSUFFIX= 101 102 for arg in "$@"; do 103 case $arg in 104 386 | arm* | mips* | ppc64* | riscv64 | s390x) 105 GOARCH=$arg 106 ;; 107 windows) 108 GOOS=$arg 109 EXESUFFIX=.exe 110 ;; 111 darwin | dragonfly | freebsd | openbsd) 112 GOOS=$arg 113 ;; 114 nodat) 115 nodat=1 116 PKGSUFFIX=${PKGSUFFIX}nodat- 117 ;; 118 noconf) 119 noconf=1 120 ;; 121 nosource) 122 nosource=1 123 ;; 124 tgz) 125 pkg=tgz 126 ;; 127 abpathtgz=*) 128 pkg=${arg##abpathtgz=} 129 ;; 130 codename=*) 131 CODENAME=${arg##codename=} 132 ;; 133 buildname=*) 134 BUILDNAME=${arg##buildname=} 135 ;; 136 esac 137 done 138 139 if [[ $nosource != 1 ]]; then 140 get_source 141 fi 142 143 export GOOS GOARCH 144 echo "Build ARGS: GOOS=${GOOS} GOARCH=${GOARCH} CODENAME=${CODENAME} BUILDNAME=${BUILDNAME}" 145 echo "PKG ARGS: pkg=${pkg}" 146 build_v2 147 148 if [[ $nodat != 1 ]]; then 149 build_dat 150 fi 151 152 if [[ $noconf != 1 ]]; then 153 copyconf 154 fi 155 156 if [[ $pkg == "zip" ]]; then 157 packzip 158 elif [[ $pkg == "tgz" ]]; then 159 packtgz 160 else 161 packtgzAbPath "$pkg" 162 fi 163 164 cleanup