├── .idea
├── encodings.xml
├── misc.xml
├── modules.xml
├── nginx_dump.iml
├── qaplug_profiles.xml
├── vcs.xml
└── workspace.xml
├── README.md
├── access.lua
├── body_filter.lua
├── config.lua
├── init.lua
├── init_worker.lua
└── util.lua
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/nginx_dump.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/qaplug_profiles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
463 |
464 |
465 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
101 |
102 |
103 |
104 | get_cookie_json()
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 | 1623994272306
188 |
189 |
190 | 1623994272306
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 | 1624006405008
199 |
200 |
201 |
202 | 1624006405008
203 |
204 |
205 | 1624007191270
206 |
207 |
208 |
209 | 1624007191270
210 |
211 |
212 | 1624008106632
213 |
214 |
215 |
216 | 1624008106632
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 | No facets are configured
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 | scala-sdk-2.11.8
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 | 1.8
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 | nginx_dump
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 | 1.8
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Nginx_dump
2 |
3 |
4 |
5 | - 该工具用于把Openresty(Nginx+Lua) 请求参数和响应 dump出来,用于旁路HTTP流量分析、风控、资产识别、API数据泄露分析等等
6 |
7 | - 请在nginx.conf http 里面添加下面的片段,目录修改为实际目录
8 |
9 | - 目前是取的x-real-ip为客户ip, 代码位于 util的 _M.get_client_ip(),如果需要改为x-forwarded-for,修改
10 | local clent_ip = _M.req_x_real_ip() 为 local clent_ip = _M.req_xxf()
11 |
12 | - 日志文件目录 在config.lua 里面配置
13 |
14 |
15 | http {
16 |
17 | lua_package_path "/usr/local/openresty/nginx/conf/nginx_dump/?.lua;;";
18 | init_by_lua_file /usr/local/openresty/nginx/conf/nginx_dump/init.lua;
19 | init_worker_by_lua_file /usr/local/openresty/nginx/conf/nginx_dump/init_worker.lua;
20 | access_by_lua_file /usr/local/openresty/nginx/conf/nginx_dump/access.lua;
21 | body_filter_by_lua_file /usr/local/openresty/nginx/conf/nginx_dump/body_filter.lua;
22 |
23 | server {
24 | listen 443 ssl;
25 | server_name localhost;
26 | }
27 |
28 | }
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/access.lua:
--------------------------------------------------------------------------------
1 | ---
2 | --- Generated by EmmyLua(https://github.com/EmmyLua)
3 | --- Created by njcx.
4 | --- DateTime: 2021-01-18 13:33
5 | ---
6 |
7 | local util = require("util")
8 | local cjson = require "cjson.safe"
9 | local config_file = require("config")
10 | local client_ip = util.get_client_ip()
11 | local user_agent = util.get_user_agent()
12 | local local_time = util.get_localtime()
13 | local host = util.get_host() --无端口
14 | local http_req_url = util.http_req_url() --完整的地址有参数
15 | local waf_host = util.get_hostname()
16 | local req_data = util.get_all_args()
17 | local req_method = util.get_method()
18 | local header = util.get_req_header_json()
19 | local cookie = util.get_cookie_json()
20 |
21 |
22 | local _M = {
23 | }
24 |
25 |
26 | function _M.waf_access_log()
27 | local _, error = pcall(function()
28 |
29 | local log_path = config_file.prod.access_log_path
30 |
31 | if env == 'dev' then
32 | log_path = config_file.dev.access_log_path
33 | end
34 | if env == 'demo' then
35 | log_path = config_file.demo.access_log_path
36 | end
37 |
38 | local client_ip = client_ip
39 | local user_agent = user_agent
40 | local local_time = local_time
41 | local host = host
42 | local url = http_req_url
43 | local waf_host = waf_host
44 | local req_data = req_data
45 | local req_method = req_method
46 | local header = header
47 | local cookie = cookie
48 | local log_json_obj = {
49 | ip = client_ip,
50 | time = local_time,
51 | host = host,
52 | useragent = user_agent,
53 | req_method =req_method,
54 | url = url,
55 | header = header,
56 | cookie = cookie,
57 | req_data=req_data,
58 | waf_host = waf_host
59 | }
60 | local log_line = cjson.encode(log_json_obj)
61 | local log_name = string.format("%s/%s-access-waf.log", log_path, os.date("%Y-%m-%d"))
62 | local file = io.open(log_name, "a")
63 | if file == nil then
64 | return
65 | end
66 | file:write(string.format("%s\n", log_line))
67 | file:flush()
68 | file:close()
69 | end
70 | )
71 | if error then
72 | util.waf_error_log(error)
73 | end
74 | end
75 |
76 |
77 |
78 | _M.waf_access_log()
79 |
80 |
--------------------------------------------------------------------------------
/body_filter.lua:
--------------------------------------------------------------------------------
1 | ---
2 | --- Generated by EmmyLua(https://github.com/EmmyLua)
3 | --- Created by njcx.
4 | --- DateTime: 2021-06-18 15:14
5 | ---
6 |
7 | local util = require("util")
8 | local client_ip = util.get_client_ip()
9 | local user_agent = util.get_user_agent()
10 | local local_time = util.get_localtime()
11 | local host = util.get_host() --无端口
12 | local waf_host = util.get_hostname()
13 | local req_data = util.get_all_args()
14 | local req_method = util.get_method()
15 | local header = util.get_req_header_json()
16 | local cookie = util.get_cookie_json()
17 | local config_file = require("config")
18 | local io = require("io")
19 | local cjson = require("cjson.safe")
20 | local string = require("string")
21 |
22 | local _M = {}
23 | function _M.waf_resp_log()
24 | local _, error = pcall(function()
25 |
26 | local log_path = config_file.prod.response_log_dir
27 |
28 | if env == 'dev' then
29 | log_path = config_file.dev.response_log_dir
30 | end
31 | if env == 'demo' then
32 | log_path = config_file.demo.response_log_dir
33 | end
34 |
35 |
36 | local chunk, eof = ngx.arg[1], ngx.arg[2]
37 | local buffered = ngx.ctx.buffered
38 | if not buffered then
39 | buffered = {}
40 | ngx.ctx.buffered = buffered
41 | end
42 | if chunk ~= "" then
43 | buffered[#buffered + 1] = chunk
44 | ngx.arg[1] = nil
45 | end
46 | if eof then
47 | local whole = table.concat(buffered)
48 | ngx.ctx.buffered = nil
49 | ngx.arg[1] = whole
50 |
51 | local log_json_obj = {
52 | ip = client_ip,
53 | time = local_time,
54 | host = host,
55 | useragent = user_agent,
56 | req_method =req_method,
57 | url = url,
58 | header = header,
59 | cookie = cookie,
60 | req_data=req_data,
61 | waf_host = waf_host,
62 | body = whole
63 | }
64 |
65 | local json_text = cjson.encode(log_json_obj) or '{}'
66 | local log_name = string.format("%s/%s-resp-waf.log", log_path, os.date("%Y-%m-%d"))
67 | local file = io.open(log_name, "a")
68 | if file == nil then
69 | return
70 | end
71 | file:write(string.format("%s\n",json_text))
72 | file:flush()
73 | file:close()
74 | end
75 |
76 | end
77 | )
78 | if error then
79 | util.waf_error_log(error)
80 | end
81 | end
82 |
83 | _M.waf_resp_log()
84 |
85 |
--------------------------------------------------------------------------------
/config.lua:
--------------------------------------------------------------------------------
1 | ---
2 | --- Generated by EmmyLua(https://github.com/EmmyLua)
3 | --- Created by njcx.
4 | --- DateTime: 2021-01-18 13:31
5 | ---
6 |
7 |
8 | local _M ={
9 | }
10 |
11 | _M.prod ={
12 |
13 | response_log_dir = "/data/log/nginx/hack/",
14 | error_log_path = '/tmp/',
15 | access_log_path = '/data/log/nginx/hack/',
16 | info_log_path = '/tmp/',
17 | }
18 |
19 | _M.dev ={
20 |
21 | response_log_dir = "/Users/njcx/openresty-1.13.6.2-bin/nginx/logs/",
22 | error_log_path = '/Users/njcx/openresty-1.13.6.2-bin/nginx/logs/',
23 | access_log_path = '/Users/njcx/openresty-1.13.6.2-bin/nginx/logs/',
24 | info_log_path = '/Users/njcx/openresty-1.13.6.2-bin/nginx/logs/',
25 |
26 | }
27 |
28 | _M.demo={
29 | response_log_dir = "/data/log/nginx/hack/",
30 | error_log_path = '/tmp/',
31 | access_log_path = '/data/log/nginx/hack/',
32 | info_log_path = '/tmp/',
33 | }
34 |
35 |
36 | function _M.config_init()
37 |
38 | --- if env == 'prod'
39 | --- info should be 'false'
40 |
41 | env = 'prod' --string- prod,dev,demo
42 | info = true --boolean- true or false
43 | ip_for_both = true --boolean- true or false
44 |
45 | --- 如果只用高防转发,请置 ip_for_both = false,可看到WAF被绕过记录
46 | --- 如果高防转发和流量直达混用, 请置 ip_for_both = true,WAF被绕过检测功能将失效
47 |
48 | end
49 |
50 | return _M
--------------------------------------------------------------------------------
/init.lua:
--------------------------------------------------------------------------------
1 | --- Generated by EmmyLua(https://github.com/EmmyLua)
2 | --- Created by njcx.
3 | --- DateTime: 2019/4/7 22:27
4 |
5 | local util = require("util")
6 | util.waf_info_log_m("nginx master process started")
--------------------------------------------------------------------------------
/init_worker.lua:
--------------------------------------------------------------------------------
1 | ---
2 | --- Generated by EmmyLua(https://github.com/EmmyLua)
3 | --- Created by njcx.
4 | --- DateTime: 2019/4/7 22:27
5 |
6 | local config = require('config')
7 | config.config_init()
8 |
9 |
--------------------------------------------------------------------------------
/util.lua:
--------------------------------------------------------------------------------
1 | ---
2 | --- Generated by EmmyLua(https://github.com/EmmyLua)
3 | --- Created by njcx.
4 | --- DateTime: 2019/4/9 10:25 AM
5 | ---
6 |
7 | local io = require("io")
8 | local cjson = require("cjson.safe")
9 | local string = require("string")
10 | local config_file = require("config")
11 | local ngx_var = ngx.var
12 | local ngx_unescape_uri = ngx.unescape_uri
13 |
14 |
15 | local _M = {
16 | }
17 |
18 |
19 | function _M.table_to_json(_obj)
20 | local json_text = cjson.encode(_obj) or '{}'
21 | return json_text
22 | end
23 |
24 |
25 | function _M.json_to_table(_obj)
26 | local table = cjson.decode(_obj) or {}
27 | return table
28 | end
29 |
30 |
31 |
32 | function _M.get_user_agent()
33 | local ua = ngx_var.http_user_agent
34 | return ua
35 | end
36 |
37 |
38 | function _M.get_cookie()
39 | local cookie = ngx_unescape_uri(ngx_var.http_cookie)
40 | return cookie
41 | end
42 |
43 | -- (nginx定义server_name)
44 | function _M.get_server_name()
45 | local host = ngx_var.server_name
46 | return host
47 | end
48 |
49 |
50 | -- (带端口)
51 | function _M.get_http_host()
52 | local host = ngx_var.http_host
53 | return host
54 | end
55 |
56 | -- (不带端口)
57 | function _M.get_host()
58 | local host = ngx_var.host
59 | return host
60 | end
61 |
62 | -- (waf主机名)
63 | function _M.get_hostname()
64 | local host = ngx_var.hostname
65 | return host
66 | end
67 |
68 | -- (地址加参数)
69 | function _M.req_url()
70 | local request_uri = ngx_unescape_uri(ngx_var.request_uri)
71 | return request_uri
72 | end
73 |
74 | -- (不包含?和后面的参数)
75 | function _M.uri()
76 | local uri = ngx_unescape_uri(ngx_var.uri)
77 | return uri
78 | end
79 |
80 | --(完整的地址,包含host,包含端口,有参数)
81 |
82 | function _M.http_req_url()
83 | local request_uri = _M.req_url()
84 | return _M.get_http_host() .. request_uri
85 | end
86 |
87 | --(完整的地址,包含host,包含端口,无参数)
88 |
89 | function _M.http_req_uri()
90 | local uri =_M.uri()
91 | return _M.get_http_host() .. uri
92 | end
93 |
94 |
95 | function _M.get_method()
96 | local method = ngx_var.request_method
97 | return method
98 | end
99 |
100 |
101 | function _M.req_get_data()
102 | local args = ngx.req.get_uri_args()
103 | local args_data = _M.get_table_to_str(args)
104 | return args_data
105 | end
106 |
107 |
108 | function _M.req_get_data_json()
109 | local args = ngx.req.get_uri_args()
110 | local json = _M.table_to_json(args)
111 | return json
112 | end
113 |
114 |
115 | function _M.req_post_data()
116 | ngx.req.read_body()
117 | local body_str = ngx.req.get_body_data()
118 | local value, err = cjson.decode(body_str)
119 | if not value then
120 | local value = ngx.req.get_post_args()
121 | if not value then
122 | return ''
123 | end
124 | return _M.get_table_to_str(value)
125 | end
126 | return _M.get_table_to_str(value)
127 | end
128 |
129 |
130 | function _M.req_post_data_json()
131 | ngx.req.read_body()
132 | local body_str = ngx.req.get_body_data()
133 | local value, err = cjson.decode(body_str)
134 | if not value then
135 | local value = ngx.req.get_post_args()
136 | if not value then
137 | return '{}'
138 | end
139 | return cjson.encode(value)
140 | end
141 | return body_str
142 | end
143 |
144 |
145 | function _M.get_headers()
146 | local header = ngx.req.get_headers()
147 | return header
148 | end
149 |
150 |
151 | function _M.get_req_header_json()
152 | local header = _M.get_headers()
153 | return _M.table_to_json(header)
154 | end
155 |
156 |
157 | function _M.split_str_table(str,reps)
158 | local sub_str_tab = {}
159 | while (true) do
160 | local pos = string.find(str, reps);
161 | if (not pos) then
162 | sub_str_tab[#sub_str_tab + 1] = str;
163 | break;
164 | end
165 | local sub_str = string.sub(str, 1, pos - 1);
166 | sub_str_tab[#sub_str_tab + 1] = sub_str;
167 | str = string.sub(str, pos + 1, #str);
168 | end
169 | return sub_str_tab
170 | end
171 |
172 |
173 | function _M.get_cookie_json()
174 | local cookie = _M.get_cookie()
175 | local kv = {}
176 | local result = {}
177 | if cookie then
178 | cookie_ = _M.split_str_table(cookie,';')
179 | for _, v in pairs(cookie_) do
180 | kv = _M.split_str_table(v,'=')
181 | result[kv[1]]=kv[2]
182 | end
183 | return _M.table_to_json(result)
184 | end
185 | return '{}'
186 | end
187 |
188 |
189 | function _M.req_referer()
190 | local referer = ngx_unescape_uri(ngx_var.http_referer)
191 | return referer
192 | end
193 |
194 |
195 | function _M.req_xxf()
196 | local header = _M.get_headers()
197 | local xxf = header["X_Forwarded_For"]
198 | return xxf
199 | end
200 |
201 |
202 | function _M.req_x_real_ip()
203 | local header = _M.get_headers()
204 | local x_real_ip = header["X-Real-IP"]
205 | return x_real_ip
206 | end
207 |
208 |
209 |
210 | function _M.get_localtime()
211 | local localtime = ngx.localtime()
212 | return localtime
213 | end
214 |
215 |
216 | function _M.get_client_ip()
217 | local remote_addr = ngx.var.remote_addr
218 | if env == 'dev' or env == 'demo' then
219 | return remote_addr
220 | end
221 |
222 | local first_ip = ""
223 | local clent_ip = _M.req_x_real_ip()
224 | if clent_ip == nil then
225 | first_ip = "unknown"
226 | if ip_for_both then
227 | return remote_addr
228 | end
229 | return first_ip
230 | else
231 | first_ip = _M.split(clent_ip, ',')[1]
232 | return first_ip
233 | end
234 | end
235 |
236 |
237 | function _M.get_all_args()
238 | local req_data = '{}'
239 | if _M.get_method() == "POST" then
240 | req_data = _M.req_post_data_json()
241 | return req_data
242 | elseif _M.get_method() == "GET" then
243 | req_data = _M.req_get_data_json()
244 | return req_data
245 | else
246 | return req_data
247 | end
248 | end
249 |
250 |
251 | function _M.waf_error_log(msg)
252 | local log_path = config_file.prod.error_log_path
253 | if env == 'dev' then
254 | log_path = config_file.dev.error_log_path
255 | end
256 | if env == 'demo' then
257 | log_path = config_file.demo.error_log_path
258 | end
259 |
260 | local log_name = string.format("%s/%s-error-waf.log", log_path, os.date("%Y-%m-%d"))
261 | local log_data = os.date("%Y-%m-%d %X")..' %s \n'
262 | local file = io.open(log_name, "a")
263 | if file == nil then
264 | return
265 | end
266 | file:write(string.format(log_data, msg))
267 | file:flush()
268 | file:close()
269 | end
270 |
271 |
272 | function _M.waf_info_log(msg)
273 | local log_path = config_file.prod.info_log_path
274 | if env == 'dev' then
275 | log_path = config_file.dev.info_log_path
276 | end
277 | if env == 'demo' then
278 | log_path = config_file.demo.info_log_path
279 | end
280 |
281 | local log_name = string.format("%s/waf_info_worker.log", log_path)
282 | local log_data = os.date("%Y-%m-%d %X")..' %s \n'
283 | local file = io.open(log_name, "a")
284 | if file == nil then
285 | return
286 | end
287 | file:write(string.format(log_data, msg))
288 | file:flush()
289 | file:close()
290 | end
291 |
292 |
293 | function _M.waf_info_log_m(msg)
294 | local log_path = config_file.prod.info_log_path
295 | if env == 'dev' then
296 | log_path = config_file.dev.info_log_path
297 | end
298 | if env == 'demo' then
299 | log_path = config_file.demo.info_log_path
300 | end
301 |
302 | local log_name = string.format("%s/waf_info_master.log", log_path)
303 | local log_data = os.date("%Y-%m-%d %X")..' %s \n'
304 | local file = io.open(log_name, "a")
305 | if file == nil then
306 | return
307 | end
308 | file:write(string.format(log_data, msg))
309 | file:flush()
310 | file:close()
311 | end
312 |
313 | --- waf_info_log_m
314 | --- nginx master 进程专用
315 |
316 |
317 | return _M
--------------------------------------------------------------------------------