install-release.sh 4.2 KB

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