install-release.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. update_software
  43. local COMPONENT=$1
  44. if [ -n "${YUM_CMD}" ]; then
  45. echo "Installing ${COMPONENT} via yum."
  46. ${YUM_CMD} -y -q install $COMPONENT
  47. elif [ -n "${APT_CMD}" ]; then
  48. echo "Installing ${COMPONENT} via apt-get."
  49. ${APT_CMD} -y -qq install $COMPONENT
  50. fi
  51. }
  52. V2RAY_RUNNING=0
  53. if pgrep "v2ray" > /dev/null ; then
  54. V2RAY_RUNNING=1
  55. fi
  56. VER="$(curl -s https://api.github.com/repos/v2ray/v2ray-core/releases/latest | grep 'tag_name' | cut -d\" -f4)"
  57. ARCH=$(uname -m)
  58. VDIS="64"
  59. if [[ "$ARCH" == "i686" ]] || [[ "$ARCH" == "i386" ]]; then
  60. VDIS="32"
  61. elif [[ "$ARCH" == *"armv7"* ]]; then
  62. VDIS="arm"
  63. elif [[ "$ARCH" == *"armv8"* ]]; then
  64. VDIS="arm64"
  65. fi
  66. DOWNLOAD_LINK="https://github.com/v2ray/v2ray-core/releases/download/${VER}/v2ray-linux-${VDIS}.zip"
  67. rm -rf /tmp/v2ray
  68. mkdir -p /tmp/v2ray
  69. install_component "curl"
  70. install_component "unzip"
  71. if [ -n "${PROXY}" ]; then
  72. echo "Downloading ${DOWNLOAD_LINK} via proxy ${PROXY}."
  73. curl -x ${PROXY} -L -H "Cache-Control: no-cache" -o "/tmp/v2ray/v2ray.zip" ${DOWNLOAD_LINK}
  74. else
  75. echo "Downloading ${DOWNLOAD_LINK} directly."
  76. curl -L -H "Cache-Control: no-cache" -o "/tmp/v2ray/v2ray.zip" ${DOWNLOAD_LINK}
  77. fi
  78. unzip "/tmp/v2ray/v2ray.zip" -d "/tmp/v2ray/"
  79. # Create folder for V2Ray log.
  80. mkdir -p /var/log/v2ray
  81. # Stop v2ray daemon if necessary.
  82. SYSTEMCTL_CMD=$(command -v systemctl)
  83. SERVICE_CMD=$(command -v service)
  84. if [ ${V2RAY_RUNNING} -eq 1 ]; then
  85. if [ -n "${SYSTEMCTL_CMD}" ]; then
  86. if [ -f "/lib/systemd/system/v2ray.service" ]; then
  87. ${SYSTEMCTL_CMD} stop v2ray
  88. fi
  89. elif [ -n "${SERVICE_CMD}" ]; then
  90. if [ -f "/etc/init.d/v2ray" ]; then
  91. ${SERVICE_CMD} v2ray stop
  92. fi
  93. fi
  94. fi
  95. # Install V2Ray binary to /usr/bin/v2ray
  96. mkdir -p /usr/bin/v2ray
  97. cp "/tmp/v2ray/v2ray-${VER}-linux-${VDIS}/v2ray" "/usr/bin/v2ray/v2ray"
  98. chmod +x "/usr/bin/v2ray/v2ray"
  99. # Install V2Ray server config to /etc/v2ray
  100. mkdir -p /etc/v2ray
  101. if [ ! -f "/etc/v2ray/config.json" ]; then
  102. cp "/tmp/v2ray/v2ray-${VER}-linux-${VDIS}/vpoint_vmess_freedom.json" "/etc/v2ray/config.json"
  103. let PORT=$RANDOM+10000
  104. sed -i "s/38291/${PORT}/g" "/etc/v2ray/config.json"
  105. UUID=$(cat /proc/sys/kernel/random/uuid)
  106. sed -i "s/8833948b-5861-4a0f-a1d6-83c5606881ff/${UUID}/g" "/etc/v2ray/config.json"
  107. echo "PORT:${PORT}"
  108. echo "UUID:${UUID}"
  109. fi
  110. if [ -n "${SYSTEMCTL_CMD}" ]; then
  111. if [ ! -f "/lib/systemd/system/v2ray.service" ]; then
  112. cp "/tmp/v2ray/v2ray-${VER}-linux-${VDIS}/systemd/v2ray.service" "/lib/systemd/system/"
  113. systemctl enable v2ray
  114. else
  115. if [ ${V2RAY_RUNNING} -eq 1 ]; then
  116. ${SYSTEMCTL_CMD} start v2ray
  117. fi
  118. fi
  119. elif [ -n "${SERVICE_CMD}" ]; then # Configure SysV if necessary.
  120. if [ ! -f "/etc/init.d/v2ray" ]; then
  121. install_component "daemon"
  122. cp "/tmp/v2ray/v2ray-${VER}-linux-${VDIS}/systemv/v2ray" "/etc/init.d/v2ray"
  123. chmod +x "/etc/init.d/v2ray"
  124. update-rc.d v2ray defaults
  125. else
  126. if [ ${V2RAY_RUNNING} -eq 1 ]; then
  127. ${SERVICE_CMD} v2ray start
  128. fi
  129. fi
  130. fi