1
0
Эх сурвалжийг харах

增加证书自动生成脚本

zjcqoo 6 жил өмнө
parent
commit
6b2b3b87c8
4 өөрчлөгдсөн 73 нэмэгдсэн , 8 устгасан
  1. 1 0
      acme/README.md
  2. 1 3
      cert/README.md
  3. 57 1
      i.sh
  4. 14 4
      nginx.conf

+ 1 - 0
acme/README.md

@@ -0,0 +1 @@
+该目录存放 HTTPS 证书申请时的 challenge(由 acme.sh 写入),用于 Let's Encrypt 服务器的验证

+ 1 - 3
cert/README.md

@@ -1,3 +1 @@
-该目录存放 HTTPS 证书,每个域名使用独立的目录。
-
-证书通过 `~/server/gen-cert/gen.sh` 生成。
+该目录存放 HTTPS 证书

+ 57 - 1
i.sh

@@ -32,6 +32,43 @@ err() {
   output $COLOR_RED $1
 }
 
+gen_cert() {
+  log "准备申请 HTTPS 证书,使用 服务器IP.xip.io 域名"
+
+  local ip_api="https://api.ipify.org"
+  log "正在获取服务器公网 IP,通过接口: $ip_api"
+
+  local ip=$(curl -s $ip_api)
+  log "服务器公网 IP: $ip"
+
+  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
+
+  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
+
+  log "证书申请完成,重启服务 ..."
+  ./server/run.sh reload
+
+  log "预览: https://zjcqoo.github.io/#test=$ip"
+}
+
+
 install() {
   log "下载 nginx 程序 ..."
   curl -O $CDN/$OS/openresty-$OPENRESTY_VER.tar.gz
@@ -69,10 +106,13 @@ install() {
   log "启动服务 ..."
   ./server/run.sh
 
-  log "服务已开启。后续维护参考 https://github.com/EtherDream/jsproxy"
+  log "服务已开启"
+  gen_cert
 }
 
 main() {
+  log "jsproxy 自动安装脚本开始执行 ..."
+
   if [[ "$SUPPORTED_OS" != *"$OS"* ]]; then
     err "当前系统 $OS 不支持自动安装。尝试编译安装"
     exit 1
@@ -89,6 +129,16 @@ main() {
     useradd jsproxy -g nobody --create-home
   fi
 
+  warn "HTTPS 证书申请需要验证 80 端口,确保 TCP:80 已添加到防火墙"
+  warn "如果当前已有 80 端口的服务,将暂时无法收到数据"
+  iptables \
+    -m comment --comment "acme challenge svc" \
+    -t nat \
+    -I PREROUTING 1 \
+    -p tcp --dport 80 \
+    -j REDIRECT \
+    --to-ports 10080
+
   local src=$0
   local dst=/home/jsproxy/i.sh
   warn "当前脚本移动到 $dst"
@@ -98,6 +148,12 @@ main() {
 
   log "切换到 jsproxy 用户,执行安装脚本 ..."
   su - jsproxy -c "$dst install"
+
+  log "恢复 80 端口 ..."
+  local line=$(iptables -t nat -L --line-numbers | grep "acme challenge svc")
+  iptables -t nat -D PREROUTING ${line%% *}
+
+  log "安装完成。后续维护参考 https://github.com/EtherDream/jsproxy"
 }
 
 

+ 14 - 4
nginx.conf

@@ -1,13 +1,23 @@
 http {
   server {
-    # server_name           example.com;
-    # ssl_certificate       cert/example.com/ecc.cer;
-    # ssl_certificate_key   cert/example.com/ecc.key;
-    # listen                8443 ssl http2;
+    ssl_certificate       cert/ecc.cer;
+    ssl_certificate_key   cert/ecc.key;
+    listen                8443 ssl http2;
     listen                8080;
     include               api.conf;
   }
 
+  server {
+    listen                10080;  # redir from 80
+    access_log            off;
+    location /.well-known/acme-challenge/ {
+      root                ../acme;
+    }
+    location = /works {
+      return              200 works;
+    }
+  }
+
   # https://nginx.org/en/docs/http/ngx_http_core_module.html
   resolver                1.1.1.1 ipv6=off;
   resolver_timeout        10s;