Browse Source

增加 HTTP 返回内容的 hash 记录

zjcqoo 6 years ago
parent
commit
af51672d42
5 changed files with 35 additions and 10 deletions
  1. 1 1
      README.md
  2. 1 0
      api.conf
  3. 23 0
      lua/http-body-hash.lua
  4. 3 3
      lua/http-enc-res-hdr.lua
  5. 7 6
      nginx.conf

+ 1 - 1
README.md

@@ -2,7 +2,7 @@
 
 https://zjcqoo.github.io/-----https://www.google.com
 
-(由于访问量较大,目前限制 Referer 只允许从 GitHub 访问)
+(目前仍在更新中,如有问题尝试用隐身模式访问)
 
 
 # 安装

+ 1 - 0
api.conf

@@ -14,6 +14,7 @@ set                   $_ver       '';
 set                   $_ref       '';
 set                   $_type      '';
 set                   $_mode      '';
+set                   $_bodyhash  '';
 
 location = /preflight {
   internal;

+ 23 - 0
lua/http-body-hash.lua

@@ -1,5 +1,28 @@
+-- ngx.arg[1] => chunk
+-- ngx.arg[2] => eof
+
+
+-- 大文件只返回首块 hash(用户从廉价带宽获取内容)
 if ngx.ctx._switched then
   local chunk = ngx.arg[1]
   ngx.arg[1] = #chunk .. ',' .. ngx.crc32_long(chunk)
   ngx.arg[2] = true
+  return
+end
+
+
+-- 计算 HTTP 返回数据的 hash(用于统计)
+if ngx.ctx._sha256 == nil then
+  local resty_sha256 = require 'resty.sha256'
+  ngx.ctx._sha256 = resty_sha256:new()
+end
+
+if ngx.arg[2] then
+  local digest = ngx.ctx._sha256:final()
+  digest = digest:sub(1, 16)
+
+  local str = require 'resty.string'
+  ngx.var._bodyhash = str.to_hex(digest)
+else
+  ngx.ctx._sha256:update(ngx.arg[1])
 end

+ 3 - 3
lua/http-enc-res-hdr.lua

@@ -111,9 +111,9 @@ local function nodeSwitched()
 end
 
 -- 节点切换功能,目前还在测试中(demo 中已开启)
--- if nodeSwitched() then
---   return
--- end
+if nodeSwitched() then
+  return
+end
 
 
 local h, err = ngx.resp.get_headers()

+ 7 - 6
nginx.conf

@@ -1,9 +1,9 @@
 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;
+    server_name           *.etherdream.com;
+    ssl_certificate       cert/etherdream.com/ecc.cer;
+    ssl_certificate_key   cert/etherdream.com/ecc.key;
+    listen                8443 ssl http2;
     listen                8080;
     include               api.conf;
   }
@@ -32,11 +32,11 @@ http {
   # https://nginx.org/en/docs/http/ngx_http_log_module.html
   # separated by tab (\t)
   log_format              log_proxy escape=none
-    '01	'                 # ver prefix
+    '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	$upstream_http_access_control_allow_origin	'
+    '$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;
@@ -61,6 +61,7 @@ http {
   lua_load_resty_core     off;
 
   map $http_origin $_origin_id {
+    ''                    'mysite';
     include               allowed-sites.conf;
   }
 }