diff --git a/lib/App/Controller/Apiv1.pm b/lib/App/Controller/Apiv1.pm index e9082f2..da254e3 100644 --- a/lib/App/Controller/Apiv1.pm +++ b/lib/App/Controller/Apiv1.pm @@ -19,7 +19,6 @@ sub routes { $route->(post => '/api/v1/paste' => 'api_post_paste'); $route->(get => '/api/v1/languages' => 'api_get_languages'); $route->(get => '/api/v1/channels' => 'api_get_channels'); - $route->(get => '/api/v1/re' => 'api_get_re'); } sub api_get_paste { @@ -63,7 +62,7 @@ sub api_post_paste { # } else { if ($channel) { # TODO config for allowing announcements my $words = $c->paste->banned_word_list_re; - unless ($code =~ $words || $who =~ $words || $desc =~ $words) { + unless ($code =~ $words || $who =~ $words || $desc =~ $words || $c->paste->is_banned_ip($c->tx->remote_address)) { $c->perlbot->announce($channel, $who, substr($desc, 0, 40), $c->req->url->base()."/p/$id"); } } @@ -101,11 +100,4 @@ sub api_get_channels { ]}); }; -sub api_get_re { - my $c=shift; - - use Data::Dumper; - $c->render(text => Dumper($c->paste->banned_word_list_re)); -} - 1; diff --git a/lib/App/Model/Paste.pm b/lib/App/Model/Paste.pm index 617a51f..4cae4d5 100644 --- a/lib/App/Model/Paste.pm +++ b/lib/App/Model/Paste.pm @@ -9,6 +9,7 @@ use DateTime; # TODO config for dbname has 'dbh' => sub {DBI->connect("dbi:SQLite:dbname=pastes.db", "", "", {RaiseError => 1, sqlite_unicode => 1})}; +has 'asndbh' => sub {DBI->connect("dbi:SQLite:dbname=asn.db", "", "", {RaiseError => 1, sqlite_unicode => 1})}; sub insert_pastebin { my $self = shift; @@ -68,4 +69,21 @@ sub banned_word_list_re { return $re; } +sub get_asn_for_ip { + my ($self, $ip) = @_; + + my ($asn) = @{$self->asndbh->selectrow_arrayref("SELECT asn FROM asn WHERE ? >= start AND ? <= end", {}, $ip, $ip) || []}[0]; + return $asn; +} + +sub is_banned_ip { + my ($self, $_ip) = @_; + my $ip = sprintf("%03d.%03d.%03d.%03d", split(/\./, $_ip)); + + my $asn = $self->get_asn_for_ip($ip); + my $row_ar = $self->dbh->selectall_arrayref("SELECT * FROM banned_ips i LEFT JOIN banned_asns a WHERE (i.ip = ? AND i.deleted <> 1) OR (a.asn = ? AND a.deleted <> 1)", {}, $ip, $asn); + + return 0+@$row_ar; +} + 1;