114 lines
3.0 KiB
PHP
Executable File
114 lines
3.0 KiB
PHP
Executable File
#!/usr/bin/php
|
|
<?php
|
|
$mypath="/opt/bgpblist";
|
|
$myurl="https://hosts.funil.de/custom/";
|
|
$mytmp=$mypath."/tmp";
|
|
@mkdir($mytmp);
|
|
if ( !file_exists($mypath."/etc/config.ini") ) {
|
|
echo "ERROR: no config file\n";
|
|
copy($mypath."/etc/default/config.ini",$mypath."/etc/config.ini");
|
|
echo "INFO: file created, edit it before next start\n";
|
|
die();
|
|
}
|
|
$conf=parse_ini_file($mypath."/etc/config.ini",TRUE);
|
|
$customer=$conf["customer"]["cust_uuid"];
|
|
$logfile=$conf["syslog"]["logfile"];
|
|
declare(ticks = 1);
|
|
logtofile("PROCESS_START");
|
|
pcntl_signal(SIGINT,"sig_handler");
|
|
pcntl_signal(SIGTERM,"sig_handler");
|
|
pcntl_signal(SIGHUP,"sig_handler");
|
|
$prev_bad=array();
|
|
$file_size=0;
|
|
$full_url=$myurl.$customer.'/csubscr_aggr.txt';
|
|
$file_loc=$mytmp."/csubscr_aggr.txt";
|
|
clear_routes();
|
|
while(TRUE) {
|
|
$cycle_beg=microtime(TRUE);
|
|
logtofile("CYCLE_BEG");
|
|
mirror_data();
|
|
$raw=@file_get_contents($file_loc);
|
|
$prev_size=$file_size;
|
|
$file_size=strlen($raw);
|
|
if ( strlen($raw) == 0 ) {
|
|
logtofile("EMPTY_REM");
|
|
$sleeptime=10;
|
|
logtofile("SLEEP: ".$sleeptime."s");
|
|
sleep($sleeptime);
|
|
continue;
|
|
} elseif ( $file_size == $prev_size ) {
|
|
logtofile("NO_CHANGE ".$prev_size);
|
|
$sleeptime=10;
|
|
logtofile("SLEEP: ".$sleeptime."s");
|
|
sleep($sleeptime);
|
|
continue;
|
|
}
|
|
$tmp=explode("\n",$raw);
|
|
$bad_nets=array();
|
|
foreach($tmp as $k => $v) {
|
|
if ( strlen(trim($v)) == 0 ) {
|
|
continue;
|
|
}
|
|
if ( ip2long(preg_replace("/\/.*/","",$v)) == 0 || preg_match("/:/",$v) ) {
|
|
unset($tmp[$k]);
|
|
}
|
|
list($net,$mask)=explode("/",$v);
|
|
if ( $mask <= 19 ) {
|
|
unset($tmp[$k]);
|
|
}
|
|
$bad_nets[$v]=TRUE;
|
|
}
|
|
if ( count($bad_nets) == 0 ) {
|
|
logtofile("EMPTY_LST");
|
|
$sleeptime=10;
|
|
logtofile("SLEEP: ".$sleeptime."s");
|
|
sleep($sleeptime);
|
|
continue;
|
|
}
|
|
foreach(array_keys($bad_nets) as $k) {
|
|
if ( !array_key_exists($k,$prev_bad) ) {
|
|
passthru("ip route add prohibit ".$k);
|
|
logtofile("ROUTE + ".$k);
|
|
}
|
|
}
|
|
foreach(array_keys($prev_bad) as $k) {
|
|
if ( !array_key_exists($k,$bad_nets) ) {
|
|
passthru("ip route del prohibit ".$k);
|
|
logtofile("ROUTE - ".$k);
|
|
}
|
|
}
|
|
$prev_bad=$bad_nets;
|
|
gc_collect_cycles();
|
|
$cycle_end=microtime(TRUE);
|
|
logtofile("CYCLE_END");
|
|
$cycle_len=number_format($cycle_end-$cycle_beg,3,".","");
|
|
logtofile("CYCLE_LEN: ".$cycle_len."s");
|
|
$sleeptime=30*ceil($cycle_len);
|
|
if ( $sleeptime < 10 ) { $sleeptime=10; }
|
|
if ( $sleeptime > 300 ) { $sleeptime=300; }
|
|
logtofile("SLEEP: ".$sleeptime."s");
|
|
sleep($sleeptime);
|
|
}
|
|
function mirror_data() {
|
|
global $full_url;
|
|
global $mytmp;
|
|
passthru("cd ".escapeshellcmd($mytmp)."; wget --mirror --no-directories --no-host-directories -q ".escapeshellcmd($full_url));
|
|
}
|
|
function logtofile($text) {
|
|
global $logfile;
|
|
$data=gmdate("Y-m-d H:i:s")." | ".$text."\n";
|
|
file_put_contents($logfile,$data,FILE_APPEND);
|
|
}
|
|
function sig_handler($sig) {
|
|
clear_routes();
|
|
exit_die();
|
|
}
|
|
function clear_routes() {
|
|
logtofile("CLEANING_RT");
|
|
passthru("ip route show | grep '^prohibit ' | awk '{print $2}' | xargs -I '{}' ip route del prohibit '{}'");
|
|
}
|
|
function exit_die() {
|
|
logtofile("EXITING...");
|
|
die();
|
|
}
|