install-release.sh 13 KB

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