1
0
Fork 0
mirror of https://github.com/perlbot/perlbuut-pastebin synced 2025-06-07 22:26:01 -04:00

Add banned ip/asn support

This commit is contained in:
Ryan Voots 2017-05-13 01:21:24 -04:00
parent 4f80f12172
commit 713333ebcb
2 changed files with 19 additions and 9 deletions

View file

@ -19,7 +19,6 @@ sub routes {
$route->(post => '/api/v1/paste' => 'api_post_paste'); $route->(post => '/api/v1/paste' => 'api_post_paste');
$route->(get => '/api/v1/languages' => 'api_get_languages'); $route->(get => '/api/v1/languages' => 'api_get_languages');
$route->(get => '/api/v1/channels' => 'api_get_channels'); $route->(get => '/api/v1/channels' => 'api_get_channels');
$route->(get => '/api/v1/re' => 'api_get_re');
} }
sub api_get_paste { sub api_get_paste {
@ -63,7 +62,7 @@ sub api_post_paste {
# } else { # } else {
if ($channel) { # TODO config for allowing announcements if ($channel) { # TODO config for allowing announcements
my $words = $c->paste->banned_word_list_re; 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"); $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; 1;

View file

@ -9,6 +9,7 @@ use DateTime;
# TODO config for dbname # TODO config for dbname
has 'dbh' => sub {DBI->connect("dbi:SQLite:dbname=pastes.db", "", "", {RaiseError => 1, sqlite_unicode => 1})}; 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 { sub insert_pastebin {
my $self = shift; my $self = shift;
@ -68,4 +69,21 @@ sub banned_word_list_re {
return $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; 1;