Skip to content

Commit 8a21402

Browse files
committed
add cdn.log
1 parent 83f2300 commit 8a21402

File tree

6 files changed

+172
-39
lines changed

6 files changed

+172
-39
lines changed

conf/dyn.conf.1002

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
user root;
2-
worker_processes 1;
2+
worker_processes 2;
33
daemon off;
44
error_log stderr info;
55
#backtrace_log /var/log/nginx/coredump.log;
@@ -15,11 +15,23 @@ http {
1515
lua_package_path '/home/github/tengcdn/lualib/?.lua;;';
1616
lua_package_cpath '/home/github/tengcdn/lualib/?.so;;';
1717
lua_shared_dict itesty_limit 100k;
18-
lua_shared_dict blocked_iplist 10m;
18+
lua_shared_dict ip_whitelist 10m;
19+
lua_shared_dict ip_blacklist 10m;
1920
lua_shared_dict req_metrics 10m;
2021
lua_shared_dict req_iplist 10m;
2122
access_by_lua_file /home/github/tengcdn/lualib/cdn/access.lua;
2223

24+
init_by_lua_block {
25+
local config = require "cdn.config"
26+
config:set("log.status", true)
27+
config:set("log.level", 1)
28+
29+
local logger = require "resty.logger"
30+
local log = logger:open("/tmp/cdn.log")
31+
log:set_level(0)
32+
config:set("log.file", log)
33+
}
34+
2335
server {
2436
listen 82;
2537
location / {
@@ -32,10 +44,29 @@ http {
3244
location / {
3345

3446
content_by_lua_block {
35-
ngx.say("81")
47+
local logger = require "resty.logger"
48+
local buss1 = logger:open("/tmp/a/a/a/cdn.log")
49+
--buss1:set_level(logger.INFO)
50+
buss1:info("hello world")
51+
ngx.say("ngx.DEBUG=", ngx.DEBUG, "ngx.INFO=", ngx.INFO, "ngx.NOTICE=", ngx.NOTICE, "ngx.WARN=", ngx.WARN, "ngx.ERR=", ngx.ERR)
3652
}
3753
}
3854

55+
location /test {
56+
access_by_lua_block {
57+
local config = require "cdn.config"
58+
config:set("test", "miss me !")
59+
}
60+
content_by_lua_block {
61+
local log = require "cdn.log"
62+
log:info("info")
63+
log:debug("debug")
64+
log:error("error")
65+
local config = require "cdn.config"
66+
ngx.say(config:get("test"))
67+
}
68+
}
69+
3970
location /limit {
4071
access_by_lua_block {
4172
local limits = require("resty.iresty_limits")

conf/dyn.conf.1003

+10-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ events {
1111

1212
http {
1313
lua_code_cache off;
14-
#lua_package_path '/home/github/tengcdn/lualib/?.lua;;';
14+
lua_package_path '/home/github/tengcdn/lualib/?.lua;;';
1515
lua_package_cpath '/home/github/tengcdn/lualib/?.so;;';
1616
lua_shared_dict itesty_limit 100k;
17-
lua_shared_dict blocked_iplist 10m;
17+
lua_shared_dict ip_blacklist 10m;
18+
lua_shared_dict ip_whitelist 10m;
1819
lua_shared_dict req_metrics 10m;
19-
lua_shared_dict req_iplist 10m;
20+
lua_shared_dict req_iplist 10m;
21+
22+
init_by_lua_block {
23+
local log = require("cdn.log")
24+
local logger = log.new()
25+
}
26+
2027
access_by_lua_block {
2128
require "cdn.access"
2229
}

lualib/cdn/access.lua

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ local ngx_now = ngx.now
1515
local ngx_exit = ngx.exit
1616
local ngx_md5 = ngx.md5
1717
local ngx_time = ngx.time
18-
local blocked_iplist = ngx.shared.blocked_iplist
18+
local ip_blacklist = ngx.shared.ip_blacklist
19+
local ip_whitelist = ngx.shared.ip_whitelist
1920
local req_iplist = ngx.shared.req_iplist
2021
local req_metrics = ngx.shared.req_metrics
2122

@@ -24,8 +25,13 @@ local cookies = cookie.get()
2425
local COOKIE_NAME = "__waf_uid"
2526
local COOKIE_KEY = "xg0j21"
2627

27-
if blocked_iplist:get(client_ip) ~= nil then
28-
if blocked_iplist:get(client_ip) >= ngx_now() then
28+
if ip_whitelist:get(client_ip) ~= nil then
29+
if ip_whitelist:get(client_ip) >= ngx_now() then
30+
return
31+
end
32+
end
33+
if ip_blacklist:get(client_ip) ~= nil then
34+
if ip_blacklist:get(client_ip) >= ngx_now() then
2935
ngx_exit(444)
3036
end
3137
end

lualib/cdn/config.lua

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local ngx = ngx
2+
local tostring, ipairs, pairs, type, tonumber, next, unpack =
3+
tostring, ipairs, pairs, type, tonumber, next, unpack
4+
5+
local _M = {
6+
_VERSION = '0.01',
7+
config = {},
8+
}
9+
10+
local mt = { __index = _M }
11+
12+
function _M.ctx(self)
13+
local id = tostring(self)
14+
local ctx = ngx.ctx[id]
15+
if not ctx then
16+
ctx = {
17+
config = {},
18+
}
19+
ngx.ctx[id] = ctx
20+
end
21+
return ctx
22+
end
23+
24+
25+
function _M.set(self, param, value)
26+
if ngx.get_phase() == "init" then
27+
self.config[param] = value
28+
else
29+
self:ctx().config[param] = value
30+
end
31+
end
32+
33+
function _M.get(self, param)
34+
local p = self:ctx().config[param]
35+
if p == nil then
36+
return self.config[param]
37+
else
38+
return p
39+
end
40+
end
41+
42+
return _M

lualib/cdn/log.lua

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
local logger = require "resty.logger"
2+
local config = require "cdn.config"
3+
local ngx = ngx
4+
local _M = {
5+
_VERSION = '0.01',
6+
}
7+
-- log level
8+
local LVL_DEBUG = 1
9+
local LVL_INFO = 2
10+
local LVL_ERROR = 3
11+
local LVL_NONE = 999
12+
13+
local mt = { __index = _M }
14+
15+
function _M.info(self, ... )
16+
if config:get('log.status') then
17+
local log_level = config:get('log.level') or LVL_NONE
18+
if log_level and log_level > LVL_INFO then return end
19+
20+
local filer = config:get('log.file')
21+
if filer then
22+
filer:info(...)
23+
else
24+
ngx.log(ngx.INFO, ...)
25+
end
26+
end
27+
end
28+
29+
function _M.debug(self, ... )
30+
if config:get('log.status') then
31+
local log_level = config:get('log.level') or LVL_NONE
32+
if log_level and log_level > LVL_DEBUG then return end
33+
34+
local filer = config:get('log.file')
35+
if filer then
36+
filer:debug(...)
37+
else
38+
ngx.log(ngx.DEBUG, ...)
39+
end
40+
end
41+
end
42+
43+
function _M.error(self, ... )
44+
if config:get('log.status') then
45+
local log_level = config:get('log.level') or LVL_NONE
46+
if log_level and log_level > LVL_ERROR then return end
47+
48+
local filer = config:get('log.file')
49+
if filer then
50+
filer:error(...)
51+
else
52+
ngx.log(ngx.ERR, ...)
53+
end
54+
end
55+
end
56+
return _M

lualib/resty/logger.lua

+21-30
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ local bor = bit.bor
1010
local setmetatable = setmetatable
1111
local localtime = ngx.localtime()
1212
local ngx = ngx
13-
local type = type
14-
13+
local tostring, ipairs, pairs, type, tonumber, next, unpack =
14+
tostring, ipairs, pairs, type, tonumber, next, unpack
1515

1616
ffi.cdef[[
1717
int write(int fd, const char *buf, int nbyte);
@@ -32,60 +32,51 @@ local LVL_INFO = 2
3232
local LVL_ERROR = 3
3333
local LVL_NONE = 999
3434

35-
module(...)
36-
37-
_VERSION = '0.1'
35+
local _M = {
36+
_VERSION = '0.01',
37+
}
3838

3939
local mt = { __index = _M }
4040

41-
function new(self, log_type, logfile)
41+
function _M.open(self, logfile)
4242
local log_level, log_fd = nil
4343

44-
local level = nil
45-
if 'debug' == log_type then
46-
level = LVL_DEBUG
47-
elseif 'info' == log_type then
48-
level = LVL_INFO
49-
elseif 'error' == log_type then
50-
level = LVL_ERROR
51-
else
52-
level = LVL_NONE
44+
local level = LVL_NONE
45+
local fd = C.open(logfile, bor(O_RDWR, O_CREAT, O_APPEND), bor(S_IRWXU, S_IRGRP, S_IROTH))
46+
if fd == -1 then
47+
ngx.log(ngx.ERR, "open log file " .. logfile .. " failed, errno: " .. tostring(ffi.errno()))
5348
end
54-
5549
return setmetatable({
5650
log_level = level,
57-
log_fd = C.open(logfile, bor(O_RDWR, O_CREAT, O_APPEND), bor(S_IRWXU, S_IRGRP, S_IROTH)),
51+
log_fd = fd,
5852
},mt)
5953
end
6054

55+
function _M.set_level(self, level)
56+
self.log_level = level
57+
end
6158

62-
function debug(self, msg)
59+
function _M.debug(self, msg)
6360
if self.log_level > LVL_DEBUG then return end;
6461

65-
local c = localtime .. "|" .."D" .. "|" .. msg .. "\n";
62+
local c = localtime .. " [DEBUG] " .. msg .. "\n";
6663
C.write(self.log_fd, c, #c);
6764
end
6865

69-
function info(self, msg)
66+
function _M.info(self, msg)
7067
if self.log_level > LVL_INFO then return end;
7168

72-
local c = localtime .. "|" .."I" .. "|" .. msg .. "\n";
69+
local c = localtime .. " [INFO] " .. msg .. "\n";
7370
C.write(self.log_fd, c, #c);
7471
end
7572

7673

77-
function error(self, msg)
74+
function _M.error(self, msg)
7875
if self.log_level > LVL_ERROR then return end;
7976

80-
local c = localtime .. "|" .."E" .. "|" .. msg .. "\n";
77+
local c = localtime .. " [ERROR] " .. msg .. "\n";
8178
C.write(self.log_fd, c, #c);
8279
end
8380

84-
local class_mt = {
85-
-- to prevent use of casual module global variables
86-
__newindex = function (table, key, val)
87-
error('attempt to write to undeclared variable "' .. key .. '"')
88-
end
89-
}
81+
return _M
9082

91-
setmetatable(_M, class_mt)

0 commit comments

Comments
 (0)