go.sh 14 KB

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