From 6df945da5900da86203e0527816690cb1d52c574 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Tue, 3 Apr 2018 23:05:04 -0400 Subject: Bug 1455495 - Replace apache with Mojolicious --- Bugzilla/Quantum/Plugin/BlockIP.pm | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Bugzilla/Quantum/Plugin/BlockIP.pm (limited to 'Bugzilla/Quantum/Plugin/BlockIP.pm') diff --git a/Bugzilla/Quantum/Plugin/BlockIP.pm b/Bugzilla/Quantum/Plugin/BlockIP.pm new file mode 100644 index 000000000..fbfffad66 --- /dev/null +++ b/Bugzilla/Quantum/Plugin/BlockIP.pm @@ -0,0 +1,43 @@ +package Bugzilla::Quantum::Plugin::BlockIP; +use 5.10.1; +use Mojo::Base 'Mojolicious::Plugin'; + +use Bugzilla::Memcached; + +use constant BLOCK_TIMEOUT => 60*60; + +my $MEMCACHED = Bugzilla::Memcached->_new()->{memcached}; + +sub register { + my ( $self, $app, $conf ) = @_; + + $app->hook(before_routes => \&_before_routes); + $app->helper(block_ip => \&_block_ip); + $app->helper(unblock_ip => \&_unblock_ip); +} + +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 _before_routes { + my ( $c ) = @_; + return if $c->stash->{'mojo.static'}; + + my $ip = $c->tx->remote_address; + if ($MEMCACHED && $MEMCACHED->get("block_ip:$ip")) { + $c->block_ip($ip); + $c->res->code(429); + $c->res->message("Too Many Requests"); + $c->res->body("Too Many Requests"); + $c->finish; + } +} + +1; -- cgit v1.2.3-24-g4f1b From 8ca2304eea840e37bcf182f21e2c14bd21ad9cd7 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Mon, 23 Jul 2018 11:24:13 -0400 Subject: more tidy --- Bugzilla/Quantum/Plugin/BlockIP.pm | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'Bugzilla/Quantum/Plugin/BlockIP.pm') diff --git a/Bugzilla/Quantum/Plugin/BlockIP.pm b/Bugzilla/Quantum/Plugin/BlockIP.pm index fbfffad66..ebeb2a4aa 100644 --- a/Bugzilla/Quantum/Plugin/BlockIP.pm +++ b/Bugzilla/Quantum/Plugin/BlockIP.pm @@ -4,34 +4,34 @@ use Mojo::Base 'Mojolicious::Plugin'; use Bugzilla::Memcached; -use constant BLOCK_TIMEOUT => 60*60; +use constant BLOCK_TIMEOUT => 60 * 60; -my $MEMCACHED = Bugzilla::Memcached->_new()->{memcached}; +my $MEMCACHED = Bugzilla::Memcached->new()->{memcached}; sub register { my ( $self, $app, $conf ) = @_; - $app->hook(before_routes => \&_before_routes); - $app->helper(block_ip => \&_block_ip); - $app->helper(unblock_ip => \&_unblock_ip); + $app->hook( before_routes => \&_before_routes ); + $app->helper( block_ip => \&_block_ip ); + $app->helper( unblock_ip => \&_unblock_ip ); } sub _block_ip { - my ($class, $ip) = @_; - $MEMCACHED->set("block_ip:$ip" => 1, BLOCK_TIMEOUT) if $MEMCACHED; + my ( $class, $ip ) = @_; + $MEMCACHED->set( "block_ip:$ip" => 1, BLOCK_TIMEOUT ) if $MEMCACHED; } sub _unblock_ip { - my ($class, $ip) = @_; + my ( $class, $ip ) = @_; $MEMCACHED->delete("block_ip:$ip") if $MEMCACHED; } sub _before_routes { - my ( $c ) = @_; + my ($c) = @_; return if $c->stash->{'mojo.static'}; my $ip = $c->tx->remote_address; - if ($MEMCACHED && $MEMCACHED->get("block_ip:$ip")) { + if ( $MEMCACHED && $MEMCACHED->get("block_ip:$ip") ) { $c->block_ip($ip); $c->res->code(429); $c->res->message("Too Many Requests"); -- cgit v1.2.3-24-g4f1b From a108778bc2784cdadfc6b2df4ec989b46dc162ca Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Mon, 23 Jul 2018 11:41:14 -0400 Subject: more tidy --- Bugzilla/Quantum/Plugin/BlockIP.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Bugzilla/Quantum/Plugin/BlockIP.pm') diff --git a/Bugzilla/Quantum/Plugin/BlockIP.pm b/Bugzilla/Quantum/Plugin/BlockIP.pm index ebeb2a4aa..058ecbf64 100644 --- a/Bugzilla/Quantum/Plugin/BlockIP.pm +++ b/Bugzilla/Quantum/Plugin/BlockIP.pm @@ -34,8 +34,8 @@ sub _before_routes { if ( $MEMCACHED && $MEMCACHED->get("block_ip:$ip") ) { $c->block_ip($ip); $c->res->code(429); - $c->res->message("Too Many Requests"); - $c->res->body("Too Many Requests"); + $c->res->message('Too Many Requests'); + $c->res->body('Too Many Requests'); $c->finish; } } -- cgit v1.2.3-24-g4f1b