@@ -130,44 +130,49 @@ sub new
130130 }
131131 }
132132
133- # Connection throttling system
134- require Data::Throttler;
135-
136- my $db_file = $config -> {' throttle' }-> {' file' } // File::Spec-> catdir($info -> tmpdir(), ' throttle' );
137- eval { # Handle YAML Errors
138- my %options = (
139- max_items => $config -> {' throttle' }-> {' max_items' } // 30, # Allow 30 requests
140- interval => $config -> {' throttle' }-> {' interval' } // 90, # Per 90 second window
141- backend => ' YAML' ,
142- backend_options => {
143- db_file => $db_file
133+ if ($ENV {' REMOTE_ADDR' }) {
134+ # Connection throttling system
135+ require Data::Throttler;
136+
137+ my $db_file = $config -> {' throttle' }-> {' file' } // File::Spec-> catdir($info -> tmpdir(), ' throttle' );
138+ eval { # Handle YAML Errors
139+ my %options = (
140+ max_items => $config -> {' throttle' }-> {' max_items' } // 30, # Allow 30 requests
141+ interval => $config -> {' throttle' }-> {' interval' } // 90, # Per 90 second window
142+ backend => ' YAML' ,
143+ backend_options => {
144+ db_file => $db_file
145+ }
146+ );
147+
148+ if (my $throttler = Data::Throttler-> new(%options )) {
149+ # Block if over the limit
150+ if (!$throttler -> try_push(key => $ENV {' REMOTE_ADDR' })) {
151+ $info -> status(429); # Too many requests
152+ sleep (1); # Slow down attackers
153+ if ($params -> {' logger' }) {
154+ $params -> {' logger' }-> info(" $ENV {REMOTE_ADDR} connexion throttled" );
155+ }
156+ return ;
157+ }
144158 }
145- );
159+ };
160+ if ($@ ) {
161+ if ($params -> {' logger' }) {
162+ $params -> {' logger' }-> notice(" Removing unparsable YAML file $db_file : $@ " );
163+ }
164+ unlink ($db_file );
165+ }
146166
147- if (my $throttler = Data::Throttler-> new(%options )) {
148- # Block if over the limit
149- if (!$throttler -> try_push(key => $ENV {' REMOTE_ADDR' })) {
150- $info -> status(429); # Too many requests
151- sleep (1); # Slow down attackers
167+ # Country based blocking
168+ if (my $lingua = $params -> {lingua }) {
169+ if ($blacklist {uc ($lingua -> country())}) {
152170 if ($params -> {' logger' }) {
153- $params -> {' logger' }-> warn (" $ENV {REMOTE_ADDR} connexion throttled " );
171+ $params -> {' logger' }-> warn (" $ENV {REMOTE_ADDR} is from a blacklisted country " . $lingua -> country() );
154172 }
155- return ;
173+ die " $ENV {REMOTE_ADDR} is from a blacklisted country " , $lingua -> country() ;
156174 }
157175 }
158- };
159- if ($@ ) {
160- if ($params -> {' logger' }) {
161- $params -> {' logger' }-> warn (" Removing unparsable YAML file $db_file " );
162- }
163- unlink ($db_file );
164- }
165-
166- # Country based blocking
167- if (my $lingua = $params -> {lingua }) {
168- if ($blacklist {uc ($lingua -> country())}) {
169- die " $ENV {REMOTE_ADDR} is from a blacklisted country " , $lingua -> country();
170- }
171176 }
172177 }
173178
0 commit comments