i.sh 3.6 KB

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