install-release.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. zipRoot() {
  131. unzip -lqq "$1" | awk -e '
  132. NR == 1 {
  133. prefix = $4;
  134. }
  135. NR != 1 {
  136. prefix_len = length(prefix);
  137. cur_len = length($4);
  138. for (len = prefix_len < cur_len ? prefix_len : cur_len; len >= 1; len -= 1) {
  139. sub_prefix = substr(prefix, 1, len);
  140. sub_cur = substr($4, 1, len);
  141. if (sub_prefix == sub_cur) {
  142. prefix = sub_prefix;
  143. break;
  144. }
  145. }
  146. if (len == 0) {
  147. prefix = "";
  148. nextfile;
  149. }
  150. }
  151. END {
  152. print prefix;
  153. }
  154. '
  155. }
  156. downloadV2Ray(){
  157. rm -rf /tmp/v2ray
  158. mkdir -p /tmp/v2ray
  159. if [[ "${DIST_SRC}" == "jsdelivr" ]]; then
  160. DOWNLOAD_LINK="https://cdn.jsdelivr.net/gh/v2ray/dist/v2ray-linux-${VDIS}.zip"
  161. else
  162. DOWNLOAD_LINK="https://github.com/v2ray/v2ray-core/releases/download/${NEW_VER}/v2ray-linux-${VDIS}.zip"
  163. fi
  164. colorEcho ${BLUE} "Downloading V2Ray: ${DOWNLOAD_LINK}"
  165. curl ${PROXY} -L -H "Cache-Control: no-cache" -o ${ZIPFILE} ${DOWNLOAD_LINK}
  166. if [ $? != 0 ];then
  167. colorEcho ${RED} "Failed to download! Please check your network or try again."
  168. return 3
  169. fi
  170. return 0
  171. }
  172. installSoftware(){
  173. COMPONENT=$1
  174. if [[ -n `command -v $COMPONENT` ]]; then
  175. return 0
  176. fi
  177. getPMT
  178. if [[ $? -eq 1 ]]; then
  179. colorEcho ${RED} "The system package manager tool isn't APT or YUM, please install ${COMPONENT} manually."
  180. return 1
  181. fi
  182. if [[ $SOFTWARE_UPDATED -eq 0 ]]; then
  183. colorEcho ${BLUE} "Updating software repo"
  184. $CMD_UPDATE
  185. SOFTWARE_UPDATED=1
  186. fi
  187. colorEcho ${BLUE} "Installing ${COMPONENT}"
  188. $CMD_INSTALL $COMPONENT
  189. if [[ $? -ne 0 ]]; then
  190. colorEcho ${RED} "Failed to install ${COMPONENT}. Please install it manually."
  191. return 1
  192. fi
  193. return 0
  194. }
  195. # return 1: not apt, yum, or zypper
  196. getPMT(){
  197. if [[ -n `command -v apt-get` ]];then
  198. CMD_INSTALL="apt-get -y -qq install"
  199. CMD_UPDATE="apt-get -qq update"
  200. elif [[ -n `command -v yum` ]]; then
  201. CMD_INSTALL="yum -y -q install"
  202. CMD_UPDATE="yum -q makecache"
  203. elif [[ -n `command -v zypper` ]]; then
  204. CMD_INSTALL="zypper -y install"
  205. CMD_UPDATE="zypper ref"
  206. else
  207. return 1
  208. fi
  209. return 0
  210. }
  211. normalizeVersion() {
  212. if [ -n "$1" ]; then
  213. case "$1" in
  214. v*)
  215. echo "$1"
  216. ;;
  217. *)
  218. echo "v$1"
  219. ;;
  220. esac
  221. else
  222. echo ""
  223. fi
  224. }
  225. # 1: new V2Ray. 0: no. 2: not installed. 3: check failed. 4: don't check.
  226. getVersion(){
  227. if [[ -n "$VERSION" ]]; then
  228. NEW_VER="$(normalizeVersion "$VERSION")"
  229. return 4
  230. else
  231. VER="$(/usr/bin/v2ray/v2ray -version 2>/dev/null)"
  232. RETVAL=$?
  233. CUR_VER="$(normalizeVersion "$(echo "$VER" | head -n 1 | cut -d " " -f2)")"
  234. TAG_URL="https://api.github.com/repos/v2ray/v2ray-core/releases/latest"
  235. NEW_VER="$(normalizeVersion "$(curl ${PROXY} -s "${TAG_URL}" --connect-timeout 10| grep 'tag_name' | cut -d\" -f4)")"
  236. if [[ $? -ne 0 ]] || [[ $NEW_VER == "" ]]; then
  237. colorEcho ${RED} "Failed to fetch release information. Please check your network or try again."
  238. return 3
  239. elif [[ $RETVAL -ne 0 ]];then
  240. return 2
  241. elif [[ $NEW_VER != $CUR_VER ]];then
  242. return 1
  243. fi
  244. return 0
  245. fi
  246. }
  247. stopV2ray(){
  248. colorEcho ${BLUE} "Shutting down V2Ray service."
  249. if [[ -n "${SYSTEMCTL_CMD}" ]] || [[ -f "/lib/systemd/system/v2ray.service" ]] || [[ -f "/etc/systemd/system/v2ray.service" ]]; then
  250. ${SYSTEMCTL_CMD} stop v2ray
  251. elif [[ -n "${SERVICE_CMD}" ]] || [[ -f "/etc/init.d/v2ray" ]]; then
  252. ${SERVICE_CMD} v2ray stop
  253. fi
  254. if [[ $? -ne 0 ]]; then
  255. colorEcho ${YELLOW} "Failed to shutdown V2Ray service."
  256. return 2
  257. fi
  258. return 0
  259. }
  260. startV2ray(){
  261. if [ -n "${SYSTEMCTL_CMD}" ] && [[ -f "/lib/systemd/system/v2ray.service" || -f "/etc/systemd/system/v2ray.service" ]]; then
  262. ${SYSTEMCTL_CMD} start v2ray
  263. elif [ -n "${SERVICE_CMD}" ] && [ -f "/etc/init.d/v2ray" ]; then
  264. ${SERVICE_CMD} v2ray start
  265. fi
  266. if [[ $? -ne 0 ]]; then
  267. colorEcho ${YELLOW} "Failed to start V2Ray service."
  268. return 2
  269. fi
  270. return 0
  271. }
  272. installV2Ray(){
  273. # Install V2Ray binary to /usr/bin/v2ray
  274. mkdir -p '/etc/v2ray' '/var/log/v2ray' && \
  275. unzip -oj "$1" "$2v2ray" "$2v2ctl" "$2geoip.dat" "$2geosite.dat" -d '/usr/bin/v2ray' && \
  276. chmod +x '/usr/bin/v2ray/v2ray' '/usr/bin/v2ray/v2ctl' || {
  277. colorEcho ${RED} "Failed to copy V2Ray binary and resources."
  278. return 1
  279. }
  280. # Install V2Ray server config to /etc/v2ray
  281. if [ ! -f '/etc/v2ray/config.json' ]; then
  282. local PORT="$(($RANDOM + 10000))"
  283. local UUID="$(cat '/proc/sys/kernel/random/uuid')"
  284. unzip -pq "$1" "$2vpoint_vmess_freedom.json" | \
  285. sed -e "s/10086/${PORT}/g; s/23ad6b10-8d1a-40f7-8ad0-e3e35cd38297/${UUID}/g;" - > \
  286. '/etc/v2ray/config.json' || {
  287. colorEcho ${YELLOW} "Failed to create V2Ray configuration file. Please create it manually."
  288. return 1
  289. }
  290. colorEcho ${BLUE} "PORT:${PORT}"
  291. colorEcho ${BLUE} "UUID:${UUID}"
  292. fi
  293. }
  294. installInitScript(){
  295. if [[ -n "${SYSTEMCTL_CMD}" ]] && [[ ! -f "/etc/systemd/system/v2ray.service" && ! -f "/lib/systemd/system/v2ray.service" ]]; then
  296. unzip -oj "$1" "$2systemd/v2ray.service" -d '/etc/systemd/system' && \
  297. systemctl enable v2ray.service
  298. elif [[ -n "${SERVICE_CMD}" ]] && [[ ! -f "/etc/init.d/v2ray" ]]; then
  299. installSoftware 'daemon' && \
  300. unzip -oj "$1" "$2systemv/v2ray" -d '/etc/init.d' && \
  301. chmod +x '/etc/init.d/v2ray' && \
  302. update-rc.d v2ray defaults
  303. fi
  304. }
  305. Help(){
  306. cat - 1>& 2 << EOF
  307. ./install-release.sh [-h] [-c] [--remove] [-p proxy] [-f] [--version vx.y.z] [-l file]
  308. -h, --help Show help
  309. -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
  310. -f, --force Force install
  311. --version Install a particular version, use --version v3.15
  312. -l, --local Install from a local file
  313. --remove Remove installed V2Ray
  314. -c, --check Check for update
  315. EOF
  316. }
  317. remove(){
  318. if [[ -n "${SYSTEMCTL_CMD}" ]] && [[ -f "/etc/systemd/system/v2ray.service" ]];then
  319. if pgrep "v2ray" > /dev/null ; then
  320. stopV2ray
  321. fi
  322. systemctl disable v2ray.service
  323. rm -rf "/usr/bin/v2ray" "/etc/systemd/system/v2ray.service"
  324. if [[ $? -ne 0 ]]; then
  325. colorEcho ${RED} "Failed to remove V2Ray."
  326. return 0
  327. else
  328. colorEcho ${GREEN} "Removed V2Ray successfully."
  329. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  330. return 0
  331. fi
  332. elif [[ -n "${SYSTEMCTL_CMD}" ]] && [[ -f "/lib/systemd/system/v2ray.service" ]];then
  333. if pgrep "v2ray" > /dev/null ; then
  334. stopV2ray
  335. fi
  336. systemctl disable v2ray.service
  337. rm -rf "/usr/bin/v2ray" "/lib/systemd/system/v2ray.service"
  338. if [[ $? -ne 0 ]]; then
  339. colorEcho ${RED} "Failed to remove V2Ray."
  340. return 0
  341. else
  342. colorEcho ${GREEN} "Removed V2Ray successfully."
  343. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  344. return 0
  345. fi
  346. elif [[ -n "${SERVICE_CMD}" ]] && [[ -f "/etc/init.d/v2ray" ]]; then
  347. if pgrep "v2ray" > /dev/null ; then
  348. stopV2ray
  349. fi
  350. rm -rf "/usr/bin/v2ray" "/etc/init.d/v2ray"
  351. if [[ $? -ne 0 ]]; then
  352. colorEcho ${RED} "Failed to remove V2Ray."
  353. return 0
  354. else
  355. colorEcho ${GREEN} "Removed V2Ray successfully."
  356. colorEcho ${BLUE} "If necessary, please remove configuration file and log file manually."
  357. return 0
  358. fi
  359. else
  360. colorEcho ${YELLOW} "V2Ray not found."
  361. return 0
  362. fi
  363. }
  364. checkUpdate(){
  365. echo "Checking for update."
  366. VERSION=""
  367. getVersion
  368. RETVAL="$?"
  369. if [[ $RETVAL -eq 1 ]]; then
  370. colorEcho ${BLUE} "Found new version ${NEW_VER} for V2Ray.(Current version:$CUR_VER)"
  371. elif [[ $RETVAL -eq 0 ]]; then
  372. colorEcho ${BLUE} "No new version. Current version is ${NEW_VER}."
  373. elif [[ $RETVAL -eq 2 ]]; then
  374. colorEcho ${YELLOW} "No V2Ray installed."
  375. colorEcho ${BLUE} "The newest version for V2Ray is ${NEW_VER}."
  376. fi
  377. return 0
  378. }
  379. main(){
  380. #helping information
  381. [[ "$HELP" == "1" ]] && Help && return
  382. [[ "$CHECK" == "1" ]] && checkUpdate && return
  383. [[ "$REMOVE" == "1" ]] && remove && return
  384. local ARCH=$(uname -m)
  385. VDIS="$(archAffix)"
  386. # extract local file
  387. if [[ $LOCAL_INSTALL -eq 1 ]]; then
  388. 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."
  389. NEW_VER=local
  390. rm -rf /tmp/v2ray
  391. ZIPFILE="$LOCAL"
  392. #FILEVDIS=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f4`
  393. #SYSTEM=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f3`
  394. #if [[ ${SYSTEM} != "linux" ]]; then
  395. # colorEcho ${RED} "The local V2Ray can not be installed in linux."
  396. # return 1
  397. #elif [[ ${FILEVDIS} != ${VDIS} ]]; then
  398. # colorEcho ${RED} "The local V2Ray can not be installed in ${ARCH} system."
  399. # return 1
  400. #else
  401. # NEW_VER=`ls /tmp/v2ray |grep v2ray-v |cut -d "-" -f2`
  402. #fi
  403. else
  404. # download via network and extract
  405. installSoftware "curl" || return $?
  406. getVersion
  407. RETVAL="$?"
  408. if [[ $RETVAL == 0 ]] && [[ "$FORCE" != "1" ]]; then
  409. colorEcho ${BLUE} "Latest version ${CUR_VER} is already installed."
  410. if [ -n "${ERROR_IF_UPTODATE}" ]; then
  411. return 10
  412. fi
  413. return
  414. elif [[ $RETVAL == 3 ]]; then
  415. return 3
  416. else
  417. colorEcho ${BLUE} "Installing V2Ray ${NEW_VER} on ${ARCH}"
  418. downloadV2Ray || return $?
  419. fi
  420. fi
  421. local ZIPROOT="$(zipRoot "${ZIPFILE}")"
  422. installSoftware unzip || return $?
  423. if [ -n "${EXTRACT_ONLY}" ]; then
  424. colorEcho ${BLUE} "Extracting V2Ray package to ${VSRC_ROOT}."
  425. if unzip -o "${ZIPFILE}" -d ${VSRC_ROOT}; then
  426. colorEcho ${GREEN} "V2Ray extracted to ${VSRC_ROOT%/}${ZIPROOT:+/${ZIPROOT%/}}, and exiting..."
  427. return 0
  428. else
  429. colorEcho ${RED} "Failed to extract V2Ray."
  430. return 2
  431. fi
  432. fi
  433. if pgrep "v2ray" > /dev/null ; then
  434. V2RAY_RUNNING=1
  435. stopV2ray
  436. fi
  437. installV2Ray "${ZIPFILE}" "${ZIPROOT}" || return $?
  438. installInitScript "${ZIPFILE}" "${ZIPROOT}" || return $?
  439. if [[ ${V2RAY_RUNNING} -eq 1 ]];then
  440. colorEcho ${BLUE} "Restarting V2Ray service."
  441. startV2ray
  442. fi
  443. colorEcho ${GREEN} "V2Ray ${NEW_VER} is installed."
  444. rm -rf /tmp/v2ray
  445. return 0
  446. }
  447. main