├── install-dat.sh └── install-release.sh /install-dat.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # shellcheck disable=SC2268 3 | 4 | # This Bash script to install the latest release of geoip.dat and geosite.dat: 5 | 6 | # https://github.com/v2fly/geoip 7 | # https://github.com/v2fly/domain-list-community 8 | 9 | # Depends on cURL, please solve it yourself 10 | 11 | # You may plan to execute this Bash script regularly: 12 | 13 | # install -m 755 install-dat-release.sh /usr/local/bin/install-dat-release 14 | 15 | # 0 0 * * * /usr/local/bin/install-dat-release > /dev/null 2>&1 16 | 17 | # You can set this variable whatever you want in shell session right before running this script by issuing: 18 | # export DAT_PATH='/usr/local/lib/v2ray' 19 | DAT_PATH=${DAT_PATH:-/usr/local/share/v2ray} 20 | 21 | DOWNLOAD_LINK_GEOIP="https://ghproxy.com/https://github.com/v2fly/geoip/releases/latest/download/geoip.dat" 22 | DOWNLOAD_LINK_GEOSITE="https://ghproxy.com/https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat" 23 | file_ip='geoip.dat' 24 | file_dlc='dlc.dat' 25 | file_site='geosite.dat' 26 | dir_tmp="$(mktemp -d)" 27 | 28 | curl() { 29 | $(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@" 30 | } 31 | 32 | check_if_running_as_root() { 33 | # If you want to run as another user, please modify $UID to be owned by this user 34 | if [[ "$UID" -ne '0' ]]; then 35 | echo "WARNING: The user currently executing this script is not root. You may encounter the insufficient privilege error." 36 | read -r -p "Are you sure you want to continue? [y/n] " cont_without_been_root 37 | if [[ x"${cont_without_been_root:0:1}" = x'y' ]]; then 38 | echo "Continuing the installation with current user..." 39 | else 40 | echo "Not running with root, exiting..." 41 | exit 1 42 | fi 43 | fi 44 | } 45 | 46 | download_files() { 47 | if ! curl -R -H 'Cache-Control: no-cache' -o "${dir_tmp}/${2}" "${1}"; then 48 | echo 'error: Download failed! Please check your network or try again.' 49 | exit 1 50 | fi 51 | if ! curl -R -H 'Cache-Control: no-cache' -o "${dir_tmp}/${2}.sha256sum" "${1}.sha256sum"; then 52 | echo 'error: Download failed! Please check your network or try again.' 53 | exit 1 54 | fi 55 | } 56 | 57 | check_sum() { 58 | ( 59 | cd "${dir_tmp}" || exit 60 | for i in "${dir_tmp}"/*.sha256sum; do 61 | if ! sha256sum -c "${i}"; then 62 | echo 'error: Check failed! Please check your network or try again.' 63 | exit 1 64 | fi 65 | done 66 | ) 67 | } 68 | 69 | install_file() { 70 | install -m 644 "${dir_tmp}"/${file_dlc} "${DAT_PATH}"/${file_site} 71 | install -m 644 "${dir_tmp}"/${file_ip} "${DAT_PATH}"/${file_ip} 72 | rm -r "${dir_tmp}" 73 | } 74 | 75 | main() { 76 | check_if_running_as_root 77 | download_files $DOWNLOAD_LINK_GEOIP $file_ip 78 | download_files $DOWNLOAD_LINK_GEOSITE $file_dlc 79 | check_sum 80 | install_file 81 | } 82 | 83 | main "$@" 84 | -------------------------------------------------------------------------------- /install-release.sh: -------------------------------------------------------------------------------- 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_current_version() { 220 | if /usr/local/bin/v2ray -version > /dev/null 2>&1; then 221 | VERSION="$(/usr/local/bin/v2ray -version | awk 'NR==1 {print $2}')" 222 | else 223 | VERSION="$(/usr/local/bin/v2ray version | awk 'NR==1 {print $2}')" 224 | fi 225 | CURRENT_VERSION="v${VERSION#v}" 226 | } 227 | 228 | get_version() { 229 | # 0: Install or update V2Ray. 230 | # 1: Installed or no new version of V2Ray. 231 | # 2: Install the specified version of V2Ray. 232 | if [[ -n "$VERSION" ]]; then 233 | RELEASE_VERSION="v${VERSION#v}" 234 | return 2 235 | fi 236 | # Determine the version number for V2Ray installed from a local file 237 | if [[ -f '/usr/local/bin/v2ray' ]]; then 238 | get_current_version 239 | if [[ "$LOCAL_INSTALL" -eq '1' ]]; then 240 | RELEASE_VERSION="$CURRENT_VERSION" 241 | return 242 | fi 243 | fi 244 | # Get V2Ray release version number 245 | TMP_FILE="$(mktemp)" 246 | if ! curl -x "${PROXY}" -sS -i -H "Accept: application/vnd.github.v3+json" -o "$TMP_FILE" 'https://api.github.com/repos/v2fly/v2ray-core/releases/latest'; then 247 | "rm" "$TMP_FILE" 248 | echo 'error: Failed to get release list, please check your network.' 249 | exit 1 250 | fi 251 | HTTP_STATUS_CODE=$(awk 'NR==1 {print $2}' "$TMP_FILE") 252 | if [[ $HTTP_STATUS_CODE -lt 200 ]] || [[ $HTTP_STATUS_CODE -gt 299 ]]; then 253 | "rm" "$TMP_FILE" 254 | echo "error: Failed to get release list, GitHub API response code: $HTTP_STATUS_CODE" 255 | exit 1 256 | fi 257 | RELEASE_LATEST="$(sed 'y/,/\n/' "$TMP_FILE" | grep 'tag_name' | awk -F '"' '{print $4}')" 258 | "rm" "$TMP_FILE" 259 | RELEASE_VERSION="v${RELEASE_LATEST#v}" 260 | # Compare V2Ray version numbers 261 | if [[ "$RELEASE_VERSION" != "$CURRENT_VERSION" ]]; then 262 | RELEASE_VERSIONSION_NUMBER="${RELEASE_VERSION#v}" 263 | RELEASE_MAJOR_VERSION_NUMBER="${RELEASE_VERSIONSION_NUMBER%%.*}" 264 | RELEASE_MINOR_VERSION_NUMBER="$(echo "$RELEASE_VERSIONSION_NUMBER" | awk -F '.' '{print $2}')" 265 | RELEASE_MINIMUM_VERSION_NUMBER="${RELEASE_VERSIONSION_NUMBER##*.}" 266 | # shellcheck disable=SC2001 267 | CURRENT_VERSION_NUMBER="$(echo "${CURRENT_VERSION#v}" | sed 's/-.*//')" 268 | CURRENT_MAJOR_VERSION_NUMBER="${CURRENT_VERSION_NUMBER%%.*}" 269 | CURRENT_MINOR_VERSION_NUMBER="$(echo "$CURRENT_VERSION_NUMBER" | awk -F '.' '{print $2}')" 270 | CURRENT_MINIMUM_VERSION_NUMBER="${CURRENT_VERSION_NUMBER##*.}" 271 | if [[ "$RELEASE_MAJOR_VERSION_NUMBER" -gt "$CURRENT_MAJOR_VERSION_NUMBER" ]]; then 272 | return 0 273 | elif [[ "$RELEASE_MAJOR_VERSION_NUMBER" -eq "$CURRENT_MAJOR_VERSION_NUMBER" ]]; then 274 | if [[ "$RELEASE_MINOR_VERSION_NUMBER" -gt "$CURRENT_MINOR_VERSION_NUMBER" ]]; then 275 | return 0 276 | elif [[ "$RELEASE_MINOR_VERSION_NUMBER" -eq "$CURRENT_MINOR_VERSION_NUMBER" ]]; then 277 | if [[ "$RELEASE_MINIMUM_VERSION_NUMBER" -gt "$CURRENT_MINIMUM_VERSION_NUMBER" ]]; then 278 | return 0 279 | else 280 | return 1 281 | fi 282 | else 283 | return 1 284 | fi 285 | else 286 | return 1 287 | fi 288 | elif [[ "$RELEASE_VERSION" == "$CURRENT_VERSION" ]]; then 289 | return 1 290 | fi 291 | } 292 | 293 | download_v2ray() { 294 | DOWNLOAD_LINK="https://ghproxy.com/https://github.com/v2fly/v2ray-core/releases/download/$RELEASE_VERSION/v2ray-linux-$MACHINE.zip" 295 | echo "Downloading V2Ray archive: $DOWNLOAD_LINK" 296 | if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "$ZIP_FILE" "$DOWNLOAD_LINK"; then 297 | echo 'error: Download failed! Please check your network or try again.' 298 | return 1 299 | fi 300 | echo "Downloading verification file for V2Ray archive: $DOWNLOAD_LINK.dgst" 301 | if ! curl -x "${PROXY}" -sSR -H 'Cache-Control: no-cache' -o "$ZIP_FILE.dgst" "$DOWNLOAD_LINK.dgst"; then 302 | echo 'error: Download failed! Please check your network or try again.' 303 | return 1 304 | fi 305 | if [[ "$(cat "$ZIP_FILE".dgst)" == 'Not Found' ]]; then 306 | echo 'error: This version does not support verification. Please replace with another version.' 307 | return 1 308 | fi 309 | 310 | # Verification of V2Ray archive 311 | CHECKSUM=$(awk -F '= ' '/256=/ {print $2}' < "${ZIP_FILE}.dgst") 312 | LOCALSUM=$(sha256sum "$ZIP_FILE" | awk '{printf $1}') 313 | if [[ "$CHECKSUM" != "$LOCALSUM" ]]; then 314 | echo 'error: SHA256 check failed! Please check your network or try again.' 315 | return 1 316 | fi 317 | } 318 | 319 | decompression() { 320 | if ! unzip -q "$1" -d "$TMP_DIRECTORY"; then 321 | echo 'error: V2Ray decompression failed.' 322 | "rm" -r "$TMP_DIRECTORY" 323 | echo "removed: $TMP_DIRECTORY" 324 | exit 1 325 | fi 326 | echo "info: Extract the V2Ray package to $TMP_DIRECTORY and prepare it for installation." 327 | } 328 | 329 | install_file() { 330 | NAME="$1" 331 | if [[ "$NAME" == 'v2ray' ]] || [[ "$NAME" == 'v2ctl' ]]; then 332 | install -m 755 "${TMP_DIRECTORY}/$NAME" "/usr/local/bin/$NAME" 333 | elif [[ "$NAME" == 'geoip.dat' ]] || [[ "$NAME" == 'geosite.dat' ]]; then 334 | install -m 644 "${TMP_DIRECTORY}/$NAME" "${DAT_PATH}/$NAME" 335 | fi 336 | } 337 | 338 | install_v2ray() { 339 | # Install V2Ray binary to /usr/local/bin/ and $DAT_PATH 340 | install_file v2ray 341 | if [[ -f "${TMP_DIRECTORY}/v2ctl" ]]; then 342 | install_file v2ctl 343 | else 344 | if [[ -f '/usr/local/bin/v2ctl' ]]; then 345 | rm '/usr/local/bin/v2ctl' 346 | fi 347 | fi 348 | install -d "$DAT_PATH" 349 | # If the file exists, geoip.dat and geosite.dat will not be installed or updated 350 | if [[ ! -f "${DAT_PATH}/.undat" ]]; then 351 | install_file geoip.dat 352 | install_file geosite.dat 353 | fi 354 | 355 | # Install V2Ray configuration file to $JSON_PATH 356 | # shellcheck disable=SC2153 357 | if [[ -z "$JSONS_PATH" ]] && [[ ! -d "$JSON_PATH" ]]; then 358 | install -d "$JSON_PATH" 359 | echo "{}" > "${JSON_PATH}/config.json" 360 | CONFIG_NEW='1' 361 | fi 362 | 363 | # Install V2Ray configuration file to $JSONS_PATH 364 | if [[ -n "$JSONS_PATH" ]] && [[ ! -d "$JSONS_PATH" ]]; then 365 | install -d "$JSONS_PATH" 366 | for BASE in 00_log 01_api 02_dns 03_routing 04_policy 05_inbounds 06_outbounds 07_transport 08_stats 09_reverse; do 367 | echo '{}' > "${JSONS_PATH}/${BASE}.json" 368 | done 369 | CONFDIR='1' 370 | fi 371 | 372 | # Used to store V2Ray log files 373 | if [[ ! -d '/var/log/v2ray/' ]]; then 374 | if id nobody | grep -qw 'nogroup'; then 375 | install -d -m 700 -o nobody -g nogroup /var/log/v2ray/ 376 | install -m 600 -o nobody -g nogroup /dev/null /var/log/v2ray/access.log 377 | install -m 600 -o nobody -g nogroup /dev/null /var/log/v2ray/error.log 378 | else 379 | install -d -m 700 -o nobody -g nobody /var/log/v2ray/ 380 | install -m 600 -o nobody -g nobody /dev/null /var/log/v2ray/access.log 381 | install -m 600 -o nobody -g nobody /dev/null /var/log/v2ray/error.log 382 | fi 383 | LOG='1' 384 | fi 385 | } 386 | 387 | install_startup_service_file() { 388 | get_current_version 389 | if [[ "$(echo "${CURRENT_VERSION#v}" | sed 's/-.*//' | awk -F'.' '{print $1}')" -gt "4" ]]; then 390 | START_COMMAND="/usr/local/bin/v2ray run" 391 | else 392 | START_COMMAND="/usr/local/bin/v2ray" 393 | fi 394 | install -m 644 "${TMP_DIRECTORY}/systemd/system/v2ray.service" /etc/systemd/system/v2ray.service 395 | install -m 644 "${TMP_DIRECTORY}/systemd/system/v2ray@.service" /etc/systemd/system/v2ray@.service 396 | mkdir -p '/etc/systemd/system/v2ray.service.d' 397 | mkdir -p '/etc/systemd/system/v2ray@.service.d/' 398 | if [[ -n "$JSONS_PATH" ]]; then 399 | "rm" -f '/etc/systemd/system/v2ray.service.d/10-donot_touch_single_conf.conf' \ 400 | '/etc/systemd/system/v2ray@.service.d/10-donot_touch_single_conf.conf' 401 | echo "# In case you have a good reason to do so, duplicate this file in the same directory and make your customizes there. 402 | # Or all changes you made will be lost! # Refer: https://www.freedesktop.org/software/systemd/man/systemd.unit.html 403 | [Service] 404 | ExecStart= 405 | ExecStart=${START_COMMAND} -confdir $JSONS_PATH" | 406 | tee '/etc/systemd/system/v2ray.service.d/10-donot_touch_multi_conf.conf' > '/etc/systemd/system/v2ray@.service.d/10-donot_touch_multi_conf.conf' 407 | else 408 | "rm" -f '/etc/systemd/system/v2ray.service.d/10-donot_touch_multi_conf.conf' \ 409 | '/etc/systemd/system/v2ray@.service.d/10-donot_touch_multi_conf.conf' 410 | echo "# In case you have a good reason to do so, duplicate this file in the same directory and make your customizes there. 411 | # Or all changes you made will be lost! # Refer: https://www.freedesktop.org/software/systemd/man/systemd.unit.html 412 | [Service] 413 | ExecStart= 414 | ExecStart=${START_COMMAND} -config ${JSON_PATH}/config.json" > '/etc/systemd/system/v2ray.service.d/10-donot_touch_single_conf.conf' 415 | echo "# In case you have a good reason to do so, duplicate this file in the same directory and make your customizes there. 416 | # Or all changes you made will be lost! # Refer: https://www.freedesktop.org/software/systemd/man/systemd.unit.html 417 | [Service] 418 | ExecStart= 419 | ExecStart=${START_COMMAND} -config ${JSON_PATH}/%i.json" > '/etc/systemd/system/v2ray@.service.d/10-donot_touch_single_conf.conf' 420 | fi 421 | echo "info: Systemd service files have been installed successfully!" 422 | echo "${red}warning: ${green}The following are the actual parameters for the v2ray service startup." 423 | echo "${red}warning: ${green}Please make sure the configuration file path is correctly set.${reset}" 424 | systemd_cat_config /etc/systemd/system/v2ray.service 425 | # shellcheck disable=SC2154 426 | if [[ x"${check_all_service_files:0:1}" = x'y' ]]; then 427 | echo 428 | echo 429 | systemd_cat_config /etc/systemd/system/v2ray@.service 430 | fi 431 | systemctl daemon-reload 432 | SYSTEMD='1' 433 | } 434 | 435 | start_v2ray() { 436 | if [[ -f '/etc/systemd/system/v2ray.service' ]]; then 437 | if systemctl start "${V2RAY_CUSTOMIZE:-v2ray}"; then 438 | echo 'info: Start the V2Ray service.' 439 | else 440 | echo 'error: Failed to start V2Ray service.' 441 | exit 1 442 | fi 443 | fi 444 | } 445 | 446 | stop_v2ray() { 447 | V2RAY_CUSTOMIZE="$(systemctl list-units | grep 'v2ray@' | awk -F ' ' '{print $1}')" 448 | if [[ -z "$V2RAY_CUSTOMIZE" ]]; then 449 | local v2ray_daemon_to_stop='v2ray.service' 450 | else 451 | local v2ray_daemon_to_stop="$V2RAY_CUSTOMIZE" 452 | fi 453 | if ! systemctl stop "$v2ray_daemon_to_stop"; then 454 | echo 'error: Stopping the V2Ray service failed.' 455 | exit 1 456 | fi 457 | echo 'info: Stop the V2Ray service.' 458 | } 459 | 460 | check_update() { 461 | if [[ -f '/etc/systemd/system/v2ray.service' ]]; then 462 | get_version 463 | local get_ver_exit_code=$? 464 | if [[ "$get_ver_exit_code" -eq '0' ]]; then 465 | echo "info: Found the latest release of V2Ray $RELEASE_VERSION . (Current release: $CURRENT_VERSION)" 466 | elif [[ "$get_ver_exit_code" -eq '1' ]]; then 467 | echo "info: No new version. The current version of V2Ray is $CURRENT_VERSION ." 468 | fi 469 | exit 0 470 | else 471 | echo 'error: V2Ray is not installed.' 472 | exit 1 473 | fi 474 | } 475 | 476 | remove_v2ray() { 477 | if systemctl list-unit-files | grep -qw 'v2ray'; then 478 | if [[ -n "$(pidof v2ray)" ]]; then 479 | stop_v2ray 480 | fi 481 | if ! ("rm" -r '/usr/local/bin/v2ray' \ 482 | "$DAT_PATH" \ 483 | '/etc/systemd/system/v2ray.service' \ 484 | '/etc/systemd/system/v2ray@.service' \ 485 | '/etc/systemd/system/v2ray.service.d' \ 486 | '/etc/systemd/system/v2ray@.service.d'); then 487 | echo 'error: Failed to remove V2Ray.' 488 | exit 1 489 | else 490 | echo 'removed: /usr/local/bin/v2ray' 491 | if [[ -f '/usr/local/bin/v2ctl' ]]; then 492 | rm '/usr/local/bin/v2ctl' 493 | echo 'removed: /usr/local/bin/v2ctl' 494 | fi 495 | echo "removed: $DAT_PATH" 496 | echo 'removed: /etc/systemd/system/v2ray.service' 497 | echo 'removed: /etc/systemd/system/v2ray@.service' 498 | echo 'removed: /etc/systemd/system/v2ray.service.d' 499 | echo 'removed: /etc/systemd/system/v2ray@.service.d' 500 | echo 'Please execute the command: systemctl disable v2ray' 501 | echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl unzip" 502 | echo 'info: V2Ray has been removed.' 503 | echo 'info: If necessary, manually delete the configuration and log files.' 504 | if [[ -n "$JSONS_PATH" ]]; then 505 | echo "info: e.g., $JSONS_PATH and /var/log/v2ray/ ..." 506 | else 507 | echo "info: e.g., $JSON_PATH and /var/log/v2ray/ ..." 508 | fi 509 | exit 0 510 | fi 511 | else 512 | echo 'error: V2Ray is not installed.' 513 | exit 1 514 | fi 515 | } 516 | 517 | # Explanation of parameters in the script 518 | show_help() { 519 | echo "usage: $0 [--remove | --version number | -c | -f | -h | -l | -p]" 520 | echo ' [-p address] [--version number | -c | -f]' 521 | echo ' --remove Remove V2Ray' 522 | echo ' --version Install the specified version of V2Ray, e.g., --version v4.18.0' 523 | echo ' -c, --check Check if V2Ray can be updated' 524 | echo ' -f, --force Force installation of the latest version of V2Ray' 525 | echo ' -h, --help Show help' 526 | echo ' -l, --local Install V2Ray from a local file' 527 | 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' 528 | exit 0 529 | } 530 | 531 | main() { 532 | check_if_running_as_root 533 | identify_the_operating_system_and_architecture 534 | judgment_parameters "$@" 535 | 536 | install_software "$package_provide_tput" 'tput' 537 | red=$(tput setaf 1) 538 | green=$(tput setaf 2) 539 | aoi=$(tput setaf 6) 540 | reset=$(tput sgr0) 541 | 542 | # Parameter information 543 | [[ "$HELP" -eq '1' ]] && show_help 544 | [[ "$CHECK" -eq '1' ]] && check_update 545 | [[ "$REMOVE" -eq '1' ]] && remove_v2ray 546 | 547 | # Two very important variables 548 | TMP_DIRECTORY="$(mktemp -d)" 549 | ZIP_FILE="${TMP_DIRECTORY}/v2ray-linux-$MACHINE.zip" 550 | 551 | # Install V2Ray from a local file, but still need to make sure the network is available 552 | if [[ "$LOCAL_INSTALL" -eq '1' ]]; then 553 | echo 'warn: Install V2Ray from a local file, but still need to make sure the network is available.' 554 | echo -n 'warn: Please make sure the file is valid because we cannot confirm it. (Press any key) ...' 555 | read -r 556 | install_software 'unzip' 'unzip' 557 | decompression "$LOCAL_FILE" 558 | else 559 | # Normal way 560 | install_software 'curl' 'curl' 561 | get_version 562 | NUMBER="$?" 563 | if [[ "$NUMBER" -eq '0' ]] || [[ "$FORCE" -eq '1' ]] || [[ "$NUMBER" -eq 2 ]]; then 564 | echo "info: Installing V2Ray $RELEASE_VERSION for $(uname -m)" 565 | download_v2ray 566 | if [[ "$?" -eq '1' ]]; then 567 | "rm" -r "$TMP_DIRECTORY" 568 | echo "removed: $TMP_DIRECTORY" 569 | exit 1 570 | fi 571 | install_software 'unzip' 'unzip' 572 | decompression "$ZIP_FILE" 573 | elif [[ "$NUMBER" -eq '1' ]]; then 574 | echo "info: No new version. The current version of V2Ray is $CURRENT_VERSION ." 575 | exit 0 576 | fi 577 | fi 578 | 579 | # Determine if V2Ray is running 580 | if systemctl list-unit-files | grep -qw 'v2ray'; then 581 | if [[ -n "$(pidof v2ray)" ]]; then 582 | stop_v2ray 583 | V2RAY_RUNNING='1' 584 | fi 585 | fi 586 | install_v2ray 587 | install_startup_service_file 588 | echo 'installed: /usr/local/bin/v2ray' 589 | if [[ -f '/usr/local/bin/v2ctl' ]]; then 590 | echo 'installed: /usr/local/bin/v2ctl' 591 | fi 592 | # If the file exists, the content output of installing or updating geoip.dat and geosite.dat will not be displayed 593 | if [[ ! -f "${DAT_PATH}/.undat" ]]; then 594 | echo "installed: ${DAT_PATH}/geoip.dat" 595 | echo "installed: ${DAT_PATH}/geosite.dat" 596 | fi 597 | if [[ "$CONFIG_NEW" -eq '1' ]]; then 598 | echo "installed: ${JSON_PATH}/config.json" 599 | fi 600 | if [[ "$CONFDIR" -eq '1' ]]; then 601 | echo "installed: ${JSON_PATH}/00_log.json" 602 | echo "installed: ${JSON_PATH}/01_api.json" 603 | echo "installed: ${JSON_PATH}/02_dns.json" 604 | echo "installed: ${JSON_PATH}/03_routing.json" 605 | echo "installed: ${JSON_PATH}/04_policy.json" 606 | echo "installed: ${JSON_PATH}/05_inbounds.json" 607 | echo "installed: ${JSON_PATH}/06_outbounds.json" 608 | echo "installed: ${JSON_PATH}/07_transport.json" 609 | echo "installed: ${JSON_PATH}/08_stats.json" 610 | echo "installed: ${JSON_PATH}/09_reverse.json" 611 | fi 612 | if [[ "$LOG" -eq '1' ]]; then 613 | echo 'installed: /var/log/v2ray/' 614 | echo 'installed: /var/log/v2ray/access.log' 615 | echo 'installed: /var/log/v2ray/error.log' 616 | fi 617 | if [[ "$SYSTEMD" -eq '1' ]]; then 618 | echo 'installed: /etc/systemd/system/v2ray.service' 619 | echo 'installed: /etc/systemd/system/v2ray@.service' 620 | fi 621 | "rm" -r "$TMP_DIRECTORY" 622 | echo "removed: $TMP_DIRECTORY" 623 | if [[ "$LOCAL_INSTALL" -eq '1' ]]; then 624 | get_version 625 | fi 626 | echo "info: V2Ray $RELEASE_VERSION is installed." 627 | echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl unzip" 628 | if [[ "$V2RAY_RUNNING" -eq '1' ]]; then 629 | start_v2ray 630 | else 631 | echo 'Please execute the command: systemctl enable v2ray; systemctl start v2ray' 632 | fi 633 | } 634 | 635 | main "$@" 636 | --------------------------------------------------------------------------------