Domain registry project http://reg.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 lines
2.9 KiB

<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/config.php';
use App\BOB;
use App\DB;
use Amp\Loop;
use Amp\Parallel\Worker\DefaultPool;
use Amp\TimeoutException;
/* Disable execution limit */
set_time_limit(0);
$pdo = (new DB($options))->pdo;
$lockfile = __DIR__ . '/checker.lock';
$full = false;
$results = [];
if($argc == 2 && $argv[1] == "full") {
$full = true;
}
echo date("H:i:s") . " [CHECKER] Checking lockfile" . PHP_EOL;
$fp = fopen($lockfile, 'c');
if(!flock($fp, LOCK_EX | LOCK_NB)) {
echo date("H:i:s") . " [CHECKER] Looks like another checker is running, exiting..." . PHP_EOL;
exit(-1);
}
echo date("H:i:s") . " [DB] Fetching hosts to check from database" . PHP_EOL;
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 date("H:i:s") . " [CHECKER] Starting FULL check" . PHP_EOL;
$STH = $pdo->query("SELECT `host`, `base32` FROM `hosts`");
} else {
$STH = $pdo->query("SELECT `host`, `base32` FROM `hosts` WHERE `disabled` = '0'");
}
$hosts = $STH->fetchAll(PDO::FETCH_KEY_PAIR);
echo date("H:i:s") . " [BOB] Starting session for checking" . PHP_EOL;
$bob = new BOB($options);
if ($bob->setnick()) {
$bob->options();
$bob->newkeys();
$bob->intun();
$bob->start();
/* Sleep 10 seconds awaitng tunnels get built */
echo date("H:i:s") . " [BOB] Session started, awaiting 10 seconds for tunnels" . PHP_EOL;
sleep(10);
} else {
echo date("H:i:s") . " [BOB] Stale session is found, using it" . PHP_EOL;
$bob->getnick();
}
/* Start async checker tasks */
try {
Loop::run(function () use (&$results, $hosts, $options) {
$pool = new DefaultPool($options['checkthreads']);
$coroutines = [];
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);
return yield $pool->shutdown();
});
//} catch (Amp\TimeoutException $ex) {
} catch (\Throwable $ex) {
echo date("H:i:s") . " [Checker] Exception: " . $ex . PHP_EOL;
}
echo date("H:i:s") . " [BOB] Stopping session" . PHP_EOL;
$bob->stop();
$bob->clear();
$bob = null;
echo date("H:i:s") . " [DB] Saving check results" . PHP_EOL;
$i = 0;
$pdo->beginTransaction();
foreach ($results as $host => $ts) {
if($ts) {
$pdo->exec("UPDATE `hosts` SET `last_seen` = FROM_UNIXTIME(" . $ts . "), `disabled` = 0, `hidden` = 0 WHERE `host` = '" . $host . "'");
$i++;
}
}
$pdo->commit();
echo date("H:i:s") . " [CHECKER] Unlocking and closing lockfile" . PHP_EOL;
fclose($fp);
echo date("H:i:s") . " [CHECKER] Result: Total hosts: " . count($results) . ", Alive: " . $i . PHP_EOL;