App-BlockWebFlooders

 view release on metacpan or  search on metacpan

script/block-web-flooders  view on Meta::CPAN

        delete $Blocked{$ip};
    }
}

sub block_ip { _block_or_unblock_ip("block", @_) }

sub unblock_ip { _block_or_unblock_ip("unblock", @_) }

sub _block_or_unblock_ips {
    my $which = shift;

    _init();

    # get IP's from command-line arguments if specified, otherwise from stdin
    my $iter;
    if (@ARGV) {
        require Array::Iter;
        $iter = Array::Iter::array_iter(\@ARGV);
    } else {
        $iter = sub { scalar <STDIN> };
    }

    while (defined(my $ip = $iter->())) {
        chomp($ip);
        unless ($ip =~ /\A$RE{ipv4}\z/) {
            warn "$PROG: Invalid IP address '$ip', skipped\n";
            next;
        }
        _block_or_unblock_ip($which, $ip, 1); # don't update messages
    }
}

sub action_block {
    _block_or_unblock_ips("block");
    _set_need_reload(1);
}

sub action_unblock {
    _block_or_unblock_ips("unblock");
    _set_need_reload(1);
}

sub action_list_blocked {
    _init();

    my $now = time();
    for (sort { $Blocked{$a} <=> $Blocked{$b} } keys %Blocked) {
        my $secs = $Opts{block_period} - ($now - $Blocked{$_});
        $secs = 0 if $secs < 0;
        if ($opt_detail) {
            printf "%s\t%d\n", $_, $secs;
        } else {
            print $_, "\n";
        }
    }
}

sub action_unblock_all {
    _init();

    local @ARGV = keys %Blocked;
    _block_or_unblock_ips("unblock");
    _set_need_reload(1);
}

sub action_run {
    #require Term::Size;
    require Time::Duration;

    #my ($columns, $rows) = Term::Size::chars *STDOUT{IO};

    my $last_check_spanel_log_time;
    my ($spanel_http_log_name, $spanel_https_log_name);

    my $last_update_output_time;
    my $last_unblock_time;
    my $last_reload_data_time;
    my $num_lines = 0;

    _init();

    local *INPUT;
    if (defined $Opts{spanel_site}) {
        require Tie::Handle::TailSwitch;
        my $dir = "/s/$Opts{spanel_site}/syslog";
        tie *INPUT, 'Tie::Handle::TailSwitch', (
            globs => ["$dir/https_access.*.log", "$dir/http_access.*.log"],
        );
    } else {
        *INPUT = \*STDIN;
    }

  LINE:
    while (1) {
        my $line = <INPUT>;
        if (!defined($line) || !length($line)) {
            sleep 0.5;
            next;
        }

        my $now = time();
        $num_lines++;
        chomp $line;
        $line =~ /\A($RE{ipv4})\s/ or do {
            warn "$PROG: Line '$line': Can't parse IP address, skipped\n";
            next;
        };
        my $ip = $1;
        next if $Blocked{$ip};

      OUTPUT:
        {
            last unless !$last_update_output_time ||
                $last_update_output_time <= $now-2;
            print "\e[2J\e[;H"; # clear screen + put cursor at top (0,0)
            printf "Blocked IPs: %s%4d%s | Log lines: %s%6d%s | Running for: %s%s%s\n",
                color('bold'), (scalar keys %Blocked), color('reset'),
                color('bold'), $num_lines, color('reset'),
                color('bold'), Time::Duration::concise(Time::Duration::duration($now-$^T, 2)), color('reset');
            $last_update_output_time = $now;
            printf "Top IPs:\n";

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 2.108 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-48ebf85a1963 )