i.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env bash
  2. CDN=https://cdn.jsdelivr.net/gh/etherdream/jsproxy-bin@master
  3. JSPROXY_VER=0.0.2
  4. PCRE_VER=8.43
  5. ZLIB_VER=1.2.11
  6. OPENSSL_VER=1.1.1b
  7. OPENRESTY_VER=1.15.8.1
  8. SUPPORTED_OS="Linux-x86_64"
  9. OS="$(uname)-$(uname -m)"
  10. NGX_DIR="$HOME/openresty"
  11. COLOR_RESET="\033[0m"
  12. COLOR_RED="\033[31m"
  13. COLOR_GREEN="\033[32m"
  14. COLOR_YELLOW="\033[33m"
  15. output() {
  16. color=$1
  17. shift 1
  18. sdata="$@"
  19. stime=$(date "+%H:%M:%S")
  20. printf "$color[jsproxy $stime]$COLOR_RESET $sdata\n"
  21. }
  22. log() {
  23. output $COLOR_GREEN $1
  24. }
  25. warn() {
  26. output $COLOR_YELLOW $1
  27. }
  28. err() {
  29. output $COLOR_RED $1
  30. }
  31. check_nginx() {
  32. NGX_EXE="$NGX_DIR/nginx/sbin/nginx"
  33. NGX_VER=$($NGX_EXE -v 2>&1)
  34. if [[ "$NGX_VER" != *"nginx version:"* ]]; then
  35. err "$NGX_EXE 无法执行!尝试编译安装"
  36. exit 1
  37. fi
  38. log "nginx 安装完成。$NGX_VER"
  39. }
  40. install_jsproxy() {
  41. log "下载 jsproxy ..."
  42. curl -s -O $CDN/server-$JSPROXY_VER.tar.gz
  43. if [ -d "server" ]; then
  44. backup="bak/$(date +%Y_%m_%d_%H_%M_%S)"
  45. warn "当前 server 目录备份到 $backup"
  46. mkdir -p $backup
  47. mv server $backup
  48. fi
  49. tar zxf server-$JSPROXY_VER.tar.gz
  50. rm -f server-$JSPROXY_VER.tar.gz
  51. log "启动服务 ..."
  52. ./server/run.sh
  53. log "检测状态 ..."
  54. RET=$(curl -s http://127.0.0.1:8080/)
  55. ERR_LOG=$PWD/server/logs/error.log
  56. if [[ "$RET" != *"origin"* ]]; then
  57. err "服务启动异常。错误日志:"
  58. tail $ERR_LOG -n100
  59. exit 1
  60. fi
  61. log "服务已开启。后续维护参考 https://github.com/EtherDream/jsproxy"
  62. }
  63. compile() {
  64. TMP_DIR="$PWD/__tmp__"
  65. mkdir -p $TMP_DIR
  66. cd $TMP_DIR
  67. log "下载 pcre 源码 ..."
  68. curl -O https://ftp.pcre.org/pub/pcre/pcre-$PCRE_VER.tar.gz
  69. tar zxf pcre-$PCRE_VER.tar.gz
  70. log "下载 zlib 源码 ..."
  71. curl -O https://zlib.net/zlib-$ZLIB_VER.tar.gz
  72. tar zxf zlib-$ZLIB_VER.tar.gz
  73. log "下载 openssl 源码 ..."
  74. curl -O https://www.openssl.org/source/openssl-$OPENSSL_VER.tar.gz
  75. tar zxf openssl-$OPENSSL_VER.tar.gz
  76. log "下载 nginx 源码 ..."
  77. curl -O https://openresty.org/download/openresty-$OPENRESTY_VER.tar.gz
  78. tar zxf openresty-$OPENRESTY_VER.tar.gz
  79. cd openresty-$OPENRESTY_VER
  80. export PATH=$PATH:/sbin
  81. log "配置中 ..."
  82. ./configure \
  83. --with-openssl=../openssl-$OPENSSL_VER \
  84. --with-pcre=../pcre-$PCRE_VER \
  85. --with-zlib=../zlib-$ZLIB_VER \
  86. --with-http_v2_module \
  87. --with-http_ssl_module \
  88. --with-pcre-jit \
  89. --prefix=$NGX_DIR
  90. log "编译中 ..."
  91. make
  92. make install
  93. log "编译完成"
  94. rm -rf $TMP_DIR
  95. check_nginx
  96. install_jsproxy
  97. }
  98. install() {
  99. log "下载 nginx 程序 ..."
  100. curl -O $CDN/$OS/openresty-$OPENRESTY_VER.tar.gz
  101. tar zxf openresty-$OPENRESTY_VER.tar.gz
  102. rm -f openresty-$OPENRESTY_VER.tar.gz
  103. check_nginx
  104. install_jsproxy
  105. }
  106. update() {
  107. if [ ! -d "server" ]; then
  108. err "当前不存在 server 目录,切换到主目录再更新"
  109. exit 1
  110. fi
  111. install_jsproxy
  112. }
  113. pack() {
  114. log "压缩 openresty ..."
  115. GZIP=-9
  116. tar cvzf openresty.tar.gz openresty
  117. }
  118. main() {
  119. if [[ "$SUPPORTED_OS" != *"$OS"* ]]; then
  120. err "当前系统 $OS 不支持自动安装。尝试编译安装"
  121. exit 1
  122. fi
  123. if [[ "$USER" != "root" ]]; then
  124. err "自动安装需要 root 权限。如果无法使用 root,尝试编译安装"
  125. exit 1
  126. fi
  127. log "创建用户 jsproxy ..."
  128. groupadd nobody
  129. useradd jsproxy -g nobody --create-home
  130. log "当前脚本移动到 $dst"
  131. src=$0
  132. dst=/home/jsproxy/i.sh
  133. mv -f $src $dst
  134. chmod +x $dst
  135. log "切换用户 jsproxy,执行安装脚本 ..."
  136. su - jsproxy -c "$dst install"
  137. }
  138. case "$1" in
  139. "install") install
  140. exit;;
  141. "compile") compile
  142. exit;;
  143. "pack") pack
  144. exit;;
  145. "update") update
  146. exit;;
  147. *) main
  148. exit;;
  149. esac