Rev 5 | Rev 77 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | #!/usr/bin/php |
2 | <?php |
||
3 | |||
4 | # |
||
11 | daniel-mar | 5 | # VGWhoIs (ViaThinkSoft Global WhoIs, a fork of generic Whois / gwhois) |
5 | daniel-mar | 6 | # Maintenance / Developer utilities |
2 | daniel-mar | 7 | # |
5 | daniel-mar | 8 | # (c) 2012-2019 by Daniel Marschall, ViaThinkSoft <info@daniel-marschall.de> |
2 | daniel-mar | 9 | # |
5 | daniel-mar | 10 | # License: https://www.gnu.org/licenses/gpl-2.0.html (GPL version 2) |
2 | daniel-mar | 11 | # |
12 | |||
13 | # TODO: also use extended stats of the RIRs, which may have avails / reserved? |
||
14 | |||
15 | error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED); |
||
16 | |||
17 | require_once __DIR__ . '/config.inc.php'; |
||
18 | require_once __DIR__ . '/../../shared/php_includes/common_functions.inc.php'; |
||
19 | require_once __DIR__ . '/rirs.inc.php'; |
||
20 | |||
21 | echo "ASN Preparation: Get IANA delegation file\n"; |
||
22 | |||
3 | daniel-mar | 23 | $iana_asn_data = explode("\n", cached_file(IANA_AS_NUMBERS, CACHE_FILE_DIR)); |
24 | $iana_asn_data = array_map('trim', $iana_asn_data); |
||
2 | daniel-mar | 25 | |
26 | echo "ASN: Parsing...\n"; |
||
27 | |||
28 | $version = trim(str_replace('-', '', $iana_asn_data[3])); |
||
29 | |||
30 | $iana_asn_data = grep($iana_asn_data, 'whois'); |
||
31 | |||
32 | $iana_asn_data = implode("\n", $iana_asn_data); |
||
33 | |||
34 | $iana_asn_data = str_replace('Assigned by RIPE NCC', '', $iana_asn_data); |
||
35 | $iana_asn_data = str_replace('Assigned by AFRINIC', '', $iana_asn_data); // do not change to 'AfriNIC' ! |
||
36 | $iana_asn_data = str_replace('Assigned by LACNIC', '', $iana_asn_data); |
||
37 | $iana_asn_data = str_replace('Assigned by APNIC', '', $iana_asn_data); |
||
38 | $iana_asn_data = str_replace('Assigned by ARIN', '', $iana_asn_data); |
||
39 | |||
40 | while (strpos($iana_asn_data, ' ') !== false) { |
||
41 | $iana_asn_data = str_replace(' ', ' ', $iana_asn_data); |
||
42 | } |
||
43 | |||
44 | $out = ''; |
||
45 | |||
46 | $ary = explode("\n", $iana_asn_data); |
||
47 | |||
48 | $res = array(); |
||
49 | |||
50 | foreach ($ary as &$x) { |
||
51 | $x = trim($x); |
||
52 | if ($x == '') continue; |
||
53 | $bry = explode(" ", $x); |
||
54 | |||
55 | $asnums = $bry[0]; |
||
56 | $whois = $bry[1]; |
||
57 | |||
58 | if (strpos($asnums, '-') === false) { |
||
59 | # Single ASN |
||
60 | $res[$whois][$asnums] = $asnums; |
||
61 | } else { |
||
62 | # ASN range |
||
63 | $tmp = explode('-', $asnums, 2); |
||
64 | $res[$whois][$tmp[0]] = $tmp[1]; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | $out .= "#: version $version\n\n"; |
||
69 | |||
70 | $out .= "# Autonomous System Numbers (ASN)\n"; |
||
11 | daniel-mar | 71 | $out .= "# Automatically generated by ".__DIR__."/vgwhois-pattern-update\n"; |
2 | daniel-mar | 72 | $out .= "# Generation timestamp: ".date('Y-m-d H:i:s \G\M\TO')."\n"; |
3 | daniel-mar | 73 | $out .= "# Source: ($version) ".IANA_AS_NUMBERS."\n"; |
2 | daniel-mar | 74 | $out .= "\n"; |
75 | |||
76 | foreach ($res as $whois => &$x) { |
||
77 | $rir = array_search($whois, $rir_whois_server); |
||
78 | |||
79 | if ($asn_additional_params[$rir] != '') { |
||
80 | $whois .= '|prefix='.$asn_additional_params[$rir].'|'; |
||
81 | } |
||
82 | |||
83 | $out .= "# ".strtoupper($rir)."\n"; |
||
84 | $out .= ":whois|$whois\n"; |
||
85 | |||
86 | foreach ($x as $begin => &$end) { |
||
87 | if ($begin == $end) { |
||
88 | # Single ASN |
||
89 | $out .= "*AS:$begin\n"; |
||
90 | } else { |
||
91 | # ASN Range |
||
92 | $out .= "*AS:$begin-$end\n"; |
||
93 | } |
||
94 | } |
||
95 | $out .= "\n"; |
||
96 | } |
||
97 | |||
98 | $out .= "# Last resort: ARIN\n"; |
||
99 | $out .= ":whois|whois.arin.net|prefix=a |\n"; |
||
100 | $out .= "^as(.*)$\n"; |
||
101 | |||
102 | echo "ASN: Write to output file\n"; |
||
103 | |||
104 | $h = fopen(PATTERN_DIR.'/asn', 'w') or die('Error opening file '.PATTERN_DIR.'/asn'); |
||
105 | fwrite($h, $out) or die('Could not write to output file'); |
||
106 | fclose($h); |
||
107 | |||
108 | echo "ASN Finished\n"; |