Browse Source

Merge branch 'dev'

zjcqoo 6 years ago
parent
commit
ac024752ec
16 changed files with 238 additions and 78 deletions
  1. 2 1
      .gitignore
  2. 15 5
      README.md
  3. 12 0
      acme.conf
  4. 0 0
      acme/.well-known/acme-challenge/.gitignore
  5. 1 0
      acme/.well-known/acme-challenge/test.txt
  6. 3 1
      allowed-sites.conf
  7. 1 1
      api.conf
  8. 6 11
      docs/cert-auto.md
  9. 0 22
      docs/deploy.md
  10. 18 13
      i.sh
  11. 43 0
      log.conf
  12. 97 0
      mime.types
  13. 3 21
      nginx.conf
  14. 9 2
      run.sh
  15. 4 1
      setup-ipset.sh
  16. 24 0
      www.conf

+ 2 - 1
.gitignore

@@ -1 +1,2 @@
-._*
+._*
+www

+ 15 - 5
README.md

@@ -1,12 +1,14 @@
 # 演示
 
-https://zjcqoo.github.io/-----https://www.google.com
+https://jsproxy.tk/-----https://www.google.com
 
 (该域名目前已被屏蔽,之前访问过的用户,只要不清浏览器缓存仍可使用)
 
 
 # 更新
 
+* 2019-05-29 nginx 增加静态资源服务,可同时支持代理接口和首页访问
+
 * 2019-05-27 增加 nio.io、sslip.io 后备域名,减少申请失败的几率
 
 * 2019-05-26 安装时自动申请证书(使用 xip.io 域名),安装后即可预览
@@ -17,7 +19,7 @@ https://zjcqoo.github.io/-----https://www.google.com
 # 安装
 
 ```bash
-curl -O https://raw.githubusercontent.com/EtherDream/jsproxy/master/i.sh && bash i.sh
+curl https://raw.githubusercontent.com/EtherDream/jsproxy/dev/i.sh | bash
 ```
 
 * 自动安装目前只支持 Linux x64,并且需要 root 权限
@@ -26,12 +28,20 @@ curl -O https://raw.githubusercontent.com/EtherDream/jsproxy/master/i.sh && bash
 
 无法满足上述条件,或想了解安装细节,可尝试[手动安装](docs/setup.md)。
 
+测试: `https://服务器IP.xip.io:8443`(具体参考脚本输出)
+
+
+## 部署
+
+Fork 本项目,进入 `gh-pages` 分支,编辑 `conf.js` 文件:
+
+* 节点列表(`node_map` 字段,包括节点 id 和节点主机)
 
-# 预览
+* 默认节点(`node_default` 字段,指定节点 id)
 
-访问 `https://zjcqoo.github.io#test=服务器IP.xip.io:8443`(参考脚本输出)
+访问 `https://用户名.github.io/jsproxy` 预览。
 
