Compare commits

...

10 commits
1.0.4 ... main

Author SHA1 Message Date
yggverse
42b1195b83 add all supported regions 2026-03-05 03:26:58 +02:00
yggverse
9f4e924592 use break as nothing to continue on any piece fail 2026-03-05 01:11:24 +02:00
yggverse
795f304789 use enumeration types 2026-03-05 01:08:13 +02:00
yggverse
a101a55715 revert getServersIPv6 method as mixed address families are not supported by legacy protocol implementation 2026-03-05 00:42:21 +02:00
yggverse
13a7c607e1 update parsing conditions, remove hardcoded ipv6 impl 2026-03-04 23:19:46 +02:00
yggverse
34390a0713 simplify port decoding 2026-03-04 22:56:37 +02:00
yggverse
167f092a28 continue, not break 2026-03-04 22:32:21 +02:00
yggverse
bacfce5f1b reorder arguments 2026-03-04 21:24:12 +02:00
yggverse
82c8564645 fix syntax error 2026-03-04 20:50:03 +02:00
yggverse
b1fce6a668 fix ipv6/xash3d-master compatibility; update method name 2026-03-04 20:47:48 +02:00

View file

@ -4,6 +4,27 @@ declare(strict_types=1);
namespace Yggverse\Hl\Xash3D; namespace Yggverse\Hl\Xash3D;
enum Family: int {
case IPv4 = 4;
case IPv6 = 16;
}
enum Region: string {
case USEastCoast = "0x00";
case USWestCoast = "0x01";
case SouthAmerica = "0x02";
case Europe = "0x03";
case Asia = "0x04";
case Australia = "0x05";
case MiddleEast = "0x06";
case Africa = "0x07";
case World = "0xff";
}
enum Game: string {
case Valve = "valve";
}
class Master class Master
{ {
private string $_host; private string $_host;
@ -14,7 +35,7 @@ class Master
public function __construct( public function __construct(
string $host, string $host,
int $port, int $port = 27010,
int $timeout = 5 int $timeout = 5
) )
{ {
@ -35,14 +56,18 @@ class Master
} }
} }
public function getServersIPv6( // Legacy protocol implementation does not support mixed address families
// in the binary master socket response, use separated method for IPv4 servers.
public function getServers(
int $limit = 100, int $limit = 100,
string $region = "\xFF", string $host = "0.0.0.0",
string $host = "0.0.0.0:0",
int $port = 0, int $port = 0,
string $gamedir = "valve" Game $game = Game::Valve,
Region $region = Region::World
): ?array ): ?array
{ {
$family = filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? Family::IPv4 : Family::IPv6;
// Init connection // Init connection
$socket = fsockopen( $socket = fsockopen(
"udp://{$this->_host}", "udp://{$this->_host}",
@ -52,6 +77,8 @@ class Master
$this->_timeout $this->_timeout
); );
$master = "{$this->_host}:{$this->_port}";
// Is connected // Is connected
if (true === is_resource($socket)) if (true === is_resource($socket))
{ {
@ -64,7 +91,7 @@ class Master
else else
{ {
$this->_errors[] = sprintf( $this->_errors[] = sprintf(
_('Connection error: %s'), _("Connection error for $master: %s"),
$message $message
); );
@ -76,9 +103,9 @@ class Master
} }
// Filter query // Filter query
if (false === fwrite($socket, "1{$region}{$host}:{$port}\0\gamedir\t{$gamedir}\0")) if (false === fwrite($socket, "1{$region->value}{$host}:{$port}\0\\gamedir\\{$game->value}\0"))
{ {
$this->_errors[] = _('Could not send socket query'); $this->_errors[] = _("Could not send socket query for $master");
$this->_fclose( $this->_fclose(
$socket $socket
@ -90,7 +117,7 @@ class Master
// Skip header // Skip header
if (false === fread($socket, 6)) if (false === fread($socket, 6))
{ {
$this->_errors[] = _('Could not init packet header'); $this->_errors[] = _("Could not init packet header for $master");
$this->_fclose( $this->_fclose(
$socket $socket
@ -104,59 +131,52 @@ class Master
for ($i = 0; $i < $limit; $i++) for ($i = 0; $i < $limit; $i++)
{ {
// Get host // Get host bytes
if (false === $host = fread($socket, 16)) if (false === $host = fread($socket, $family->value))
{ {
$this->_errors[] = _("Invalid `host` fragment in packet at $i for $master");
break; break;
} }
// Is end of packet // End of packet
if (true === str_ends_with(bin2hex($host), bin2hex("\0\0\0\0\0\0"))) if (true === str_ends_with(bin2hex($host), bin2hex("\0\0\0\0\0\0")))
{ {
break; break;
} }
// Skip invalid host value // Get host string
if (false === $host = inet_ntop($host)) if (false === $host = inet_ntop($host))
{ {
// Shift port bytes $this->_errors[] = _("Invalid `host` value in packet at $i for $master");
fread($socket, 2); break;
continue;
} }
// Decode first byte of port // Get port bytes
if (false === $byte1 = fread($socket, 1)) if (false === $p = fread($socket, 2))
{ {
// Shift port byte $this->_errors[] = _("Invalid `port` fragment in packet at $i for $master");
fread($socket, 1); break;
continue;
} }
// Decode second byte of port // Get port value
if (false === $byte2 = fread($socket, 1)) if (false === $p = unpack('nport', $p))
{ {
continue; $this->_errors[] = _("Invalid `port` value in packet at $i for $master");
break;
} }
// Calculate port value // Validate result
$port = ord($byte1) * 256 + ord($byte2); if (false === filter_var($host, FILTER_VALIDATE_IP, $family == Family::IPv6 ? FILTER_FLAG_IPV6
: FILTER_FLAG_IPV4) || empty($p['port']))
// Validate IPv6 result
if (
false !== strpos($host, '.') || // filter_var not always works with mixed IPv6
false === filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ||
false === filter_var($port, FILTER_VALIDATE_INT)
)
{ {
continue; $this->_errors[] = _("Invalid socket address in packet at $i for $master");
break;
} }
$servers["[{$host}]:{$port}"] = // keep unique $servers["{$host}{$p['port']}"] = // keep unique
[ [
'host' => $host, 'host' => $host,
'port' => $port 'port' => $p['port']
]; ];
} }