blob: 4e9a4be5c8990fc7043406f01c0eb608ea5bac3c (
plain)
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
|
package Bugzilla::ModPerl::BlockIP;
use 5.10.1;
use strict;
use warnings;
use Apache2::RequestRec ();
use Apache2::Connection ();
use Apache2::Const -compile => qw(OK);
use Cache::Memcached::Fast;
use constant BLOCK_TIMEOUT => 60*60;
my $MEMCACHED = Bugzilla::Memcached->_new()->{memcached};
my $STATIC_URI = qr{
^/
(?: extensions/[^/]+/web
| robots\.txt
| __heartbeat__
| __lbheartbeat__
| __version__
| images
| skins
| js
| errors
)
}xms;
sub block_ip {
my ($class, $ip) = @_;
$MEMCACHED->set("block_ip:$ip" => 1, BLOCK_TIMEOUT) if $MEMCACHED;
}
sub unblock_ip {
my ($class, $ip) = @_;
$MEMCACHED->delete("block_ip:$ip") if $MEMCACHED;
}
sub handler {
my $r = shift;
return Apache2::Const::OK if $r->uri =~ $STATIC_URI;
my $ip = $r->headers_in->{'X-Forwarded-For'};
if ($ip) {
$ip = (split(/\s*,\s*/ms, $ip))[-1];
}
else {
$ip = $r->connection->remote_ip;
}
if ($MEMCACHED && $MEMCACHED->get("block_ip:$ip")) {
__PACKAGE__->block_ip($ip);
$r->status_line("429 Too Many Requests");
# 500 is used here because apache 2.2 doesn't understand 429.
# the above line and the return value together mean we produce 429.
# Any other variation doesn't work.
$r->custom_response(500, "Too Many Requests");
return 429;
}
else {
return Apache2::Const::OK;
}
}
1;
|