How to block all internet traffic from China

The Perl module IP::China provides a way to block all web accesses from China using a Perl script. Here is an example CGI script which demonstrates sending a "forbidden" response if the remote user address is from China.
#!/usr/bin/perl
use warnings;
use strict;
use IP::China 'chinese_ip';

my $remote_addr = $ENV{REMOTE_ADDR};
if ($remote_addr) {
    if (chinese_ip ($remote_addr)) {
        print <<EOF;
Content-Type: text/plain
Status: 403

Sorry!
EOF
        exit;
    }
    else {
        # Provide another response.
    }
}

(download)

You can test it by running it with a Chinese internet address:
REMOTE_ADDR=182.118.20.173 perl block-china.pl
It will print out
Content-Type: text/plain
Status: 403

Sorry!
At the Github repository there is a version in C as well. This Perl module is based on GeoLite Free Downloadable Databases.

Web links


Copyright © Ben Bullock 2009-2024. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com). / Privacy / Disclaimer