install-release.sh 13 KB

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