install-release.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. CHECK=""
  19. FORCE=""
  20. HELP=""
  21. #######color code########
  22. RED="31m" # Error message
  23. GREEN="32m" # Success message
  24. YELLOW="33m" # Warning message
  25. BLUE="36m" # Info message
  26. #########################
  27. while [[ $# > 0 ]];do
  28. key="$1"
  29. case $key in
  30. -p|--proxy)
  31. PROXY="-x ${2}"
  32. shift # past argument
  33. ;;
  34. -h|--help)
  35. HELP="1"
  36. ;;
  37. -f|--force)
  38. FORCE="1"
  39. ;;
  40. -c|--check)
  41. CHECK="1"
  42. ;;
  43. --remove)
  44. REMOVE="1"
  45. ;;
  46. --version)
  47. VERSION="$2"
  48. shift
  49. ;;
  50. -l|--local)
  51. LOCAL="$2"
  52. LOCAL_INSTALL="1"
  53. shift
  54. ;;
  55. *)
  56. # unknown option
  57. ;;
  58. esac
  59. shift # past argument or value
  60. done
  61. ###############################
  62. colorEcho(){
  63. COLOR=$1
  64. echo -e "\033[${COLOR}${@:2}\033[0m"
  65. }
  66. sysArch(){
  67. ARCH=$(uname -m)
  68. if [[ "$ARCH" == "i686" ]] || [[ "$ARCH" == "i386" ]]; then
  69. VDIS="32"
  70. elif [[ "$ARCH" == *"armv7"* ]] || [[ "$ARCH" == "armv6l" ]]; then
  71. VDIS="arm"
  72. elif [[ "$ARCH" == *"armv8"* ]] || [[ "$ARCH" == "aarch64" ]]; then
  73. VDIS="arm64"
  74. elif [[ "$ARCH" == *"mips64le"* ]]; then
  75. VDIS="mips64le"
  76. elif [[ "$ARCH" == *"mips64"* ]]; then
  77. VDIS="mips64"
  78. elif [[ "$ARCH" == *"mipsle"* ]]; then
  79. VDIS="mipsle"
  80. elif [[ "$ARCH" == *"mips"* ]]; then
  81. VDIS="mips"
  82. elif [[ "$ARCH" == *"s390x"* ]]; then
  83. VDIS="s390x"
  84. fi
  85. return 0
  86. }
  87. downloadV2Ray(){
  88. rm -rf /tmp/v2ray
  89. mkdir -p /tmp/v2ray
  90. colorEcho ${BLUE} "Downloading V2Ray."
  91. DOWNLOAD_LINK="https://github.com/v2ray/v2ray-core/releases/download/${NEW_VER}/v2ray-linux-${VDIS}.zip"
  92. curl ${PROXY} -L -H "Cache-Control: no-cache" -o ${ZIPFILE} ${DOWNLOAD_LINK}
  93. if [ $? != 0 ];then
  94. colorEcho ${RED} "Failed to download! Please check your network or try again."
  95. return 3
  96. fi
  97. return 0
  98. }
  99. installSoftware(){
  100. COMPONENT=$1
  101. if [[ -n `command -v $COMPONENT` ]]; then
  102. return 0
  103. fi
  104. getPMT
  105. if [[ $? -eq 1 ]]; then
  106. colorEcho ${RED} "The system package manager tool isn't APT or YUM, please install ${COMPONENT} manually."
  107. return 1
  108. fi
  109. if [[ $SOFTWARE_UPDATED -eq 0 ]]; then
  110. colorEcho ${BLUE} "Updating software repo"
  111. $CMD_UPDATE
  112. SOFTWARE_UPDATED=1
  113. fi
  114. colorEcho ${BLUE} "Installing ${COMPONENT}"
  115. $CMD_INSTALL $COMPONENT
  116. if [[ $? -ne 0 ]]; then
  117. colorEcho ${RED} "Install ${COMPONENT} fail, please install it manually."
  118. return 1
  119. fi
  120. return 0
  121. }
  122. # return 1: not apt or yum
  123. getPMT(){
  124. if [[ -n `command -v apt-get` ]];then
  125. CMD_INSTALL="apt-get -y -qq install"
  126. CMD_UPDATE="apt-get -qq update"
  127. elif [[ -n `command -v yum` ]]; then
  128. CMD_INSTALL="yum -y -q install"
  129. CMD_UPDATE="yum -q makecache"
  130. else
  131. return 1
  132. fi
  133. return 0
  134. }
  135. extract(){
  136. colorEcho ${BLUE}"Extracting V2Ray package to /tmp/v2ray."
  137. mkdir -p /tmp/v2ray
  138. unzip $1 -d "/tmp/v2ray/"
  139. if [[ $? -ne 0 ]]; then
  140. colorEcho ${RED} "Extracting V2Ray failed!"
  141. return 2
  142. fi
  143. return 0
  144. }
  145. # 1: new V2Ray. 0: no. 2: not installed. 3: check failed. 4: don't check.
  146. getVersion(){
  147. if [[ -n "$VERSION" ]]; then
  148. NEW_VER="$VERSION"
  149. return 4
  150. else
  151. VER=`/usr/bin/v2ray/v2ray -version 2>/dev/null`
  152. RETVAL="$?"
  153. CUR_VER=`echo $VER | head -n 1 | cut -d " " -f2`
  154. TAG_URL="https://api.github.com/repos/v2ray/v2ray-core/releases/latest"
  155. NEW_VER=`curl ${PROXY} -s ${TAG_URL} --connect-timeout 10| grep 'tag_name' | cut -d\" -f4`
  156. if [[ $? -ne 0 ]] || [[ $NEW_VER == "" ]]; then
  157. colorEcho ${RED} "Network error! Please check your network or try again."
  158. return 3
  159. elif [[ $RETVAL -ne 0 ]];then
  160. return 2
  161. elif [[ "$NEW_VER" != "$CUR_VER" ]];then
  162. return 1
  163. fi
  164. return 0
  165. fi
  166. }
  167. stopV2ray(){
  168. SYSTEMCTL_CMD=$(command -v systemctl)
  169. SERVICE_CMD=$(command -v service)
  170. colorEcho ${BLUE} "Shutting down V2Ray service."
  171. if [[ -n "${SYSTEMCTL_CMD}" ]] || [[ -f "/lib/systemd/system/v2ray.service" ]] || [[ -f "/etc/systemd/system/v2ray.service" ]]; then
  172. ${SYSTEMCTL_CMD} stop v2ray
  173. elif [[ -n "${SERVICE_CMD}" ]] || [[ -f "/etc/init.d/v2ray" ]]; then
  174. ${SERVICE_CMD} v2ray stop
  175. fi
  176. if [[ $? -ne 0 ]]; then
  177. colorEcho ${YELLOW} "Failed to shutdown V2Ray service."
  178. return 2
  179. fi
  180. return 0
  181. }
  182. startV2ray(){
  183. SYSTEMCTL_CMD=$(command -v systemctl)
  184. SERVICE_CMD=$(command -v service)
  185. if [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/lib/systemd/system/v2ray.service" ]; then
  186. ${SYSTEMCTL_CMD} start v2ray
  187. elif [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/etc/systemd/system/v2ray.service" ]; then
  188. ${SYSTEMCTL_CMD} start v2ray
  189. elif [ -n "${SERVICE_CMD}" ] && [ -f "/etc/init.d/v2ray" ]; then
  190. ${SERVICE_CMD} v2ray start
  191. fi
  192. if [[ $? -ne 0 ]]; then
  193. colorEcho ${YELLOW} "Failed to start V2Ray service."
  194. return 2
  195. fi
  196. return 0
  197. }
  198. copyFile() {
  199. NAME=$1
  200. ERROR=`cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/${NAME}" "/usr/bin/v2ray/${NAME}" 2>&1`
  201. if [[ $? -ne 0 ]]; then
  202. colorEcho ${YELLOW} "${ERROR}"
  203. return 1
  204. fi
  205. return 0
  206. }
  207. makeExecutable() {
  208. chmod +x "/usr/bin/v2ray/$1"
  209. }
  210. installV2Ray(){
  211. # Install V2Ray binary to /usr/bin/v2ray
  212. mkdir -p /usr/bin/v2ray
  213. copyFile v2ray || return $?
  214. makeExecutable v2ray
  215. copyFile v2ctl
  216. makeExecutable v2ctl
  217. copyFile geoip.dat
  218. copyFile geosite.dat
  219. # Install V2Ray server config to /etc/v2ray
  220. if [[ ! -f "/etc/v2ray/config.json" ]]; then
  221. mkdir -p /etc/v2ray
  222. mkdir -p /var/log/v2ray
  223. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/vpoint_vmess_freedom.json" "/etc/v2ray/config.json"
  224. if [[ $? -ne 0 ]]; then
  225. colorEcho ${YELLOW} "Create V2Ray configuration file error, pleases create it manually."
  226. return 1
  227. fi
  228. let PORT=$RANDOM+10000
  229. UUID=$(cat /proc/sys/kernel/random/uuid)
  230. sed -i "s/10086/${PORT}/g" "/etc/v2ray/config.json"
  231. sed -i "s/23ad6b10-8d1a-40f7-8ad0-e3e35cd38297/${UUID}/g" "/etc/v2ray/config.json"
  232. colorEcho ${BLUE} "PORT:${PORT}"
  233. colorEcho ${BLUE} "UUID:${UUID}"
  234. fi
  235. return 0
  236. }
  237. installInitScript(){
  238. SYSTEMCTL_CMD=$(command -v systemctl)
  239. SERVICE_CMD=$(command -v service)
  240. if [[ -n "${SYSTEMCTL_CMD}" ]];then
  241. if [[ ! -f "/etc/systemd/system/v2ray.service" ]]; then
  242. if [[ ! -f "/lib/systemd/system/v2ray.service" ]]; then
  243. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemd/v2ray.service" "/etc/systemd/system/"
  244. systemctl enable v2ray.service
  245. fi
  246. fi
  247. return
  248. elif [[ -n "${SERVICE_CMD}" ]] && [[ ! -f "/etc/init.d/v2ray" ]]; then
  249. installSoftware "daemon" || return $?
  250. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemv/v2ray" "/etc/init.d/v2ray"
  251. chmod +x "/etc/init.d/v2ray"
  252. update-rc.d v2ray defaults
  253. fi
  254. return
  255. }
  256. Help(){
  257. echo "./install-release.sh [-h] [-c] [-p proxy] [-f] [--version vx.y.z] [-l file]"
  258. echo " -h, --help Show help"
  259. 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"
  260. echo " -f, --force Force install"
  261. echo " --version Install a particular version, use --version v3.15"
  262. echo " -l, --local Install from a local file"
  263. echo " --remove Remove installed V2Ray"
  264. echo " -c, --check Check for update"
  265. return 0
  266. }
  267. remove(){
  268. SYSTEMCTL_CMD=$(command -v systemctl)
  269. SERVICE_CMD=$(command -v service)
  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 "Install 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