Add --reinstall & --not-update-service

This commit is contained in:
kirin10000 2020-12-21 14:55:57 +08:00 committed by GitHub
parent 4ab608ccba
commit b0975ec2a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -26,6 +26,34 @@ JSON_PATH=${JSON_PATH:-/usr/local/etc/xray}
# Set this variable only if you want this script to check all the systemd unit file:
# export check_all_service_files='yes'
# Gobal verbals
# Xray current version
CURRENT_VERSION=""
# Xray latest release version
RELEASE_LATEST=""
# Xray version will be installed
INSTALL_VERSION=""
# --help
HELP='0'
# --check
CHECK='0'
# --remove
REMOVE='0'
# --force
FORCE='0'
# --reinstall
REINSTALL='0'
# --not-update-service
N_UP_SERVICE='0'
# --version ?
SPECIFIED_VERSION=""
# --local ?
LOCAL_FILE=""
# --proxy ?
PROXY=""
# --install-user ?
INSTALL_USER=""
curl() {
$(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
}
@ -148,10 +176,7 @@ identify_the_operating_system_and_architecture() {
## Demo function for processing parameters
judgment_parameters() {
HELP='0'
CHECK='0'
REMOVE='0'
LOCAL_INSTALL='0'
local local_install='0'
local temp_version='0'
while [[ "$#" -gt '0' ]]; do
case "$1" in
@ -164,7 +189,7 @@ judgment_parameters() {
exit 1
fi
temp_version='1'
VERSION="$2"
SPECIFIED_VERSION="$2"
shift
;;
'-c' | '--check')
@ -177,7 +202,7 @@ judgment_parameters() {
HELP='1'
;;
'-l' | '--local')
LOCAL_INSTALL='1'
local_install='1'
if [[ -z "$2" ]]; then
echo "error: Please specify the correct local file."
exit 1
@ -201,6 +226,12 @@ judgment_parameters() {
INSTALL_USER="$2"
shift
;;
'--reinstall')
REINSTALL='1'
;;
'--not-update-service')
N_UP_SERVICE='1'
;;
*)
echo "$0: unknown option -- -"
exit 1
@ -212,12 +243,8 @@ judgment_parameters() {
echo "--help, --check and --remove can't be used together."
exit 1
fi
if ((temp_version+LOCAL_INSTALL>1)); then
echo "--version and --local can't be used together."
exit 1
fi
if ((temp_version+CHECK>1)); then
echo "--version and --check can't be used together."
if ((temp_version+local_install+REINSTALL>1)); then
echo "--version,--reinstall and --local can't be used together."
exit 1
fi
}
@ -254,31 +281,16 @@ install_software() {
}
get_current_version() {
# Get the CURRENT_VERSION
if [[ -f '/usr/local/bin/xray' ]]; then
CURRENT_VERSION="$(/usr/local/bin/xray -version | awk 'NR==1 {print $2}')"
CURRENT_VERSION="v${CURRENT_VERSION#v}"
else
CURRENT_VERSION=""
fi
}
get_version() {
# 0: There is a version can be installed (RELEASE_VERSION).
# 1: The current version is the latest.
# 2: The specified version is same to the current version.
# RELEASE_VERSION, RELEASE_LATEST and CURRENT_VERSION will be used in a later process
# Get the CURRENT_VERSION
get_current_version
# Check if there is a specified version
if [[ -n "$VERSION" ]]; then
RELEASE_VERSION="v${VERSION#v}"
if [[ "$RELEASE_VERSION" == "$CURRENT_VERSION" ]]; then
return 2
else
return 0
fi
fi
get_latest_version() {
# Get Xray latest release version number
TMP_FILE="$(mktemp)"
if ! curl -x "${PROXY}" -sS -H "Accept: application/vnd.github.v3+json" -o "$TMP_FILE" 'https://api.github.com/repos/XTLS/Xray-core/releases/latest'; then
@ -288,31 +300,36 @@ get_version() {
fi
RELEASE_LATEST="$(sed 'y/,/\n/' "$TMP_FILE" | grep 'tag_name' | awk -F '"' '{print $4}')"
if [[ -z "$RELEASE_LATEST" ]]; then
"rm" "$TMP_FILE"
echo "error: Failed to get the latest release version"
exit 1
fi
"rm" "$TMP_FILE"
RELEASE_LATEST="v${RELEASE_LATEST#v}"
}
# Check if the current version is latest
RELEASE_VERSION="${RELEASE_LATEST}"
if [[ "$RELEASE_VERSION" != "$CURRENT_VERSION" ]]; then
RELEASE_VERSIONSION_NUMBER="${RELEASE_VERSION#v}"
RELEASE_MAJOR_VERSION_NUMBER="${RELEASE_VERSIONSION_NUMBER%%.*}"
RELEASE_MINOR_VERSION_NUMBER="$(echo "$RELEASE_VERSIONSION_NUMBER" | awk -F '.' '{print $2}')"
RELEASE_MINIMUM_VERSION_NUMBER="${RELEASE_VERSIONSION_NUMBER##*.}"
version_gt() {
# compare two version
# 0: $1 > $2
# 1: $1 <= $2
if [[ "$1" != "$2" ]]; then
local temp_1_version_number="${1#v}"
local temp_1_major_version_number="${temp_1_version_number%%.*}"
local temp_1_minor_version_number="$(echo "$temp_1_version_number" | awk -F '.' '{print $2}')"
local temp_1_minimunm_version_number="${temp_1_version_number##*.}"
# shellcheck disable=SC2001
CURRENT_VERSIONSION_NUMBER="$(echo "${CURRENT_VERSION#v}" | sed 's/-.*//')"
CURRENT_MAJOR_VERSION_NUMBER="${CURRENT_VERSIONSION_NUMBER%%.*}"
CURRENT_MINOR_VERSION_NUMBER="$(echo "$CURRENT_VERSIONSION_NUMBER" | awk -F '.' '{print $2}')"
CURRENT_MINIMUM_VERSION_NUMBER="${CURRENT_VERSIONSION_NUMBER##*.}"
if [[ "$RELEASE_MAJOR_VERSION_NUMBER" -gt "$CURRENT_MAJOR_VERSION_NUMBER" ]]; then
local temp_2_version_number="${2#v}"
local temp_2_major_version_number="${temp_2_version_number%%.*}"
local temp_2_minor_version_number="$(echo "$temp_2_version_number" | awk -F '.' '{print $2}')"
local temp_2_minimunm_version_number="${temp_2_version_number##*.}"
if [[ "$temp_1_major_version_number" -gt "$temp_2_major_version_number" ]]; then
return 0
elif [[ "$RELEASE_MAJOR_VERSION_NUMBER" -eq "$CURRENT_MAJOR_VERSION_NUMBER" ]]; then
if [[ "$RELEASE_MINOR_VERSION_NUMBER" -gt "$CURRENT_MINOR_VERSION_NUMBER" ]]; then
elif [[ "$temp_1_major_version_number" -eq "$temp_2_major_version_number" ]]; then
if [[ "$temp_1_minor_version_number" -gt "$temp_2_minor_version_number" ]]; then
return 0
elif [[ "$RELEASE_MINOR_VERSION_NUMBER" -eq "$CURRENT_MINOR_VERSION_NUMBER" ]]; then
if [[ "$RELEASE_MINIMUM_VERSION_NUMBER" -gt "$CURRENT_MINIMUM_VERSION_NUMBER" ]]; then
elif [[ "$temp_1_minor_version_number" -eq "$temp_2_minor_version_number" ]]; then
if [[ "$temp_1_minimunm_version_number" -gt "$temp_2_minimunm_version_number" ]]; then
return 0
else
return 1
@ -323,13 +340,13 @@ get_version() {
else
return 1
fi
elif [[ "$RELEASE_VERSION" == "$CURRENT_VERSION" ]]; then
elif [[ "$1" == "$2" ]]; then
return 1
fi
}
download_xray() {
DOWNLOAD_LINK="https://github.com/XTLS/Xray-core/releases/download/$RELEASE_VERSION/Xray-linux-$MACHINE.zip"
DOWNLOAD_LINK="https://github.com/XTLS/Xray-core/releases/download/$INSTALL_VERSION/Xray-linux-$MACHINE.zip"
echo "Downloading Xray archive: $DOWNLOAD_LINK"
if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "$ZIP_FILE" "$DOWNLOAD_LINK"; then
echo 'error: Download failed! Please check your network or try again.'
@ -521,7 +538,9 @@ stop_xray() {
check_update() {
if [[ -f '/etc/systemd/system/xray.service' ]]; then
get_version
get_current_version
get_latest_version
version_gt $RELEASE_LATEST $CURRENT_VERSION
local get_ver_exit_code=$?
if [[ "$get_ver_exit_code" -eq '0' ]]; then
echo "info: Found the latest release of Xray $RELEASE_LATEST . (Current release: $CURRENT_VERSION)"
@ -576,14 +595,16 @@ remove_xray() {
show_help() {
echo "usage: $0 [OPTION]..."
echo 'OPTION:'
echo ' --remove Remove Xray'
echo ' --version Install the specified version of Xray, e.g., --version v1.0.0'
echo ' -c, --check Check if Xray can be updated'
echo ' -f, --force Force installation of the latest version of Xray'
echo ' -h, --help Show help'
echo ' -l, --local Install Xray from a local file'
echo ' -p, --proxy Download through a proxy server, e.g., -p http://127.0.0.1:8118 or -p socks5://127.0.0.1:1080'
echo ' -u, --install-user Install Xray in specified user, e.g, -u root'
echo ' --remove Remove Xray'
echo ' --version Install the specified version of Xray, e.g., --version v1.0.0'
echo ' -c, --check Check if Xray can be updated'
echo ' -f, --force Force installation of the latest version of Xray'
echo ' -h, --help Show help'
echo ' -l, --local Install Xray from a local file'
echo ' -p, --proxy Download through a proxy server, e.g., -p http://127.0.0.1:8118 or -p socks5://127.0.0.1:1080'
echo ' -u, --install-user Install Xray in specified user, e.g, -u root'
echo ' --reinstall Reinstall current Xray version'
echo " --not-update-service Don't change service file if it is exist"
exit 0
}
@ -611,34 +632,50 @@ main() {
ZIP_FILE="${TMP_DIRECTORY}/Xray-linux-$MACHINE.zip"
# Install Xray from a local file, but still need to make sure the network is available
if [[ "$LOCAL_INSTALL" -eq '1' ]]; then
if [[ -n "$LOCAL_FILE" ]]; then
echo 'warn: Install Xray from a local file, but still need to make sure the network is available.'
echo -n 'warn: Please make sure the file is valid because we cannot confirm it. (Press any key) ...'
read -r
install_software 'unzip' 'unzip'
decompression "$LOCAL_FILE"
else
# Normal way
install_software 'curl' 'curl'
get_version
NUMBER="$?"
if [[ "$NUMBER" -eq '0' ]] || [[ "$FORCE" -eq '1' ]]; then
echo "info: Installing Xray $RELEASE_VERSION for $(uname -m)"
download_xray
if [[ "$?" -eq '1' ]]; then
"rm" -r "$TMP_DIRECTORY"
echo "removed: $TMP_DIRECTORY"
get_current_version
if [[ "$REINSTALL" -eq '1' ]]; then
if [[ -z "$CURRENT_VERSION" ]]; then
echo "error: Xray is not installed"
exit 1
fi
install_software 'unzip' 'unzip'
decompression "$ZIP_FILE"
elif [[ "$NUMBER" -eq '1' ]]; then
echo "info: No new version. The current version of Xray is $CURRENT_VERSION ."
exit 0
elif [[ "$NUMBER" -eq '2' ]]; then
echo "info: The current version is same as the specified version. The version is $CURRENT_VERSION ."
exit 0
INSTALL_VERSION="$CURRENT_VERSION"
echo "info: Reinstalling Xray $CURRENT_VERSION"
elif [[ -n "$SPECIFIED_VERSION" ]]; then
SPECIFIED_VERSION="v${SPECIFIED_VERSION#v}"
if [[ "$CURRENT_VERSION" == "$SPECIFIED_VERSION" ]] && [[ "$FORCE" -eq '0' ]]; then
echo "info: The current version is same as the specified version. The version is $CURRENT_VERSION ."
exit 0
fi
INSTALL_VERSION="$SPECIFIED_VERSION"
echo "info: Installing specified Xray version $INSTALL_VERSION for $(uname -m)"
else
install_software 'curl' 'curl'
get_latest_version
version_gt $RELEASE_LATEST $CURRENT_VERSION
local temp_number=$?
if [[ "$temp_number" -eq '1' ]] && [[ "$FORCE" -eq '0' ]]; then
echo "info: No new version. The current version of Xray is $CURRENT_VERSION ."
exit 0
fi
INSTALL_VERSION=$RELEASE_LATEST
echo "info: Installing Xray $INSTALL_VERSION for $(uname -m)"
fi
install_software 'curl' 'curl'
install_software 'unzip' 'unzip'
download_xray
if [[ "$?" -eq '1' ]]; then
"rm" -r "$TMP_DIRECTORY"
echo "removed: $TMP_DIRECTORY"
exit 1
fi
decompression "$ZIP_FILE"
fi
# Determine if Xray is running
@ -649,7 +686,7 @@ main() {
fi
fi
install_xray
install_startup_service_file
([[ "$N_UP_SERVICE" -eq '1' ]] && [[ -f '/etc/systemd/system/xray.service' ]]) || install_startup_service_file
echo 'installed: /usr/local/bin/xray'
# If the file exists, the content output of installing or updating geoip.dat and geosite.dat will not be displayed
if [[ ! -f "${DAT_PATH}/.undat" ]]; then