修改选项逻辑 (#10)

Co-authored-by: DuckSoft <realducksoft@gmail.com>
This commit is contained in:
kirin10000 2020-12-24 16:21:13 +08:00 committed by GitHub
parent b0975ec2a1
commit 3b45090873
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -33,26 +33,34 @@ CURRENT_VERSION=""
RELEASE_LATEST="" RELEASE_LATEST=""
# Xray version will be installed # Xray version will be installed
INSTALL_VERSION="" INSTALL_VERSION=""
# --help # install
HELP='0' INSTALL='0'
# --check # install-geodata
CHECK='0' INSTALL_GEODATA='0'
# --remove # remove
REMOVE='0' REMOVE='0'
# help
HELP='0'
# check
CHECK='0'
# --force # --force
FORCE='0' FORCE='0'
# --reinstall # --install-user ?
REINSTALL='0' INSTALL_USER=""
# --without-geodata
NO_GEODATA='0'
# --not-update-service # --not-update-service
N_UP_SERVICE='0' N_UP_SERVICE='0'
# --reinstall
REINSTALL='0'
# --version ? # --version ?
SPECIFIED_VERSION="" SPECIFIED_VERSION=""
# --local ? # --local ?
LOCAL_FILE="" LOCAL_FILE=""
# --proxy ? # --proxy ?
PROXY="" PROXY=""
# --install-user ? # --purge
INSTALL_USER="" PURGE='0'
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 "$@"
@ -180,9 +188,27 @@ judgment_parameters() {
local temp_version='0' local temp_version='0'
while [[ "$#" -gt '0' ]]; do while [[ "$#" -gt '0' ]]; do
case "$1" in case "$1" in
'--remove') 'install')
INSTALL='1'
;;
'install-geodata')
INSTALL_GEODATA='1'
;;
'remove' | '--remove')
REMOVE='1' REMOVE='1'
;; ;;
'help')
HELP='1'
;;
'check')
CHECK='1'
;;
'--without-geodata')
NO_GEODATA='1'
;;
'--purge')
PURGE='1'
;;
'--version') '--version')
if [[ -z "$2" ]]; then if [[ -z "$2" ]]; then
echo "error: Please specify the correct version." echo "error: Please specify the correct version."
@ -192,15 +218,9 @@ judgment_parameters() {
SPECIFIED_VERSION="$2" SPECIFIED_VERSION="$2"
shift shift
;; ;;
'-c' | '--check')
CHECK='1'
;;
'-f' | '--force') '-f' | '--force')
FORCE='1' FORCE='1'
;; ;;
'-h' | '--help')
HELP='1'
;;
'-l' | '--local') '-l' | '--local')
local_install='1' local_install='1'
if [[ -z "$2" ]]; then if [[ -z "$2" ]]; then
@ -239,11 +259,13 @@ judgment_parameters() {
esac esac
shift shift
done done
if ((HELP+CHECK+REMOVE>1)); then if ((INSTALL+INSTALL_GEODATA+HELP+CHECK+REMOVE==0)); then
echo "--help, --check and --remove can't be used together." INSTALL='1'
elif ((INSTALL+INSTALL_GEODATA+HELP+CHECK+REMOVE>1)); then
echo 'You can only choose one action.'
exit 1 exit 1
fi fi
if ((temp_version+local_install+REINSTALL>1)); then if [[ "$INSTALL" -eq '1' ]] && ((temp_version+local_install+REINSTALL>1)); then
echo "--version,--reinstall and --local can't be used together." echo "--version,--reinstall and --local can't be used together."
exit 1 exit 1
fi fi
@ -396,11 +418,12 @@ install_file() {
install_xray() { install_xray() {
# Install Xray binary to /usr/local/bin/ and $DAT_PATH # Install Xray binary to /usr/local/bin/ and $DAT_PATH
install_file xray install_file xray
install -d "$DAT_PATH"
# If the file exists, geoip.dat and geosite.dat will not be installed or updated # If the file exists, geoip.dat and geosite.dat will not be installed or updated
if [[ ! -f "${DAT_PATH}/.undat" ]]; then if [[ "$NO_GEODATA" -eq '0' ]] && [[ ! -f "${DAT_PATH}/.undat" ]]; then
install -d "$DAT_PATH"
install_file geoip.dat install_file geoip.dat
install_file geosite.dat install_file geosite.dat
GEODATA='1'
fi fi
# Install Xray configuration file to $JSON_PATH # Install Xray configuration file to $JSON_PATH
@ -536,6 +559,41 @@ stop_xray() {
echo 'info: Stop the Xray service.' echo 'info: Stop the Xray service.'
} }
install_geodata() {
download_geodata() {
if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "${dir_tmp}/${2}" "${1}"; then
echo 'error: Download failed! Please check your network or try again.'
exit 1
fi
if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "${dir_tmp}/${2}.sha256sum" "${1}.sha256sum"; then
echo 'error: Download failed! Please check your network or try again.'
exit 1
fi
}
local download_link_geoip="https://github.com/v2fly/geoip/releases/latest/download/geoip.dat"
local download_link_geosite="https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat"
local file_ip='geoip.dat'
local file_dlc='dlc.dat'
local file_site='geosite.dat'
local dir_tmp="$(mktemp -d)"
[[ ! -f '/usr/local/bin/xray' ]] && echo "warning: Xray was not installed"
download_geodata $download_link_geoip $file_ip
download_geodata $download_link_geosite $file_dlc
cd "${dir_tmp}" || exit
for i in "${dir_tmp}"/*.sha256sum; do
if ! sha256sum -c "${i}"; then
echo 'error: Check failed! Please check your network or try again.'
exit 1
fi
done
cd - > /dev/null
install -d "$DAT_PATH"
install -m 644 "${dir_tmp}"/${file_dlc} "${DAT_PATH}"/${file_site}
install -m 644 "${dir_tmp}"/${file_ip} "${DAT_PATH}"/${file_ip}
rm -r "${dir_tmp}"
exit 0
}
check_update() { check_update() {
if [[ -f '/etc/systemd/system/xray.service' ]]; then if [[ -f '/etc/systemd/system/xray.service' ]]; then
get_current_version get_current_version
@ -559,30 +617,36 @@ remove_xray() {
if [[ -n "$(pidof xray)" ]]; then if [[ -n "$(pidof xray)" ]]; then
stop_xray stop_xray
fi fi
if ! ("rm" -r '/usr/local/bin/xray' \ local delete_files=('/usr/local/bin/xray' '/etc/systemd/system/xray.service' '/etc/systemd/system/xray@.service' '/etc/systemd/system/xray.service.d' '/etc/systemd/system/xray@.service.d')
"$DAT_PATH" \ [[ -d "$DAT_PATH" ]] && delete_files+=("$DAT_PATH")
'/etc/systemd/system/xray.service' \ if [[ "$PURGE" -eq '1' ]]; then
'/etc/systemd/system/xray@.service' \ if [[ -z "$JSONS_PATH" ]]; then
'/etc/systemd/system/xray.service.d' \ delete_files+=("$JSON_PATH")
'/etc/systemd/system/xray@.service.d'); then else
delete_files+=("$JSONS_PATH")
fi
delete_files+=('/var/log/xray')
fi
systemctl disable xray
if ! ("rm" -r "${delete_files[@]}"); then
echo 'error: Failed to remove Xray.' echo 'error: Failed to remove Xray.'
exit 1 exit 1
else else
echo 'removed: /usr/local/bin/xray' for i in ${!delete_files[@]}
echo "removed: $DAT_PATH" do
echo 'removed: /etc/systemd/system/xray.service' echo "removed: ${delete_files[$i]}"
echo 'removed: /etc/systemd/system/xray@.service' done
echo 'removed: /etc/systemd/system/xray.service.d' systemctl daemon-reload
echo 'removed: /etc/systemd/system/xray@.service.d'
echo 'Please execute the command: systemctl disable xray'
echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl unzip" echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl unzip"
echo 'info: Xray has been removed.' echo 'info: Xray has been removed.'
if [[ "$PURGE" -eq '0' ]]; then
echo 'info: If necessary, manually delete the configuration and log files.' echo 'info: If necessary, manually delete the configuration and log files.'
if [[ -n "$JSONS_PATH" ]]; then if [[ -n "$JSONS_PATH" ]]; then
echo "info: e.g., $JSONS_PATH and /var/log/xray/ ..." echo "info: e.g., $JSONS_PATH and /var/log/xray/ ..."
else else
echo "info: e.g., $JSON_PATH and /var/log/xray/ ..." echo "info: e.g., $JSON_PATH and /var/log/xray/ ..."
fi fi
fi
exit 0 exit 0
fi fi
else else
@ -593,18 +657,32 @@ remove_xray() {
# Explanation of parameters in the script # Explanation of parameters in the script
show_help() { show_help() {
echo "usage: $0 [OPTION]..." echo "usage: $0 [OPTION]... ACTION"
echo
echo 'ACTION:'
echo ' install Install/Update Xray'
echo ' install-geodata Install/Update geoip.dat and geosite.dat only'
echo ' remove Remove Xray'
echo ' help Show help'
echo ' check Check if Xray can be updated'
echo 'If no action is specified, then install will be selected'
echo
echo 'OPTION:' echo 'OPTION:'
echo ' --remove Remove Xray' echo ' install:'
echo ' --version Install the specified version of Xray, e.g., --version v1.0.0' 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 install even though the versions are same'
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 ' -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 ' --reinstall Reinstall current Xray version'
echo " --not-update-service Don't change service file if it is exist" echo " --not-update-service Don't change service files if they are exist"
echo " --without-geodata Don't install/update geoip.dat and geosite.dat"
echo ' install-geodata:'
echo ' -p, --proxy Download through a proxy server'
echo ' remove:'
echo ' --purge Remove all the Xray files, include logs, configs, etc'
echo ' check:'
echo ' -p, --proxy Check new version through a proxy server'
exit 0 exit 0
} }
@ -623,6 +701,7 @@ main() {
[[ "$HELP" -eq '1' ]] && show_help [[ "$HELP" -eq '1' ]] && show_help
[[ "$CHECK" -eq '1' ]] && check_update [[ "$CHECK" -eq '1' ]] && check_update
[[ "$REMOVE" -eq '1' ]] && remove_xray [[ "$REMOVE" -eq '1' ]] && remove_xray
[[ "$INSTALL_GEODATA" -eq '1' ]] && install_geodata
# Check if the user is effective # Check if the user is effective
check_install_user check_install_user
@ -689,7 +768,7 @@ main() {
([[ "$N_UP_SERVICE" -eq '1' ]] && [[ -f '/etc/systemd/system/xray.service' ]]) || 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 [[ "$GEODATA" -eq '1' ]]; then
echo "installed: ${DAT_PATH}/geoip.dat" echo "installed: ${DAT_PATH}/geoip.dat"
echo "installed: ${DAT_PATH}/geosite.dat" echo "installed: ${DAT_PATH}/geosite.dat"
fi fi
@ -725,7 +804,12 @@ main() {
if [[ "$XRAY_RUNNING" -eq '1' ]]; then if [[ "$XRAY_RUNNING" -eq '1' ]]; then
start_xray start_xray
else else
echo 'Please execute the command: systemctl enable xray; systemctl start xray' systemctl enable xray --now
if [[ "$?" -eq 0 ]]; then
echo "info: Enable and start the Xray service"
else
echo "warning: Failed to enable and start the Xray service"
fi
fi fi
} }