-部署到自己的 github.io 或其他站点,可参考[站点部署](docs/deploy.md)。
+GitHub 支持[自定义域名](https://help.github.com/en/articles/using-a-custom-domain-with-github-pages)。也可以将文件发布到自己的 Web 服务器上
 
 
 # 维护

+ 12 - 0
acme.conf

@@ -0,0 +1,12 @@
+#
+# 提供 Let's Encrypt 服务验证 challenge
+#
+server {
+  # 从 80 端口重定向过来,因为 jsproxy 用户没有权限开启 80 端口
+  listen                10080;
+  access_log            logs/acme.log combined;
+
+  location /.well-known/acme-challenge/ {
+    root                ../acme;
+  }
+}

+ 0 - 0
acme/.well-known/acme-challenge/.gitignore


+ 1 - 0
acme/.well-known/acme-challenge/test.txt

@@ -0,0 +1 @@
+ok

+ 3 - 1
allowed-sites.conf

@@ -6,13 +6,15 @@
 # 注意 URL 不包含路径部分(结尾没有 /)
 #
 http://127.0.0.1                  '127';
+http://127.0.0.1:8080             '127';
 http://localhost                  'lo';
+http://localhost:8080             'lo';
 
 # 接口和网站同源,这种情况下 origin 为空
 ''                                'mysite';
 
 # ~ 开头为正则匹配,此处允许 github.io 所有子站点
-~^https://([\w-]+).github.io$     'gh-$1';
+~^https://([\w-]+)\.github\.io$     'gh-$1';
 
 # 允许任何站点使用
 # ~(.*)                             '$1';

+ 1 - 1
api.conf

@@ -22,7 +22,7 @@ location = /preflight {
   more_set_headers
     'access-control-allow-origin: *'
     'access-control-allow-methods: GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS'
-    'access-control-allow-headers: --raw-info,--level,--url,--referer,--cookie,--origin,--ext,--aceh,--ver,--type,--mode,accept,accept-charset,accept-encoding,accept-language,accept-datetime,authorization,cache-control,content-length,content-type,date,if-match,if-modified-since,if-none-match,if-range,if-unmodified-since,max-forwards,pragma,range,te,upgrade,upgrade-insecure-requests,x-requested-with,chrome-proxy'
+    'access-control-allow-headers: --raw-info,--level,--url,--referer,--cookie,--origin,--ext,--aceh,--ver,--type,--mode,accept,accept-charset,accept-encoding,accept-language,accept-datetime,authorization,cache-control,content-length,content-type,date,if-match,if-modified-since,if-none-match,if-range,if-unmodified-since,max-forwards,pragma,range,te,upgrade,upgrade-insecure-requests,x-requested-with,chrome-proxy,purpose'
     'access-control-max-age: 1728000'
   ;
   return              204;

+ 6 - 11
docs/cert-auto.md

@@ -6,27 +6,23 @@
 iptables -t nat -I PREROUTING 1 -p tcp --dport 80 -j REDIRECT --to-ports 10080
 ```
 
-2.安装 acme.sh
+> 外部访问 http://服务器IP/.well-known/acme-challenge/test.txt 可验证是否正常。返回 `ok` 说明正常。
 
-该脚本依赖 `openssl`,否则无法生成证书(大部分系统默认已安装)
-
-```bash
-yum install -y openssl
-```
-
-安装 acme.sh(无需 root 权限,在 `jsproxy` 用户下安装)
+2.安装 acme.sh(无需 root 权限,在 `jsproxy` 用户下安装)
 
 ```bash
 su - jsproxy
 curl https://raw.githubusercontent.com/Neilpang/acme.sh/master/acme.sh | INSTALLONLINE=1  sh
 ```
 
+> 部分精简系统可能没有 `openssl` 导致运行失败,需提前安装依赖(例如 `yum install -y openssl`)
+
 3.申请证书
 
 ```bash
 # 服务器公网 IP
 ip=$(curl -s https://api.ipify.org)
-domain="$ip.xip.io"
+domain=$ip.xip.io
 
 dist=~/server/cert/$domain
 mkdir -p $dist
@@ -47,7 +43,6 @@ mkdir -p $dist
 
 如果申请失败(例如提示 `rate limit exceeded`),尝试将 `xip.io` 换成 `nip.io`、`sslip.io` 等其他类似的域名。
 
-
 4.生成配置文件:
 
 ```conf
@@ -62,7 +57,7 @@ ssl_certificate_key   cert/$domain/ecc.key;
 
 5.验证
 
-访问 `https://服务器IP.xip.io:8443/`,没出现证书错误即成功(404 等错误不影响)
+访问 `https://服务器IP.xip.io:8443/`,没出现证书错误即成功。
 
 6.关闭 80 端口转发
 

+ 0 - 22
docs/deploy.md

@@ -1,22 +0,0 @@
-# 站点部署
-
-## 使用自己的 github.io
-
-1.进入 https://github.com/zjcqoo/zjcqoo.github.io 点击 fork。
-
-2.进入 Settings 页面,仓库重命名成 `my.github.io`(假设用户名为 `my`)
-
-3.进入 `conf.js` 文件,参考备注修改:
-
-* 节点列表(`node_map` 字段,包括节点 id 和节点主机)
-
-* 默认节点(`node_default` 字段,指定节点 id)
-
-4.访问 `https://my.github.io` 预览
-
-> 本项目支持子路径。仓库可重命名成任何名字(例如 x),然后创建 `gh-pages` 分支,通过 `https://my.github.io/x` 也能访问。
-
-
-## 使用任意域名
-
-[自定义 github pages 域名](https://help.github.com/en/articles/using-a-custom-domain-with-github-pages),或者将文件发布到其他 Web 服务器上。

+ 18 - 13
i.sh

@@ -1,10 +1,14 @@
 #!/usr/bin/env bash
 
-BIN_URL=https://raw.githubusercontent.com/EtherDream/jsproxy-bin/master/
+{ # this ensures the entire script is downloaded #
 
-JSPROXY_VER=master
+JSPROXY_VER=dev
 OPENRESTY_VER=1.15.8.1
 
+SRC_URL=https://raw.githubusercontent.com/EtherDream/jsproxy/$JSPROXY_VER
+BIN_URL=https://raw.githubusercontent.com/EtherDream/jsproxy-bin/master
+ZIP_URL=https://codeload.github.com/EtherDream/jsproxy/tar.gz
+
 SUPPORTED_OS="Linux-x86_64"
 OS="$(uname)-$(uname -m)"
 USER=$(whoami)
@@ -107,7 +111,7 @@ ssl_certificate_key   cert/$domain/ecc.key;
       log "证书申请完成,重启服务 ..."
       server/run.sh reload
 
-      log "在线预览: https://zjcqoo.github.io/#test=$domain:8443"
+      log "在线预览: https://$domain:8443"
       break
     fi
 
@@ -136,10 +140,16 @@ install() {
   log "nginx path: $NGX_DIR"
 
   log "下载代理服务 ..."
-  curl -o jsproxy.tar.gz https://codeload.github.com/EtherDream/jsproxy/tar.gz/$JSPROXY_VER
+  curl -o jsproxy.tar.gz $ZIP_URL/$JSPROXY_VER
   tar zxf jsproxy.tar.gz
   rm -f jsproxy.tar.gz
 
+  log "下载静态资源 ..."
+  curl -o www.tar.gz $ZIP_URL/gh-pages
+  mkdir jsproxy-$JSPROXY_VER/www
+  tar zxf www.tar.gz -C jsproxy-$JSPROXY_VER/www --strip-components=1
+  rm -f www.tar.gz
+
   if [ -x server/run.sh ]; then
     warn "尝试停止当前服务 ..."
     server/run.sh quit
@@ -190,15 +200,8 @@ main() {
     -j REDIRECT \
     --to-ports 10080
 
-  local src=$0
-  local dst=$INSTALL_DIR/i.sh
-  warn "当前脚本移动到 $dst"
-
-  mv -f $src $dst
-  chmod +x $dst
-
   log "切换到 jsproxy 用户,执行安装脚本 ..."
-  su - jsproxy -c "$dst install"
+  su - jsproxy -c "curl -s $SRC_URL/i.sh | bash -s install"
 
   local line=$(iptables -t nat -L --line-numbers | grep "acme challenge svc")
   iptables -t nat -D PREROUTING ${line%% *}
@@ -214,4 +217,6 @@ case $1 in
   gen_cert;;
 *)
   main;;
-esac
+esac
+
+} # this ensures the entire script is downloaded #

+ 43 - 0
log.conf

@@ -0,0 +1,43 @@
+#
+# 日志格式定义
+# https://nginx.org/en/docs/http/ngx_http_log_module.html
+#
+# 分隔: tab (\t)
+# 前缀: 格式版本。格式变化时递增,方便解析
+# 备注:
+# origin_id
+#   请求源的别名,参考 allowed-sites.conf
+# ver
+#   前端配置的版本,定义于 www/conf.js
+# remote_addr
+#   用户 IP,目前未考虑 XFF
+# level
+#   实验中。记录切换状态(首次请求为 1。切换失败再次请求为 0,表示不接受切换)
+# switched
+#   实验中。记录是否切换到廉价节点(未切换则为空,有切换则记录资源体积大小)
+# bodyhash
+#   返回内容的 SHA256,用于统计重复内容
+# upstream_http_access_control_allow_origin
+#   统计支持 cors 的站点,用于加入直接列表
+# ref
+#   请求 referer,不包括 `https://example.com/-----` 部分
+# mode
+#   前端 request.mode 属性
+# type
+#   前端 request.destination 属性
+#
+log_format              log_proxy escape=none
+  '02	'
+  '$time_iso8601	$_origin_id	$_ver	$remote_addr	'
+  '$_level	$_switched	$upstream_cache_status	$request_time	'
+  '$request_length	$bytes_sent	'
+  '$request_method	$_url	$status	$_bodyhash	$upstream_http_access_control_allow_origin	'
+  '$http_user_agent	$_ref	$_mode	$_type'
+;
+
+log_format              log_www escape=none
+  '01	'
+  '$time_iso8601	$remote_addr	$request_time	'
+  '$request_method	$uri	$http_host	$status	'
+  '$http_user_agent'
+;

+ 97 - 0
mime.types

@@ -0,0 +1,97 @@
+
+types {
+    text/html                                        html htm shtml;
+    text/css                                         css;
+    text/xml                                         xml;
+    image/gif                                        gif;
+    image/jpeg                                       jpeg jpg;
+    application/javascript                           js;
+    application/atom+xml                             atom;
+    application/rss+xml                              rss;
+
+    text/mathml                                      mml;
+    text/plain                                       txt;
+    text/vnd.sun.j2me.app-descriptor                 jad;
+    text/vnd.wap.wml                                 wml;
+    text/x-component                                 htc;
+
+    image/png                                        png;
+    image/svg+xml                                    svg svgz;
+    image/tiff                                       tif tiff;
+    image/vnd.wap.wbmp                               wbmp;
+    image/webp                                       webp;
+    image/x-icon                                     ico;
+    image/x-jng                                      jng;
+    image/x-ms-bmp                                   bmp;
+
+    font/woff                                        woff;
+    font/woff2                                       woff2;
+
+    application/java-archive                         jar war ear;
+    application/json                                 json;
+    application/mac-binhex40                         hqx;
+    application/msword                               doc;
+    application/pdf                                  pdf;
+    application/postscript                           ps eps ai;
+    application/rtf                                  rtf;
+    application/vnd.apple.mpegurl                    m3u8;
+    application/vnd.google-earth.kml+xml             kml;
+    application/vnd.google-earth.kmz                 kmz;
+    application/vnd.ms-excel                         xls;
+    application/vnd.ms-fontobject                    eot;
+    application/vnd.ms-powerpoint                    ppt;
+    application/vnd.oasis.opendocument.graphics      odg;
+    application/vnd.oasis.opendocument.presentation  odp;
+    application/vnd.oasis.opendocument.spreadsheet   ods;
+    application/vnd.oasis.opendocument.text          odt;
+    application/vnd.openxmlformats-officedocument.presentationml.presentation
+                                                     pptx;
+    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
+                                                     xlsx;
+    application/vnd.openxmlformats-officedocument.wordprocessingml.document
+                                                     docx;
+    application/vnd.wap.wmlc                         wmlc;
+    application/x-7z-compressed                      7z;
+    application/x-cocoa                              cco;
+    application/x-java-archive-diff                  jardiff;
+    application/x-java-jnlp-file                     jnlp;
+    application/x-makeself                           run;
+    application/x-perl                               pl pm;
+    application/x-pilot                              prc pdb;
+    application/x-rar-compressed                     rar;
+    application/x-redhat-package-manager             rpm;
+    application/x-sea                                sea;
+    application/x-shockwave-flash                    swf;
+    application/x-stuffit                            sit;
+    application/x-tcl                                tcl tk;
+    application/x-x509-ca-cert                       der pem crt;
+    application/x-xpinstall                          xpi;
+    application/xhtml+xml                            xhtml;
+    application/xspf+xml                             xspf;
+    application/zip                                  zip;
+
+    application/octet-stream                         bin exe dll;
+    application/octet-stream                         deb;
+    application/octet-stream                         dmg;
+    application/octet-stream                         iso img;
+    application/octet-stream                         msi msp msm;
+
+    audio/midi                                       mid midi kar;
+    audio/mpeg                                       mp3;
+    audio/ogg                                        ogg;
+    audio/x-m4a                                      m4a;
+    audio/x-realaudio                                ra;
+
+    video/3gpp                                       3gpp 3gp;
+    video/mp2t                                       ts;
+    video/mp4                                        mp4;
+    video/mpeg                                       mpeg mpg;
+    video/quicktime                                  mov;
+    video/webm                                       webm;
+    video/x-flv                                      flv;
+    video/x-m4v                                      m4v;
+    video/x-mng                                      mng;
+    video/x-ms-asf                                   asx asf;
+    video/x-ms-wmv                                   wmv;
+    video/x-msvideo                                  avi;
+}

+ 3 - 21
nginx.conf

@@ -1,20 +1,12 @@
 http {
+  include                 log.conf;
   server {
     listen                8080;
     include               cert/cert.conf;
     include               api.conf;
+    include               www.conf;
   }
-
-  server {
-    listen                10080;  # redir from 80
-    access_log            off;
-    location /.well-known/acme-challenge/ {
-      root                ../acme;
-    }
-    location = /works {
-      return              200 works;
-    }
-  }
+  include                 acme.conf;
 
   # https://nginx.org/en/docs/http/ngx_http_core_module.html
   resolver                1.1.1.1 ipv6=off;
@@ -37,16 +29,6 @@ http {
   limit_req_zone          $binary_remote_addr zone=reqip:16m rate=100r/s;
   limit_req               zone=reqip burst=200 nodelay;
 
-  # https://nginx.org/en/docs/http/ngx_http_log_module.html
-  # separated by tab (\t)
-  log_format              log_proxy escape=none
-    '02	'                 # ver prefix
-    '$time_iso8601	$_origin_id	$_ver	$remote_addr	'
-    '$_level	$_switched	$upstream_cache_status	$request_time	'
-    '$request_length	$bytes_sent	'
-    '$request_method	$_url	$status	$_bodyhash	$upstream_http_access_control_allow_origin	'
-    '$http_user_agent	$_ref	$_mode	$_type'
-  ;
   access_log              logs/proxy.log log_proxy buffer=64k flush=1s;
 
   # https://nginx.org/cn/docs/http/ngx_http_proxy_module.html

+ 9 - 2
run.sh

@@ -1,8 +1,15 @@
-NGX_BIN=$HOME/openresty/nginx/sbin/nginx
+#
+# 该脚本封装 nginx 调用,可在任意位置执行
+#
+# 启动:./run.sh 
+# 重启:./run.sh -s reload
+# 关闭:./run.sh -s quit 
+# 
+NGX_BIN=~/openresty/nginx/sbin/nginx
 CUR_DIR=$(cd `dirname $0` && pwd)
 
 if [ $1 ]; then
   PARAM="-s $1"
 fi
 
-$NGX_BIN -c $CUR_DIR/nginx.conf -p $CUR_DIR/nginx $PARAM
+$NGX_BIN -c $CUR_DIR/nginx.conf -p $CUR_DIR/nginx $PARAM

+ 4 - 1
setup-ipset.sh

@@ -1,4 +1,7 @@
-# 需要 root 运行
+#
+# 该脚本用于禁止 jsporxy 用户访问内网(针对 TCP)
+# 需要 root 权限运行,依赖 ipset 命令
+#
 ipset create ngx-ban-dstip hash:net
 
 # 该策略对 jsproxy 用户的所有程序都生效

+ 24 - 0
www.conf

@@ -0,0 +1,24 @@
+#
+# 提供 www 目录的静态资源服务
+#
+include               mime.types;
+sendfile              on;
+charset               utf-8;
+
+# 安装步骤多,节省的流量不多,暂时不开
+# brotli_static         on;
+# gzip_static           on;
+gzip                  on;
+
+log_not_found         off;
+error_page            404 = /404.html;
+
+location = /404.html {
+  internal;
+  root                ../www;
+}
+
+location / {
+  access_log          logs/access.log log_www buffer=64k flush=1s;
+  root                ../www;
+}