install-release.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. # If not specify, default meaning of return value:
  5. # 0: Success
  6. # 1: System error
  7. # 2: Application error
  8. # 3: Network error
  9. CUR_VER=""
  10. NEW_VER=""
  11. ARCH=""
  12. VDIS="64"
  13. ZIPFILE="/tmp/v2ray/v2ray.zip"
  14. V2RAY_RUNNING=0
  15. CMD_INSTALL=""
  16. CMD_UPDATE=""
  17. SOFTWARE_UPDATED=0
  18. SYSTEMCTL_CMD=$(command -v systemctl 2>/dev/null)
  19. SERVICE_CMD=$(command -v service 2>/dev/null)
  20. CHECK=""
  21. FORCE=""
  22. HELP=""
  23. #######color code########
  24. RED="31m" # Error message
  25. GREEN="32m" # Success message
  26. YELLOW="33m" # Warning message
  27. BLUE="36m" # Info message
  28. #########################
  29. while [[ $# > 0 ]];do
  30. key="$1"
  31. case $key in
  32. -p|--proxy)
  33. PROXY="-x ${2}"
  34. shift # past argument
  35. ;;
  36. -h|--help)
  37. HELP="1"
  38. ;;
  39. -f|--force)
  40. FORCE="1"
  41. ;;
  42. -c|--check)
  43. CHECK="1"
  44. ;;
  45. --remove)
  46. REMOVE="1"
  47. ;;
  48. --version)
  49. VERSION="$2"
  50. shift
  51. ;;
  52. -l|--local)
  53. LOCAL="$2"
  54. LOCAL_INSTALL="1"
  55. shift
  56. ;;
  57. *)
  58. # unknown option
  59. ;;
  60. esac
  61. shift # past argument or value
  62. done
  63. ###############################
  64. colorEcho(){
  65. COLOR=$1
  66. echo -e "\033[${COLOR}${@:2}\033[0m"
  67. }
  68. sysArch(){
  69. ARCH=$(uname -m)
  70. if [[ "$ARCH" == "i686" ]] || [[ "$ARCH" == "i386" ]]; then
  71. VDIS="32"
  72. elif [[ "$ARCH" == *"armv7"* ]] || [[ "$ARCH" == "armv6l" ]]; then
  73. VDIS="arm"
  74. elif [[ "$ARCH" == *"armv8"* ]] || [[ "$ARCH" == "aarch64" ]]; then
  75. VDIS="arm64"
  76. elif [[ "$ARCH" == *"mips64le"* ]]; then
  77. VDIS="mips64le"
  78. elif [[ "$ARCH" == *"mips64"* ]]; then
  79. VDIS="mips64"
  80. elif [[ "$ARCH" == *"mipsle"* ]]; then
  81. VDIS="mipsle"
  82. elif [[ "$ARCH" == *"mips"* ]]; then
  83. VDIS="mips"
  84. elif [[ "$ARCH" == *"s390x"* ]]; then
  85. VDIS="s390x"
  86. fi
  87. return 0
  88. }
  89. downloadV2Ray(){
  90. rm -rf /tmp/v2ray
  91. mkdir -p /tmp/v2ray
  92. colorEcho ${BLUE} "Downloading V2Ray."
  93. DOWNLOAD_LINK="https://github.com/v2ray/v2ray-core/releases/download/${NEW_VER}/v2ray-linux-${VDIS}.zip"
  94. curl ${PROXY} -L -H "Cache-Control: no-cache" -o ${ZIPFILE} ${DOWNLOAD_LINK}
  95. if [ $? != 0 ];then
  96. colorEcho ${RED} "Failed to download! Please check your network or try again."
  97. return 3
  98. fi
  99. return 0
  100. }
  101. installSoftware(){
  102. COMPONENT=$1
  103. if [[ -n `command -v $COMPONENT` ]]; then
  104. return 0
  105. fi
  106. getPMT
  107. if [[ $? -eq 1 ]]; then
  108. colorEcho ${RED} "The system package manager tool isn't APT or YUM, please install ${COMPONENT} manually."
  109. return 1
  110. fi
  111. if [[ $SOFTWARE_UPDATED -eq 0 ]]; then
  112. colorEcho ${BLUE} "Updating software repo"
  113. $CMD_UPDATE
  114. SOFTWARE_UPDATED=1
  115. fi
  116. colorEcho ${BLUE} "Installing ${COMPONENT}"
  117. $CMD_INSTALL $COMPONENT
  118. if [[ $? -ne 0 ]]; then
  119. colorEcho ${RED} "Failed to install ${COMPONENT}. Please install it manually."
  120. return 1
  121. fi
  122. return 0
  123. }
  124. # return 1: not apt, yum, or zypper
  125. getPMT(){
  126. if [[ -n `command -v apt-get` ]];then
  127. CMD_INSTALL="apt-get -y -qq install"
  128. CMD_UPDATE="apt-get -qq update"
  129. elif [[ -n `command -v yum` ]]; then
  130. CMD_INSTALL="yum -y -q install"
  131. CMD_UPDATE="yum -q makecache"
  132. elif [[ -n `command -v zypper` ]]; then
  133. CMD_INSTALL="zypper -y install"
  134. CMD_UPDATE="zypper ref"
  135. else
  136. return 1
  137. fi
  138. return 0
  139. }
  140. extract(){
  141. colorEcho ${BLUE}"Extracting V2Ray package to /tmp/v2ray."
  142. mkdir -p /tmp/v2ray
  143. unzip $1 -d "/tmp/v2ray/"
  144. if [[ $? -ne 0 ]]; then
  145. colorEcho ${RED} "Failed to extract V2Ray."
  146. return 2
  147. fi
  148. return 0
  149. }
  150. # 1: new V2Ray. 0: no. 2: not installed. 3: check failed. 4: don't check.
  151. getVersion(){
  152. if [[ -n "$VERSION" ]]; then
  153. NEW_VER="$VERSION"
  154. return 4
  155. else
  156. VER=`/usr/bin/v2ray/v2ray -version 2>/dev/null`
  157. RETVAL="$?"
  158. CUR_VER=`echo $VER | head -n 1 | cut -d " " -f2`
  159. TAG_URL="https://api.github.com/repos/v2ray/v2ray-core/releases/latest"
  160. NEW_VER=`curl ${PROXY} -s ${TAG_URL} --connect-timeout 10| grep 'tag_name' | cut -d\" -f4`
  161. if [[ $? -ne 0 ]] || [[ $NEW_VER == "" ]]; then
  162. colorEcho ${RED} "Failed to fetch release information. Please check your network or try again."
  163. return 3
  164. elif [[ $RETVAL -ne 0 ]];then
  165. return 2
  166. elif [[ "$NEW_VER" != "$CUR_VER" ]];then
  167. return 1
  168. fi
  169. return 0
  170. fi
  171. }
  172. stopV2ray(){
  173. colorEcho ${BLUE} "Shutting down V2Ray service."
  174. if [[ -n "${SYSTEMCTL_CMD}" ]] || [[ -f "/lib/systemd/system/v2ray.service" ]] || [[ -f "/etc/systemd/system/v2ray.service" ]]; then
  175. ${SYSTEMCTL_CMD} stop v2ray
  176. elif [[ -n "${SERVICE_CMD}" ]] || [[ -f "/etc/init.d/v2ray" ]]; then
  177. ${SERVICE_CMD} v2ray stop
  178. fi
  179. if [[ $? -ne 0 ]]; then
  180. colorEcho ${YELLOW} "Failed to shutdown V2Ray service."
  181. return 2
  182. fi
  183. return 0
  184. }
  185. startV2ray(){
  186. if [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/lib/systemd/system/v2ray.service" ]; then
  187. ${SYSTEMCTL_CMD} start v2ray
  188. elif [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/etc/systemd/system/v2ray.service" ]; then
  189. ${SYSTEMCTL_CMD} start v2ray
  190. elif [ -n "${SERVICE_CMD}" ] && [ -f "/etc/init.d/v2ray" ]; then
  191. ${SERVICE_CMD} v2ray start
  192. fi
  193. if [[ $? -ne 0 ]]; then
  194. colorEcho ${YELLOW} "Failed to start V2Ray service."
  195. return 2
  196. fi
  197. return 0
  198. }
  199. copyFile() {
  200. NAME=$1
  201. ERROR=`cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/${NAME}" "/usr/bin/v2ray/${NAME}" 2>&1`
  202. if [[ $? -ne 0 ]]; then
  203. colorEcho ${YELLOW} "${ERROR}"
  204. return 1
  205. fi
  206. return 0
  207. }
  208. makeExecutable() {
  209. chmod +x "/usr/bin/v2ray/$1"
  210. }
  211. installV2Ray(){
  212. # Install V2Ray binary to /usr/bin/v2ray
  213. mkdir -p /usr/bin/v2ray
  214. copyFile v2ray
  215. if [[ $? -ne 0 ]]; then
  216. colorEcho ${RED} "Failed to copy V2Ray binary and resources."
  217. return 1
  218. fi
  219. makeExecutable v2ray
  220. copyFile v2ctl && makeExecutable v2ctl
  221. copyFile geoip.dat
  222. copyFile geosite.dat
  223. # Install V2Ray server config to /etc/v2ray
  224. if [[ ! -f "/etc/v2ray/config.json" ]]; then
  225. mkdir -p /etc/v2ray
  226. mkdir -p /var/log/v2ray
  227. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/vpoint_vmess_freedom.json" "/etc/v2ray/config.json"
  228. if [[ $? -ne 0 ]]; then
  229. colorEcho ${YELLOW} "Failed to create V2Ray configuration file. Please create it manually."
  230. return 1
  231. fi
  232. let PORT=$RANDOM+10000
  233. UUID=$(cat /proc/sys/kernel/random/uuid)
  234. sed -i "s/10086/${PORT}/g" "/etc/v2ray/config.json"
  235. sed -i "s/23ad6b10-8d1a-40f7-8ad0-e3e35cd38297/${UUID}/g" "/etc/v2ray/config.json"
  236. colorEcho ${BLUE} "PORT:${PORT}"
  237. colorEcho ${BLUE} "UUID:${UUID}"
  238. fi
  239. return 0
  240. }
  241. installInitScript(){
  242. if [[ -n "${SYSTEMCTL_CMD}" ]];then
  243. if [[ ! -f "/etc/systemd/system/v2ray.service" ]]; then
  244. if [[ ! -f "/lib/systemd/system/v2ray.service" ]]; then
  245. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemd/v2ray.service" "/etc/systemd/system/"
  246. systemctl enable v2ray.service
  247. fi
  248. fi
  249. return
  250. elif [[ -n "${SERVICE_CMD}" ]] && [[ ! -f "/etc/init.d/v2ray" ]]; then
  251. installSoftware "daemon" || return $?
  252. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemv/v2ray" "/etc/init.d/v2ray"
  253. chmod +x "/etc/init.d/v2ray"
  254. update-rc.d v2ray defaults
  255. fi
  256. return
  257. }
  258. Help(){
  259. echo "./install-release.sh [-h] [-c] [--remove] [-p proxy] [-f] [--version vx.y.z] [-l file]"
  260. echo " -h, --help Show help"
  261. echo " -p, --proxy To download through a proxy server, use -p socks5://127.0.0.1:1080 or -p http://127.0.0.1:3128 etc"
  262. echo " -f, --force Force install"
  263. echo " --version Install a particular version, use --version v3.15"
  264. echo " -l, --local Install from a local file"
  265. echo " --remove Remove installed V2Ray"
  266. echo " -c, --check Check for update"
  267. return 0
  268. }
  269. remove(){
  270. if [[ -n "${SYSTEMCTL_CMD}" ]] && [[ -f "/etc/systemd/system/v2ray.service" ]];then
  271. if pgrep "v2ray" > /dev/null ; then
  272. stopV2ray
  273. fi
  274. systemctl disable v2ray.service
  275. rm -rf "/usr/bin/v2ray" "/etc/systemd/system/v2ray.service"
  276. if [[ $? -ne 0 ]]; then
  277. colorEcho ${RED} "Failed to remove V2Ray."
  278. return 0
  279. else
  280. colorEcho ${GREEN} "Removed V2Ray successfully."
  281. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  282. return 0
  283. fi
  284. elif [[ -n "${SYSTEMCTL_CMD}" ]] && [[ -f "/lib/systemd/system/v2ray.service" ]];then
  285. if pgrep "v2ray" > /dev/null ; then
  286. stopV2ray
  287. fi
  288. systemctl disable v2ray.service
  289. rm -rf "/usr/bin/v2ray" "/lib/systemd/system/v2ray.service"
  290. if [[ $? -ne 0 ]]; then
  291. colorEcho ${RED} "Failed to remove V2Ray."
  292. return 0
  293. else
  294. colorEcho ${GREEN} "Removed V2Ray successfully."
  295. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  296. return 0
  297. fi
  298. elif [[ -n "${SERVICE_CMD}" ]] && [[ -f "/etc/init.d/v2ray" ]]; then
  299. if pgrep "v2ray" > /dev/null ; then
  300. stopV2ray
  301. fi
  302. rm -rf "/usr/bin/v2ray" "/etc/init.d/v2ray"
  303. if [[ $? -ne 0 ]]; then
  304. colorEcho ${RED} "Failed to remove V2Ray."
  305. return 0
  306. else
  307. colorEcho ${GREEN} "Removed V2Ray successfully."
  308. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  309. return 0
  310. fi
  311. else
  312. colorEcho ${YELLOW} "V2Ray not found."
  313. return 0
  314. fi
  315. }
  316. checkUpdate(){
  317. echo "Checking for update."
  318. VERSION=""
  319. getVersion
  320. RETVAL="$?"
  321. if [[ $RETVAL -eq 1 ]]; then
  322. colorEcho ${BLUE} "Found new version ${NEW_VER} for V2Ray.(Current version:$CUR_VER)"
  323. elif [[ $RETVAL -eq 0 ]]; then
  324. colorEcho ${BLUE} "No new version. Current version is ${NEW_VER}."
  325. elif [[ $RETVAL -eq 2 ]]; then
  326. colorEcho ${YELLOW} "No V2Ray installed."
  327. colorEcho ${BLUE} "The newest version for V2Ray is ${NEW_VER}."
  328. fi
  329. return 0
  330. }
  331. main(){
  332. #helping information
  333. [[ "$HELP" == "1" ]] && Help && return
  334. [[ "$CHECK" == "1" ]] && checkUpdate && return
  335. [[ "$REMOVE" == "1" ]] && remove && return
  336. sysArch
  337. # extract local file
  338. if [[ $LOCAL_INSTALL -eq 1 ]]; then
  339. echo "Installing V2Ray via local file"
  340. installSoftware unzip || return $?
  341. rm -rf /tmp/v2ray
  342. extract $LOCAL || return $?
  343. FILEVDIS=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f4`
  344. SYSTEM=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f3`
  345. if [[ ${SYSTEM} != "linux" ]]; then
  346. colorEcho ${RED} "The local V2Ray can not be installed in linux."
  347. return 1
  348. elif [[ ${FILEVDIS} != ${VDIS} ]]; then
  349. colorEcho ${RED} "The local V2Ray can not be installed in ${ARCH} system."
  350. return 1
  351. else
  352. NEW_VER=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f2`
  353. fi
  354. else
  355. # download via network and extract
  356. installSoftware "curl" || return $?
  357. getVersion
  358. RETVAL="$?"
  359. if [[ $RETVAL == 0 ]] && [[ "$FORCE" != "1" ]]; then
  360. colorEcho ${BLUE} "Latest version ${NEW_VER} is already installed."
  361. return
  362. elif [[ $RETVAL == 3 ]]; then
  363. return 3
  364. else
  365. colorEcho ${BLUE} "Installing V2Ray ${NEW_VER} on ${ARCH}"
  366. downloadV2Ray || return $?
  367. installSoftware unzip || return $?
  368. extract ${ZIPFILE} || return $?
  369. fi
  370. fi
  371. if pgrep "v2ray" > /dev/null ; then
  372. V2RAY_RUNNING=1
  373. stopV2ray
  374. fi
  375. installV2Ray || return $?
  376. installInitScript || return $?
  377. if [[ ${V2RAY_RUNNING} -eq 1 ]];then
  378. colorEcho ${BLUE} "Restarting V2Ray service."
  379. startV2ray
  380. fi
  381. colorEcho ${GREEN} "V2Ray ${NEW_VER} is installed."
  382. rm -rf /tmp/v2ray
  383. return 0
  384. }
  385. main