install-release.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/bin/bash
  2. # This file is accessible as https://install.direct/go.sh
  3. # Original source is located at github.com/v2ray/v2ray-core/release/install-release.sh
  4. while [[ $# > 0 ]]
  5. do
  6. key="$1"
  7. case $key in
  8. -p|--proxy)
  9. PROXY="$2"
  10. shift # past argument
  11. ;;
  12. -h|--help)
  13. HELP="1"
  14. ;;
  15. -f|--force)
  16. FORCE="1"
  17. ;;
  18. --version)
  19. VERSION="$2"
  20. shift
  21. ;;
  22. --local)
  23. LOCAL="$2"
  24. shift
  25. ;;
  26. *)
  27. # unknown option
  28. ;;
  29. esac
  30. shift # past argument or value
  31. done
  32. if [[ "$HELP" == "1" ]]; then
  33. echo "./install-release.sh [-p proxy] [-h] [-f] [--version vx.y.z] [--local file]"
  34. echo "-p: To download through a proxy server, use -p socks5://127.0.0.1:1080 or -p http://127.0.0.1:3128 etc"
  35. echo "-h: Show help"
  36. echo "-f: Force install"
  37. echo "--version: Install a particular version"
  38. echo "--local: Install from a local file"
  39. exit
  40. fi
  41. YUM_CMD=$(command -v yum)
  42. APT_CMD=$(command -v apt-get)
  43. SOFTWARE_UPDATED=0
  44. function update_software() {
  45. if [ ${SOFTWARE_UPDATED} -eq 1 ]; then
  46. return
  47. fi
  48. if [ -n "${YUM_CMD}" ]; then
  49. echo "Updating software repo via yum."
  50. ${YUM_CMD} -q makecache
  51. elif [ -n "${APT_CMD}" ]; then
  52. echo "Updating software repo via apt-get."
  53. ${APT_CMD} -qq update
  54. fi
  55. SOFTWARE_UPDATED=1
  56. }
  57. function install_component() {
  58. local COMPONENT=$1
  59. COMPONENT_CMD=$(command -v $COMPONENT)
  60. if [ -n "${COMPONENT_CMD}" ]; then
  61. return
  62. fi
  63. update_software
  64. if [ -n "${YUM_CMD}" ]; then
  65. echo "Installing ${COMPONENT} via yum."
  66. ${YUM_CMD} -y -q install $COMPONENT
  67. elif [ -n "${APT_CMD}" ]; then
  68. echo "Installing ${COMPONENT} via apt-get."
  69. ${APT_CMD} -y -qq install $COMPONENT
  70. fi
  71. }
  72. V2RAY_RUNNING=0
  73. if pgrep "v2ray" > /dev/null ; then
  74. V2RAY_RUNNING=1
  75. fi
  76. if [ -n "$VERSION" ]; then
  77. VER="$VERSION"
  78. else
  79. VER="$(curl -s https://api.github.com/repos/v2ray/v2ray-core/releases/latest | grep 'tag_name' | cut -d\" -f4)"
  80. CUR_VER="$(/usr/bin/v2ray/v2ray -version | head -n 1 | cut -d " " -f2)"
  81. if [[ "$VER" == "$CUR_VER" ]] && [[ "$FORCE" != "1" ]]; then
  82. echo "Lastest version $VER is already installed. Exiting..."
  83. exit
  84. fi
  85. fi
  86. ARCH=$(uname -m)
  87. VDIS="64"
  88. if [[ "$ARCH" == "i686" ]] || [[ "$ARCH" == "i386" ]]; then
  89. VDIS="32"
  90. elif [[ "$ARCH" == *"armv7"* ]] || [[ "$ARCH" == "armv6l" ]]; then
  91. VDIS="arm"
  92. elif [[ "$ARCH" == *"armv8"* ]]; then
  93. VDIS="arm64"
  94. fi
  95. rm -rf /tmp/v2ray
  96. mkdir -p /tmp/v2ray
  97. echo "Installing V2Ray ${VER} on ${ARCH}"
  98. if [ -n "$LOCAL" ]; then
  99. cp "$LOCAL" "/tmp/v2ray/v2ray.zip"
  100. else
  101. DOWNLOAD_LINK="https://github.com/v2ray/v2ray-core/releases/download/${VER}/v2ray-linux-${VDIS}.zip"
  102. install_component "curl"
  103. if [ -n "${PROXY}" ]; then
  104. echo "Downloading ${DOWNLOAD_LINK} via proxy ${PROXY}."
  105. curl -x ${PROXY} -L -H "Cache-Control: no-cache" -o "/tmp/v2ray/v2ray.zip" ${DOWNLOAD_LINK}
  106. else
  107. echo "Downloading ${DOWNLOAD_LINK} directly."
  108. curl -L -H "Cache-Control: no-cache" -o "/tmp/v2ray/v2ray.zip" ${DOWNLOAD_LINK}
  109. fi
  110. fi
  111. echo "Extracting V2Ray package to /tmp/v2ray."
  112. install_component "unzip"
  113. unzip "/tmp/v2ray/v2ray.zip" -d "/tmp/v2ray/"
  114. # Create folder for V2Ray log.
  115. mkdir -p /var/log/v2ray
  116. # Stop v2ray daemon if necessary.
  117. SYSTEMCTL_CMD=$(command -v systemctl)
  118. SERVICE_CMD=$(command -v service)
  119. if [ ${V2RAY_RUNNING} -eq 1 ]; then
  120. echo "Shutting down V2Ray service."
  121. if [ -n "${SYSTEMCTL_CMD}" ]; then
  122. if [ -f "/lib/systemd/system/v2ray.service" ]; then
  123. ${SYSTEMCTL_CMD} stop v2ray
  124. fi
  125. elif [ -n "${SERVICE_CMD}" ]; then
  126. if [ -f "/etc/init.d/v2ray" ]; then
  127. ${SERVICE_CMD} v2ray stop
  128. fi
  129. fi
  130. fi
  131. # Install V2Ray binary to /usr/bin/v2ray
  132. mkdir -p /usr/bin/v2ray
  133. cp "/tmp/v2ray/v2ray-${VER}-linux-${VDIS}/v2ray" "/usr/bin/v2ray/v2ray"
  134. chmod +x "/usr/bin/v2ray/v2ray"
  135. # Install V2Ray server config to /etc/v2ray
  136. mkdir -p /etc/v2ray
  137. if [ ! -f "/etc/v2ray/config.json" ]; then
  138. cp "/tmp/v2ray/v2ray-${VER}-linux-${VDIS}/vpoint_vmess_freedom.json" "/etc/v2ray/config.json"
  139. let PORT=$RANDOM+10000
  140. sed -i "s/10086/${PORT}/g" "/etc/v2ray/config.json"
  141. UUID=$(cat /proc/sys/kernel/random/uuid)
  142. sed -i "s/23ad6b10-8d1a-40f7-8ad0-e3e35cd38297/${UUID}/g" "/etc/v2ray/config.json"
  143. echo "PORT:${PORT}"
  144. echo "UUID:${UUID}"
  145. fi
  146. if [ -n "${SYSTEMCTL_CMD}" ]; then
  147. if [ ! -f "/lib/systemd/system/v2ray.service" ]; then
  148. cp "/tmp/v2ray/v2ray-${VER}-linux-${VDIS}/systemd/v2ray.service" "/lib/systemd/system/"
  149. systemctl enable v2ray
  150. else
  151. if [ ${V2RAY_RUNNING} -eq 1 ]; then
  152. echo "Restarting V2Ray service."
  153. ${SYSTEMCTL_CMD} start v2ray
  154. fi
  155. fi
  156. elif [ -n "${SERVICE_CMD}" ]; then # Configure SysV if necessary.
  157. if [ ! -f "/etc/init.d/v2ray" ]; then
  158. install_component "daemon"
  159. cp "/tmp/v2ray/v2ray-${VER}-linux-${VDIS}/systemv/v2ray" "/etc/init.d/v2ray"
  160. chmod +x "/etc/init.d/v2ray"
  161. update-rc.d v2ray defaults
  162. else
  163. if [ ${V2RAY_RUNNING} -eq 1 ]; then
  164. echo "Restarting V2Ray service."
  165. ${SERVICE_CMD} v2ray start
  166. fi
  167. fi
  168. fi
  169. echo "V2Ray ${VER} is installed."