1
0
Fork 0
dn42-roagen/roagen.php

212 lines
6.9 KiB
PHP
Raw Normal View History

<?php
// Before we begin. Ensure registry repository is up-to-date
2018-12-10 17:44:12 +00:00
echo shell_exec("/usr/bin/git -C ../registry/ pull origin master:master 2>&1");
/*
* Function:
* startsWith( $string, 'word')
*
* Find lines beginning with 'word'.
*/
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
/*
* Function:
* endsWith( $string, 'word')
*
* Find lines ending with 'word'.
*/
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
// Create empty array $roas
$roas = array();
/*
*
* IPv6 route objects
*
*/
// Set directory for which all files are to be scanned
2018-12-10 17:44:12 +00:00
$files = scandir('../registry/data/route6/');
$h = 0; // count up when reading new file
$i = 0; // used when loop through sub-arrays. I.e. when route objects have more than one listed origin asn
foreach($files as $file)
{
2018-12-10 17:44:12 +00:00
$handle = fopen("../registry/data/route6/$file", "r");
while (($line = fgets($handle)) !== false) {
// extract var $ta
if (startsWith($line,'source')) {
$source = array();
preg_match('/([A-Z0-4]+)/',explode(':', $line)[1], $source);
$ta = $source[1];
}
// extract var $route
// extract var $maskLength
if (startsWith($line,'route6')) {
$prefix = array();
$line2 = explode('6: ', $line);
// validate v6 netmasks with following cmd
2018-12-10 17:44:12 +00:00
// cmd: ls -1 ../registry/data/route6/ | egrep -o "\b\_[0-9]+\b" | sed 's/\_//' | sort -n | uniq
preg_match('/([a-f0-9\:]{0,128})\/(32|40|4[4-9]|5[0-9]|6[0-4]|80)/', $line2[1], $prefix);
$route = $prefix[0];
$mask = $prefix[2];
}
2018-12-10 18:31:49 +00:00
// extract var $maxLength
2018-12-10 20:17:25 +00:00
/*if (startsWith($line,'max-length')) {
2018-12-10 18:31:49 +00:00
$maxLength = array();
2018-12-10 18:40:40 +00:00
preg_match('/([0-9]+)/',explode(':', $line)[1], $maxLength);
2018-12-10 19:40:44 +00:00
$maxLength = $maxLength[1];
} else {
$maxLength = '';
2018-12-10 20:17:25 +00:00
}*/
// extract var $asn
if (startsWith($line, 'origin')) {
$asn = array();
preg_match('/AS[0-9]+/', explode(':', $line)[1], $asn);
if (count($asn) > 1) {
foreach ($asn as $key => $value) {
if (!empty($value)) {
$roas['roas'][$i]['asn'] = $asn[0];
$roas['roas'][$i]['prefix'] = $route;
2018-12-10 20:17:25 +00:00
/*if (isset($ta)) {
2018-12-10 19:12:12 +00:00
if ($ta == 'DN42') {
$mask = ($mask <= 64 ? '64' : $mask);
2018-12-10 19:12:12 +00:00
if ($maxLength != '') $mask = $maxLength;
}
} else {
$ta = 'NULL';
2018-12-10 20:17:25 +00:00
}*/
2018-12-10 18:40:40 +00:00
$roas['roas'][$i]['maxLength'] = $mask;
2018-12-10 20:20:22 +00:00
$roas['roas'][$i]['ta'] = (empty($ta) ? '' : $ta);
$i++;
}
}
} else {
$roas['roas'][$i]['asn'] = $asn[0];
$roas['roas'][$i]['prefix'] = $route;
2018-12-10 20:17:25 +00:00
/*if (isset($ta)) {
2018-12-10 19:12:12 +00:00
if ($ta == 'DN42') {
$mask = ($mask <= 64 ? '64' : $mask);
2018-12-10 19:12:12 +00:00
if ($maxLength != '') $mask = $maxLength;
}
} else {
$ta = 'NULL';
2018-12-10 20:17:25 +00:00
}*/
2018-12-10 18:40:40 +00:00
$roas['roas'][$i]['maxLength'] = $mask;
2018-12-10 20:20:22 +00:00
$roas['roas'][$i]['ta'] = (empty($ta) ? '' : $ta);
$i++;
}
}
}
fclose($handle); // close $file when finished reading data
$h++;
}
/*
*
* IPv4 route objects
*
*/
// Set directory for which all files are to be scanned
2018-12-10 17:44:12 +00:00
$files = scandir('../registry/data/route/');
foreach($files as $file)
{
2018-12-10 17:44:12 +00:00
$handle = fopen("../registry/data/route/$file", "r");
while (($line = fgets($handle)) !== false) {
// extract var $ta
if (startsWith($line,'source')) {
$source = array();
preg_match('/([A-Z0-4]+)/',explode(':', $line)[1], $source);
$ta = $source[1];
}
// extract var $route
// extract var $maskLength
if (startsWith($line,'route')) {
$prefix = array();
preg_match('/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(3[0-2]|[0-2]?[0-9])/', explode(':', $line)[1], $prefix);
$route = $prefix[0];
$mask = $prefix[5];
}
2018-12-10 19:12:12 +00:00
// extract var $maxLength
2018-12-10 20:17:25 +00:00
/*$maxLength = ''; // ensure var is null when starting new loop
2018-12-10 18:31:49 +00:00
if (startsWith($line,'max-length')) {
$maxLength = array();
2018-12-10 18:40:40 +00:00
preg_match('/([0-9]+)/',explode(':', $line)[1], $maxLength);
2018-12-10 19:40:44 +00:00
$maxLength = $maxLength[1];
} else {
$maxLength = '';
2018-12-10 20:17:25 +00:00
}*/
// extract var $asn
if (startsWith($line, 'origin')) {
$asn = array();
preg_match('/AS[0-9]+/', explode(':', $line)[1], $asn);
if (count($asn) > 1) {
foreach ($asn as $key => $value) {
if (!empty($value)) {
$roas['roas'][$i]['asn'] = $asn[0];
$roas['roas'][$i]['prefix'] = $route;
2018-12-10 20:17:25 +00:00
/*if (isset($ta)) {
2018-12-10 19:12:12 +00:00
if ($ta == 'DN42') {
$mask = ($mask <= 28 ? '28' : $mask);
2018-12-10 19:12:12 +00:00
if ($maxLength != '') $mask = $maxLength;
}
} else {
$ta = 'NULL';
2018-12-10 20:17:25 +00:00
}*/
2018-12-10 18:40:40 +00:00
$roas['roas'][$i]['maxLength'] = $mask;
2018-12-10 20:20:22 +00:00
$roas['roas'][$i]['ta'] = (empty($ta) ? '' : $ta);
$i++;
}
}
} else {
$roas['roas'][$i]['asn'] = $asn[0];
$roas['roas'][$i]['prefix'] = $route;
2018-12-10 20:17:25 +00:00
/*if (isset($ta)) {
2018-12-10 19:12:12 +00:00
if ($ta == 'DN42') {
$mask = ($mask <= 28 ? '28' : $mask);
2018-12-10 19:12:12 +00:00
if ($maxLength != '') $mask = $maxLength;
}
} else {
$ta = 'NULL';
2018-12-10 20:17:25 +00:00
}*/
2018-12-10 18:40:40 +00:00
$roas['roas'][$i]['maxLength'] = $mask;
2018-12-10 20:20:22 +00:00
$roas['roas'][$i]['ta'] = (empty($ta) ? '' : $ta);
$i++;
}
}
}
fclose($handle); // close $file when finished reading data
$h++;
}
// Do JSON encoding before writing result to file
$json = json_encode($roas, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
// Write JSON to file
$fp = fopen('dn42-rpki-export.json', 'w');
fwrite($fp, $json);
fclose($fp);
2018-12-10 17:55:56 +00:00
// Commit and push to all git remote repositories
echo shell_exec("./update.sh 2>&1");
?>