Browse Source

save check result timestamp, reformat code

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

56
lib/bob.php

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

16
lib/checker.php

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

9
lib/db.php

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

5
lib/router.php

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

60
lib/utils.php

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

Loading…
Cancel
Save