Creating a .htaccess deny list from okean.com data

| No Comments
I want to keep the comments open on this blog, but I keep getting hit with tons of comment spam, particularly from China.  Okean.com has an excellent list of Chinese IP blocks at http://www.okean.com/antispam/china.html.  The following script reads the data file and formats the CIDR's into .htaccess "deny from" format.  The output can be pasted into your .htacces file.  You do need to have an "order allow, deny" at the start and "allow from all" at the end!
The script stacks the IP addresses up until the $line_max limit is reached.  You can set it as high
as 8190, but I prefer shorter lines.  The default is 100.

The site owner does ask that you hit the data no more than one in 12 hours, which is very reasonable since it does not change very often.

# create_deny.pl - create .htaccess deny lines from Okean format CIDR list

use strict;
use warnings;
use LWP::Simple;

my $url      = shift || 'http://www.okean.com/chinacidr.txt';
my $line_max = shift || 100;

my $cidr_list = get($url)
    or die "Unable to get $url!\n";

my $line_start = 'deny from';
my $line = $line_start;

print "# Start $url\n";

CIDR_LINE:
for (split /\n/, $cidr_list) {
    next CIDR_LINE if $_ =~ /\s*\#/;
    next CIDR_LINE if $_ =~ /^$/;
    my ($cidr) = split / /;
    if (length($line) + length($cidr) + 1 > $line_max) {
        print $line, "\n";
        $line = $line_start;
    }
    $line .= " $cidr";
}

if ($line ne $line_start) {
    print $line, "\n";
}

print "# End $url\n";

exit 0;

Leave a comment

About this Entry

This page contains a single entry by Bill Ruppert published on April 16, 2013 12:26 PM.

Updated Net::Google::Analytics Example was the previous entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.38