Simple Connection Rate Limit
I'm having some really odd results in using the following aFlex rule. I would expect that the logic would reject inbound requests for a period of 20 seconds following 10 consecutive bad requests. After 5 (not 10) I see the blocks and once the delay time has expired I see the requests fulfilled. However without sending any further bad requests I still get blocked after about 10 sec.
I'm writing direct to the sessions table and incrementing a value. Personally the logic seems right but obviously it's not. Can someone help?
when RULE_INIT {
set ::BAD_REQUESTS 10
set ::DELAY 20
}
when HTTP_REQUEST {
set IP [IP::client_addr]
set bad_request_limit [table lookup rate_limit $IP]
if {$bad_request_limit >= $::BAD_REQUESTS} {
reject
return
}
}
when HTTP_RESPONSE {
if {([HTTP::status] == 404) or ([HTTP::status] == 500) or ([HTTP::status] == 503)} {
set counter [table lookup rate_limit $IP]
switch $counter {
"" {
table set rate_limit $IP 1 indefinite $::DELAY
}
$::BAD_REQUESTS {
table incr rate_limit $IP
table lifetime rate_limit $IP $::DELAY
}
default {table incr rate_limit $IP}
}
return
}
}
I'm writing direct to the sessions table and incrementing a value. Personally the logic seems right but obviously it's not. Can someone help?
when RULE_INIT {
set ::BAD_REQUESTS 10
set ::DELAY 20
}
when HTTP_REQUEST {
set IP [IP::client_addr]
set bad_request_limit [table lookup rate_limit $IP]
if {$bad_request_limit >= $::BAD_REQUESTS} {
reject
return
}
}
when HTTP_RESPONSE {
if {([HTTP::status] == 404) or ([HTTP::status] == 500) or ([HTTP::status] == 503)} {
set counter [table lookup rate_limit $IP]
switch $counter {
"" {
table set rate_limit $IP 1 indefinite $::DELAY
}
$::BAD_REQUESTS {
table incr rate_limit $IP
table lifetime rate_limit $IP $::DELAY
}
default {table incr rate_limit $IP}
}
return
}
}
0
Comments
Bind this to a free VIP or VPORT and use it as: http://[IP]:[PORT]/status:[table_name]
For example: http://192.168.1.34:8884/status:blacklist