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: # Set this variable only if you want this script to check all the systemd unit file:
# export check_all_service_files='yes' # 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() { curl() {
$(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@" $(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 ## Demo function for processing parameters
judgment_parameters() { judgment_parameters() {
HELP='0' local local_install='0'
CHECK='0'
REMOVE='0'
LOCAL_INSTALL='0'
local temp_version='0' local temp_version='0'
while [[ "$#" -gt '0' ]]; do while [[ "$#" -gt '0' ]]; do
case "$1" in case "$1" in
@ -164,7 +189,7 @@ judgment_parameters() {
exit 1 exit 1
fi fi
temp_version='1' temp_version='1'
VERSION="$2" SPECIFIED_VERSION="$2"
shift shift
;; ;;
'-c' | '--check') '-c' | '--check')
@ -177,7 +202,7 @@ judgment_parameters() {
HELP='1' HELP='1'
;; ;;
'-l' | '--local') '-l' | '--local')
LOCAL_INSTALL='1' local_install='1'
if [[ -z "$2" ]]; then if [[ -z "$2" ]]; then
echo "error: Please specify the correct local file." echo "error: Please specify the correct local file."
exit 1 exit 1
@ -201,6 +226,12 @@ judgment_parameters() {
INSTALL_USER="$2" INSTALL_USER="$2"
shift shift
;; ;;
'--reinstall')
REINSTALL='1'
;;
'--not-update-service')
N_UP_SERVICE='1'
;;
*) *)
echo "$0: unknown option -- -" echo "$0: unknown option -- -"
exit 1 exit 1
@ -212,12 +243,8 @@ judgment_parameters() {
echo "--help, --check and --remove can't be used together." echo "--help, --check and --remove can't be used together."
exit 1 exit 1
fi fi
if ((temp_version+LOCAL_INSTALL>1)); then if ((temp_version+local_install+REINSTALL>1)); then
echo "--version and --local can't be used together." echo "--version,--reinstall 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."
exit 1 exit 1
fi fi
} }
@ -254,31 +281,16 @@ install_software() {
} }
get_current_version() { get_current_version() {
# Get the CURRENT_VERSION
if [[ -f '/usr/local/bin/xray' ]]; then if [[ -f '/usr/local/bin/xray' ]]; then
CURRENT_VERSION="$(/usr/local/bin/xray -version | awk 'NR==1 {print $2}')" CURRENT_VERSION="$(/usr/local/bin/xray -version | awk 'NR==1 {print $2}')"
CURRENT_VERSION="v${CURRENT_VERSION#v}" CURRENT_VERSION="v${CURRENT_VERSION#v}"
else
CURRENT_VERSION=""
fi fi
} }
get_version() { get_latest_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 Xray latest release version number # Get Xray latest release version number
TMP_FILE="$(mktemp)" 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 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 fi
RELEASE_LATEST="$(sed 'y/,/\n/' "$TMP_FILE" | grep 'tag_name' | awk -F '"' '{print $4}')" RELEASE_LATEST="$(sed 'y/,/\n/' "$TMP_FILE" | grep 'tag_name' | awk -F '"' '{print $4}')"
if [[ -z "$RELEASE_LATEST" ]]; then if [[ -z "$RELEASE_LATEST" ]]; then
"rm" "$TMP_FILE"
echo "error: Failed to get the latest release version" echo "error: Failed to get the latest release version"
exit 1 exit 1
fi fi
"rm" "$TMP_FILE" "rm" "$TMP_FILE"
RELEASE_LATEST="v${RELEASE_LATEST#v}" RELEASE_LATEST="v${RELEASE_LATEST#v}"
}
# Check if the current version is latest version_gt() {
RELEASE_VERSION="${RELEASE_LATEST}" # compare two version
if [[ "$RELEASE_VERSION" != "$CURRENT_VERSION" ]]; then # 0: $1 > $2
RELEASE_VERSIONSION_NUMBER="${RELEASE_VERSION#v}" # 1: $1 <= $2
RELEASE_MAJOR_VERSION_NUMBER="${RELEASE_VERSIONSION_NUMBER%%.*}"
RELEASE_MINOR_VERSION_NUMBER="$(echo "$RELEASE_VERSIONSION_NUMBER" | awk -F '.' '{print $2}')" if [[ "$1" != "$2" ]]; then
RELEASE_MINIMUM_VERSION_NUMBER="${RELEASE_VERSIONSION_NUMBER##*.}" 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 # shellcheck disable=SC2001
CURRENT_VERSIONSION_NUMBER="$(echo "${CURRENT_VERSION#v}" | sed 's/-.*//')" local temp_2_version_number="${2#v}"
CURRENT_MAJOR_VERSION_NUMBER="${CURRENT_VERSIONSION_NUMBER%%.*}" local temp_2_major_version_number="${temp_2_version_number%%.*}"
CURRENT_MINOR_VERSION_NUMBER="$(echo "$CURRENT_VERSIONSION_NUMBER" | awk -F '.' '{print $2}')" local temp_2_minor_version_number="$(echo "$temp_2_version_number" | awk -F '.' '{print $2}')"
CURRENT_MINIMUM_VERSION_NUMBER="${CURRENT_VERSIONSION_NUMBER##*.}" local temp_2_minimunm_version_number="${temp_2_version_number##*.}"
if [[ "$RELEASE_MAJOR_VERSION_NUMBER" -gt "$CURRENT_MAJOR_VERSION_NUMBER" ]]; then if [[ "$temp_1_major_version_number" -gt "$temp_2_major_version_number" ]]; then
return 0 return 0
elif [[ "$RELEASE_MAJOR_VERSION_NUMBER" -eq "$CURRENT_MAJOR_VERSION_NUMBER" ]]; then elif [[ "$temp_1_major_version_number" -eq "$temp_2_major_version_number" ]]; then
if [[ "$RELEASE_MINOR_VERSION_NUMBER" -gt "$CURRENT_MINOR_VERSION_NUMBER" ]]; then if [[ "$temp_1_minor_version_number" -gt "$temp_2_minor_version_number" ]]; then
return 0 return 0
elif [[ "$RELEASE_MINOR_VERSION_NUMBER" -eq "$CURRENT_MINOR_VERSION_NUMBER" ]]; then elif [[ "$temp_1_minor_version_number" -eq "$temp_2_minor_version_number" ]]; then
if [[ "$RELEASE_MINIMUM_VERSION_NUMBER" -gt "$CURRENT_MINIMUM_VERSION_NUMBER" ]]; then if [[ "$temp_1_minimunm_version_number" -gt "$temp_2_minimunm_version_number" ]]; then
return 0 return 0
else else
return 1 return 1
@ -323,13 +340,13 @@ get_version() {
else else
return 1 return 1
fi fi
elif [[ "$RELEASE_VERSION" == "$CURRENT_VERSION" ]]; then elif [[ "$1" == "$2" ]]; then
return 1 return 1
fi fi
} }
download_xray() { 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" echo "Downloading Xray archive: $DOWNLOAD_LINK"
if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "$ZIP_FILE" "$DOWNLOAD_LINK"; then 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.' echo 'error: Download failed! Please check your network or try again.'
@ -521,7 +538,9 @@ stop_xray() {
check_update() { check_update() {
if [[ -f '/etc/systemd/system/xray.service' ]]; then 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=$? local get_ver_exit_code=$?
if [[ "$get_ver_exit_code" -eq '0' ]]; then if [[ "$get_ver_exit_code" -eq '0' ]]; then
echo "info: Found the latest release of Xray $RELEASE_LATEST . (Current release: $CURRENT_VERSION)" echo "info: Found the latest release of Xray $RELEASE_LATEST . (Current release: $CURRENT_VERSION)"
@ -584,6 +603,8 @@ show_help() {
echo ' -l, --local Install Xray from a local file' 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 ' -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 ' -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 exit 0
} }
@ -611,34 +632,50 @@ main() {
ZIP_FILE="${TMP_DIRECTORY}/Xray-linux-$MACHINE.zip" ZIP_FILE="${TMP_DIRECTORY}/Xray-linux-$MACHINE.zip"
# Install Xray from a local file, but still need to make sure the network is available # 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 '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) ...' echo -n 'warn: Please make sure the file is valid because we cannot confirm it. (Press any key) ...'
read -r read -r
install_software 'unzip' 'unzip' install_software 'unzip' 'unzip'
decompression "$LOCAL_FILE" decompression "$LOCAL_FILE"
else else
# Normal way get_current_version
if [[ "$REINSTALL" -eq '1' ]]; then
if [[ -z "$CURRENT_VERSION" ]]; then
echo "error: Xray is not installed"
exit 1
fi
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' install_software 'curl' 'curl'
get_version get_latest_version
NUMBER="$?" version_gt $RELEASE_LATEST $CURRENT_VERSION
if [[ "$NUMBER" -eq '0' ]] || [[ "$FORCE" -eq '1' ]]; then local temp_number=$?
echo "info: Installing Xray $RELEASE_VERSION for $(uname -m)" 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 download_xray
if [[ "$?" -eq '1' ]]; then if [[ "$?" -eq '1' ]]; then
"rm" -r "$TMP_DIRECTORY" "rm" -r "$TMP_DIRECTORY"
echo "removed: $TMP_DIRECTORY" echo "removed: $TMP_DIRECTORY"
exit 1 exit 1
fi fi
install_software 'unzip' 'unzip'
decompression "$ZIP_FILE" 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
fi
fi fi
# Determine if Xray is running # Determine if Xray is running
@ -649,7 +686,7 @@ main() {
fi fi
fi fi
install_xray 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' 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 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 if [[ ! -f "${DAT_PATH}/.undat" ]]; then