make socket connections isolated

This commit is contained in:
ghost 2024-01-13 20:03:23 +02:00
parent 550efd120f
commit 4f8fffed17

View File

@ -6,8 +6,11 @@ namespace Yggverse\Hl\Xash3D;
class Master class Master
{ {
private $_socket; private string $_host;
private $_errors = []; private int $_port;
private int $_timeout;
private array $_errors = [];
public function __construct( public function __construct(
string $host, string $host,
@ -15,37 +18,19 @@ class Master
int $timeout = 5 int $timeout = 5
) )
{ {
$this->_socket = fsockopen( $this->_host = $host;
"udp://{$host}", $this->_port = $port;
$port, $this->_timeout = $timeout;
$code,
$message,
$timeout
);
if (is_resource($this->_socket))
{
stream_set_timeout(
$this->_socket,
$timeout
);
}
else
{
$this->_errors[] = sprintf(
_('Connection error: %s'),
$message
);
}
} }
public function __destruct() private function _fclose(
mixed $socket
)
{ {
if (true === is_resource($this->_socket)) if (true === is_resource($socket))
{ {
fclose( fclose(
$this->_socket $socket
); );
} }
} }
@ -58,27 +43,59 @@ class Master
string $gamedir = "valve" string $gamedir = "valve"
): ?array ): ?array
{ {
// Init connection
$socket = fsockopen(
"udp://{$this->_host}",
$this->_port,
$code,
$message,
$this->_timeout
);
// Is connected // Is connected
if (false === is_resource($this->_socket)) if (true === is_resource($socket))
{ {
$this->_errors[] = _('Socket connection error'); stream_set_timeout(
$socket,
$this->_timeout
);
}
else
{
$this->_errors[] = sprintf(
_('Connection error: %s'),
$message
);
$this->_fclose(
$socket
);
return null; return null;
} }
// Filter query // Filter query
if (false === fwrite($this->_socket, "1{$region}{$host}:{$port}\0\gamedir\t{$gamedir}\0")) if (false === fwrite($socket, "1{$region}{$host}:{$port}\0\gamedir\t{$gamedir}\0"))
{ {
$this->_errors[] = _('Could not send socket query'); $this->_errors[] = _('Could not send socket query');
$this->_fclose(
$socket
);
return null; return null;
} }
// Skip header // Skip header
if (false === fread($this->_socket, 6)) if (false === fread($socket, 6))
{ {
$this->_errors[] = _('Could not init packet header'); $this->_errors[] = _('Could not init packet header');
$this->_fclose(
$socket
);
return null; return null;
} }
@ -88,7 +105,7 @@ class Master
for ($i = 0; $i < $limit; $i++) for ($i = 0; $i < $limit; $i++)
{ {
// Get host // Get host
if (false === $host = fread($this->_socket, 16)) if (false === $host = fread($socket, 16))
{ {
break; break;
} }
@ -103,22 +120,22 @@ class Master
if (false === $host = inet_ntop($host)) if (false === $host = inet_ntop($host))
{ {
// Shift port bytes // Shift port bytes
fread($this->_socket, 2); fread($socket, 2);
continue; continue;
} }
// Decode first byte for port // Decode first byte of port
if (false === $byte1 = fread($this->_socket, 1)) if (false === $byte1 = fread($socket, 1))
{ {
// Shift port byte // Shift port byte
fread($this->_socket, 1); fread($socket, 1);
continue; continue;
} }
// Decode second byte for port // Decode second byte of port
if (false === $byte2 = fread($this->_socket, 1)) if (false === $byte2 = fread($socket, 1))
{ {
continue; continue;
} }
@ -143,6 +160,10 @@ class Master
]; ];
} }
$this->_fclose(
$socket
);
return $servers; return $servers;
} }