Canonical function naming

pull/1/head
Dct Mei 2020-04-12 11:01:38 +08:00
parent e6d16bc69d
commit 47f73bf301
No known key found for this signature in database
GPG Key ID: 50BF8B712DCAD7EA
1 changed files with 216 additions and 199 deletions

View File

@ -12,7 +12,7 @@
# If the script executes incorrectly, go to: # If the script executes incorrectly, go to:
# https://github.com/v2fly/fhs-install-v2ray/issues # https://github.com/v2fly/fhs-install-v2ray/issues
# Judge computer systems and architecture identify_the_operating_system_and_architecture() {
if [[ "$(uname)" == 'Linux' ]]; then if [[ "$(uname)" == 'Linux' ]]; then
case "$(uname -m)" in case "$(uname -m)" in
'i386' | 'i686') 'i386' | 'i686')
@ -86,8 +86,9 @@ else
echo "error: This operating system is not supported." echo "error: This operating system is not supported."
exit 1 exit 1
fi fi
}
# Judgment parameters judgment_parameters() {
if [[ "$#" -gt '0' ]]; then if [[ "$#" -gt '0' ]]; then
case "$1" in case "$1" in
'--remove') '--remove')
@ -153,6 +154,7 @@ if [[ "$#" -gt '0' ]]; then
;; ;;
esac esac
PROXY="-x $2" PROXY="-x $2"
# Parameters available through a proxy server # Parameters available through a proxy server
if [[ "$#" -gt '2' ]]; then if [[ "$#" -gt '2' ]]; then
case "$3" in case "$3" in
@ -190,8 +192,9 @@ if [[ "$#" -gt '0' ]]; then
;; ;;
esac esac
fi fi
}
installSoftware() { install_software() {
COMPONENT="$1" COMPONENT="$1"
if [[ -n "$(command -v $COMPONENT)" ]]; then if [[ -n "$(command -v $COMPONENT)" ]]; then
return return
@ -203,7 +206,8 @@ installSoftware() {
fi fi
echo "info: $COMPONENT is installed." echo "info: $COMPONENT is installed."
} }
versionNumber() {
version_number() {
case "$1" in case "$1" in
'v'*) 'v'*)
echo "$1" echo "$1"
@ -213,7 +217,8 @@ versionNumber() {
;; ;;
esac esac
} }
getVersion() {
get_version() {
# 0: Install or update V2Ray. # 0: Install or update V2Ray.
# 1: Installed or no new version of V2Ray. # 1: Installed or no new version of V2Ray.
# 2: Install the specified version of V2Ray. # 2: Install the specified version of V2Ray.
@ -221,15 +226,16 @@ getVersion() {
# Determine the version number for V2Ray installed from a local file # Determine the version number for V2Ray installed from a local file
if [[ -f '/usr/local/bin/v2ray' ]]; then if [[ -f '/usr/local/bin/v2ray' ]]; then
VERSION="$(/usr/local/bin/v2ray -version)" VERSION="$(/usr/local/bin/v2ray -version)"
CURRENT_VERSION="$(versionNumber $(echo $VERSION | head -n 1 | awk -F ' ' '{print $2}'))" CURRENT_VERSION="$(version_number $(echo $VERSION | head -n 1 | awk -F ' ' '{print $2}'))"
if [[ "$LOCAL_INSTALL" -eq '1' ]]; then if [[ "$LOCAL_INSTALL" -eq '1' ]]; then
RELEASE_VERSION="$CURRENT_VERSION" RELEASE_VERSION="$CURRENT_VERSION"
return return
fi fi
fi fi
# Get V2Ray release version number # Get V2Ray release version number
TMP_FILE="$(mktemp)" TMP_FILE="$(mktemp)"
installSoftware curl install_software curl
curl ${PROXY} -o "$TMP_FILE" https://api.github.com/repos/v2ray/v2ray-core/releases/latest -s curl ${PROXY} -o "$TMP_FILE" https://api.github.com/repos/v2ray/v2ray-core/releases/latest -s
if [[ "$?" -ne '0' ]]; then if [[ "$?" -ne '0' ]]; then
rm "$TMP_FILE" rm "$TMP_FILE"
@ -238,7 +244,8 @@ getVersion() {
fi fi
RELEASE_LATEST="$(cat $TMP_FILE | sed 'y/,/\n/' | grep 'tag_name' | awk -F '"' '{print $4}')" RELEASE_LATEST="$(cat $TMP_FILE | sed 'y/,/\n/' | grep 'tag_name' | awk -F '"' '{print $4}')"
rm "$TMP_FILE" rm "$TMP_FILE"
RELEASE_VERSION="$(versionNumber $RELEASE_LATEST)" RELEASE_VERSION="$(version_number $RELEASE_LATEST)"
# Compare V2Ray version numbers # Compare V2Ray version numbers
if [[ "$RELEASE_VERSION" != "$CURRENT_VERSION" ]]; then if [[ "$RELEASE_VERSION" != "$CURRENT_VERSION" ]]; then
RELEASE_VERSIONSION_NUMBER="${RELEASE_VERSION#v}" RELEASE_VERSIONSION_NUMBER="${RELEASE_VERSION#v}"
@ -270,11 +277,12 @@ getVersion() {
return 1 return 1
fi fi
else else
RELEASE_VERSION="$(versionNumber $VERSION)" RELEASE_VERSION="$(version_number $VERSION)"
return 2 return 2
fi fi
} }
downloadV2Ray() {
download_v2ray() {
mkdir "$TMP_DIRECTORY" mkdir "$TMP_DIRECTORY"
DOWNLOAD_LINK="https://github.com/v2ray/v2ray-core/releases/download/$RELEASE_VERSION/v2ray-linux-$MACHINE.zip" DOWNLOAD_LINK="https://github.com/v2ray/v2ray-core/releases/download/$RELEASE_VERSION/v2ray-linux-$MACHINE.zip"
echo "Downloading V2Ray archive: $DOWNLOAD_LINK" echo "Downloading V2Ray archive: $DOWNLOAD_LINK"
@ -293,6 +301,7 @@ downloadV2Ray() {
echo 'error: This version does not support verification. Please replace with another version.' echo 'error: This version does not support verification. Please replace with another version.'
return 1 return 1
fi fi
# Verification of V2Ray archive # Verification of V2Ray archive
for LISTSUM in 'md5' 'sha1' 'sha256' 'sha512'; do for LISTSUM in 'md5' 'sha1' 'sha256' 'sha512'; do
SUM="$(${LISTSUM}sum $ZIP_FILE | sed 's/ .*//')" SUM="$(${LISTSUM}sum $ZIP_FILE | sed 's/ .*//')"
@ -303,6 +312,7 @@ downloadV2Ray() {
fi fi
done done
} }
decompression() { decompression() {
unzip -q "$1" -d "$TMP_DIRECTORY" unzip -q "$1" -d "$TMP_DIRECTORY"
if [[ "$?" -ne '0' ]]; then if [[ "$?" -ne '0' ]]; then
@ -313,7 +323,8 @@ decompression() {
fi fi
echo "info: Extract the V2Ray package to $TMP_DIRECTORY and prepare it for installation." echo "info: Extract the V2Ray package to $TMP_DIRECTORY and prepare it for installation."
} }
installFile() {
install_file() {
NAME="$1" NAME="$1"
if [[ "$NAME" == 'v2ray' ]] || [[ "$NAME" == 'v2ctl' ]]; then if [[ "$NAME" == 'v2ray' ]] || [[ "$NAME" == 'v2ctl' ]]; then
install -m 755 "${TMP_DIRECTORY}$NAME" "/usr/local/bin/$NAME" install -m 755 "${TMP_DIRECTORY}$NAME" "/usr/local/bin/$NAME"
@ -321,13 +332,14 @@ installFile() {
install -m 755 "${TMP_DIRECTORY}$NAME" "/usr/local/lib/v2ray/$NAME" install -m 755 "${TMP_DIRECTORY}$NAME" "/usr/local/lib/v2ray/$NAME"
fi fi
} }
installV2Ray() {
install_v2ray() {
# Install V2Ray binary to /usr/local/bin/ and /usr/local/lib/v2ray/ # Install V2Ray binary to /usr/local/bin/ and /usr/local/lib/v2ray/
installFile v2ray install_file v2ray
installFile v2ctl install_file v2ctl
install -d /usr/local/lib/v2ray/ install -d /usr/local/lib/v2ray/
installFile geoip.dat install_file geoip.dat
installFile geosite.dat install_file geosite.dat
# Install V2Ray configuration file to /usr/local/etc/v2ray/ # Install V2Ray configuration file to /usr/local/etc/v2ray/
if [[ ! -d '/usr/local/etc/v2ray/' ]]; then if [[ ! -d '/usr/local/etc/v2ray/' ]]; then
@ -348,10 +360,11 @@ installV2Ray() {
LOG='1' LOG='1'
fi fi
} }
installStartupServiceFile() {
install_startup_service_file() {
if [[ ! -f '/etc/systemd/system/v2ray.service' ]]; then if [[ ! -f '/etc/systemd/system/v2ray.service' ]]; then
mkdir "${TMP_DIRECTORY}systemd/system/" mkdir "${TMP_DIRECTORY}systemd/system/"
installSoftware curl install_software curl
curl ${PROXY} -o "${TMP_DIRECTORY}systemd/system/v2ray.service" https://raw.githubusercontent.workers.dev/v2fly/fhs-install-v2ray/master/systemd/system/v2ray.service -s curl ${PROXY} -o "${TMP_DIRECTORY}systemd/system/v2ray.service" https://raw.githubusercontent.workers.dev/v2fly/fhs-install-v2ray/master/systemd/system/v2ray.service -s
if [[ "$?" -ne '0' ]]; then if [[ "$?" -ne '0' ]]; then
echo 'error: Failed to start service file download! Please check your network or try again.' echo 'error: Failed to start service file download! Please check your network or try again.'
@ -368,7 +381,7 @@ installStartupServiceFile() {
fi fi
} }
startV2Ray() { start_v2ray() {
if [[ -f '/etc/systemd/system/v2ray.service' ]]; then if [[ -f '/etc/systemd/system/v2ray.service' ]]; then
systemctl start v2ray systemctl start v2ray
fi fi
@ -378,7 +391,8 @@ startV2Ray() {
fi fi
echo 'info: Start the V2Ray service.' echo 'info: Start the V2Ray service.'
} }
stopV2Ray() {
stop_v2ray() {
if [[ -f '/etc/systemd/system/v2ray.service' ]]; then if [[ -f '/etc/systemd/system/v2ray.service' ]]; then
systemctl stop v2ray systemctl stop v2ray
fi fi
@ -389,9 +403,9 @@ stopV2Ray() {
echo 'info: Stop the V2Ray service.' echo 'info: Stop the V2Ray service.'
} }
checkUpdate() { check_update() {
if [[ -f '/etc/systemd/system/v2ray.service' ]]; then if [[ -f '/etc/systemd/system/v2ray.service' ]]; then
getVersion get_version
if [[ "$?" -eq '0' ]]; then if [[ "$?" -eq '0' ]]; then
echo "info: Found the latest release of V2Ray $RELEASE_VERSION . (Current release: $CURRENT_VERSION)" echo "info: Found the latest release of V2Ray $RELEASE_VERSION . (Current release: $CURRENT_VERSION)"
elif [[ "$?" -eq '1' ]]; then elif [[ "$?" -eq '1' ]]; then
@ -404,10 +418,10 @@ checkUpdate() {
fi fi
} }
removeV2Ray() { remove_v2ray() {
if [[ -f '/etc/systemd/system/v2ray.service' ]]; then if [[ -f '/etc/systemd/system/v2ray.service' ]]; then
if [[ -n "$(pgrep v2ray)" ]]; then if [[ -n "$(pgrep v2ray)" ]]; then
stopV2Ray stop_v2ray
fi fi
NAME="$1" NAME="$1"
rm /usr/local/bin/v2ray rm /usr/local/bin/v2ray
@ -438,7 +452,7 @@ removeV2Ray() {
} }
# Explanation of parameters in the script # Explanation of parameters in the script
showHelp() { show_help() {
echo "usage: $0 [--remove | --version number | -c | -f | -h | -l | -p]" echo "usage: $0 [--remove | --version number | -c | -f | -h | -l | -p]"
echo ' [-p address] [--version number | -c | -f]' echo ' [-p address] [--version number | -c | -f]'
echo ' --remove Remove V2Ray' echo ' --remove Remove V2Ray'
@ -452,10 +466,13 @@ showHelp() {
} }
main() { main() {
identify_the_operating_system_and_architecture
judgment_parameters
# helping information # helping information
[[ "$HELP" -eq '1' ]] && showHelp [[ "$HELP" -eq '1' ]] && show_help
[[ "$CHECK" -eq '1' ]] && checkUpdate [[ "$CHECK" -eq '1' ]] && check_update
[[ "$REMOVE" -eq '1' ]] && removeV2Ray [[ "$REMOVE" -eq '1' ]] && remove_v2ray
# Two very important variables # Two very important variables
TMP_DIRECTORY="$(mktemp -du)/" TMP_DIRECTORY="$(mktemp -du)/"
@ -467,23 +484,23 @@ main() {
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 read
${PACKAGE_MANAGEMENT_UPDATE} ${PACKAGE_MANAGEMENT_UPDATE}
installSoftware unzip install_software unzip
mkdir "$TMP_DIRECTORY" mkdir "$TMP_DIRECTORY"
decompression "$LOCAL_FILE" decompression "$LOCAL_FILE"
else else
# Normal way # Normal way
${PACKAGE_MANAGEMENT_UPDATE} ${PACKAGE_MANAGEMENT_UPDATE}
getVersion get_version
NUMBER="$?" NUMBER="$?"
if [[ "$NUMBER" -eq '0' ]] || [[ "$FORCE" -eq '1' ]] || [[ "$NUMBER" -eq 2 ]]; then if [[ "$NUMBER" -eq '0' ]] || [[ "$FORCE" -eq '1' ]] || [[ "$NUMBER" -eq 2 ]]; then
echo "info: Installing V2Ray $RELEASE_VERSION for $(uname -m)" echo "info: Installing V2Ray $RELEASE_VERSION for $(uname -m)"
downloadV2Ray download_v2ray
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 0 exit 0
fi fi
installSoftware unzip install_software unzip
decompression "$ZIP_FILE" decompression "$ZIP_FILE"
elif [[ "$NUMBER" -eq '1' ]]; then elif [[ "$NUMBER" -eq '1' ]]; then
echo "info: No new version. The current version of V2Ray is $CURRENT_VERSION ." echo "info: No new version. The current version of V2Ray is $CURRENT_VERSION ."
@ -493,11 +510,11 @@ main() {
# Determine if V2Ray is running # Determine if V2Ray is running
if [[ -n "$(pgrep v2ray)" ]]; then if [[ -n "$(pgrep v2ray)" ]]; then
stopV2Ray stop_v2ray
V2RAY_RUNNING='1' V2RAY_RUNNING='1'
fi fi
installV2Ray install_v2ray
installStartupServiceFile install_startup_service_file
echo 'installed: /usr/local/bin/v2ray' echo 'installed: /usr/local/bin/v2ray'
echo 'installed: /usr/local/bin/v2ctl' echo 'installed: /usr/local/bin/v2ctl'
echo 'installed: /usr/local/lib/v2ray/geoip.dat' echo 'installed: /usr/local/lib/v2ray/geoip.dat'
@ -525,12 +542,12 @@ main() {
echo "removed: $TMP_DIRECTORY" echo "removed: $TMP_DIRECTORY"
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"
if [[ "$V2RAY_RUNNING" -eq '1' ]]; then if [[ "$V2RAY_RUNNING" -eq '1' ]]; then
startV2Ray start_v2ray
else else
echo 'Please execute the command: systemctl enable v2ray; systemctl start v2ray' echo 'Please execute the command: systemctl enable v2ray; systemctl start v2ray'
fi fi
if [[ "$LOCAL_INSTALL" -eq '1' ]]; then if [[ "$LOCAL_INSTALL" -eq '1' ]]; then
getVersion get_version
fi fi
echo "info: V2Ray $RELEASE_VERSION is installed." echo "info: V2Ray $RELEASE_VERSION is installed."
} }