@@ -12,6 +12,7 @@ local timer_at = ngx.timer.at
12
12
local add_ban = utils .add_ban
13
13
local is_whitelisted = utils .is_whitelisted
14
14
local is_banned = utils .is_banned
15
+ local get_country = utils .get_country
15
16
local get_security_mode = utils .get_security_mode
16
17
local tostring = tostring
17
18
@@ -39,6 +40,16 @@ function badbehavior:log()
39
40
end
40
41
-- Get security mode
41
42
local security_mode = get_security_mode (self .ctx )
43
+ -- Get country
44
+ local country = " local"
45
+ local err
46
+ if self .ctx .bw .ip_is_global then
47
+ country , err = get_country (self .ctx .bw .remote_addr )
48
+ if not country then
49
+ country = " unknown"
50
+ self .logger :log (ERR , " can't get country code " .. err )
51
+ end
52
+ end
42
53
-- Call increase function later and with cosocket enabled
43
54
local ok , err = timer_at (
44
55
0 ,
@@ -49,7 +60,8 @@ function badbehavior:log()
49
60
tonumber (self .variables [" BAD_BEHAVIOR_THRESHOLD" ]),
50
61
self .use_redis ,
51
62
self .ctx .bw .server_name ,
52
- security_mode
63
+ security_mode ,
64
+ country
53
65
)
54
66
if not ok then
55
67
return self :ret (false , " can't create increase timer : " .. err )
@@ -67,7 +79,17 @@ function badbehavior:log_stream()
67
79
end
68
80
69
81
-- luacheck: ignore 212
70
- function badbehavior .increase (premature , ip , count_time , ban_time , threshold , use_redis , server_name , security_mode )
82
+ function badbehavior .increase (
83
+ premature ,
84
+ ip ,
85
+ count_time ,
86
+ ban_time ,
87
+ threshold ,
88
+ use_redis ,
89
+ server_name ,
90
+ security_mode ,
91
+ country
92
+ )
71
93
-- Instantiate objects
72
94
local logger = require " bunkerweb.logger" :new (" badbehavior" )
73
95
local datastore = require " bunkerweb.datastore" :new ()
@@ -108,7 +130,7 @@ function badbehavior.increase(premature, ip, count_time, ban_time, threshold, us
108
130
-- Store local ban
109
131
if counter > threshold then
110
132
if security_mode == " block" then
111
- ok , err = add_ban (ip , " bad behavior" , ban_time , server_name )
133
+ ok , err = add_ban (ip , " bad behavior" , ban_time , server_name , country )
112
134
if not ok then
113
135
logger :log (ERR , " (increase) can't save ban : " .. err )
114
136
return
0 commit comments