Domain registry project http://reg.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.7 KiB

<?php
namespace App;
class BOB {
private $sock;
protected $options = [
"bob_host" => "127.0.0.1",
"bob_port" => "2827",
"bob_options" => "inbound.quantity=5 outbound.quantity=5 inbound.length=1 outbound.length=1 i2cp.leaseSetType=3",
"bob_nick" => "hostchecker",
];
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)
{
throw new \ErrorException("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
}
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)
{
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))
{
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]);
}
}
public function __destruct()
{
$command = "quit\n";
socket_write($this->sock, $command, strlen($command));
socket_read($this->sock, 1024, PHP_NORMAL_READ);
socket_close($this->sock);
}
public function setnick()
{
$command = "setnick " . $this->options["bob_nick"] . "\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(!preg_match('/^OK/', $response))
echo "BOB response: " . $response;
}
public function options()
{
$options = explode(" ", $this->options["bob_options"]);
foreach($options as $option)
{
$command = "option " . $option . "\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(!preg_match('/^OK/', $response))
echo "BOB response: " . $response;
}
}
public function newkeys()
{
$command = "newkeys\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(!preg_match('/^OK/', $response))
echo "BOB response: " . $response;
}
public function start()
{
$command = "start\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(!preg_match('/^OK/', $response))
echo "BOB response: " . $response;
}
public function intun()
{
$command = "inhost 127.0.0.1\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(!preg_match('/^OK/', $response))
echo "BOB response: " . $response;
$command = "inport " . rand(1024, 65535) . "\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(!preg_match('/^OK/', $response))
echo "BOB response: " . $response;
}
public function getnick()
{
$command = "getnick " . $this->options["bob_nick"] . "\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(!preg_match('/^OK/', $response))
echo "BOB response: " . $response;
}
public function lookup(string $address): bool
{
$command = "lookup " . $address . "\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(preg_match('/^OK/', $response))
return true;
return false;
}
public function lookuplocal(string $address): bool
{
$command = "lookuplocal " . $address . "\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(preg_match('/^OK/', $response))
return true;
return false;
}
public function stop()
{
$command = "stop\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(!preg_match('/^OK/', $response))
echo "BOB response: " . $response;
}
public function clear()
{
$command = "clear\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(!preg_match('/^OK/', $response))
echo "BOB response: " . $response;
}
public function zap()
{
$command = "zap\n";
socket_write($this->sock, $command, strlen($command));
$response = socket_read($this->sock, 1024, PHP_NORMAL_READ);
if(!preg_match('/^OK/', $response))
echo "BOB response: " . $response;
}
}