Subversion Repositories vgwhois

Rev

Rev 5 | Rev 77 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/php
<?php

#
#  VGWhoIs (ViaThinkSoft Global WhoIs, a fork of generic Whois / gwhois)
#  Maintenance / Developer utilities
#
#  (c) 2012-2019 by Daniel Marschall, ViaThinkSoft <info@daniel-marschall.de>
#
#  License: https://www.gnu.org/licenses/gpl-2.0.html (GPL version 2)
#

# TODO: also use extended stats of the RIRs, which may have avails / reserved?

error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED);

require_once __DIR__ . '/config.inc.php';
require_once __DIR__ . '/../../shared/php_includes/common_functions.inc.php';
require_once __DIR__ . '/rirs.inc.php';

echo "ASN Preparation: Get IANA delegation file\n";

$iana_asn_data = explode("\n", cached_file(IANA_AS_NUMBERS, CACHE_FILE_DIR));
$iana_asn_data = array_map('trim', $iana_asn_data);

echo "ASN: Parsing...\n";

$version = trim(str_replace('-', '', $iana_asn_data[3]));

$iana_asn_data = grep($iana_asn_data, 'whois');

$iana_asn_data = implode("\n", $iana_asn_data);

$iana_asn_data = str_replace('Assigned by RIPE NCC', '', $iana_asn_data);
$iana_asn_data = str_replace('Assigned by AFRINIC',  '', $iana_asn_data); // do not change to 'AfriNIC' !
$iana_asn_data = str_replace('Assigned by LACNIC',   '', $iana_asn_data);
$iana_asn_data = str_replace('Assigned by APNIC',    '', $iana_asn_data);
$iana_asn_data = str_replace('Assigned by ARIN',     '', $iana_asn_data);

while (strpos($iana_asn_data, '  ') !== false) {
        $iana_asn_data = str_replace('  ', ' ', $iana_asn_data);
}

$out = '';

$ary = explode("\n", $iana_asn_data);

$res = array();

foreach ($ary as &$x) {
        $x = trim($x);
        if ($x == '') continue;
        $bry = explode(" ", $x);

        $asnums = $bry[0];
        $whois  = $bry[1];

        if (strpos($asnums, '-') === false) {
                # Single ASN
                $res[$whois][$asnums] = $asnums;
        } else {
                # ASN range
                $tmp = explode('-', $asnums, 2);
                $res[$whois][$tmp[0]] = $tmp[1];
        }
}

$out .= "#: version $version\n\n";

$out .= "# Autonomous System Numbers (ASN)\n";
$out .= "# Automatically generated by ".__DIR__."/vgwhois-pattern-update\n";
$out .= "# Generation timestamp: ".date('Y-m-d H:i:s \G\M\TO')."\n";
$out .= "# Source: ($version) ".IANA_AS_NUMBERS."\n";
$out .= "\n";

foreach ($res as $whois => &$x) {
        $rir = array_search($whois, $rir_whois_server);

        if ($asn_additional_params[$rir] != '') {
                $whois .= '|prefix='.$asn_additional_params[$rir].'|';
        }

        $out .= "# ".strtoupper($rir)."\n";
        $out .= ":whois|$whois\n";

        foreach ($x as $begin => &$end) {
                if ($begin == $end) {
                        # Single ASN
                        $out .= "*AS:$begin\n";
                } else {
                        # ASN Range
                        $out .= "*AS:$begin-$end\n";
                }
        }
        $out .= "\n";
}

$out .= "# Last resort: ARIN\n";
$out .= ":whois|whois.arin.net|prefix=a |\n";
$out .= "^as(.*)$\n";

echo "ASN: Write to output file\n";

$h = fopen(PATTERN_DIR.'/asn', 'w') or die('Error opening file '.PATTERN_DIR.'/asn');
fwrite($h, $out) or die('Could not write to output file');
fclose($h);

echo "ASN Finished\n";