diff options
author | Dylan William Hardison <dylan@hardison.net> | 2018-04-04 05:05:04 +0200 |
---|---|---|
committer | Dylan William Hardison <dylan@hardison.net> | 2018-08-04 18:25:17 +0200 |
commit | 2e9c9f5b9d1bff165a55dd7dbe54d81b2adbfde1 (patch) | |
tree | c76c12b9daf7b32be1554ab9a5f4b8a72e16a523 /Bugzilla/Quantum/Plugin/BlockIP.pm | |
parent | 7f09ef208e09c85ed7087ab32e89da29bde837db (diff) | |
download | bugzilla-2e9c9f5b9d1bff165a55dd7dbe54d81b2adbfde1.tar.gz bugzilla-2e9c9f5b9d1bff165a55dd7dbe54d81b2adbfde1.tar.xz |
Bug 1455495 - Replace apache with Mojolicious
Diffstat (limited to 'Bugzilla/Quantum/Plugin/BlockIP.pm')
-rw-r--r-- | Bugzilla/Quantum/Plugin/BlockIP.pm | 43 |
1 files changed, 43 insertions, 0 deletions
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; |