Browse Source

push recent updates

Signed-off-by: r4sas <r4sas@i2pmail.org>
master
R4SAS 2 years ago
parent
commit
57b6299440
Signed by: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 6
      checker.php
  2. 5
      config.php.dist
  3. 6
      fetch.php
  4. 9
      lib/bob.php
  5. 6
      lib/checker.php
  6. 9
      lib/utils.php
  7. 4
      public/index.php
  8. 2
      templates/add.twig
  9. 2
      templates/alive.twig
  10. 2
      templates/all.twig
  11. 2
      templates/hidden.twig
  12. 2
      templates/latest.twig
  13. 2
      templates/search.twig
  14. 42
      views/add.php
  15. 30
      views/api.php

6
checker.php

@ -14,7 +14,7 @@ $pdo = (new DB($options))->pdo; @@ -14,7 +14,7 @@ $pdo = (new DB($options))->pdo;
$results = [];
/* Fetch hosts for check from database */
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 {
@ -22,7 +22,7 @@ if ((($options['fullhour'] >= 0) && ($options['fullhour'] <= 23)) && date('H') = @@ -22,7 +22,7 @@ if ((($options['fullhour'] >= 0) && ($options['fullhour'] <= 23)) && date('H') =
}
$hosts = $STH->fetchAll(PDO::FETCH_KEY_PAIR);
/* Start temporary BOB tunnel for checking */
echo "[BOB] Starting session for checking" . PHP_EOL;
$bob = new BOB($options);
$bob->setnick();
$bob->options();
@ -31,7 +31,7 @@ $bob->intun(); @@ -31,7 +31,7 @@ $bob->intun();
$bob->start();
/* Sleep 10 seconds awaitng tunnels get built */
echo "BOB session started, awaiting 10 seconds for tunnels" . PHP_EOL;
echo "[BOB] Session started, awaiting 10 seconds for tunnels" . PHP_EOL;
sleep(10);
/* Start async checker tasks */

5
config.php.dist

@ -14,9 +14,10 @@ $options = [ @@ -14,9 +14,10 @@ $options = [
/* I2P settings */
'bob_host' => '127.0.0.1',
'bob_port' => '2827',
'bob_options' => 'inbound.quantity=3 outbound.quantity=3 inbound.length=1 outbound.length=1 i2cp.leaseSetType=3',
'bob_options' => 'inbound.quantity=3 outbound.quantity=3 inbound.length=1 outbound.length=1 i2cp.leaseSetType=3 i2cp.dontPublishLeaseSet=true',
'bob_nick' => 'hostchecker',
'check_tries' => 1,
'check_tries' => 2, // lookup tries
'retry_delay' => 3, // delay in seconds between tries
'http_proxy' => 'tcp://127.0.0.1:4444', // this is HTTP proxy, which must be specified as tcp protocol because we using stream context
/* Service settings */

6
fetch.php

@ -58,7 +58,11 @@ foreach ($lists as $list) { @@ -58,7 +58,11 @@ foreach ($lists as $list) {
$domain = "";
$record = $util->parseHostRecord($buffer);
if (!$util->isValidDomain($record['host'], $error)) {
if (!isset($record['host'])) {
echo "Error while validating record " . $buffer . ": No host is found." . PHP_EOL;
continue;
} else if (!$util->isValidDomain($record['host'], $error)) {
echo "Error while validating " . $record['host'] . ": " . $error . PHP_EOL;
continue;

9
lib/bob.php

@ -25,6 +25,9 @@ class BOB { @@ -25,6 +25,9 @@ class BOB {
}
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)
{
@ -33,11 +36,13 @@ class BOB { @@ -33,11 +36,13 @@ class BOB {
/* Reading BOB greeting */
$response = socket_read($this->sock, 1024);
if(!preg_match('/OK/', $response))
{
throw new \ErrorException("BOB returned incorrect response on connect");
throw new \ErrorException("BOB returned incorrect response on connect or timed out");
}
/* Post-init timeouts setting */
socket_set_option($this->sock, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 180, 'usec' => 180 * 1000]);
}
}

6
lib/checker.php

@ -30,7 +30,7 @@ class Checker implements Task @@ -30,7 +30,7 @@ class Checker implements Task
$bob->getnick();
$result = false;
for ($i = 0, $tries = $this->options['check_tries']; $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");
if(!$result) {
$result = $bob->lookup($this->base32 . ".b32.i2p");
@ -39,6 +39,10 @@ class Checker implements Task @@ -39,6 +39,10 @@ class Checker implements Task
if ($result) {
break;
}
// 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);
}
$bob = null;

9
lib/utils.php

@ -212,7 +212,7 @@ class Utils { @@ -212,7 +212,7 @@ class Utils {
{
$record = [];
/* Return empty array if no data provided */
/* Return empty array if no data provided */
if(!strlen($data))
return $record;
@ -223,6 +223,11 @@ class Utils { @@ -223,6 +223,11 @@ class Utils {
/* Parse host record */
$host = preg_split("/\=/", $tmp[0], 2);
/* Return empty array if host or b64 is not set */
if(!isset($host[0]) || !isset($host[1]))
return $record;
$record["host"] = $host[0];
$record["b64"] = $host[1];
@ -234,7 +239,7 @@ class Utils { @@ -234,7 +239,7 @@ class Utils {
$cmdlist = preg_split("/#/", $tmp[1]);
foreach($cmdlist as $i)
{
list($name,$value) = explode('=', $i, 2);
list($name, $value) = explode('=', $i, 2);
$cmds[$name] = $value;
}

4
public/index.php

@ -23,6 +23,10 @@ $r->addRoute('^/all/?([0-9]+)?/?', function($url, $page = 1) { @@ -23,6 +23,10 @@ $r->addRoute('^/all/?([0-9]+)?/?', function($url, $page = 1) {
require __DIR__ . '/../views/all.php';
});
$r->addRoute('^/api/?(all|status)?/?([^\?/]*)?/?(?:\?|$)', function($url, $command, $query = "") {
require __DIR__ . '/../views/api.php';
});
$r->addRoute('^/jump/?([^\?/]*)/?(?:\?|$)', function($url, $query = "") {
require __DIR__ . '/../views/jump.php';
});

2
templates/add.twig

@ -19,6 +19,8 @@ @@ -19,6 +19,8 @@
<h3 class="adder__title title">Subdomain successfuly added</h3>
{% elseif result.command == 'changedest' %}
<h3 class="adder__title title">Domain destination successfuly changed</h3>
{% elseif result.reregister %}
<h3 class="adder__title title">Domain successfuly re-registered</h3>
{% else %}
<h3 class="adder__title title">Domain successfuly added</h3>
{% endif %}

2
templates/alive.twig

@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
<th class="table__cell"><abbr title="Address Helper">AH</abbr></th>
<th class="table__cell"><abbr title="Base32 address">B32</abbr></th>
<th class="table__cell table__cell_full-b32">Full Base32</th>
<th class="table__cell table__cell_date">Last seen</th>
<th class="table__cell table__cell_date">Last seen (UTC)</th>
</tr>
</thead>
<tbody class="table__body">

2
templates/all.twig

@ -16,7 +16,7 @@ @@ -16,7 +16,7 @@
<th class="table__cell"><abbr title="Address Helper">AH</abbr></th>
<th class="table__cell"><abbr title="Base32 address">B32</abbr></th>
<th class="table__cell table__cell_full-b32">Full Base32</th>
<th class="table__cell table__cell_date">Last seen</th>
<th class="table__cell table__cell_date">Last seen (UTC)</th>
</tr>
</thead>
<tbody class="table__body">

2
templates/hidden.twig

@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
<th class="table__cell"><abbr title="Address Helper">AH</abbr></th>
<th class="table__cell"><abbr title="Base32 address">B32</abbr></th>
<th class="table__cell table__cell_full-b32">Full Base32</th>
<th class="table__cell table__cell_date">Last seen</th>
<th class="table__cell table__cell_date">Last seen (UTC)</th>
</tr>
</thead>
<tbody class="table__body">

2
templates/latest.twig

@ -16,7 +16,7 @@ @@ -16,7 +16,7 @@
<th class="table__cell"><abbr title="Address Helper">AH</abbr></th>
<th class="table__cell"><abbr title="Base32 address">B32</abbr></th>
<th class="table__cell table__cell_full-b32">Full Base32</th>
<th class="table__cell table__cell_date"><abbr title="Hover to see last seen time">Added</abbr></th>
<th class="table__cell table__cell_date"><abbr title="Hover to see last seen time">Added (UTC)</abbr></th>
</tr>
</thead>
<tbody class="table__body">

2
templates/search.twig

@ -24,7 +24,7 @@ @@ -24,7 +24,7 @@
<th class="table__cell"><abbr title="Address Helper">AH</abbr></th>
<th class="table__cell"><abbr title="Base32 address">B32</abbr></th>
<th class="table__cell table__cell_full-b32">Full Base32</th>
<th class="table__cell table__cell_date">Last seen</th>
<th class="table__cell table__cell_date">Last seen (UTC)</th>
</tr>
</thead>
<tbody class="table__body">

42
views/add.php

@ -32,11 +32,16 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) { @@ -32,11 +32,16 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
$parsed = $util->parseHostRecord($record);
if (!$util->isValidDomain($parsed['host'], $error)) {
if (!isset($parsed['host'])) {
$result["error"] = "Error while validating: Incorrect Auth string";
} else if (!$util->isValidDomain($parsed['host'], $error)) {
$result["error"] = "Error while validating: " . $error;
} else {
if ($util->isPunycodeDomain($parsed['host'])) {
$domain = idn_to_utf8($parsed['host'], 0, INTL_IDNA_VARIANT_UTS46);
} else {
$domain = $parsed['host'];
}
@ -48,10 +53,10 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) { @@ -48,10 +53,10 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
$result["error"] = "Error while validating: " . $error[0];
} else {
/* Check if such domain name already registered */
$STH = $pdo->query("SELECT COUNT(*) FROM `hosts` WHERE `host` = '" . $domain . "' LIMIT 1");
if (isset($parsed["commands"]["action"])) {
/* Check if such domain name already registered */
$STH = $pdo->query("SELECT COUNT(*) FROM `hosts` WHERE `host` = '" . $domain . "' LIMIT 1");
switch ($parsed["commands"]["action"]) {
case 'addsubdomain':
if ($STH->fetchColumn() == 1) {
@ -125,7 +130,7 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) { @@ -125,7 +130,7 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
/* Getting domain level (2LD, 3LD and etc.) and validating that domain at higher level is registered (2LD if registering 3LD). */
} else if (sizeof(explode(".", $domain)) > 2) {
$result["error"] = "Error while validating: you can't register subdomain (sub.example.i2p) as domain alias using addname action.";
$result["error"] = "Error while validating: you can't register subdomain (sub.example.i2p) as domain alias using addname action.";
} else if (!isset($parsed["commands"]["oldname"])) {
$result["error"] = "Error while validating: required fields not found. Re-check your registration string.";
@ -161,9 +166,16 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) { @@ -161,9 +166,16 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
}
} else {
if($STH->fetchColumn() == 1) {
/* Check if such domain name already registered */
$STH = $pdo->query("SELECT `host`, `base32`, `base64`, `initial`, `disabled` FROM `hosts` WHERE `host` = '" . $domain . "' LIMIT 1");
$row = $STH->fetch(PDO::FETCH_ASSOC);
if(count($row) && !$row['disabled']) {
$result["error"] = "Error while validating: That domain is already registered.";
} else if(count($row) && $row['disabled'] && $row['initial']) {
$result["error"] = "Error while validating: That domain is reserved and can't be re-registered automatically. Please contact service support team.";
} else {
if (isset($parsed["commands"]["oldname"]) || isset($parsed["commands"]["olddest"]) || isset($parsed["commands"]["oldsig"])) {
$result["error"] = "Error while validating: unexpected fields found.";
@ -172,6 +184,24 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) { @@ -172,6 +184,24 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
$result["error"] = "Error while validating: you can't register subdomain without specific action field.";
} else {
$result["reregister"] = false;
if(count($row) && $row['disabled']) { /* processing disabled domain */
$log = "[" . date("d-M-Y H:i:s e") . "] Re-registering attempt for " . $row['host'] . "! Next records will be deleted:" . PHP_EOL;
/* print all records, which will be deleted*/
$STH = $pdo->query("SELECT `host`, `base32`, `base64` FROM `hosts` WHERE `host` = '" . $domain . "' OR `host` LIKE '%." . $domain . "'");
$hosts = $STH->fetchAll(PDO::FETCH_ASSOC);
foreach ($hosts as $host) {
$log .= "Host: " . $host['host'] . PHP_EOL . "Base32: " . $host['base32'] . PHP_EOL . "Base64: " . $host['base64'] . PHP_EOL;
}
file_put_contents(__DIR__ . '/../logs/reg.log', $log, FILE_APPEND);
/* remove domain and subdomains if any found */
$pdo->exec("DELETE FROM `hosts` WHERE `host` = '" . $domain . "' OR `host` LIKE '%." . $domain . "'");
$result["reregister"] = true;
}
$base32 = $util->b32from64($parsed["b64"]);
/* Adding to database 2LD domain */

30
views/api.php

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
<?php
require_once __DIR__ . '/../config.php';
$all = false;
$pdo = (new App\DB($options))->pdo;
header('Content-Type: application/json');
if (isset($_GET["all"]))
$all = true;
if(!$command || !in_array($command, ['all', 'status']))
exit(json_encode(["status" => "error", "message" => "Incorrect request"]));
if($command == "all") {
$STH = $pdo->query ("SELECT `base32`, UNIX_TIMESTAMP(`last_seen`) as `last_seen` FROM `hosts` " .
"WHERE `approved` = 1 AND `disabled` = 0" .
($all ? " " : " AND `blacklisted` = 0 "));
$STH->setFetchMode(PDO::FETCH_ASSOC);
$rows = $STH->fetchAll();
$data = [];
foreach($rows as $row) {
$data[$row["base32"] . ".b32.i2p"] = $row["last_seen"];
}
exit(json_encode($data, JSON_PRETTY_PRINT));
}
Loading…
Cancel
Save