Facebook
From Gray Hornbill, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 91
  1. #!/usr/bin/env bash
  2. # shellcheck disable=SC2268
  3.  
  4. # The files installed by the script conform to the Filesystem Hierarchy Standard:
  5. # https://wiki.linuxfoundation.org/lsb/fhs
  6.  
  7. # The URL of the script project is:
  8. # https://github.com/v2fly/fhs-install-v2ray
  9.  
  10. # The URL of the script is:
  11. # https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh
  12.  
  13. # If the script executes incorrectly, go to:
  14. # https://github.com/v2fly/fhs-install-v2ray/issues
  15.  
  16. # You can set this variable whatever you want in shell session right before running this script by issuing:
  17. # export DAT_PATH='/usr/local/share/v2ray'
  18. DAT_PATH=${DAT_PATH:-/usr/local/share/v2ray}
  19.  
  20. # You can set this variable whatever you want in shell session right before running this script by issuing:
  21. # export JSON_PATH='/usr/local/etc/v2ray'
  22. JSON_PATH=${JSON_PATH:-/usr/local/etc/v2ray}
  23.  
  24. # Set this variable only if you are starting v2ray with multiple configuration files:
  25. # export JSONS_PATH='/usr/local/etc/v2ray'
  26.  
  27. # Set this variable only if you want this script to check all the systemd unit file:
  28. # export check_all_service_files='yes'
  29.  
  30. curl() {
  31.   $(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
  32. }
  33.  
  34. systemd_cat_config() {
  35.   if systemd-analyze --help | grep -qw 'cat-config'; then
  36.     systemd-analyze --no-pager cat-config "$@"
  37.     echo
  38.   else
  39.     echo "${aoi}~~~~~~~~~~~~~~~~"
  40.     cat "$@" "$1".d/*
  41.     echo "${aoi}~~~~~~~~~~~~~~~~"
  42.     echo "${red}warning: ${green}The systemd version on the current operating system is too low."
  43.     echo "${red}warning: ${green}Please consider to upgrade the systemd or the operating system.${reset}"
  44.     echo
  45.   fi
  46. }
  47.  
  48. check_if_running_as_root() {
  49.   # If you want to run as another user, please modify $UID to be owned by this user
  50.   if [[ "$UID" -ne '0' ]]; then
  51.     echo "WARNING: The user currently executing this script is not root. You may encounter the insufficient privilege error."
  52.     read -r -p "Are you sure you want to continue? [y/n] " cont_without_been_root
  53.     if [[ x"${cont_without_been_root:0:1}" = x'y' ]]; then
  54.       echo "Continuing the installation with current user..."
  55.     else
  56.       echo "Not running with root, exiting..."
  57.       exit 1
  58.     fi
  59.   fi
  60. }
  61.  
  62. identify_the_operating_system_and_architecture() {
  63.   if [[ "$(uname)" == 'Linux' ]]; then
  64.     case "$(uname -m)" in
  65.       'i386' | 'i686')
  66.         MACHINE='32'
  67.         ;;
  68.       'amd64' | 'x86_64')
  69.         MACHINE='64'
  70.         ;;
  71.       'armv5tel')
  72.         MACHINE='arm32-v5'
  73.         ;;
  74.       'armv6l')
  75.         MACHINE='arm32-v6'
  76.         grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
  77.         ;;
  78.       'armv7' | 'armv7l')
  79.         MACHINE='arm32-v7a'
  80.         grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
  81.         ;;
  82.       'armv8' | 'aarch64')
  83.         MACHINE='arm64-v8a'
  84.         ;;
  85.       'mips')
  86.         MACHINE='mips32'
  87.         ;;
  88.       'mipsle')
  89.         MACHINE='mips32le'
  90.         ;;
  91.       'mips64')
  92.         MACHINE='mips64'
  93.         ;;
  94.       'mips64le')
  95.         MACHINE='mips64le'
  96.         ;;
  97.       'ppc64')
  98.         MACHINE='ppc64'
  99.         ;;
  100.       'ppc64le')
  101.         MACHINE='ppc64le'
  102.         ;;
  103.       'riscv64')
  104.         MACHINE='riscv64'
  105.         ;;
  106.       's390x')
  107.         MACHINE='s390x'
  108.         ;;
  109.       *)
  110.         echo "error: The architecture is not supported."
  111.         exit 1
  112.         ;;
  113.     esac
  114.     if [[ ! -f '/etc/os-release' ]]; then
  115.       echo "error: Don't use outdated Linux distributions."
  116.       exit 1
  117.     fi
  118.     # Do not combine this judgment condition with the following judgment condition.
  119.     ## Be aware of Linux distribution like Gentoo, which kernel supports switch between Systemd and OpenRC.
  120.     ### Refer: https://github.com/v2fly/fhs-install-v2ray/issues/84#issuecomment-688574989
  121.     if [[ -f /.dockerenv ]] || grep -q 'docker\|lxc' /proc/1/cgroup && [[ "$(type -P systemctl)" ]]; then
  122.       true
  123.     elif [[ -d /run/systemd/system ]] || grep -q systemd <(ls -l /sbin/init); then
  124.       true
  125.     else
  126.       echo "error: Only Linux distributions using systemd are supported."
  127.       exit 1
  128.     fi
  129.     if [[ "$(type -P apt)" ]]; then
  130.       PACKAGE_MANAGEMENT_INSTALL='apt -y --no-install-recommends install'
  131.       PACKAGE_MANAGEMENT_REMOVE='apt purge'
  132.       package_provide_tput='ncurses-bin'
  133.     elif [[ "$(type -P dnf)" ]]; then
  134.       PACKAGE_MANAGEMENT_INSTALL='dnf -y install'
  135.       PACKAGE_MANAGEMENT_REMOVE='dnf remove'
  136.       package_provide_tput='ncurses'
  137.     elif [[ "$(type -P yum)" ]]; then
  138.       PACKAGE_MANAGEMENT_INSTALL='yum -y install'
  139.       PACKAGE_MANAGEMENT_REMOVE='yum remove'
  140.       package_provide_tput='ncurses'
  141.     elif [[ "$(type -P zypper)" ]]; then
  142.       PACKAGE_MANAGEMENT_INSTALL='zypper install -y --no-recommends'
  143.       PACKAGE_MANAGEMENT_REMOVE='zypper remove'
  144.       package_provide_tput='ncurses-utils'
  145.     elif [[ "$(type -P pacman)" ]]; then
  146.       PACKAGE_MANAGEMENT_INSTALL='pacman -Syu --noconfirm'
  147.       PACKAGE_MANAGEMENT_REMOVE='pacman -Rsn'
  148.       package_provide_tput='ncurses'
  149.     else
  150.       echo "error: The script does not support the package manager in this operating system."
  151.       exit 1
  152.     fi
  153.   else
  154.     echo "error: This operating system is not supported."
  155.     exit 1
  156.   fi
  157. }
  158.  
  159. ## Demo function for processing parameters
  160. judgment_parameters() {
  161.   while [[ "$#" -gt '0' ]]; do
  162.     case "$1" in
  163.       '--remove')
  164.         if [[ "$#" -gt '1' ]]; then
  165.           echo 'error: Please enter the correct parameters.'
  166.           exit 1
  167.         fi
  168.         REMOVE='1'
  169.         ;;
  170.       '--version')
  171.         VERSION="${2:?error: Please specify the correct version.}"
  172.         break
  173.         ;;
  174.       '-c' | '--check')
  175.         CHECK='1'
  176.         break
  177.         ;;
  178.       '-f' | '--force')
  179.         FORCE='1'
  180.         break
  181.         ;;
  182.       '-h' | '--help')
  183.         HELP='1'
  184.         break
  185.         ;;
  186.       '-l' | '--local')
  187.         LOCAL_INSTALL='1'
  188.         LOCAL_FILE="${2:?error: Please specify the correct local file.}"
  189.         break
  190.         ;;
  191.       '-p' | '--proxy')
  192.         if [[ -z "${2:?error: Please specify the proxy server address.}" ]]; then
  193.           exit 1
  194.         fi
  195.         PROXY="$2"
  196.         shift
  197.         ;;
  198.       *)
  199.         echo "$0: unknown option -- -"
  200.         exit 1
  201.         ;;
  202.     esac
  203.     shift
  204.   done
  205. }
  206.  
  207. install_software() {
  208.   package_name="$1"
  209.   file_to_detect="$2"
  210.   type -P "$file_to_detect" > /dev/null 2>&1 && return
  211.   if ${PACKAGE_MANAGEMENT_INSTALL} "$package_name"; then
  212.     echo "info: $package_name is installed."
  213.   else
  214.     echo "error: Installation of $package_name failed, please check your network."
  215.     exit 1
  216.   fi
  217. }
  218.  
  219. get_version() {
  220.   # 0: Install or update V2Ray.
  221.   # 1: Installed or no new version of V2Ray.
  222.   # 2: Install the specified version of V2Ray.
  223.   if [[ -n "$VERSION" ]]; then
  224.     RELEASE_VERSION="v${VERSION#v}"
  225.     return 2
  226.   fi
  227.   # Determine the version number for V2Ray installed from a local file
  228.   if [[ -f '/usr/local/bin/v2ray' ]]; then
  229.     VERSION="$(/usr/local/bin/v2ray -version | awk 'NR==1 {print $2}')"
  230.     CURRENT_VERSION="v${VERSION#v}"
  231.     if [[ "$LOCAL_INSTALL" -eq '1' ]]; then
  232.       RELEASE_VERSION="$CURRENT_VERSION"
  233.       return
  234.     fi
  235.   fi
  236.   # Get V2Ray release version number
  237.   TMP_FILE="$(mktemp)"
  238.   if ! curl -x "${PROXY}" -sS -H "Accept: application/vnd.github.v3+json" -o "$TMP_FILE" 'https://api.github.com/repos/v2fly/v2ray-core/releases/latest'; then
  239.     "rm" "$TMP_FILE"
  240.     echo 'error: Failed to get release list, please check your network.'
  241.     exit 1
  242.   fi
  243.   RELEASE_LATEST="$(sed 'y/,/\n/' "$TMP_FILE" | grep 'tag_name' | awk -F '"' '{print $4}')"
  244.   "rm" "$TMP_FILE"
  245.   RELEASE_VERSION="v${RELEASE_LATEST#v}"
  246.   # Compare V2Ray version numbers
  247.   if [[ "$RELEASE_VERSION" != "$CURRENT_VERSION" ]]; then
  248.     RELEASE_VERSIONSION_NUMBER="${RELEASE_VERSION#v}"
  249.     RELEASE_MAJOR_VERSION_NUMBER="${RELEASE_VERSIONSION_NUMBER%%.*}"
  250.     RELEASE_MINOR_VERSION_NUMBER="$(echo "$RELEASE_VERSIONSION_NUMBER" | awk -F '.' '{print $2}')"
  251.     RELEASE_MINIMUM_VERSION_NUMBER="${RELEASE_VERSIONSION_NUMBER##*.}"
  252.     # shellcheck disable=SC2001
  253.     CURRENT_VERSIONSION_NUMBER="$(echo "${CURRENT_VERSION#v}" | sed 's/-.*//')"
  254.     CURRENT_MAJOR_VERSION_NUMBER="${CURRENT_VERSIONSION_NUMBER%%.*}"
  255.     CURRENT_MINOR_VERSION_NUMBER="$(echo "$CURRENT_VERSIONSION_NUMBER" | awk -F '.' '{print $2}')"
  256.     CURRENT_MINIMUM_VERSION_NUMBER="${CURRENT_VERSIONSION_NUMBER##*.}"
  257.     if [[ "$RELEASE_MAJOR_VERSION_NUMBER" -gt "$CURRENT_MAJOR_VERSION_NUMBER" ]]; then
  258.       return 0
  259.     elif [[ "$RELEASE_MAJOR_VERSION_NUMBER" -eq "$CURRENT_MAJOR_VERSION_NUMBER" ]]; then
  260.       if [[ "$RELEASE_MINOR_VERSION_NUMBER" -gt "$CURRENT_MINOR_VERSION_NUMBER" ]]; then
  261.         return 0
  262.       elif [[ "$RELEASE_MINOR_VERSION_NUMBER" -eq "$CURRENT_MINOR_VERSION_NUMBER" ]]; then
  263.         if [[ "$RELEASE_MINIMUM_VERSION_NUMBER" -gt "$CURRENT_MINIMUM_VERSION_NUMBER" ]]; then
  264.           return 0
  265.         else
  266.           return 1
  267.         fi
  268.       else
  269.         return 1
  270.       fi
  271.     else
  272.       return 1
  273.     fi
  274.   elif [[ "$RELEASE_VERSION" == "$CURRENT_VERSION" ]]; then
  275.     return 1
  276.   fi
  277. }
  278.  
  279. download_v2ray() {
  280.   DOWNLOAD_LINK="https://github.com/v2fly/v2ray-core/releases/download/$RELEASE_VERSION/v2ray-linux-$MACHINE.zip"
  281.   echo "Downloading V2Ray archive: $DOWNLOAD_LINK"
  282.   if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "$ZIP_FILE" "$DOWNLOAD_LINK"; then
  283.     echo 'error: Download failed! Please check your network or try again.'
  284.     return 1
  285.   fi
  286.   echo "Downloading verification file for V2Ray archive: $DOWNLOAD_LINK.dgst"
  287.   if ! curl -x "${PROXY}" -sSR -H 'Cache-Control: no-cache' -o "$ZIP_FILE.dgst" "$DOWNLOAD_LINK.dgst"; then
  288.     echo 'error: Download failed! Please check your network or try again.'
  289.     return 1
  290.   fi
  291.   if [[ "$(cat "$ZIP_FILE".dgst)" == 'Not Found' ]]; then
  292.     echo 'error: This version does not support verification. Please replace with another version.'
  293.     return 1
  294.   fi
  295.  
  296.   # Verification of V2Ray archive
  297.   for LISTSUM in 'md5' 'sha1' 'sha256' 'sha512'; do
  298.     SUM="$(${LISTSUM}sum "$ZIP_FILE" | sed 's/ .*//')"
  299.     CHECKSUM="$(grep ${LISTSUM^^} "$ZIP_FILE".dgst | grep "$SUM" -o -a | uniq)"
  300.     if [[ "$SUM" != "$CHECKSUM" ]]; then
  301.       echo 'error: Check failed! Please check your network or try again.'
  302.       return 1
  303.     fi
  304.   done
  305. }
  306.  
  307. decompression() {
  308.   if ! unzip -q "$1" -d "$TMP_DIRECTORY"; then
  309.     echo 'error: V2Ray decompression failed.'
  310.     "rm" -r "$TMP_DIRECTORY"
  311.     echo "removed: $TMP_DIRECTORY"
  312.     exit 1
  313.   fi
  314.   echo "info: Extract the V2Ray package to $TMP_DIRECTORY and prepare it for installation."
  315. }
  316.  
  317. install_file() {
  318.   NAME="$1"
  319.   if [[ "$NAME" == 'v2ray' ]] || [[ "$NAME" == 'v2ctl' ]]; then
  320.     install -m 755 "${TMP_DIRECTORY}/$NAME" "/usr/local/bin/$NAME"
  321.   elif [[ "$NAME" == 'geoip.dat' ]] || [[ "$NAME" == 'geosite.dat' ]]; then
  322.     install -m 644 "${TMP_DIRECTORY}/$NAME" "${DAT_PATH}/$NAME"
  323.   fi
  324. }
  325.  
  326. install_v2ray() {
  327.   # Install V2Ray binary to /usr/local/bin/ and $DAT_PATH
  328.   install_file v2ray
  329.   install_file v2ctl
  330.   install -d "$DAT_PATH"
  331.   # If the file exists, geoip.dat and geosite.dat will not be installed or updated
  332.   if [[ ! -f "${DAT_PATH}/.undat" ]]; then
  333.     install_file geoip.dat
  334.     install_file geosite.dat
  335.   fi
  336.  
  337.   # Install V2Ray configuration file to $JSON_PATH
  338.   # shellcheck disable=SC2153
  339.   if [[ -z "$JSONS_PATH" ]] && [[ ! -d "$JSON_PATH" ]]; then
  340.     install -d "$JSON_PATH"
  341.     echo "{}" > "${JSON_PATH}/config.json"
  342.     CONFIG_NEW='1'
  343.   fi
  344.  
  345.   # Install V2Ray configuration file to $JSONS_PATH
  346.   if [[ -n "$JSONS_PATH" ]] && [[ ! -d "$JSONS_PATH" ]]; then
  347.     install -d "$JSONS_PATH"
  348.     for BASE in 00_log 01_api 02_dns 03_routing 04_policy 05_inbounds 06_outbounds 07_transport 08_stats 09_reverse; do
  349.       echo '{}' > "${JSONS_PATH}/${BASE}.json"
  350.     done
  351.     CONFDIR='1'
  352.   fi
  353.  
  354.   # Used to store V2Ray log files
  355.   if [[ ! -d '/var/log/v2ray/' ]]; then
  356.     if id nobody | grep -qw 'nogroup'; then
  357.       install -d -m 700 -o nobody -g nogroup /var/log/v2ray/
  358.       install -m 600 -o nobody -g nogroup /dev/null /var/log/v2ray/access.log
  359.       install -m 600 -o nobody -g nogroup /dev/null /var/log/v2ray/error.log
  360.     else
  361.       install -d -m 700 -o nobody -g nobody /var/log/v2ray/
  362.       install -m 600 -o nobody -g nobody /dev/null /var/log/v2ray/access.log
  363.       install -m 600 -o nobody -g nobody /dev/null /var/log/v2ray/error.log
  364.     fi
  365.     LOG='1'
  366.   fi
  367. }
  368.  
  369. install_startup_service_file() {
  370.   install -m 644 "${TMP_DIRECTORY}/systemd/system/v2ray.service" /etc/systemd/system/v2ray.service
  371.   install -m 644 "${TMP_DIRECTORY}/systemd/system/[email protected]" /etc/systemd/system/[email protected]
  372.   mkdir -p '/etc/systemd/system/v2ray.service.d'
  373.   mkdir -p '/etc/systemd/system/[email protected]/'
  374.   if [[ -n "$JSONS_PATH" ]]; then
  375.     "rm" -f '/etc/systemd/system/v2ray.service.d/10-donot_touch_single_conf.conf' \
  376.       '/etc/systemd/system/[email protected]/10-donot_touch_single_conf.conf'
  377.     echo "# In case you have a good reason to do so, duplicate this file in the same directory and make your customizes there.
  378. # Or all changes you made will be lost!  # Refer: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
  379. [Service]
  380. ExecStart=
  381. ExecStart=/usr/local/bin/v2ray -confdir $JSONS_PATH" |
  382.       tee '/etc/systemd/system/v2ray.service.d/10-donot_touch_multi_conf.conf' > \
  383.         '/etc/systemd/system/[email protected]/10-donot_touch_multi_conf.conf'
  384.   else
  385.     "rm" -f '/etc/systemd/system/v2ray.service.d/10-donot_touch_multi_conf.conf' \
  386.       '/etc/systemd/system/[email protected]/10-donot_touch_multi_conf.conf'
  387.     echo "# In case you have a good reason to do so, duplicate this file in the same directory and make your customizes there.
  388. # Or all changes you made will be lost!  # Refer: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
  389. [Service]
  390. ExecStart=
  391. ExecStart=/usr/local/bin/v2ray -config ${JSON_PATH}/config.json" > \
  392.       '/etc/systemd/system/v2ray.service.d/10-donot_touch_single_conf.conf'
  393.     echo "# In case you have a good reason to do so, duplicate this file in the same directory and make your customizes there.
  394. # Or all changes you made will be lost!  # Refer: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
  395. [Service]
  396. ExecStart=
  397. ExecStart=/usr/local/bin/v2ray -config ${JSON_PATH}/%i.json" > \
  398.       '/etc/systemd/system/[email protected]/10-donot_touch_single_conf.conf'
  399.   fi
  400.   echo "info: Systemd service files have been installed successfully!"
  401.   echo "${red}warning: ${green}The following are the actual parameters for the v2ray service startup."
  402.   echo "${red}warning: ${green}Please make sure the configuration file path is correctly set.${reset}"
  403.   systemd_cat_config /etc/systemd/system/v2ray.service
  404.   # shellcheck disable=SC2154
  405.   if [[ x"${check_all_service_files:0:1}" = x'y' ]]; then
  406.     echo
  407.     echo
  408.     systemd_cat_config /etc/systemd/system/[email protected]
  409.   fi
  410.   systemctl daemon-reload
  411.   SYSTEMD='1'
  412. }
  413.  
  414. start_v2ray() {
  415.   if [[ -f '/etc/systemd/system/v2ray.service' ]]; then
  416.     if systemctl start "${V2RAY_CUSTOMIZE:-v2ray}"; then
  417.       echo 'info: Start the V2Ray service.'
  418.     else
  419.       echo 'error: Failed to start V2Ray service.'
  420.       exit 1
  421.     fi
  422.   fi
  423. }
  424.  
  425. stop_v2ray() {
  426.   V2RAY_CUSTOMIZE="$(systemctl list-units | grep 'v2ray@' | awk -F ' ' '{print $1}')"
  427.   if [[ -z "$V2RAY_CUSTOMIZE" ]]; then
  428.     local v2ray_daemon_to_stop='v2ray.service'
  429.   else
  430.     local v2ray_daemon_to_stop="$V2RAY_CUSTOMIZE"
  431.   fi
  432.   if ! systemctl stop "$v2ray_daemon_to_stop"; then
  433.     echo 'error: Stopping the V2Ray service failed.'
  434.     exit 1
  435.   fi
  436.   echo 'info: Stop the V2Ray service.'
  437. }
  438.  
  439. check_update() {
  440.   if [[ -f '/etc/systemd/system/v2ray.service' ]]; then
  441.     get_version
  442.     local get_ver_exit_code=$?
  443.     if [[ "$get_ver_exit_code" -eq '0' ]]; then
  444.       echo "info: Found the latest release of V2Ray $RELEASE_VERSION . (Current release: $CURRENT_VERSION)"
  445.     elif [[ "$get_ver_exit_code" -eq '1' ]]; then
  446.       echo "info: No new version. The current version of V2Ray is $CURRENT_VERSION ."
  447.     fi
  448.     exit 0
  449.   else
  450.     echo 'error: V2Ray is not installed.'
  451.     exit 1
  452.   fi
  453. }
  454.  
  455. remove_v2ray() {
  456.   if systemctl list-unit-files | grep -qw 'v2ray'; then
  457.     if [[ -n "$(pidof v2ray)" ]]; then
  458.       stop_v2ray
  459.     fi
  460.     if ! ("rm" -r '/usr/local/bin/v2ray' \
  461.       '/usr/local/bin/v2ctl' \
  462.       "$DAT_PATH" \
  463.       '/etc/systemd/system/v2ray.service' \
  464.       '/etc/systemd/system/[email protected]' \
  465.       '/etc/systemd/system/v2ray.service.d' \
  466.       '/etc/systemd/system/[email protected]'); then
  467.       echo 'error: Failed to remove V2Ray.'
  468.       exit 1
  469.     else
  470.       echo 'removed: /usr/local/bin/v2ray'
  471.       echo 'removed: /usr/local/bin/v2ctl'
  472.       echo "removed: $DAT_PATH"
  473.       echo 'removed: /etc/systemd/system/v2ray.service'
  474.       echo 'removed: /etc/systemd/system/[email protected]'
  475.       echo 'removed: /etc/systemd/system/v2ray.service.d'
  476.       echo 'removed: /etc/systemd/system/[email protected]'
  477.       echo 'Please execute the command: systemctl disable v2ray'
  478.       echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl unzip"
  479.       echo 'info: V2Ray has been removed.'
  480.       echo 'info: If necessary, manually delete the configuration and log files.'
  481.       if [[ -n "$JSONS_PATH" ]]; then
  482.         echo "info: e.g., $JSONS_PATH and /var/log/v2ray/ ..."
  483.       else
  484.         echo "info: e.g., $JSON_PATH and /var/log/v2ray/ ..."
  485.       fi
  486.       exit 0
  487.     fi
  488.   else
  489.     echo 'error: V2Ray is not installed.'
  490.     exit 1
  491.   fi
  492. }
  493.  
  494. # Explanation of parameters in the script
  495. show_help() {
  496.   echo "usage: $0 [--remove | --version number | -c | -f | -h | -l | -p]"
  497.   echo '  [-p address] [--version number | -c | -f]'
  498.   echo '  --remove        Remove V2Ray'
  499.   echo '  --version       Install the specified version of V2Ray, e.g., --version v4.18.0'
  500.   echo '  -c, --check     Check if V2Ray can be updated'
  501.   echo '  -f, --force     Force installation of the latest version of V2Ray'
  502.   echo '  -h, --help      Show help'
  503.   echo '  -l, --local     Install V2Ray from a local file'
  504.   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'
  505.   exit 0
  506. }
  507.  
  508. main() {
  509.   check_if_running_as_root
  510.   identify_the_operating_system_and_architecture
  511.   judgment_parameters "$@"
  512.  
  513.   install_software "$package_provide_tput" 'tput'
  514.   red=$(tput setaf 1)
  515.   green=$(tput setaf 2)
  516.   aoi=$(tput setaf 6)
  517.   reset=$(tput sgr0)
  518.  
  519.   # Parameter information
  520.   [[ "$HELP" -eq '1' ]] && show_help
  521.   [[ "$CHECK" -eq '1' ]] && check_update
  522.   [[ "$REMOVE" -eq '1' ]] && remove_v2ray
  523.  
  524.   # Two very important variables
  525.   TMP_DIRECTORY="$(mktemp -d)"
  526.   ZIP_FILE="${TMP_DIRECTORY}/v2ray-linux-$MACHINE.zip"
  527.  
  528.   # Install V2Ray from a local file, but still need to make sure the network is available
  529.   if [[ "$LOCAL_INSTALL" -eq '1' ]]; then
  530.     echo 'warn: Install V2Ray from a local file, but still need to make sure the network is available.'
  531.     echo -n 'warn: Please make sure the file is valid because we cannot confirm it. (Press any key) ...'
  532.     read -r
  533.     install_software 'unzip' 'unzip'
  534.     decompression "$LOCAL_FILE"
  535.   else
  536.     # Normal way
  537.     install_software 'curl' 'curl'
  538.     get_version
  539.     NUMBER="$?"
  540.     if [[ "$NUMBER" -eq '0' ]] || [[ "$FORCE" -eq '1' ]] || [[ "$NUMBER" -eq 2 ]]; then
  541.       echo "info: Installing V2Ray $RELEASE_VERSION for $(uname -m)"
  542.       download_v2ray
  543.       if [[ "$?" -eq '1' ]]; then
  544.         "rm" -r "$TMP_DIRECTORY"
  545.         echo "removed: $TMP_DIRECTORY"
  546.         exit 1
  547.       fi
  548.       install_software 'unzip' 'unzip'
  549.       decompression "$ZIP_FILE"
  550.     elif [[ "$NUMBER" -eq '1' ]]; then
  551.       echo "info: No new version. The current version of V2Ray is $CURRENT_VERSION ."
  552.       exit 0
  553.     fi
  554.   fi
  555.  
  556.   # Determine if V2Ray is running
  557.   if systemctl list-unit-files | grep -qw 'v2ray'; then
  558.     if [[ -n "$(pidof v2ray)" ]]; then
  559.       stop_v2ray
  560.       V2RAY_RUNNING='1'
  561.     fi
  562.   fi
  563.   install_v2ray
  564.   install_startup_service_file
  565.   echo 'installed: /usr/local/bin/v2ray'
  566.   echo 'installed: /usr/local/bin/v2ctl'
  567.   # If the file exists, the content output of installing or updating geoip.dat and geosite.dat will not be displayed
  568.   if [[ ! -f "${DAT_PATH}/.undat" ]]; then
  569.     echo "installed: ${DAT_PATH}/geoip.dat"
  570.     echo "installed: ${DAT_PATH}/geosite.dat"
  571.   fi
  572.   if [[ "$CONFIG_NEW" -eq '1' ]]; then
  573.     echo "installed: ${JSON_PATH}/config.json"
  574.   fi
  575.   if [[ "$CONFDIR" -eq '1' ]]; then
  576.     echo "installed: ${JSON_PATH}/00_log.json"
  577.     echo "installed: ${JSON_PATH}/01_api.json"
  578.     echo "installed: ${JSON_PATH}/02_dns.json"
  579.     echo "installed: ${JSON_PATH}/03_routing.json"
  580.     echo "installed: ${JSON_PATH}/04_policy.json"
  581.     echo "installed: ${JSON_PATH}/05_inbounds.json"
  582.     echo "installed: ${JSON_PATH}/06_outbounds.json"
  583.     echo "installed: ${JSON_PATH}/07_transport.json"
  584.     echo "installed: ${JSON_PATH}/08_stats.json"
  585.     echo "installed: ${JSON_PATH}/09_reverse.json"
  586.   fi
  587.   if [[ "$LOG" -eq '1' ]]; then
  588.     echo 'installed: /var/log/v2ray/'
  589.     echo 'installed: /var/log/v2ray/access.log'
  590.     echo 'installed: /var/log/v2ray/error.log'
  591.   fi
  592.   if [[ "$SYSTEMD" -eq '1' ]]; then
  593.     echo 'installed: /etc/systemd/system/v2ray.service'
  594.     echo 'installed: /etc/systemd/system/[email protected]'
  595.   fi
  596.   "rm" -r "$TMP_DIRECTORY"
  597.   echo "removed: $TMP_DIRECTORY"
  598.   if [[ "$LOCAL_INSTALL" -eq '1' ]]; then
  599.     get_version
  600.   fi
  601.   echo "info: V2Ray $RELEASE_VERSION is installed."
  602.   echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl unzip"
  603.   if [[ "$V2RAY_RUNNING" -eq '1' ]]; then
  604.     start_v2ray
  605.   else
  606.     echo 'Please execute the command: systemctl enable v2ray; systemctl start v2ray'
  607.   fi
  608. }
  609.  
  610. main "$@"