install-release.sh 12 KB

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