From eefa53f6b1734382b8265f1637d7bed56e19c67d Mon Sep 17 00:00:00 2001 From: xymopen Date: Tue, 7 Jan 2020 23:22:25 +0800 Subject: [PATCH 1/4] style: Remove tailing space --- release/install-release.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/release/install-release.sh b/release/install-release.sh index 01ee2aeafc..689b2f75c9 100755 --- a/release/install-release.sh +++ b/release/install-release.sh @@ -146,11 +146,11 @@ installSoftware(){ getPMT if [[ $? -eq 1 ]]; then colorEcho ${RED} "The system package manager tool isn't APT or YUM, please install ${COMPONENT} manually." - return 1 + return 1 fi if [[ $SOFTWARE_UPDATED -eq 0 ]]; then colorEcho ${BLUE} "Updating software repo" - $CMD_UPDATE + $CMD_UPDATE SOFTWARE_UPDATED=1 fi @@ -376,7 +376,7 @@ remove(){ colorEcho ${GREEN} "Removed V2Ray successfully." colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually." return 0 - fi + fi else colorEcho ${YELLOW} "V2Ray not found." return 0 @@ -404,7 +404,7 @@ main(){ [[ "$HELP" == "1" ]] && Help && return [[ "$CHECK" == "1" ]] && checkUpdate && return [[ "$REMOVE" == "1" ]] && remove && return - + sysArch # extract local file if [[ $LOCAL_INSTALL -eq 1 ]]; then From 77a151080c45190c49aac8b0bf9ed480136e8ec9 Mon Sep 17 00:00:00 2001 From: xymopen Date: Tue, 7 Jan 2020 23:06:25 +0800 Subject: [PATCH 2/4] refactor: Minor changes to install-release.sh * Add missing CLI argument variables * Remove key, COLOR variables * Standardize EXTRACT_ONLY, ERROR_IF_UPTODATE variables * Print output to stdout * Print help using redirection * Combine configuration conditions --- release/install-release.sh | 73 ++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/release/install-release.sh b/release/install-release.sh index 689b2f75c9..0d9fc3efc3 100755 --- a/release/install-release.sh +++ b/release/install-release.sh @@ -9,16 +9,26 @@ # 2: Application error # 3: Network error +# CLI arguments +PROXY='' +HELP='' +FORCE='' +CHECK='' +REMOVE='' +VERSION='' +VSRC_ROOT='/tmp/v2ray' +EXTRACT_ONLY='' +LOCAL='' +LOCAL_INSTALL='' +DIST_SRC='github' +ERROR_IF_UPTODATE='' + CUR_VER="" NEW_VER="" ARCH="" VDIS="64" ZIPFILE="/tmp/v2ray/v2ray.zip" V2RAY_RUNNING=0 -VSRC_ROOT="/tmp/v2ray" -EXTRACT_ONLY=0 -ERROR_IF_UPTODATE=0 -DIST_SRC="github" CMD_INSTALL="" CMD_UPDATE="" @@ -27,10 +37,6 @@ SOFTWARE_UPDATED=0 SYSTEMCTL_CMD=$(command -v systemctl 2>/dev/null) SERVICE_CMD=$(command -v service 2>/dev/null) -CHECK="" -FORCE="" -HELP="" - #######color code######## RED="31m" # Error message GREEN="32m" # Success message @@ -39,9 +45,8 @@ BLUE="36m" # Info message ######################### -while [[ $# > 0 ]];do - key="$1" - case $key in +while [[ $# > 0 ]]; do + case "$1" in -p|--proxy) PROXY="-x ${2}" shift # past argument @@ -90,8 +95,7 @@ done ############################### colorEcho(){ - COLOR=$1 - echo -e "\033[${COLOR}${@:2}\033[0m" + echo -e "\033[${1}${@:2}\033[0m" 1>& 2 } sysArch(){ @@ -242,9 +246,7 @@ stopV2ray(){ } startV2ray(){ - if [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/lib/systemd/system/v2ray.service" ]; then - ${SYSTEMCTL_CMD} start v2ray - elif [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/etc/systemd/system/v2ray.service" ]; then + if [ -n "${SYSTEMCTL_CMD}" ] && [[ -f "/lib/systemd/system/v2ray.service" || -f "/etc/systemd/system/v2ray.service" ]]; then ${SYSTEMCTL_CMD} start v2ray elif [ -n "${SERVICE_CMD}" ] && [ -f "/etc/init.d/v2ray" ]; then ${SERVICE_CMD} v2ray start @@ -306,33 +308,28 @@ installV2Ray(){ installInitScript(){ - if [[ -n "${SYSTEMCTL_CMD}" ]];then - if [[ ! -f "/etc/systemd/system/v2ray.service" ]]; then - if [[ ! -f "/lib/systemd/system/v2ray.service" ]]; then - cp "${VSRC_ROOT}/systemd/v2ray.service" "/etc/systemd/system/" - systemctl enable v2ray.service - fi - fi - return + if [[ -n "${SYSTEMCTL_CMD}" ]] && [[ ! -f "/etc/systemd/system/v2ray.service" && ! -f "/lib/systemd/system/v2ray.service" ]]; then + cp "${VSRC_ROOT}/systemd/v2ray.service" "/etc/systemd/system/" + systemctl enable v2ray.service elif [[ -n "${SERVICE_CMD}" ]] && [[ ! -f "/etc/init.d/v2ray" ]]; then installSoftware "daemon" || return $? cp "${VSRC_ROOT}/systemv/v2ray" "/etc/init.d/v2ray" chmod +x "/etc/init.d/v2ray" update-rc.d v2ray defaults fi - return } Help(){ - echo "./install-release.sh [-h] [-c] [--remove] [-p proxy] [-f] [--version vx.y.z] [-l file]" - echo " -h, --help Show help" - echo " -p, --proxy To download through a proxy server, use -p socks5://127.0.0.1:1080 or -p http://127.0.0.1:3128 etc" - echo " -f, --force Force install" - echo " --version Install a particular version, use --version v3.15" - echo " -l, --local Install from a local file" - echo " --remove Remove installed V2Ray" - echo " -c, --check Check for update" - return 0 + cat - 1>& 2 << EOF +./install-release.sh [-h] [-c] [--remove] [-p proxy] [-f] [--version vx.y.z] [-l file] + -h, --help Show help + -p, --proxy To download through a proxy server, use -p socks5://127.0.0.1:1080 or -p http://127.0.0.1:3128 etc + -f, --force Force install + --version Install a particular version, use --version v3.15 + -l, --local Install from a local file + --remove Remove installed V2Ray + -c, --check Check for update +EOF } remove(){ @@ -431,7 +428,7 @@ main(){ RETVAL="$?" if [[ $RETVAL == 0 ]] && [[ "$FORCE" != "1" ]]; then colorEcho ${BLUE} "Latest version ${CUR_VER} is already installed." - if [[ "${ERROR_IF_UPTODATE}" == "1" ]]; then + if [ -n "${ERROR_IF_UPTODATE}" ]; then return 10 fi return @@ -443,9 +440,9 @@ main(){ installSoftware unzip || return $? extract ${ZIPFILE} || return $? fi - fi - - if [[ "${EXTRACT_ONLY}" == "1" ]]; then + fi + + if [ -n "${EXTRACT_ONLY}" ]; then colorEcho ${GREEN} "V2Ray extracted to ${VSRC_ROOT}, and exiting..." return 0 fi From 410457e1e39f7128a14459aa063163e6bbe0a681 Mon Sep 17 00:00:00 2001 From: xymopen Date: Tue, 7 Jan 2020 23:21:34 +0800 Subject: [PATCH 3/4] refactor: Extract `normalizeVersion()` --- release/install-release.sh | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/release/install-release.sh b/release/install-release.sh index 0d9fc3efc3..63dbca3bc0 100755 --- a/release/install-release.sh +++ b/release/install-release.sh @@ -198,27 +198,33 @@ extract(){ return 0 } +normalizeVersion() { + if [ -n "$1" ]; then + case "$1" in + v*) + echo "$1" + ;; + *) + echo "v$1" + ;; + esac + else + echo "" + fi +} # 1: new V2Ray. 0: no. 2: not installed. 3: check failed. 4: don't check. getVersion(){ if [[ -n "$VERSION" ]]; then - NEW_VER="$VERSION" - if [[ ${NEW_VER} != v* ]]; then - NEW_VER=v${NEW_VER} - fi + NEW_VER="$(normalizeVersion "$VERSION")" return 4 else - VER=`/usr/bin/v2ray/v2ray -version 2>/dev/null` - RETVAL="$?" - CUR_VER=`echo $VER | head -n 1 | cut -d " " -f2` - if [[ ${CUR_VER} != v* ]]; then - CUR_VER=v${CUR_VER} - fi + VER="$(/usr/bin/v2ray/v2ray -version 2>/dev/null)" + RETVAL=$? + CUR_VER="$(normalizeVersion "$(echo "$VER" | head -n 1 | cut -d " " -f2)")" TAG_URL="https://api.github.com/repos/v2ray/v2ray-core/releases/latest" - NEW_VER=`curl ${PROXY} -s ${TAG_URL} --connect-timeout 10| grep 'tag_name' | cut -d\" -f4` - if [[ ${NEW_VER} != v* ]]; then - NEW_VER=v${NEW_VER} - fi + NEW_VER="$(normalizeVersion "$(curl ${PROXY} -s "${TAG_URL}" --connect-timeout 10| grep 'tag_name' | cut -d\" -f4)")" + if [[ $? -ne 0 ]] || [[ $NEW_VER == "" ]]; then colorEcho ${RED} "Failed to fetch release information. Please check your network or try again." return 3 From 01365e92d258bf31a5966483134036dd2a030398 Mon Sep 17 00:00:00 2001 From: xymopen Date: Wed, 8 Jan 2020 10:40:48 +0800 Subject: [PATCH 4/4] refactor: Remove side effect from `sysArch()` --- release/install-release.sh | 72 ++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/release/install-release.sh b/release/install-release.sh index 63dbca3bc0..4562adea5e 100755 --- a/release/install-release.sh +++ b/release/install-release.sh @@ -25,8 +25,7 @@ ERROR_IF_UPTODATE='' CUR_VER="" NEW_VER="" -ARCH="" -VDIS="64" +VDIS='' ZIPFILE="/tmp/v2ray/v2ray.zip" V2RAY_RUNNING=0 @@ -98,30 +97,47 @@ colorEcho(){ echo -e "\033[${1}${@:2}\033[0m" 1>& 2 } -sysArch(){ - ARCH=$(uname -m) - if [[ "$ARCH" == "i686" ]] || [[ "$ARCH" == "i386" ]]; then - VDIS="32" - elif [[ "$ARCH" == *"armv7"* ]] || [[ "$ARCH" == "armv6l" ]]; then - VDIS="arm" - elif [[ "$ARCH" == *"armv8"* ]] || [[ "$ARCH" == "aarch64" ]]; then - VDIS="arm64" - elif [[ "$ARCH" == *"mips64le"* ]]; then - VDIS="mips64le" - elif [[ "$ARCH" == *"mips64"* ]]; then - VDIS="mips64" - elif [[ "$ARCH" == *"mipsle"* ]]; then - VDIS="mipsle" - elif [[ "$ARCH" == *"mips"* ]]; then - VDIS="mips" - elif [[ "$ARCH" == *"s390x"* ]]; then - VDIS="s390x" - elif [[ "$ARCH" == "ppc64le" ]]; then - VDIS="ppc64le" - elif [[ "$ARCH" == "ppc64" ]]; then - VDIS="ppc64" - fi - return 0 +archAffix(){ + case "${1:-"$(uname -m)"}" in + i686|i386) + echo '32' + ;; + x86_64|amd64) + echo '64' + ;; + *armv7*|armv6l) + echo 'arm' + ;; + *armv8*|aarch64) + echo 'arm64' + ;; + *mips64le*) + echo 'mips64le' + ;; + *mips64*) + echo 'mips64' + ;; + *mipsle*) + echo 'mipsle' + ;; + *mips*) + echo 'mips' + ;; + *s390x*) + echo 's390x' + ;; + ppc64le) + echo 'ppc64le' + ;; + ppc64) + echo 'ppc64' + ;; + *) + return 1 + ;; + esac + + return 0 } downloadV2Ray(){ @@ -408,7 +424,9 @@ main(){ [[ "$CHECK" == "1" ]] && checkUpdate && return [[ "$REMOVE" == "1" ]] && remove && return - sysArch + local ARCH=$(uname -m) + VDIS="$(archAffix)" + # extract local file if [[ $LOCAL_INSTALL -eq 1 ]]; then colorEcho ${YELLOW} "Installing V2Ray via local file. Please make sure the file is a valid V2Ray package, as we are not able to determine that."