|
|
|
<?php
|
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
require __DIR__ . '/config.php';
|
|
|
|
|
|
|
|
use App\BOB;
|
|
|
|
use App\DB;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Parallel\Worker\DefaultPool;
|
|
|
|
|
|
|
|
/* Disable execution limit */
|
|
|
|
set_time_limit(0);
|
|
|
|
|
|
|
|
$pdo = (new DB($options))->pdo;
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
|
|
|
|
echo "[DB] Fetching hosts to check from database" . PHP_EOL;
|
|
|
|
if ((($options['fullhour'] >= 0) && ($options['fullhour'] <= 23)) && date('H') == $options['fullhour']) { // check all hosts at full check hour
|
|
|
|
$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 "[BOB] Starting session for checking" . PHP_EOL;
|
|
|
|
$bob = new BOB($options);
|
|
|
|
$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);
|
|
|
|
|
|
|
|
/* Start async checker tasks */
|
|
|
|
Loop::run(function () use (&$results, $hosts, $options) {
|
|
|
|
$pool = new DefaultPool(64);
|
|
|
|
|
|
|
|
$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();
|
|
|
|
});
|
|
|
|
|
|
|
|
echo "[BOB] Stopping session" . PHP_EOL;
|
|
|
|
$bob->stop();
|
|
|
|
$bob->clear();
|
|
|
|
$bob = null;
|
|
|
|
|
|
|
|
|
|
|
|
echo "[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 "Result: Total hosts: " . count($results) . ", Alive: " . $i . PHP_EOL;
|