summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Quantum/Plugin
diff options
context:
space:
mode:
authorDylan William Hardison <dylan@hardison.net>2018-06-21 21:41:05 +0200
committerDylan William Hardison <dylan@hardison.net>2018-06-28 22:41:59 +0200
commite7b05770d892573ec47e70a764545ecf950fe343 (patch)
tree07f332a43cbb653a403303b6b1f8b677caf3dae7 /Bugzilla/Quantum/Plugin
parentb167dfd575095cd574560a054673b0d3e78d9966 (diff)
downloadbugzilla-e7b05770d892573ec47e70a764545ecf950fe343.tar.gz
bugzilla-e7b05770d892573ec47e70a764545ecf950fe343.tar.xz
port BlockIP to mojolicious
Diffstat (limited to 'Bugzilla/Quantum/Plugin')
-rw-r--r--Bugzilla/Quantum/Plugin/BlockIP.pm44
1 files changed, 44 insertions, 0 deletions
diff --git a/Bugzilla/Quantum/Plugin/BlockIP.pm b/Bugzilla/Quantum/Plugin/BlockIP.pm
new file mode 100644
index 000000000..57f61c71f
--- /dev/null
+++ b/Bugzilla/Quantum/Plugin/BlockIP.pm
@@ -0,0 +1,44 @@
+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;
+ $c->app->log->debug("remote ip: $ip");
+ 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;