install-release.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 [[ -f "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemd/v2ray.service" ]]; then
  243. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemd/v2ray.service" "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemd_v2ray.service"
  244. fi
  245. if [[ -f "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemv/v2ray" ]]; then
  246. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemv/v2ray" "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemv_v2ray"
  247. fi
  248. if [[ -n "${SYSTEMCTL_CMD}" ]];then
  249. if [[ ! -f "/etc/systemd/system/v2ray.service" ]]; then
  250. if [[ ! -f "/lib/systemd/system/v2ray.service" ]]; then
  251. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemd_v2ray.service" "/etc/systemd/system/"
  252. systemctl enable v2ray.service
  253. fi
  254. fi
  255. return
  256. elif [[ -n "${SERVICE_CMD}" ]] && [[ ! -f "/etc/init.d/v2ray" ]]; then
  257. installSoftware "daemon" || return $?
  258. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemv_v2ray" "/etc/init.d/v2ray"
  259. chmod +x "/etc/init.d/v2ray"
  260. update-rc.d v2ray defaults
  261. fi
  262. return
  263. }
  264. Help(){
  265. echo "./install-release.sh [-h] [-c] [--remove] [-p proxy] [-f] [--version vx.y.z] [-l file]"
  266. echo " -h, --help Show help"
  267. 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"
  268. echo " -f, --force Force install"
  269. echo " --version Install a particular version, use --version v3.15"
  270. echo " -l, --local Install from a local file"
  271. echo " --remove Remove installed V2Ray"
  272. echo " -c, --check Check for update"
  273. return 0
  274. }
  275. remove(){
  276. if [[ -n "${SYSTEMCTL_CMD}" ]] && [[ -f "/etc/systemd/system/v2ray.service" ]];then
  277. if pgrep "v2ray" > /dev/null ; then
  278. stopV2ray
  279. fi
  280. systemctl disable v2ray.service
  281. rm -rf "/usr/bin/v2ray" "/etc/systemd/system/v2ray.service"
  282. if [[ $? -ne 0 ]]; then
  283. colorEcho ${RED} "Failed to remove V2Ray."
  284. return 0
  285. else
  286. colorEcho ${GREEN} "Removed V2Ray successfully."
  287. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  288. return 0
  289. fi
  290. elif [[ -n "${SYSTEMCTL_CMD}" ]] && [[ -f "/lib/systemd/system/v2ray.service" ]];then
  291. if pgrep "v2ray" > /dev/null ; then
  292. stopV2ray
  293. fi
  294. systemctl disable v2ray.service
  295. rm -rf "/usr/bin/v2ray" "/lib/systemd/system/v2ray.service"
  296. if [[ $? -ne 0 ]]; then
  297. colorEcho ${RED} "Failed to remove V2Ray."
  298. return 0
  299. else
  300. colorEcho ${GREEN} "Removed V2Ray successfully."
  301. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  302. return 0
  303. fi
  304. elif [[ -n "${SERVICE_CMD}" ]] && [[ -f "/etc/init.d/v2ray" ]]; then
  305. if pgrep "v2ray" > /dev/null ; then
  306. stopV2ray
  307. fi
  308. rm -rf "/usr/bin/v2ray" "/etc/init.d/v2ray"
  309. if [[ $? -ne 0 ]]; then
  310. colorEcho ${RED} "Failed to remove V2Ray."
  311. return 0
  312. else
  313. colorEcho ${GREEN} "Removed V2Ray successfully."
  314. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  315. return 0
  316. fi
  317. else
  318. colorEcho ${YELLOW} "V2Ray not found."
  319. return 0
  320. fi
  321. }
  322. checkUpdate(){
  323. echo "Checking for update."
  324. VERSION=""
  325. getVersion
  326. RETVAL="$?"
  327. if [[ $RETVAL -eq 1 ]]; then
  328. colorEcho ${BLUE} "Found new version ${NEW_VER} for V2Ray.(Current version:$CUR_VER)"
  329. elif [[ $RETVAL -eq 0 ]]; then
  330. colorEcho ${BLUE} "No new version. Current version is ${NEW_VER}."
  331. elif [[ $RETVAL -eq 2 ]]; then
  332. colorEcho ${YELLOW} "No V2Ray installed."
  333. colorEcho ${BLUE} "The newest version for V2Ray is ${NEW_VER}."
  334. fi
  335. return 0
  336. }
  337. main(){
  338. #helping information
  339. [[ "$HELP" == "1" ]] && Help && return
  340. [[ "$CHECK" == "1" ]] && checkUpdate && return
  341. [[ "$REMOVE" == "1" ]] && remove && return
  342. sysArch
  343. # extract local file
  344. if [[ $LOCAL_INSTALL -eq 1 ]]; then
  345. echo "Installing V2Ray via local file"
  346. installSoftware unzip || return $?
  347. rm -rf /tmp/v2ray
  348. extract $LOCAL || return $?
  349. FILEVDIS=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f4`
  350. SYSTEM=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f3`
  351. if [[ ${SYSTEM} != "linux" ]]; then
  352. colorEcho ${RED} "The local V2Ray can not be installed in linux."
  353. return 1
  354. elif [[ ${FILEVDIS} != ${VDIS} ]]; then
  355. colorEcho ${RED} "The local V2Ray can not be installed in ${ARCH} system."
  356. return 1
  357. else
  358. NEW_VER=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f2`
  359. fi
  360. else
  361. # download via network and extract
  362. installSoftware "curl" || return $?
  363. getVersion
  364. RETVAL="$?"
  365. if [[ $RETVAL == 0 ]] && [[ "$FORCE" != "1" ]]; then
  366. colorEcho ${BLUE} "Latest version ${NEW_VER} is already installed."
  367. return
  368. elif [[ $RETVAL == 3 ]]; then
  369. return 3
  370. else
  371. colorEcho ${BLUE} "Installing V2Ray ${NEW_VER} on ${ARCH}"
  372. downloadV2Ray || return $?
  373. installSoftware unzip || return $?
  374. extract ${ZIPFILE} || return $?
  375. fi
  376. fi
  377. if pgrep "v2ray" > /dev/null ; then
  378. V2RAY_RUNNING=1
  379. stopV2ray
  380. fi
  381. installV2Ray || return $?
  382. installInitScript || return $?
  383. if [[ ${V2RAY_RUNNING} -eq 1 ]];then
  384. colorEcho ${BLUE} "Restarting V2Ray service."
  385. startV2ray
  386. fi
  387. colorEcho ${GREEN} "V2Ray ${NEW_VER} is installed."
  388. rm -rf /tmp/v2ray
  389. return 0
  390. }
  391. main