Verified Commit 728c86a4 authored by Luc Didry's avatar Luc Didry

Add ban CLI tool for banning and unbanning IP addresses

parent 30281419
......@@ -2,6 +2,7 @@ Revision history for Perl application Lstu
0.21-0 2018-??-??
- Allow to delete URLs and ban IPs from safebrowsingcheck
- Add ban CLI tool for banning and unbanning IP addresses
0.20-2 2018-10-07
- Handle very long URLs
......
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lstu::Command::ban;
use Mojo::Base 'Mojolicious::Command';
use FindBin qw($Bin);
use File::Spec qw(catfile);
use Mojo::Util qw(getopt);
use Lstu::DB::Ban;
has description => 'Ban IPs addresses for ten years, or unban them';
has usage => sub { shift->extract_usage };
sub run {
my $c = shift;
my @args = @_;
my $cfile = Mojo::File->new($Bin, '..' , 'lstu.conf');
if (defined $ENV{MOJO_CONFIG}) {
$cfile = Mojo::File->new($ENV{MOJO_CONFIG});
unless (-e $cfile->to_abs) {
$cfile = Mojo::File->new($Bin, '..', $ENV{MOJO_CONFIG});
}
}
my $config = $c->app->plugin('Config', {
file => $cfile,
default => {
prefix => '/',
provisioning => 100,
provis_step => 5,
length => 8,
secret => ['hfudsifdsih'],
page_offset => 10,
theme => 'default',
ban_min_strike => 3,
ban_whitelist => [],
ban_blacklist => [],
minion => {
enabled => 0,
db_path => 'minion.db'
},
session_duration => 3600,
dbtype => 'sqlite',
db_path => 'lstu.db',
max_redir => 2,
skip_spamhaus => 0,
safebrowsing_api_key => '',
memcached_servers => [],
x_frame_options => 'DENY',
x_content_type_options => 'nosniff',
x_xss_protection => '1; mode=block',
log_creator_ip => 0,
}
});
$c->app->plugin('Lstu::Plugin::Helpers');
getopt \@args,
'b|ban=s{1,}' => \my @ban_ips,
'u|unban=s{1,}' => \my @unban_ips;
for my $ip (@ban_ips) {
Lstu::DB::Ban->new(
app => $c->app,
ip => $ip
)->ban_ten_years;
}
say sprintf("%d banned IP addresses", scalar(@ban_ips)) if (@ban_ips);
for my $ip (@unban_ips) {
Lstu::DB::Ban->new(
app => $c->app,
ip => $ip
)->unban;
}
say sprintf("%d unbanned IP addresses", scalar(@unban_ips)) if (@unban_ips);
say $c->extract_usage unless (scalar(@ban_ips) || scalar(@unban_ips));
}
=encoding utf8
=head1 NAME
Lstu::Command::ban - Ban IPs addresses for ten years, or unban them
=head1 SYNOPSIS
Usage:
carton exec script/lstu ban -b|--ban <ip> <ip> Ban the space separated IP addresses for ten years
carton exec script/lstu ban -u|--unban <ip> <ip> Unban the space separated IP addresses
Please note that you can pass the --ban and --unban options at the same time.
=cut
1;
......@@ -241,6 +241,30 @@ sub clear {
$c->app->dbi->db->query('DELETE FROM ban WHERE until < ?', time);
}
=head2 unban
=over 1
=item B<Usage> : C<$c-E<gt>unban>
=item B<Arguments> : none
=item B<Purpose> : unban IP address
=item B<Returns> : the Lstu::DB::Ban object
=back
=cut
sub unban {
my $c = shift;
$c->app->dbi->db->query('DELETE from ban WHERE ip = ?', $c->ip);
return $c;
}
=head2 delete_all
=over 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment