i.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env bash
  2. CDN=https://cdn.jsdelivr.net/gh/etherdream/jsproxy-bin@master
  3. JSPROXY_VER=0.0.9
  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 "$NGX_VER"
  39. log "nginx path: $NGX_DIR"
  40. }
  41. install_jsproxy() {
  42. log "下载代理服务 ..."
  43. curl -s -O $CDN/server-$JSPROXY_VER.tar.gz
  44. if [ -x ./server/run.sh ]; then
  45. warn "尝试停止当前服务 ..."
  46. ./server/run.sh quit
  47. fi
  48. if [ -d "server" ]; then
  49. backup="$PWD/bak/$(date +%Y_%m_%d_%H_%M_%S)"
  50. warn "当前 server 目录备份到 $backup"
  51. mkdir -p $backup
  52. mv server $backup
  53. fi
  54. tar zxf server-$JSPROXY_VER.tar.gz
  55. rm -f server-$JSPROXY_VER.tar.gz
  56. log "启动服务 ..."
  57. ./server/run.sh
  58. log "服务已开启。后续维护参考 https://github.com/EtherDream/jsproxy"
  59. }
  60. compile() {
  61. TMP_DIR="$PWD/__tmp__"
  62. mkdir -p $TMP_DIR
  63. cd $TMP_DIR
  64. log "下载 pcre 源码 ..."
  65. curl -O https://ftp.pcre.org/pub/pcre/pcre-$PCRE_VER.tar.gz
  66. tar zxf pcre-$PCRE_VER.tar.gz
  67. log "下载 zlib 源码 ..."
  68. curl -O https://zlib.net/zlib-$ZLIB_VER.tar.gz
  69. tar zxf zlib-$ZLIB_VER.tar.gz
  70. log "下载 openssl 源码 ..."
  71. curl -O https://www.openssl.org/source/openssl-$OPENSSL_VER.tar.gz
  72. tar zxf openssl-$OPENSSL_VER.tar.gz
  73. log "下载 nginx 源码 ..."
  74. curl -O https://openresty.org/download/openresty-$OPENRESTY_VER.tar.gz
  75. tar zxf openresty-$OPENRESTY_VER.tar.gz
  76. cd openresty-$OPENRESTY_VER
  77. export PATH=$PATH:/sbin
  78. log "配置中 ..."
  79. ./configure \
  80. --with-openssl=../openssl-$OPENSSL_VER \
  81. --with-pcre=../pcre-$PCRE_VER \
  82. --with-zlib=../zlib-$ZLIB_VER \
  83. --with-http_v2_module \
  84. --with-http_ssl_module \
  85. --with-pcre-jit \
  86. --prefix=$NGX_DIR
  87. log "编译中 ..."
  88. make
  89. make install
  90. log "编译完成"
  91. rm -rf $TMP_DIR
  92. check_nginx
  93. install_jsproxy
  94. }
  95. install() {
  96. log "下载 nginx 程序 ..."
  97. curl -O $CDN/$OS/openresty-$OPENRESTY_VER.tar.gz
  98. tar zxf openresty-$OPENRESTY_VER.tar.gz
  99. rm -f openresty-$OPENRESTY_VER.tar.gz
  100. check_nginx
  101. install_jsproxy
  102. }
  103. update() {
  104. install_jsproxy
  105. }
  106. pack() {
  107. log "压缩 openresty ..."
  108. GZIP=-9
  109. tar cvzf openresty.tar.gz openresty
  110. log "done"
  111. ls -la
  112. }
  113. main() {
  114. if [[ "$SUPPORTED_OS" != *"$OS"* ]]; then
  115. err "当前系统 $OS 不支持自动安装。尝试编译安装"
  116. exit 1
  117. fi
  118. if [[ "$USER" != "root" ]]; then
  119. err "自动安装需要 root 权限。如果无法使用 root,尝试编译安装"
  120. exit 1
  121. fi
  122. if ! id -u jsproxy > /dev/null 2>&1 ; then
  123. log "创建用户 jsproxy ..."
  124. groupadd nobody > /dev/null 2>&1
  125. useradd jsproxy -g nobody --create-home
  126. fi
  127. src=$0
  128. dst=/home/jsproxy/i.sh
  129. warn "当前脚本移动到 $dst"
  130. mv -f $src $dst
  131. chmod +x $dst
  132. log "切换到 jsproxy 用户,执行安装脚本 ..."
  133. su - jsproxy -c "$dst install"
  134. }
  135. case "$1" in
  136. "install") install
  137. exit;;
  138. "compile") compile
  139. exit;;
  140. "update") update
  141. exit;;
  142. "pack") pack
  143. exit;;
  144. *) main
  145. exit;;
  146. esac