Browse Source

save check result timestamp, reformat code

Signed-off-by: r4sas <r4sas@i2pmail.org>
master
R4SAS 3 years ago
parent
commit
a1149f931d
Signed by: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 17
      checker.php
  2. 56
      lib/bob.php
  3. 16
      lib/checker.php
  4. 9
      lib/db.php
  5. 5
      lib/router.php
  6. 60
      lib/utils.php

17
checker.php

@ -17,6 +17,7 @@ $results = []; @@ -17,6 +17,7 @@ $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'");
}
@ -42,8 +43,7 @@ Loop::run(function () use (&$results, $hosts, $options) { @@ -42,8 +43,7 @@ Loop::run(function () use (&$results, $hosts, $options) {
foreach ($hosts as $host => $base32) {
$coroutines[$host] = Amp\call(function () use ($pool, $base32, $options) {
$result = yield $pool->enqueue(new App\Checker($base32, $options));
return $result;
return yield $pool->enqueue(new App\Checker($base32, $options));
});
}
@ -52,20 +52,19 @@ Loop::run(function () use (&$results, $hosts, $options) { @@ -52,20 +52,19 @@ Loop::run(function () use (&$results, $hosts, $options) {
return yield $pool->shutdown();
});
/* Stop BOB tunnel and terminate */
echo "[BOB] Stopping session" . PHP_EOL;
$bob->stop();
$bob->clear();
$bob = null;
/* Update last seen time in DB */
echo "[DB] Saving check results" . PHP_EOL;
$i = 0;
$pdo->beginTransaction();
foreach ($results as $host => $result)
{
if($result)
{
$pdo->exec("UPDATE `hosts` SET `last_seen` = current_timestamp(), `disabled` = 0, `hidden` = 0 WHERE `host` = '" . $host . "'");
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++;
}
}

56
lib/bob.php

@ -12,32 +12,27 @@ class BOB { @@ -12,32 +12,27 @@ class BOB {
"bob_nick" => "hostchecker",
];
public function __construct(array $options = [])
{
public function __construct(array $options = []) {
ob_implicit_flush();
$this->options = array_merge($this->options, (array) $options);
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->sock === false)
{
if ($this->sock === false) {
throw new \ErrorException("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
}
else
{
} else {
socket_set_option($this->sock, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 10, 'usec' => 10 * 1000]);
socket_set_option($this->sock, SOL_SOCKET, SO_SNDTIMEO, ['sec' => 10, 'usec' => 10 * 1000]);
$result = socket_connect($this->sock, $this->options["bob_host"], $this->options["bob_port"]);
if ($result === false)
{
if ($result === false) {
throw new \ErrorException("socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)));
}
/* Reading BOB greeting */
$response = socket_read($this->sock, 1024);
if(!preg_match('/OK/', $response))
{
if(!preg_match('/OK/', $response)) {
throw new \ErrorException("BOB returned incorrect response on connect or timed out");
}
@ -46,8 +41,7 @@ class BOB { @@ -46,8 +41,7 @@ class BOB {
}
}
public function __destruct()
{
public function __destruct() {
$command = "quit\n";
socket_write($this->sock, $command, strlen($command));
@ -56,8 +50,7 @@ class BOB { @@ -56,8 +50,7 @@ class BOB {
socket_close($this->sock);
}
public function setnick()
{
public function setnick() {
$command = "setnick " . $this->options["bob_nick"] . "\n";
socket_write($this->sock, $command, strlen($command));
@ -66,12 +59,10 @@ class BOB { @@ -66,12 +59,10 @@ class BOB {
echo "BOB response: " . $response;
}
public function options()
{
public function options() {
$options = explode(" ", $this->options["bob_options"]);
foreach($options as $option)
{
foreach($options as $option) {
$command = "option " . $option . "\n";
socket_write($this->sock, $command, strlen($command));
@ -81,8 +72,7 @@ class BOB { @@ -81,8 +72,7 @@ class BOB {
}
}
public function newkeys()
{
public function newkeys() {
$command = "newkeys\n";
socket_write($this->sock, $command, strlen($command));
@ -91,8 +81,7 @@ class BOB { @@ -91,8 +81,7 @@ class BOB {
echo "BOB response: " . $response;
}
public function start()
{
public function start() {
$command = "start\n";
socket_write($this->sock, $command, strlen($command));
@ -101,8 +90,7 @@ class BOB { @@ -101,8 +90,7 @@ class BOB {
echo "BOB response: " . $response;
}
public function intun()
{
public function intun() {
$command = "inhost 127.0.0.1\n";
socket_write($this->sock, $command, strlen($command));
@ -118,8 +106,7 @@ class BOB { @@ -118,8 +106,7 @@ class BOB {
echo "BOB response: " . $response;
}
public function getnick()
{
public function getnick() {
$command = "getnick " . $this->options["bob_nick"] . "\n";
socket_write($this->sock, $command, strlen($command));
@ -128,8 +115,7 @@ class BOB { @@ -128,8 +115,7 @@ class BOB {
echo "BOB response: " . $response;
}
public function lookup(string $address): bool
{
public function lookup(string $address): bool {
$command = "lookup " . $address . "\n";
socket_write($this->sock, $command, strlen($command));
@ -141,8 +127,7 @@ class BOB { @@ -141,8 +127,7 @@ class BOB {
return false;
}
public function lookuplocal(string $address): bool
{
public function lookuplocal(string $address): bool {
$command = "lookuplocal " . $address . "\n";
socket_write($this->sock, $command, strlen($command));
@ -154,8 +139,7 @@ class BOB { @@ -154,8 +139,7 @@ class BOB {
return false;
}
public function stop()
{
public function stop() {
$command = "stop\n";
socket_write($this->sock, $command, strlen($command));
@ -164,8 +148,7 @@ class BOB { @@ -164,8 +148,7 @@ class BOB {
echo "BOB response: " . $response;
}
public function clear()
{
public function clear() {
$command = "clear\n";
socket_write($this->sock, $command, strlen($command));
@ -174,8 +157,7 @@ class BOB { @@ -174,8 +157,7 @@ class BOB {
echo "BOB response: " . $response;
}
public function zap()
{
public function zap() {
$command = "zap\n";
socket_write($this->sock, $command, strlen($command));

16
lib/checker.php

@ -9,13 +9,11 @@ use Amp\Parallel\Worker\TaskFailureError; @@ -9,13 +9,11 @@ use Amp\Parallel\Worker\TaskFailureError;
use Amp\Parallel\Worker\TaskFailureException;
use Amp\Parallel\Worker\TaskFailureThrowable;
class Checker implements Task
{
class Checker implements Task {
private $options = [];
private $base32;
public function __construct($base32, $options = [])
{
public function __construct($base32, $options = []) {
$this->options = array_merge($this->options, (array) $options);
$this->base32 = $base32;
}
@ -24,12 +22,11 @@ class Checker implements Task @@ -24,12 +22,11 @@ class Checker implements Task
* @param Environment $environment
* @return \Amp\Promise|\Generator|mixed
*/
public function run(Environment $environment)
{
public function run(Environment $environment) {
$bob = new BOB($this->options);
$bob->getnick();
$result = false;
$ts = 0;
for ($i = 0, $tries = $this->options['check_tries'], $sleep = $this->options['retry_delay']; $i < $tries; ++$i) {
$result = $bob->lookuplocal($this->base32 . ".b32.i2p");
if(!$result) {
@ -37,6 +34,7 @@ class Checker implements Task @@ -37,6 +34,7 @@ class Checker implements Task
}
if ($result) {
$ts = time();
break;
}
@ -46,8 +44,8 @@ class Checker implements Task @@ -46,8 +44,8 @@ class Checker implements Task
}
$bob = null;
echo "Processed " . $this->base32 . ": " . ($result ? "online" : "offline") . PHP_EOL;
return $result;
echo "Processed " . $this->base32 . ": " . ($ts ? "online" : "offline") . PHP_EOL;
return $ts;
}
}

9
lib/db.php

@ -4,8 +4,7 @@ namespace App; @@ -4,8 +4,7 @@ namespace App;
use PDO;
class DB
{
class DB {
public $pdo;
protected $options = [
@ -15,8 +14,7 @@ class DB @@ -15,8 +14,7 @@ class DB
"db_name" => "regi2p",
];
public function __construct(array $options = [])
{
public function __construct(array $options = []) {
$this->options = array_merge($this->options, (array) $options);
try {
@ -31,8 +29,7 @@ class DB @@ -31,8 +29,7 @@ class DB
}
}
public function __destruct()
{
public function __destruct() {
$pdo = null;
}
}

5
lib/router.php

@ -2,8 +2,7 @@ @@ -2,8 +2,7 @@
namespace App;
class Router
{
class Router {
private $routes;
private $errRoute;
@ -20,9 +19,11 @@ class Router @@ -20,9 +19,11 @@ class Router
if (preg_match($pattern, $_SERVER['REQUEST_URI'], $params)) {
array_shift($params);
array_unshift($params, $_SERVER['REQUEST_URI']);
return call_user_func_array($function, array_values($params));
}
}
return call_user_func($this->errRoute);
}
}

60
lib/utils.php

@ -14,8 +14,7 @@ class Utils { @@ -14,8 +14,7 @@ class Utils {
* @param string $data
* @return string
*/
private static function b32encode(string $data): string
{
private static function b32encode(string $data): string {
if (empty($data)) {
return "";
}
@ -47,8 +46,7 @@ class Utils { @@ -47,8 +46,7 @@ class Utils {
* @param string $data
* @return string
*/
private static function b64decode(string $data): string
{
private static function b64decode(string $data): string {
return base64_decode(strtr($data, static::b64Trans));
}
@ -77,8 +75,7 @@ class Utils { @@ -77,8 +75,7 @@ class Utils {
* @param string $value The value to check.
* @return bool Whether the value is in ASCII character encoding.
*/
public static function isAscii($value): bool
{
public static function isAscii($value): bool {
return ('ASCII' === mb_detect_encoding($value, 'ASCII', true));
}
@ -104,30 +101,25 @@ class Utils { @@ -104,30 +101,25 @@ class Utils {
* @param string $data
* @return bool
*/
public static function isValidBase64(string $data): bool
{
public static function isValidBase64(string $data): bool {
$len = strlen($data);
if($len < 516 || $len > 616)
{
if($len < 516 || $len > 616) {
return false;
}
/* .i2p in string */
if(preg_match('/\.i2p/', $data))
{
if(preg_match('/\.i2p/', $data)) {
return false;
}
/* DSA-SHA1. Will reject them because they are old (length 516) */
if($len == 516 && preg_match('/^[a-zA-Z0-9\-~]+AA$/', $data))
{
if($len == 516 && preg_match('/^[a-zA-Z0-9\-~]+AA$/', $data)) {
return false;
}
/* ECDSA or EdDSA */
if($len == 524 && !preg_match('/^[a-zA-Z0-9\-~]+AEAA[Ec]AAA==$/', $data))
{
if($len == 524 && !preg_match('/^[a-zA-Z0-9\-~]+AEAA[Ec]AAA==$/', $data)) {
return false;
}
@ -139,8 +131,7 @@ class Utils { @@ -139,8 +131,7 @@ class Utils {
* @param string $data
* @return string
*/
public static function b32from64(string $data): string
{
public static function b32from64(string $data): string {
return self::b32encode(hash('sha256', self::b64decode($data), true));
}
@ -149,42 +140,35 @@ class Utils { @@ -149,42 +140,35 @@ class Utils {
* @param string $data
* @return bool
*/
public static function isValidDomain(string $data, string &$result): bool
{
public static function isValidDomain(string $data, string &$result): bool {
$len = strlen($data);
if($len < 5 || $len > 255) // 255 max: rfc2181, section 11
{
if($len < 5 || $len > 255) { // 255 max: rfc2181, section 11
$result = "Domain must be longer than 5 and lesser than 255 chars.";
return false;
}
if(preg_match('/(?:^b32|\.b32)\.i2p$/', $data))
{
if(preg_match('/(?:^b32|\.b32)\.i2p$/', $data)) {
$result = "Domain can't be b32.i2p or end with .b32.i2p.";
return false;
}
if(preg_match('/(?:^console|\.console)\.i2p$/', $data))
{
if(preg_match('/(?:^console|\.console)\.i2p$/', $data)) {
$result = "Domain can't be console.i2p or end with .console.i2p.";
return false;
}
if(preg_match('/(?:^proxy|\.proxy)\.i2p$/', $data))
{
if(preg_match('/(?:^proxy|\.proxy)\.i2p$/', $data)) {
$result = "Domain can't be proxy.i2p or end with .proxy.i2p.";
return false;
}
if(preg_match('/(?:^router|\.router)\.i2p$/', $data))
{
if(preg_match('/(?:^router|\.router)\.i2p$/', $data)) {
$result = "Domain can't be router.i2p or end with .router.i2p.";
return false;
}
if(filter_var($data, FILTER_VALIDATE_DOMAIN) !== false && preg_match('/\.i2p$/', $data))
{
if(filter_var($data, FILTER_VALIDATE_DOMAIN) !== false && preg_match('/\.i2p$/', $data)) {
return true;
}
@ -192,8 +176,7 @@ class Utils { @@ -192,8 +176,7 @@ class Utils {
return false;
}
public static function verifyHostRecord(string $data, &$output): bool
{
public static function verifyHostRecord(string $data, &$output): bool {
$retval = null;
if (strpos($data, "&") !== false)
@ -208,8 +191,7 @@ class Utils { @@ -208,8 +191,7 @@ class Utils {
return false;
}
public static function parseHostRecord(string $data): array
{
public static function parseHostRecord(string $data): array {
$record = [];
/* Return empty array if no data provided */
@ -232,13 +214,11 @@ class Utils { @@ -232,13 +214,11 @@ class Utils {
$record["b64"] = $host[1];
/* Parse extended commands */
if(isset($tmp[1]))
{
if(isset($tmp[1])) {
$cmds = [];
$cmdlist = preg_split("/#/", $tmp[1]);
foreach($cmdlist as $i)
{
foreach($cmdlist as $i) {
list($name, $value) = explode('=', $i, 2);
$cmds[$name] = $value;
}

Loading…
Cancel
Save