install-release.sh 13 KB

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