Browse Source

证书申请失败自动重试

zjcqoo 6 years ago
parent
commit
14fcdf1a21
4 changed files with 79 additions and 37 deletions
  1. 1 1
      README.md
  2. 3 0
      cert/.gitignore
  3. 3 0
      docs/cert-auto.md
  4. 72 36
      i.sh

+ 1 - 1
README.md

@@ -27,7 +27,7 @@ curl -O https://raw.githubusercontent.com/EtherDream/jsproxy/master/i.sh && bash
 
 # 预览
 
-访问 `https://zjcqoo.github.io#test=服务器IP.xip.io:8443` 
+访问 `https://zjcqoo.github.io#test=服务器IP.xip.io:8443`(参考脚本输出)
 
 部署到自己的 github.io 或其他站点,可参考[站点部署](docs/deploy.md)。
 

+ 3 - 0
cert/.gitignore

@@ -0,0 +1,3 @@
+!.gitignore
+!README.md
+!cert.conf

+ 3 - 0
docs/cert-auto.md

@@ -37,6 +37,9 @@ mkdir -p $dist
   --fullchain-file $dist/ecc.cer
 ```
 
+如果申请失败(例如提示 `rate limit exceeded`),尝试将 `xip.io` 换成 `nip.io`、`sslip.io` 等其他类似的域名。
+
+
 4.生成配置文件:
 
 ```conf

+ 72 - 36
i.sh

@@ -7,7 +7,21 @@ OPENRESTY_VER=1.15.8.1
 
 SUPPORTED_OS="Linux-x86_64"
 OS="$(uname)-$(uname -m)"
-USER=`whoami`
+USER=$(whoami)
+
+INSTALL_DIR=/home/jsproxy
+NGX_DIR=$INSTALL_DIR/openresty
+
+DOMAIN_SUFFIX=(
+  xip.io
+  nip.io
+  sslip.io
+)
+
+GET_IP_API=(
+  https://api.ipify.org
+  https://bot.whatismyipaddress.com/
+)
 
 COLOR_RESET="\033[0m"
 COLOR_RED="\033[31m"
@@ -32,64 +46,86 @@ err() {
 }
 
 gen_cert() {
-  log "准备申请 HTTPS 证书,使用 服务器IP.xip.io 域名"
+  local ip=""
+
+  for i in ${GET_IP_API[@]}; do
+    log "服务器公网 IP 获取中,通过接口 $i"
+    ip=$(curl -s $i)
+
+    if [[ ! $ip ]]; then
+      warn "获取失败"
+      continue
+    fi
 
-  local ip_api="https://api.ipify.org"
-  log "正在获取服务器公网 IP,通过接口: $ip_api"
+    if [[ $(ipcalc -c $ip 2>&1) ]]; then
+      warn "无效 IP:$ip"
+      continue
+    fi
 
-  local ip=$(curl -s $ip_api)
-  log "服务器公网 IP: $ip"
+    break
+  done
+
+  if [[ $ip ]]; then
+    log "服务器公网 IP: $ip"
+  else
+    err "服务器公网 IP 获取失败,无法申请证书"
+    exit 1
+  fi
 
   log "安装 acme.sh 脚本 ..."
   curl https://raw.githubusercontent.com/Neilpang/acme.sh/master/acme.sh | INSTALLONLINE=1  sh
 
   local acme=~/.acme.sh/acme.sh
-  local domain=$ip.xip.io
-
-  local dist=server/cert/$domain
-  mkdir -p $dist
-
-  log "执行 acme.sh 脚本 ..."
-  $acme \
-    --issue \
-    -d $domain \
-    --keylength ec-256 \
-    --webroot server/acme
-
-  $acme \
-    --install-cert \
-    -d $domain \
-    --ecc \
-    --key-file $dist/ecc.key \
-    --fullchain-file $dist/ecc.cer
-
-  if [ ! -s $dist/ecc.key ] || [ ! -s $dist/ecc.cer ]; then
-    err "证书申请失败!"
-    exit 1
-  fi
 
-  echo "
+  for i in ${DOMAIN_SUFFIX[@]}; do
+    local domain=$ip.$i
+    log "尝试为域名 $domain 申请证书 ..."
+
+    local dist=server/cert/$domain
+    mkdir -p $dist
+
+    $acme \
+      --issue \
+      -d $domain \
+      --keylength ec-256 \
+      --webroot server/acme
+
+    $acme \
+      --install-cert \
+      -d $domain \
+      --ecc \
+      --key-file $dist/ecc.key \
+      --fullchain-file $dist/ecc.cer
+
+    if [ -s $dist/ecc.key ] && [ -s $dist/ecc.cer ]; then
+      echo "# generated by i.sh
 listen                8443 ssl http2;
 ssl_certificate       cert/$domain/ecc.cer;
 ssl_certificate_key   cert/$domain/ecc.key;
 " > server/cert/cert.conf
 
-  log "证书申请完成,重启服务 ..."
-  server/run.sh reload
+      log "证书申请完成,重启服务 ..."
+      server/run.sh reload
 
-  log "在线预览: https://zjcqoo.github.io/#test=$domain:8443"
+      log "在线预览: https://zjcqoo.github.io/#test=$domain:8443"
+      break
+    fi
+
+    err "证书申请失败!"
+    rm -rf $dist
+  done
 }
 
 
 install() {
-  cd /home/jsproxy
+  cd $INSTALL_DIR
 
   log "下载 nginx 程序 ..."
   curl -O $BIN_URL/$OS/openresty-$OPENRESTY_VER.tar.gz
   tar zxf openresty-$OPENRESTY_VER.tar.gz
   rm -f openresty-$OPENRESTY_VER.tar.gz
 
-  local ngx_exe=openresty/nginx/sbin/nginx
+  local ngx_exe=$NGX_DIR/nginx/sbin/nginx
   local ngx_ver=$($ngx_exe -v 2>&1)
 
   if [[ "$ngx_ver" != *"nginx version:"* ]]; then
@@ -155,7 +191,7 @@ main() {
     --to-ports 10080
 
   local src=$0
-  local dst=/home/jsproxy/i.sh
+  local dst=$INSTALL_DIR/i.sh
   warn "当前脚本移动到 $dst"
 
   mv -f $src $dst