fevangelou
6/29/2016 - 10:32 AM

CIDR lists

CIDR lists

<?php

echo "Downloading...\n";

copy("http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip","/tmp/GeoIPCountryCSV.zip");
$f=fopen('zip:///tmp/GeoIPCountryCSV.zip#GeoIPCountryWhois.csv','r');

echo "Generating";

$data=NULL;
$c=0;

while (($line = fgetcsv($f,0,',')) !== FALSE) {
  $country=$line[4];
  if ((($c++) % 1000==0)) echo ".";
  $mask=round(32-log(ip2long($line[1])-ip2long($line[0]),2));å
  if (strpos($mask,'INF')!== false) $mask='32';
  $data[$country][]="$line[0]/$mask\n";
}

echo "\n";

foreach ($data as $country => $ranges) {
  echo "Writing $country\n";
  array_unshift($ranges,"# Generated ".date('r')."\n");
  file_put_contents("$country.cidr",$ranges);
}