2021-02-21 16:37:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
use App\BOB;
|
|
|
|
use Amp\Parallel\Worker\Environment;
|
|
|
|
use Amp\Parallel\Worker\Task;
|
|
|
|
use Amp\Parallel\Worker\TaskFailureError;
|
|
|
|
use Amp\Parallel\Worker\TaskFailureException;
|
|
|
|
use Amp\Parallel\Worker\TaskFailureThrowable;
|
|
|
|
|
2022-06-07 19:04:15 +00:00
|
|
|
class Checker implements Task {
|
2021-02-21 16:37:27 +00:00
|
|
|
private $options = [];
|
|
|
|
private $base32;
|
|
|
|
|
2022-06-07 19:04:15 +00:00
|
|
|
public function __construct($base32, $options = []) {
|
2021-02-21 16:37:27 +00:00
|
|
|
$this->options = array_merge($this->options, (array) $options);
|
|
|
|
$this->base32 = $base32;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Environment $environment
|
|
|
|
* @return \Amp\Promise|\Generator|mixed
|
|
|
|
*/
|
2022-06-07 19:04:15 +00:00
|
|
|
public function run(Environment $environment) {
|
2021-02-21 16:37:27 +00:00
|
|
|
$bob = new BOB($this->options);
|
|
|
|
$bob->getnick();
|
2021-03-09 11:56:35 +00:00
|
|
|
|
2022-06-07 19:04:15 +00:00
|
|
|
$ts = 0;
|
2022-06-06 17:57:49 +00:00
|
|
|
for ($i = 0, $tries = $this->options['check_tries'], $sleep = $this->options['retry_delay']; $i < $tries; ++$i) {
|
2021-03-09 11:56:35 +00:00
|
|
|
$result = $bob->lookuplocal($this->base32 . ".b32.i2p");
|
|
|
|
if(!$result) {
|
|
|
|
$result = $bob->lookup($this->base32 . ".b32.i2p");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result) {
|
2022-06-07 19:04:15 +00:00
|
|
|
$ts = time();
|
2021-03-09 11:56:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-06-06 17:57:49 +00:00
|
|
|
|
|
|
|
// sleep before next try if we must do more that 1 tries to lookup destination
|
|
|
|
if($tries > 1 && $i < $tries) // do not sleep if that is last try
|
|
|
|
sleep($sleep);
|
2021-03-09 11:56:35 +00:00
|
|
|
}
|
2021-02-21 16:37:27 +00:00
|
|
|
|
|
|
|
$bob = null;
|
2022-06-07 19:04:15 +00:00
|
|
|
echo "Processed " . $this->base32 . ": " . ($ts ? "online" : "offline") . PHP_EOL;
|
|
|
|
return $ts;
|
2021-02-21 16:37:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|