| 1 | package Plack::Middleware::Greylist; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: throttle requests with different rates based on net blocks | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | # RECOMMEND PREREQ: Cache::FastMmap 1.52 | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # RECOMMEND PREREQ: Ref::Util::XS | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use v5.20; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use parent qw( Plack::Middleware ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use HTTP::Status qw/ HTTP_FORBIDDEN HTTP_TOO_MANY_REQUESTS /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use List::Util 1.29 qw/ pairs /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Module::Load qw/ load /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use Net::IP::LPM; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Plack::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use Plack::Util::Accessor qw/ default_rate rules cache file _match greylist retry_after cache_config callback /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use Ref::Util qw/ is_plain_arrayref is_coderef /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use Time::Seconds qw/ ONE_MINUTE /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use experimental qw/ postderef signatures /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | our $VERSION = 'v0.8.2'; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 5 | ▶ | sub prepare_app($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=27 SCAR=35.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 5 | ▶ | $self->default_rate(-1) unless defined $self->default_rate; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 5 | ▶ | die "default_rate must be a positive integer" unless $self->default_rate =~ /^[1-9][0-9]*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | my $config = $self->cache_config; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 5 | ▶ | $self->cache_config( $config //= {} ) unless defined $config; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$config //= {}
MC/DC0%0/1
$config //= {}
$config
Truth table0%0/2
A
A= $config
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 5 | ▶ | $config->{init_file} //= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"init_file"} //= 0
MC/DC0%0/1
$config->{"init_file"} //= 0
$config->{"init_file"}
Truth table50%1/2
A
A= $config->{"init_file"}
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 5 | ▶ | $config->{unlink_on_exit} //= !$config->{init_file}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"unlink_on_exit"} //= !$config->{"init_file"}
MC/DC0%0/1
$config->{"unlink_on_exit"} //= !$config->{"init_file"}
$config->{"unlink_on_exit"}
Truth table50%1/2
A
A= $config->{"unlink_on_exit"}
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | ▶ | $config->{serializer} //= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"serializer"} //= ""
MC/DC0%0/1
$config->{"serializer"} //= ""
$config->{"serializer"}
Truth table50%1/2
A
A= $config->{"serializer"}
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | ▶ | my $expiry = $config->{expire_time} //= ONE_MINUTE; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"expire_time"} //= 60
MC/DC100%1/1
$config->{"expire_time"} //= 60
$config->{"expire_time"}
Truth table100%2/2
A
A= $config->{"expire_time"}
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | $self->retry_after( $config->{expire_time} + 1 ) unless defined $self->retry_after; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | die "retry_after must be a positive integer greater than $expiry seconds" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->retry_after =~ /^[1-9][0-9]*$/u and $self->retry_after > $expiry
MC/DC0%0/2
$self->retry_after =~ /^[1-9][0-9]*$/u and $self->retry_after > $expiry
$self->retry_after =~ /^[1-9][0-9]*$/u
$self->retry_after > $expiry
Truth table33%1/3
A and B
A= $self->retry_after =~ /^[1-9][0-9]*$/uB= $self->retry_after > $expiry
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | unless $self->retry_after =~ /^[1-9][0-9]*$/ && $self->retry_after > $expiry; | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 5 | ▶ | unless ( $self->cache ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | ▶ | my $file = $self->file // $config->{share_file}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->file // $config->{"share_file"}
MC/DC0%0/2
$self->file // $config->{"share_file"}
$self->file
$config->{"share_file"}
Truth table66%2/3
A // B
A= $self->fileB= $config->{"share_file"}
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | ▶ | die "No cache was set" unless defined $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | $config->{share_file} = "$file"; | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | load Cache::FastMmap; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | ▶ | die "Cache::FastMmap version 1.52 or newer is required" if Cache::FastMmap->VERSION < 1.52; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 5 | my $cache = Cache::FastMmap->new(%$config); | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | $self->cache( | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 89 | ▶ | sub($ip) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | return $cache->get_and_set( | ||||||||||||||||||||||||||||||||||||||||||
| 59 | $ip, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 89 | ▶ | sub( $, $count, $opts ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 89 | ▶ | $count //= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$count //= 0
MC/DC100%1/1
$count //= 0
$count
Truth table100%2/2
A
A= $count
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 89 | return ( $count + 1, { expire_on => $opts->{expire_on} } ); | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 89 | ); | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | my $match = Net::IP::LPM->new; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 5 | ▶ | $self->_match( sub($ip) { $match->lookup($ip) } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 5 | my @blocks; | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 5 | ▶ | if ( my $greylist = $self->greylist ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 5 | push @blocks, ( $greylist->%* ); | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | $self->rules( my $rules = {} ); | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 5 | my %codes = ( whitelist => -1, allowed => -1, blacklist => 0, rejected => 0, norobots => 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 5 | my %types = ( ip => '', netblock => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 5 | for my $line ( pairs @blocks ) { | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 15 | my ( $block, $rule ) = $line->@*; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 15 | ▶ | $rule = [ split /\s+/, $rule ] unless is_plain_arrayref($rule); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 15 | my ( $rate, $type ) = $rule->@*; | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 15 | ▶ | $type //= "ip"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$type //= "ip"
MC/DC100%1/1
$type //= "ip"
$type
Truth table100%2/2
A
A= $type
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 15 | ▶ | my $mask = $types{$type} // $type; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$types{$type} // $type
MC/DC0%0/2
$types{$type} // $type
$types{$type}
$type
Truth table66%2/3
A // B
A= $types{$type}B= $type
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 15 | ▶ | $mask = $block if $mask eq "1"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 15 | ▶ | $rate //= "rejected"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$rate //= "rejected"
MC/DC0%0/1
$rate //= "rejected"
$rate
Truth table50%1/2
A
A= $rate
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 15 | ▶ | if ( exists $codes{$rate} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | $mask = $rate if $mask eq ""; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | $rate = $codes{$rate}; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 15 | $rules->{$block} = [ $rate, $mask ]; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 15 | $match->add( $block => $block ); | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | ▶ | if ( my $fn = $self->callback ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | die "callback must be a code reference" unless is_coderef($fn); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | $self->callback( | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 15 | ▶ | sub($info) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 15 | my $env = $info->{env}; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 15 | my $msg = $info->{message}; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 15 | ▶ | if ( my $log = $env->{'psgix.logger'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 15 | $log->( { message => $msg, level => 'warn' } ); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | $env->{'psgi.errors'}->print($msg); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 15 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 101 | ▶ | sub call( $self, $env ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 101 | my $ip = $env->{REMOTE_ADDR}; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 101 | my $name = $self->_match->($ip); | |||||||||||||||||||||||||||||||||||||||||
| 132 | 101 | ▶ | my $rule = $name ? $self->rules->{$name} : [ $self->default_rate ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 101 | my $rate = $rule->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 101 | ▶ | if ( $rate == 0 && $rule->[1] && $rule->[1] eq "norobots" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$rate == 0 and $rule->[1] and $rule->[1] eq "norobots"
$rate == 0 and $rule->[1]
MC/DC100%3/3
$rate == 0 and $rule->[1] and $rule->[1] eq "norobots"
$rate == 0
$rule->[1]
$rule->[1] eq "norobots"
Truth table100%4/4
A and B and C
A= $rate == 0B= $rule->[1]C= $rule->[1] eq "norobots"
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 3 | ▶ | if ( $env->{PATH_INFO} eq "/robots.txt" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | $rate = ONE_MINUTE; # one request/second | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 101 | ▶ | if ( $rate >= 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 89 | my $limit = $rate == 0; | |||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 89 | ▶ | my ($hits) = $self->cache->( $rule->[1] || $ip ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$rule->[1] || $ip
MC/DC0%0/2
$rule->[1] || $ip
$rule->[1]
$ip
Truth table66%2/3
A || B
A= $rule->[1]B= $ip
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 89 | ▶ | $limit = $hits > $rate ? $hits : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 89 | ▶ | if ($limit) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 17 | ▶ | my $block = $name || "default"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$name || 'default'
MC/DC100%1/1
$name || 'default'
$name
Truth table100%2/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 17 | ▶ | if ( my $fn = $self->callback ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 17 | ▶ | $fn->( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||||||||||||||||
| 156 | env => $env, | ||||||||||||||||||||||||||||||||||||||||||
| 157 | ip => $ip, | ||||||||||||||||||||||||||||||||||||||||||
| 158 | hits => $limit, | ||||||||||||||||||||||||||||||||||||||||||
| 159 | rate => $rate, | ||||||||||||||||||||||||||||||||||||||||||
| 160 | block => $block, | ||||||||||||||||||||||||||||||||||||||||||
| 161 | message => "Rate limiting ${ip} after ${limit}/${rate} for ${block}", | ||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | ) or return $self->app->($env); | ||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 16 | ▶ | if ( $rate == 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 5 | return [ HTTP_FORBIDDEN, [], ["Forbbidden"] ]; | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | return [ | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 11 | HTTP_TOO_MANY_REQUESTS, | |||||||||||||||||||||||||||||||||||||||||
| 175 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 176 | "Retry-After" => $self->retry_after, | ||||||||||||||||||||||||||||||||||||||||||
| 177 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 178 | ["Too Many Requests"] | ||||||||||||||||||||||||||||||||||||||||||
| 179 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 84 | return $self->app->($env); | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||||||||||||||||