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