mirror of
https://github.com/pi-hole/web.git
synced 2026-04-24 18:59:48 +01:00
@@ -6,30 +6,8 @@
|
||||
* This file is copyright under the latest version of the EUPL.
|
||||
* Please see LICENSE file for your rights under this license. */
|
||||
|
||||
function testFTL($address)
|
||||
function connectFTL($address, $port=4711)
|
||||
{
|
||||
if($address === "127.0.0.1")
|
||||
{
|
||||
$ret = shell_exec("pidof pihole-FTL");
|
||||
return intval($ret);
|
||||
}
|
||||
// We cannot relly test for a distant FTL instance
|
||||
// in the same way, so for any other IP address
|
||||
// we simply return true here and rely on the API
|
||||
// socket connection itself to fail if there is nothing
|
||||
// on that address
|
||||
return true;
|
||||
}
|
||||
|
||||
function connectFTL($address, $port=4711, $quiet=true)
|
||||
{
|
||||
$timeout = 3;
|
||||
|
||||
if(!$quiet)
|
||||
{
|
||||
echo "Attempting to connect to '$address' on port '$port'...\n";
|
||||
}
|
||||
|
||||
if($address == "127.0.0.1")
|
||||
{
|
||||
// Read port
|
||||
@@ -38,136 +16,42 @@ function connectFTL($address, $port=4711, $quiet=true)
|
||||
$port = intval($portfile);
|
||||
}
|
||||
|
||||
// Create a TCP/IP socket
|
||||
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
|
||||
or die("socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n");
|
||||
|
||||
socket_set_nonblock($socket) or die("Unable to set nonblock on socket\n");
|
||||
|
||||
$time = time();
|
||||
while (!@socket_connect($socket, $address, $port))
|
||||
{
|
||||
$err = socket_last_error($socket);
|
||||
if ($err == 115 || $err == 114)
|
||||
{
|
||||
if ((time() - $time) >= $timeout)
|
||||
{
|
||||
socket_close($socket);
|
||||
die("Connection timed out.\n");
|
||||
}
|
||||
// Wait for 1 millisecond
|
||||
usleep(1000);
|
||||
continue;
|
||||
}
|
||||
die(socket_strerror($err) . "\n");
|
||||
}
|
||||
|
||||
socket_set_block($socket) or die("Unable to set block on socket\n");
|
||||
|
||||
// Set timeout to 3 seconds
|
||||
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, ['sec'=>$timeout, 'usec'=>0]);
|
||||
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, ['sec'=>$timeout, 'usec'=>0]);
|
||||
|
||||
if(!$quiet)
|
||||
{
|
||||
echo "Success!\n\n";
|
||||
}
|
||||
// Open Internet socket connection
|
||||
$socket = @fsockopen($address, $port, $timeout = 1.0);
|
||||
|
||||
return $socket;
|
||||
}
|
||||
function sendRequestFTL($requestin, $quiet=true)
|
||||
function sendRequestFTL($requestin)
|
||||
{
|
||||
global $socket;
|
||||
|
||||
$request = ">".$requestin;
|
||||
if(!$quiet)
|
||||
{
|
||||
echo "Sending request (".$request.")...\n";
|
||||
}
|
||||
|
||||
socket_write($socket, $request, strlen($request)) or die("Could not send data to server\n");
|
||||
if(!$quiet)
|
||||
{
|
||||
echo "OK.\n";
|
||||
}
|
||||
fwrite($socket, $request) or die("Could not send data to server\n");
|
||||
}
|
||||
|
||||
function getResponseFTL($quiet=true)
|
||||
function getResponseFTL()
|
||||
{
|
||||
global $socket;
|
||||
if(!$quiet)
|
||||
{
|
||||
echo "Reading response:\n";
|
||||
}
|
||||
|
||||
$response = [];
|
||||
|
||||
while(true)
|
||||
{
|
||||
$out = socket_read($socket, 2048, PHP_NORMAL_READ);
|
||||
if(!$quiet)
|
||||
{
|
||||
echo $out;
|
||||
}
|
||||
$out = fgets($socket, 128);
|
||||
if(strrpos($out,"---EOM---") !== false)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
$out = rtrim($out);
|
||||
if(strlen($out) > 0)
|
||||
{
|
||||
$response[] = $out;
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
function disconnectFTL($quiet=true)
|
||||
function disconnectFTL()
|
||||
{
|
||||
global $socket;
|
||||
if(!$quiet)
|
||||
{
|
||||
echo "Closing socket...";
|
||||
}
|
||||
|
||||
socket_close($socket);
|
||||
|
||||
if(!$quiet)
|
||||
{
|
||||
echo "OK.\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
$clients = array();
|
||||
function resolveHostname($clientip, $printIP = false)
|
||||
{
|
||||
global $clients;
|
||||
$ipaddr = strtolower($clientip);
|
||||
if(array_key_exists($clientip, $clients))
|
||||
{
|
||||
// Entry already exists
|
||||
$clientname = $clients[$ipaddr];
|
||||
if($printIP)
|
||||
return $clientname."|".$clientip;
|
||||
return $clientname;
|
||||
}
|
||||
|
||||
else if(filter_var($clientip, FILTER_VALIDATE_IP))
|
||||
{
|
||||
// Get host name of client and convert to lower case
|
||||
$clientname = strtolower(gethostbyaddr($ipaddr));
|
||||
}
|
||||
else
|
||||
{
|
||||
// This is already a host name
|
||||
$clientname = $ipaddr;
|
||||
}
|
||||
// Buffer result
|
||||
$clients[$ipaddr] = $clientname;
|
||||
|
||||
if($printIP)
|
||||
return $clientname."|".$clientip;
|
||||
return $clientname;
|
||||
fclose($socket);
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user