@@ -26,7 +26,7 @@ function _M.new(options)
26
26
27
27
local cache , err = lrucache .new (cache_buffer_count )
28
28
if not cache then
29
- fatal_error (' failed to create the cache: ' .. (err or ' unknown' ))
29
+ fatal_error (string.format ( ' failed to create the cache: %s ' , (err or ' unknown' ) ))
30
30
end
31
31
32
32
local statsd_client = nil
@@ -45,7 +45,7 @@ function _M.new(options)
45
45
timeout = options .timeout or 10 ,
46
46
threshold = iprepd_threshold ,
47
47
api_key_hdr = {
48
- [' Authorization' ] = ' APIKey ' .. iprepd_api_key ,
48
+ [' Authorization' ] = string.format ( ' APIKey %s ' , iprepd_api_key ) ,
49
49
},
50
50
cache = cache ,
51
51
cache_ttl = options .cache_ttl or 30 ,
@@ -56,23 +56,28 @@ function _M.new(options)
56
56
statsd_max_buffer_count = options .statsd_max_buffer_count or 100 ,
57
57
statsd_flush_timer = options .statsd_flush_timer or 5 ,
58
58
dont_block = options .dont_block or 0 ,
59
+ verbose = options .verbose or 0 ,
59
60
whitelist = whitelist ,
60
61
}
61
62
62
63
return setmetatable (self , mt )
63
64
end
64
65
65
66
function _M .check (self , ip )
67
+ self :debug_log (string.format (" Checking %s" , ip ))
66
68
ngx .req .set_header (' X-Foxsec-IP-Reputation-Below-Threshold' , ' false' )
67
69
ngx .req .set_header (' X-Foxsec-Block' , ' false' )
68
70
if self .whitelist then
69
71
if iputils .ip_in_cidrs (ip , self .whitelist ) then
72
+ self :debug_log (string.format (" %s in whitelist" , ip ))
70
73
return
71
74
end
72
75
end
73
76
77
+
74
78
local reputation = self :get_reputation (ip )
75
79
if reputation then
80
+ self :debug_log (string.format (" Got reputation of %d for %s" , reputation , ip ))
76
81
ngx .req .set_header (' X-Foxsec-IP-Reputation' , tostring (reputation ))
77
82
if reputation <= self .threshold then
78
83
ngx .req .set_header (' X-Foxsec-IP-Reputation-Below-Threshold' , ' true' )
@@ -82,23 +87,20 @@ function _M.check(self, ip)
82
87
end
83
88
84
89
if self .dont_block == 1 then
85
- ngx .log (ngx .ERR , ip .. ' is below threshold with a reputation of ' .. reputation )
90
+ ngx .log (ngx .ERR , string.format ( " %s is below threshold with a reputation of %d " , ip , reputation ) )
86
91
else
87
- ngx .log (ngx .ERR , ip .. ' rejected with a reputation of ' .. reputation )
92
+ ngx .log (ngx .ERR , string.format ( " %s rejected with a reputation of %d " , ip , reputation ) )
88
93
if self .statsd then
89
94
self .statsd .incr (" iprepd.status.rejected" )
90
95
end
91
96
ngx .exit (ngx .HTTP_FORBIDDEN )
92
97
end
93
- else
94
- if self .statsd then
95
- self .statsd .incr (" iprepd.status.accepted" )
96
- end
97
- end
98
98
99
- return
99
+ return
100
+ end
100
101
end
101
102
103
+ self :debug_log (string.format (" %s accepted" , ip ))
102
104
if self .statsd then
103
105
self .statsd .incr (" iprepd.status.accepted" )
104
106
end
@@ -110,40 +112,42 @@ function _M.get_reputation(self, ip)
110
112
if not reputation then
111
113
local httpc = http .new ()
112
114
httpc :set_timeout (self .timeout )
113
- local resp , err = httpc :request_uri (self . url .. ' / ' .. ip , {
115
+ local resp , err = httpc :request_uri (string.format ( " %s/%s " , self . url , ip ) , {
114
116
method = " GET" ,
115
117
headers = self .api_key_hdr ,
116
118
})
117
119
if err then
118
- if self .statsd and err == " timeout " then
119
- self .statsd .incr (" iprepd.err.timeout " )
120
+ if self .statsd then
121
+ self .statsd .incr (" iprepd.err." .. err )
120
122
end
121
- ngx .log (ngx .ERR , ' Error with request to iprepd: ' .. err )
123
+ ngx .log (ngx .ERR , string.format ( " Error with request to iprepd: %s " , err ) )
122
124
return nil
123
125
end
124
126
125
127
-- If the IP was found
126
128
if resp .status == 200 then
127
129
reputation = cjson .decode (resp .body )[' reputation' ]
128
- if reputation and reputation >= 0 and reputation <= 100 then
129
- self .cache :set (ip , reputation , self .cache_ttl )
130
- else
130
+ if not reputation then
131
131
ngx .log (ngx .ERR , ' Unable to parse `reputation` value from response body' )
132
132
end
133
133
elseif resp .status == 404 then
134
- self . cache : set ( ip , 100 , self . cache_ttl )
134
+ reputation = 100
135
135
else
136
- ngx .log (ngx .ERR , ' iprepd responded with a ' .. resp . status .. ' http status code' )
136
+ ngx .log (ngx .ERR , string.format ( " iprepd responded with a %d http status code" , resp . status ) )
137
137
if self .statsd then
138
138
self .statsd .incr (" iprepd.err." .. resp .status )
139
139
end
140
140
if self .cache_errors == 1 then
141
- ngx . log ( ngx . ERR , ' cache_errors is enabled, setting reputation of ' .. ip .. ' to 100 within the cache ' )
142
- self . cache : set ( ip , 100 , self . cache_ttl )
141
+ reputation = 100
142
+ self : debug_log ( string.format ( " cache_errors is enabled, setting reputation of %s to 100 within the cache " , ip ) )
143
143
end
144
144
end
145
145
end
146
146
147
+ if reputation and reputation >= 0 and reputation <= 100 then
148
+ self .cache :set (ip , reputation , self .cache_ttl )
149
+ end
150
+
147
151
return reputation
148
152
end
149
153
@@ -163,4 +167,10 @@ function _M.config_flush_timer(self)
163
167
ngx .timer .every (self .statsd_flush_timer , self .async_flush_stats , self )
164
168
end
165
169
170
+ function _M .debug_log (self , msg )
171
+ if self .verbose == 1 then
172
+ ngx .log (ngx .ERR , string.format (" [verbose] %s" , msg ))
173
+ end
174
+ end
175
+
166
176
return _M
0 commit comments