2021-02-21 16:37:27 +00:00
< ? php
require __DIR__ . '/vendor/autoload.php' ;
require __DIR__ . '/config.php' ;
use App\BOB ;
use App\DB ;
use Amp\Loop ;
use Amp\Parallel\Worker\DefaultPool ;
2023-02-19 11:46:13 +00:00
use Amp\TimeoutException ;
2021-02-21 16:37:27 +00:00
/* Disable execution limit */
set_time_limit ( 0 );
$pdo = ( new DB ( $options )) -> pdo ;
2023-02-19 11:46:13 +00:00
$lockfile = __DIR__ . '/checker.lock' ;
$full = false ;
2021-02-21 16:37:27 +00:00
$results = [];
2023-02-19 11:46:13 +00:00
if ( $argc == 2 && $argv [ 1 ] == " full " ) {
$full = true ;
}
echo " [CHECKER] Checking lockfile " . PHP_EOL ;
$fp = fopen ( $lockfile , 'c' );
if ( ! flock ( $fp , LOCK_EX | LOCK_NB )) {
echo " [CHECKER] Looks like another checker is running, exiting... " . PHP_EOL ;
exit ( - 1 );
}
2022-06-06 17:57:49 +00:00
echo " [DB] Fetching hosts to check from database " . PHP_EOL ;
2023-02-19 11:46:13 +00:00
if ((( $options [ 'fullhour' ] >= 0 ) && ( $options [ 'fullhour' ] <= 23 )) && date ( 'H' ) == $options [ 'fullhour' ] || $full ) { // check all hosts at full check hour or when argument 'full' passed
echo " [CHECKER] Starting FULL check " . PHP_EOL ;
2021-04-22 13:15:11 +00:00
$STH = $pdo -> query ( " SELECT `host`, `base32` FROM `hosts` " );
2022-06-07 19:04:15 +00:00
2021-04-22 13:15:11 +00:00
} else {
$STH = $pdo -> query ( " SELECT `host`, `base32` FROM `hosts` WHERE `disabled` = '0' " );
}
2021-02-21 16:37:27 +00:00
$hosts = $STH -> fetchAll ( PDO :: FETCH_KEY_PAIR );
2022-06-06 17:57:49 +00:00
echo " [BOB] Starting session for checking " . PHP_EOL ;
2021-02-21 16:37:27 +00:00
$bob = new BOB ( $options );
2022-06-29 18:38:53 +00:00
if ( $bob -> setnick ()) {
$bob -> options ();
$bob -> newkeys ();
$bob -> intun ();
$bob -> start ();
/* Sleep 10 seconds awaitng tunnels get built */
echo " [BOB] Session started, awaiting 10 seconds for tunnels " . PHP_EOL ;
sleep ( 10 );
} else {
echo " [BOB] Stale session is found, using it " . PHP_EOL ;
$bob -> getnick ();
}
2021-02-21 16:37:27 +00:00
/* Start async checker tasks */
2023-02-19 11:46:13 +00:00
try {
Loop :: run ( function () use ( & $results , $hosts , $options ) {
$pool = new DefaultPool ( $options [ 'checkthreads' ]);
2021-02-21 16:37:27 +00:00
2023-02-19 11:46:13 +00:00
$coroutines = [];
2021-02-21 16:37:27 +00:00
2023-02-19 11:46:13 +00:00
foreach ( $hosts as $host => $base32 ) {
$coroutines [ $host ] = Amp\call ( function () use ( $pool , $base32 , $options ) {
return yield $pool -> enqueue ( new App\Checker ( $base32 , $options ));
});
}
$results = yield Amp\Promise\all ( $coroutines );
2021-02-21 16:37:27 +00:00
2023-02-19 11:46:13 +00:00
return yield $pool -> shutdown ();
});
//} catch (Amp\TimeoutException $ex) {
} catch ( \Throwable $ex ) {
echo " [Checker] Exception: " . $ex . PHP_EOL ;
}
2021-02-21 16:37:27 +00:00
2022-06-07 19:04:15 +00:00
echo " [BOB] Stopping session " . PHP_EOL ;
2021-02-21 16:37:27 +00:00
$bob -> stop ();
$bob -> clear ();
$bob = null ;
2022-06-07 19:04:15 +00:00
echo " [DB] Saving check results " . PHP_EOL ;
2021-02-21 16:37:27 +00:00
$i = 0 ;
$pdo -> beginTransaction ();
2022-06-07 19:04:15 +00:00
foreach ( $results as $host => $ts ) {
if ( $ts ) {
$pdo -> exec ( " UPDATE `hosts` SET `last_seen` = FROM_UNIXTIME( " . $ts . " ), `disabled` = 0, `hidden` = 0 WHERE `host` = ' " . $host . " ' " );
2021-02-21 16:37:27 +00:00
$i ++ ;
}
}
$pdo -> commit ();
2023-02-19 11:46:13 +00:00
echo " [CHECKER] Unlocking and closing lockfile " . PHP_EOL ;
fclose ( $fp );
echo " [CHECKER] Result: Total hosts: " . count ( $results ) . " , Alive: " . $i . PHP_EOL ;