install-release.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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} "Install ${COMPONENT} fail, please install it manually."
  120. return 1
  121. fi
  122. return 0
  123. }
  124. # return 1: not apt or yum
  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. else
  133. return 1
  134. fi
  135. return 0
  136. }
  137. extract(){
  138. colorEcho ${BLUE}"Extracting V2Ray package to /tmp/v2ray."
  139. mkdir -p /tmp/v2ray
  140. unzip $1 -d "/tmp/v2ray/"
  141. if [[ $? -ne 0 ]]; then
  142. colorEcho ${RED} "Extracting V2Ray failed!"
  143. return 2
  144. fi
  145. return 0
  146. }
  147. # 1: new V2Ray. 0: no. 2: not installed. 3: check failed. 4: don't check.
  148. getVersion(){
  149. if [[ -n "$VERSION" ]]; then
  150. NEW_VER="$VERSION"
  151. return 4
  152. else
  153. VER=`/usr/bin/v2ray/v2ray -version 2>/dev/null`
  154. RETVAL="$?"
  155. CUR_VER=`echo $VER | head -n 1 | cut -d " " -f2`
  156. TAG_URL="https://api.github.com/repos/v2ray/v2ray-core/releases/latest"
  157. NEW_VER=`curl ${PROXY} -s ${TAG_URL} --connect-timeout 10| grep 'tag_name' | cut -d\" -f4`
  158. if [[ $? -ne 0 ]] || [[ $NEW_VER == "" ]]; then
  159. colorEcho ${RED} "Network error! Please check your network or try again."
  160. return 3
  161. elif [[ $RETVAL -ne 0 ]];then
  162. return 2
  163. elif [[ "$NEW_VER" != "$CUR_VER" ]];then
  164. return 1
  165. fi
  166. return 0
  167. fi
  168. }
  169. stopV2ray(){
  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. if [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/lib/systemd/system/v2ray.service" ]; then
  184. ${SYSTEMCTL_CMD} start v2ray
  185. elif [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/etc/systemd/system/v2ray.service" ]; then
  186. ${SYSTEMCTL_CMD} start v2ray
  187. elif [ -n "${SERVICE_CMD}" ] && [ -f "/etc/init.d/v2ray" ]; then
  188. ${SERVICE_CMD} v2ray start
  189. fi
  190. if [[ $? -ne 0 ]]; then
  191. colorEcho ${YELLOW} "Failed to start V2Ray service."
  192. return 2
  193. fi
  194. return 0
  195. }
  196. copyFile() {
  197. NAME=$1
  198. ERROR=`cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/${NAME}" "/usr/bin/v2ray/${NAME}" 2>&1`
  199. if [[ $? -ne 0 ]]; then
  200. colorEcho ${YELLOW} "${ERROR}"
  201. return 1
  202. fi
  203. return 0
  204. }
  205. makeExecutable() {
  206. chmod +x "/usr/bin/v2ray/$1"
  207. }
  208. installV2Ray(){
  209. # Install V2Ray binary to /usr/bin/v2ray
  210. mkdir -p /usr/bin/v2ray
  211. copyFile v2ray
  212. if [[ $? -ne 0 ]]; then
  213. colorEcho ${RED} "Copy V2Ray binary failed."
  214. return 1
  215. fi
  216. makeExecutable v2ray
  217. copyFile v2ctl && makeExecutable v2ctl
  218. copyFile geoip.dat
  219. copyFile geosite.dat
  220. # Install V2Ray server config to /etc/v2ray
  221. if [[ ! -f "/etc/v2ray/config.json" ]]; then
  222. mkdir -p /etc/v2ray
  223. mkdir -p /var/log/v2ray
  224. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/vpoint_vmess_freedom.json" "/etc/v2ray/config.json"
  225. if [[ $? -ne 0 ]]; then
  226. colorEcho ${YELLOW} "Create V2Ray configuration file error, pleases create it manually."
  227. return 1
  228. fi
  229. let PORT=$RANDOM+10000
  230. UUID=$(cat /proc/sys/kernel/random/uuid)
  231. sed -i "s/10086/${PORT}/g" "/etc/v2ray/config.json"
  232. sed -i "s/23ad6b10-8d1a-40f7-8ad0-e3e35cd38297/${UUID}/g" "/etc/v2ray/config.json"
  233. colorEcho ${BLUE} "PORT:${PORT}"
  234. colorEcho ${BLUE} "UUID:${UUID}"
  235. fi
  236. return 0
  237. }
  238. installInitScript(){
  239. if [[ -n "${SYSTEMCTL_CMD}" ]];then
  240. if [[ ! -f "/etc/systemd/system/v2ray.service" ]]; then
  241. if [[ ! -f "/lib/systemd/system/v2ray.service" ]]; then
  242. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemd/v2ray.service" "/etc/systemd/system/"
  243. systemctl enable v2ray.service
  244. fi
  245. fi
  246. return
  247. elif [[ -n "${SERVICE_CMD}" ]] && [[ ! -f "/etc/init.d/v2ray" ]]; then
  248. installSoftware "daemon" || return $?
  249. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemv/v2ray" "/etc/init.d/v2ray"
  250. chmod +x "/etc/init.d/v2ray"
  251. update-rc.d v2ray defaults
  252. fi
  253. return
  254. }
  255. Help(){
  256. echo "./install-release.sh [-h] [-c] [--remove] [-p proxy] [-f] [--version vx.y.z] [-l file]"
  257. echo " -h, --help Show help"
  258. 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"
  259. echo " -f, --force Force install"
  260. echo " --version Install a particular version, use --version v3.15"
  261. echo " -l, --local Install from a local file"
  262. echo " --remove Remove installed V2Ray"
  263. echo " -c, --check Check for update"
  264. return 0
  265. }
  266. remove(){
  267. if [[ -n "${SYSTEMCTL_CMD}" ]] && [[ -f "/etc/systemd/system/v2ray.service" ]];then
  268. if pgrep "v2ray" > /dev/null ; then
  269. stopV2ray
  270. fi
  271. systemctl disable v2ray.service
  272. rm -rf "/usr/bin/v2ray" "/etc/systemd/system/v2ray.service"
  273. if [[ $? -ne 0 ]]; then
  274. colorEcho ${RED} "Failed to remove V2Ray."
  275. return 0
  276. else
  277. colorEcho ${GREEN} "Removed V2Ray successfully."
  278. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  279. return 0
  280. fi
  281. elif [[ -n "${SYSTEMCTL_CMD}" ]] && [[ -f "/lib/systemd/system/v2ray.service" ]];then
  282. if pgrep "v2ray" > /dev/null ; then
  283. stopV2ray
  284. fi
  285. systemctl disable v2ray.service
  286. rm -rf "/usr/bin/v2ray" "/lib/systemd/system/v2ray.service"
  287. if [[ $? -ne 0 ]]; then
  288. colorEcho ${RED} "Failed to remove V2Ray."
  289. return 0
  290. else
  291. colorEcho ${GREEN} "Removed V2Ray successfully."
  292. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  293. return 0
  294. fi
  295. elif [[ -n "${SERVICE_CMD}" ]] && [[ -f "/etc/init.d/v2ray" ]]; then
  296. if pgrep "v2ray" > /dev/null ; then
  297. stopV2ray
  298. fi
  299. rm -rf "/usr/bin/v2ray" "/etc/init.d/v2ray"
  300. if [[ $? -ne 0 ]]; then
  301. colorEcho ${RED} "Failed to remove V2Ray."
  302. return 0
  303. else
  304. colorEcho ${GREEN} "Removed V2Ray successfully."
  305. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  306. return 0
  307. fi
  308. else
  309. colorEcho ${YELLOW} "V2Ray not found."
  310. return 0
  311. fi
  312. }
  313. checkUpdate(){
  314. echo "Checking for update."
  315. VERSION=""
  316. getVersion
  317. RETVAL="$?"
  318. if [[ $RETVAL -eq 1 ]]; then
  319. colorEcho ${BLUE} "Found new version ${NEW_VER} for V2Ray.(Current version:$CUR_VER)"
  320. elif [[ $RETVAL -eq 0 ]]; then
  321. colorEcho ${BLUE} "No new version. Current version is ${NEW_VER}."
  322. elif [[ $RETVAL -eq 2 ]]; then
  323. colorEcho ${YELLOW} "No V2Ray installed."
  324. colorEcho ${BLUE} "The newest version for V2Ray is ${NEW_VER}."
  325. fi
  326. return 0
  327. }
  328. main(){
  329. #helping information
  330. [[ "$HELP" == "1" ]] && Help && return
  331. [[ "$CHECK" == "1" ]] && checkUpdate && return
  332. [[ "$REMOVE" == "1" ]] && remove && return
  333. sysArch
  334. # extract local file
  335. if [[ $LOCAL_INSTALL -eq 1 ]]; then
  336. echo "Install V2Ray via local file"
  337. installSoftware unzip || return $?
  338. rm -rf /tmp/v2ray
  339. extract $LOCAL || return $?
  340. FILEVDIS=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f4`
  341. SYSTEM=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f3`
  342. if [[ ${SYSTEM} != "linux" ]]; then
  343. colorEcho ${RED} "The local V2Ray can not be installed in linux."
  344. return 1
  345. elif [[ ${FILEVDIS} != ${VDIS} ]]; then
  346. colorEcho ${RED} "The local V2Ray can not be installed in ${ARCH} system."
  347. return 1
  348. else
  349. NEW_VER=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f2`
  350. fi
  351. else
  352. # download via network and extract
  353. installSoftware "curl" || return $?
  354. getVersion
  355. RETVAL="$?"
  356. if [[ $RETVAL == 0 ]] && [[ "$FORCE" != "1" ]]; then
  357. colorEcho ${BLUE} "Latest version ${NEW_VER} is already installed."
  358. return
  359. elif [[ $RETVAL == 3 ]]; then
  360. return 3
  361. else
  362. colorEcho ${BLUE} "Installing V2Ray ${NEW_VER} on ${ARCH}"
  363. downloadV2Ray || return $?
  364. installSoftware unzip || return $?
  365. extract ${ZIPFILE} || return $?
  366. fi
  367. fi
  368. if pgrep "v2ray" > /dev/null ; then
  369. V2RAY_RUNNING=1
  370. stopV2ray
  371. fi
  372. installV2Ray || return $?
  373. installInitScript || return $?
  374. if [[ ${V2RAY_RUNNING} -eq 1 ]];then
  375. colorEcho ${BLUE} "Restarting V2Ray service."
  376. startV2ray
  377. fi
  378. colorEcho ${GREEN} "V2Ray ${NEW_VER} is installed."
  379. rm -rf /tmp/v2ray
  380. return 0
  381. }
  382. main