Updated roagen.php - aligned test better

This commit is contained in:
netravnen 2018-12-14 17:29:19 +00:00
parent ebc4055b81
commit f355060e9a

View file

@ -1,12 +1,12 @@
<?php <?php
// Before we begin. Ensure registry repository is up-to-date // Before we begin. Ensure registry repository is up-to-date
echo shell_exec("/usr/bin/git -C ../registry/ pull origin master:master 2>&1"); echo shell_exec ("/usr/bin/git -C ../registry/ pull origin master:master 2>&1");
/* /*
* Function: * Function:
* startsWith ($string, "word", $length) * startsWith ($string, "word", $length)
* *
* Find lines beginning with "word". Optionally * Find lines beginning with "word". Optionally
* give the length of the string you are looking for. * give the length of the string you are looking for.
*/ */
@ -20,8 +20,8 @@ function startsWith ($haystack, $needle, $length = "0")
/* /*
* Function: * Function:
* endsWith ($string, "word") * endsWith ($string, "word")
* *
* Find lines ending with "word". * Find lines ending with "word".
*/ */
function endsWith ($haystack, $needle) function endsWith ($haystack, $needle)
@ -36,8 +36,8 @@ function endsWith ($haystack, $needle)
/* /*
* Function: * Function:
* trim_special_chars ($string) * trim_special_chars ($string)
* *
* Remove special characters. * Remove special characters.
*/ */
function trim_special_chars ($string) function trim_special_chars ($string)
@ -49,8 +49,8 @@ function trim_special_chars ($string)
$roas = array(); $roas = array();
// Set folders we need to scan. // Set folders we need to scan.
$files6 = scandir("../registry/data/route6/"); $files6 = scandir ("../registry/data/route6/");
$files4 = scandir("../registry/data/route/"); $files4 = scandir ("../registry/data/route/");
/* /*
* *
@ -67,7 +67,7 @@ foreach ($files6 as $file)
/* /*
* route6 with maxLength value set: * route6 with maxLength value set:
* - fd42:5d71:219::/48 * - fd42:5d71:219::/48
* *
* $ cat ../registry/data/route6/fd42:5d71:219::_48 * $ cat ../registry/data/route6/fd42:5d71:219::_48
* route6: fd42:5d71:219::/48 * route6: fd42:5d71:219::/48
* origin: AS4242420119 * origin: AS4242420119
@ -75,18 +75,18 @@ foreach ($files6 as $file)
* mnt-by: JRB0001-MNT * mnt-by: JRB0001-MNT
* source: DN42 * source: DN42
*/ */
$data = file("../registry/data/route6/$file"); $data = file ("../registry/data/route6/$file");
foreach ($data as $str) foreach ($data as $str)
{ {
$str = trim_special_chars ($str); $str = trim_special_chars ($str);
if (startsWith ($str, ("max"), 3)) $raw_array[$i]["max"] = $str; if (startsWith ($str, "max", 3)) $raw_array[$i]["max"] = $str;
elseif (startsWith ($str, ("source"), 6)) $raw_array[$i]["source"] = $str; elseif (startsWith ($str, "source", 6)) $raw_array[$i]["source"] = $str;
elseif (startsWith ($str, ("route"), 5)) $raw_array[$i]["route"] = $str; elseif (startsWith ($str, "route", 5)) $raw_array[$i]["route"] = $str;
elseif (startsWith ($str, ("origin"), 6)) $raw_array[$i]["asn"][$j++] = $str; elseif (startsWith ($str, "origin", 6)) $raw_array[$i]["asn"][$j++] = $str;
// Catch max-length not set in route object. // Catch max-length not set in route object.
if (empty ($raw_array[$i]["max"])) $raw_array[$i]["max"] = -1; if (empty ($raw_array[$i]["max"])) $raw_array[$i]["max"] = -1;
} }
@ -100,45 +100,46 @@ foreach ($raw_array as $sub_array)
// Extract prefix and subnet size // Extract prefix and subnet size
// Match prefix sizes 29-64, 80. // Match prefix sizes 29-64, 80.
$prefix = array(); $prefix = array();
preg_match("/([a-f0-9\:]{0,128})\/(29|[3-5][0-9]|6[0-4]|80)/", preg_match ("/([a-f0-9\:]{0,128})\/(29|[3-5][0-9]|6[0-4]|80)/",
explode("6: ", $sub_array["route"])[1], explode ("6: ", $sub_array["route"])[1],
$prefix); $prefix);
// Extract ta information // Extract ta information
$source = array(); $source = array();
preg_match ("/([A-Z0-4]+)/", preg_match ("/([A-Z0-4]+)/",
explode(":", $sub_array["source"])[1], explode (":", $sub_array["source"])[1],
$source); $source);
// Try to extract max-length information // Try to extract max-length information
$maxlength = array(); $maxlength = array();
if (($sub_array["max"]) != -1) if (($sub_array["max"]) != -1)
preg_match ("/([0-9]+)/", preg_match ("/([0-9]+)/",
explode(":", $sub_array["max"])[1], explode (":", $sub_array["max"])[1],
$maxlength); $maxlength);
// Store extracted values // Store extracted values
$_prefix = $prefix[0]; $_prefix = $prefix[0];
$_ta = (isset ($source[0]) ? $source[0] : ""); $_ta = (isset ($source[0]) ? $source[0] : "");
// We need to do conditional setting of maxLength to avoid errornous output. // We need to do conditional setting of maxLength to avoid errornous output.
if (($sub_array["max"]) != -1) if (($sub_array["max"]) != -1)
$_maxlength = (isset ($maxlength[0]) ? $maxlength[0] : ""); $_maxlength = (isset ($maxlength[0]) ? $maxlength[0] : "");
else else
// Do fallback to default prefix size if max-length was not set. // Do fallback to default prefix size if max-length was not set.
$_maxlength = $prefix[2]; $_maxlength = $prefix[2];
// Loop through each asn in single route6 object and assign // Loop through each asn in single route6 object and assign
// other values accordingly. // other values accordingly.
foreach ($sub_array["asn"] as $asn) foreach ($sub_array["asn"] as $asn)
{ {
// Extract ASxxxxx from string. // Extract ASxxxxx from string.
preg_match ("/AS[0-9]+/", explode (":", $asn)[1], $_asn); preg_match ("/AS[0-9]+/", explode (":", $asn)[1], $_asn);
$roas["roas"][$k]["asn"] = $_asn[0]; $roas["roas"][$k]["asn"] = $_asn[0];
$roas["roas"][$k]["prefix"] = $_prefix; $roas["roas"][$k]["prefix"] = $_prefix;
$roas["roas"][$k]["maxLength"] = $_maxlength; $roas["roas"][$k]["maxLength"] = $_maxlength;
$roas["roas"][$k]["ta"] = $_ta; $roas["roas"][$k]["ta"] = $_ta;
$k++; $k++;
} }
} }
@ -150,7 +151,7 @@ foreach ($raw_array as $sub_array)
*/ */
$i = 0; // Counter used with tmp $raw_array. $i = 0; // Counter used with tmp $raw_array.
$raw_array = array(); // tmp array() used for storing data to be processed $raw_array = array(); // tmp array() used for storing data to be processed
foreach ($files4 as $file) foreach ($files4 as $file)
{ {
$j = 0; $j = 0;
@ -158,7 +159,7 @@ foreach ($files4 as $file)
/* /*
* route with maxLength value set: * route with maxLength value set:
* - 172.20.1.0/24 * - 172.20.1.0/24
* *
* $ cat ../registry/data/route/172.20.1.0_24 * $ cat ../registry/data/route/172.20.1.0_24
* route: 172.20.1.0/24 * route: 172.20.1.0/24
* origin: AS4242420119 * origin: AS4242420119
@ -166,18 +167,18 @@ foreach ($files4 as $file)
* mnt-by: JRB0001-MNT * mnt-by: JRB0001-MNT
* source: DN42 * source: DN42
*/ */
$data = file("../registry/data/route/$file"); $data = file ("../registry/data/route/$file");
foreach ($data as $str) foreach ($data as $str)
{ {
$str = trim_special_chars ($str); $str = trim_special_chars ($str);
if (startsWith ($str, ("max"), 3)) $raw_array[$i]["max"] = $str; if (startsWith ($str, "max", 3)) $raw_array[$i]["max"] = $str;
elseif (startsWith ($str, ("source"), 6)) $raw_array[$i]["source"] = $str; elseif (startsWith ($str, "source", 6)) $raw_array[$i]["source"] = $str;
elseif (startsWith ($str, ("route"), 5)) $raw_array[$i]["route"] = $str; elseif (startsWith ($str, "route", 5)) $raw_array[$i]["route"] = $str;
elseif (startsWith ($str, ("origin"), 6)) $raw_array[$i]["asn"][$j++] = $str; elseif (startsWith ($str, "origin", 6)) $raw_array[$i]["asn"][$j++] = $str;
// Catch max-length not set in route object. // Catch max-length not set in route object.
if (empty ($raw_array[$i]["max"])) $raw_array[$i]["max"] = -1; if (empty ($raw_array[$i]["max"])) $raw_array[$i]["max"] = -1;
} }
@ -189,58 +190,59 @@ foreach ($raw_array as $sub_array)
// Extract prefix and subnet size // Extract prefix and subnet size
// Match prefix sizes 8-32. // Match prefix sizes 8-32.
$prefix = array(); $prefix = array();
preg_match("/([0-9\.]{7,15})\/([8-9]|[1-2][0-9]|3[0-2])/", preg_match ("/([0-9\.]{7,15})\/([8-9]|[1-2][0-9]|3[0-2])/",
explode(":", $sub_array["route"])[1], explode (":", $sub_array["route"])[1],
$prefix); $prefix);
// Extract ta information // Extract ta information
$source = array(); $source = array();
preg_match ("/([A-Z0-4]+)/", preg_match ("/([A-Z0-4]+)/",
explode(":", $sub_array["source"])[1], explode (":", $sub_array["source"])[1],
$source); $source);
// Try to extract max-length information // Try to extract max-length information
$maxlength = array(); $maxlength = array();
if (($sub_array["max"]) != -1) if (($sub_array["max"]) != -1)
preg_match ("/([0-9]+)/", preg_match ("/([0-9]+)/",
explode(":", $sub_array["max"])[1], explode (":", $sub_array["max"])[1],
$maxlength); $maxlength);
// Store extracted values // Store extracted values
$_prefix = $prefix[0]; $_prefix = $prefix[0];
$_ta = (isset ($source[0]) ? $source[0] : ""); $_ta = (isset ($source[0]) ? $source[0] : "");
// We need to do conditional setting of maxLength to avoid errornous output. // We need to do conditional setting of maxLength to avoid errornous output.
if (($sub_array["max"]) != -1) if (($sub_array["max"]) != -1)
$_maxlength = (isset ($maxlength[0]) ? $maxlength[0] : ""); $_maxlength = (isset ($maxlength[0]) ? $maxlength[0] : "");
else else
// Do fallback to default prefix size if max-length was not set. // Do fallback to default prefix size if max-length was not set.
$_maxlength = $prefix[2]; $_maxlength = $prefix[2];
// Loop through each asn in single route6 object and assign // Loop through each asn in single route6 object and assign
// other values accordingly. // other values accordingly.
foreach ($sub_array["asn"] as $asn) foreach ($sub_array["asn"] as $asn)
{ {
// Extract ASxxxxx from string. // Extract ASxxxxx from string.
preg_match ("/AS[0-9]+/", explode (":", $asn)[1], $_asn); preg_match ("/AS[0-9]+/", explode (":", $asn)[1], $_asn);
$roas["roas"][$k]["asn"] = $_asn[0]; $roas["roas"][$k]["asn"] = $_asn[0];
$roas["roas"][$k]["prefix"] = $_prefix; $roas["roas"][$k]["prefix"] = $_prefix;
$roas["roas"][$k]["maxLength"] = $_maxlength; $roas["roas"][$k]["maxLength"] = $_maxlength;
$roas["roas"][$k]["ta"] = $_ta; $roas["roas"][$k]["ta"] = $_ta;
$k++; $k++;
} }
} }
// Do JSON encoding before writing result to file // Do JSON encoding before writing result to file
$json = json_encode($roas, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT); $json = json_encode ($roas, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
// Write JSON to file // Write JSON to file
$fp = fopen('dn42-rpki-export.json', 'w'); $fp = fopen ('dn42-rpki-export.json', 'w');
fwrite($fp, $json); fwrite ($fp, $json);
fclose($fp); fclose ($fp);
// Commit and push to all git remote repositories // Commit and push to all git remote repositories
echo shell_exec("./update.sh 2>&1"); echo shell_exec ("./update.sh 2>&1");
?> ?>