install-release.sh 4.1 KB

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