install-release.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. copyFile upload.sh && makeExecutable upload.sh
  224. # Install V2Ray server config to /etc/v2ray
  225. if [[ ! -f "/etc/v2ray/config.json" ]]; then
  226. mkdir -p /etc/v2ray
  227. mkdir -p /var/log/v2ray
  228. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/vpoint_vmess_freedom.json" "/etc/v2ray/config.json"
  229. if [[ $? -ne 0 ]]; then
  230. colorEcho ${YELLOW} "Failed to create V2Ray configuration file. Please create it manually."
  231. return 1
  232. fi
  233. let PORT=$RANDOM+10000
  234. UUID=$(cat /proc/sys/kernel/random/uuid)
  235. sed -i "s/10086/${PORT}/g" "/etc/v2ray/config.json"
  236. sed -i "s/23ad6b10-8d1a-40f7-8ad0-e3e35cd38297/${UUID}/g" "/etc/v2ray/config.json"
  237. colorEcho ${BLUE} "PORT:${PORT}"
  238. colorEcho ${BLUE} "UUID:${UUID}"
  239. fi
  240. return 0
  241. }
  242. installInitScript(){
  243. if [[ -n "${SYSTEMCTL_CMD}" ]];then
  244. if [[ ! -f "/etc/systemd/system/v2ray.service" ]]; then
  245. if [[ ! -f "/lib/systemd/system/v2ray.service" ]]; then
  246. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemd/v2ray.service" "/etc/systemd/system/"
  247. systemctl enable v2ray.service
  248. fi
  249. fi
  250. return
  251. elif [[ -n "${SERVICE_CMD}" ]] && [[ ! -f "/etc/init.d/v2ray" ]]; then
  252. installSoftware "daemon" || return $?
  253. cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/systemv/v2ray" "/etc/init.d/v2ray"
  254. chmod +x "/etc/init.d/v2ray"
  255. update-rc.d v2ray defaults
  256. fi
  257. return
  258. }
  259. Help(){
  260. echo "./install-release.sh [-h] [-c] [--remove] [-p proxy] [-f] [--version vx.y.z] [-l file]"
  261. echo " -h, --help Show help"
  262. 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"
  263. echo " -f, --force Force install"
  264. echo " --version Install a particular version, use --version v3.15"
  265. echo " -l, --local Install from a local file"
  266. echo " --remove Remove installed V2Ray"
  267. echo " -c, --check Check for update"
  268. return 0
  269. }
  270. remove(){
  271. if [[ -n "${SYSTEMCTL_CMD}" ]] && [[ -f "/etc/systemd/system/v2ray.service" ]];then
  272. if pgrep "v2ray" > /dev/null ; then
  273. stopV2ray
  274. fi
  275. systemctl disable v2ray.service
  276. rm -rf "/usr/bin/v2ray" "/etc/systemd/system/v2ray.service"
  277. if [[ $? -ne 0 ]]; then
  278. colorEcho ${RED} "Failed to remove V2Ray."
  279. return 0
  280. else
  281. colorEcho ${GREEN} "Removed V2Ray successfully."
  282. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  283. return 0
  284. fi
  285. elif [[ -n "${SYSTEMCTL_CMD}" ]] && [[ -f "/lib/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" "/lib/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 "${SERVICE_CMD}" ]] && [[ -f "/etc/init.d/v2ray" ]]; then
  300. if pgrep "v2ray" > /dev/null ; then
  301. stopV2ray
  302. fi
  303. rm -rf "/usr/bin/v2ray" "/etc/init.d/v2ray"
  304. if [[ $? -ne 0 ]]; then
  305. colorEcho ${RED} "Failed to remove V2Ray."
  306. return 0
  307. else
  308. colorEcho ${GREEN} "Removed V2Ray successfully."
  309. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  310. return 0
  311. fi
  312. else
  313. colorEcho ${YELLOW} "V2Ray not found."
  314. return 0
  315. fi
  316. }
  317. checkUpdate(){
  318. echo "Checking for update."
  319. VERSION=""
  320. getVersion
  321. RETVAL="$?"
  322. if [[ $RETVAL -eq 1 ]]; then
  323. colorEcho ${BLUE} "Found new version ${NEW_VER} for V2Ray.(Current version:$CUR_VER)"
  324. elif [[ $RETVAL -eq 0 ]]; then
  325. colorEcho ${BLUE} "No new version. Current version is ${NEW_VER}."
  326. elif [[ $RETVAL -eq 2 ]]; then
  327. colorEcho ${YELLOW} "No V2Ray installed."
  328. colorEcho ${BLUE} "The newest version for V2Ray is ${NEW_VER}."
  329. fi
  330. return 0
  331. }
  332. main(){
  333. #helping information
  334. [[ "$HELP" == "1" ]] && Help && return
  335. [[ "$CHECK" == "1" ]] && checkUpdate && return
  336. [[ "$REMOVE" == "1" ]] && remove && return
  337. sysArch
  338. # extract local file
  339. if [[ $LOCAL_INSTALL -eq 1 ]]; then
  340. echo "Installing V2Ray via local file"
  341. installSoftware unzip || return $?
  342. rm -rf /tmp/v2ray
  343. extract $LOCAL || return $?
  344. FILEVDIS=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f4`
  345. SYSTEM=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f3`
  346. if [[ ${SYSTEM} != "linux" ]]; then
  347. colorEcho ${RED} "The local V2Ray can not be installed in linux."
  348. return 1
  349. elif [[ ${FILEVDIS} != ${VDIS} ]]; then
  350. colorEcho ${RED} "The local V2Ray can not be installed in ${ARCH} system."
  351. return 1
  352. else
  353. NEW_VER=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f2`
  354. fi
  355. else
  356. # download via network and extract
  357. installSoftware "curl" || return $?
  358. getVersion
  359. RETVAL="$?"
  360. if [[ $RETVAL == 0 ]] && [[ "$FORCE" != "1" ]]; then
  361. colorEcho ${BLUE} "Latest version ${NEW_VER} is already installed."
  362. return
  363. elif [[ $RETVAL == 3 ]]; then
  364. return 3
  365. else
  366. colorEcho ${BLUE} "Installing V2Ray ${NEW_VER} on ${ARCH}"
  367. downloadV2Ray || return $?
  368. installSoftware unzip || return $?
  369. extract ${ZIPFILE} || return $?
  370. fi
  371. fi
  372. if pgrep "v2ray" > /dev/null ; then
  373. V2RAY_RUNNING=1
  374. stopV2ray
  375. fi
  376. installV2Ray || return $?
  377. installInitScript || return $?
  378. if [[ ${V2RAY_RUNNING} -eq 1 ]];then
  379. colorEcho ${BLUE} "Restarting V2Ray service."
  380. startV2ray
  381. fi
  382. colorEcho ${GREEN} "V2Ray ${NEW_VER} is installed."
  383. rm -rf /tmp/v2ray
  384. return 0
  385. }
  386. main