mirror of
https://github.com/YGGverse/hl-php.git
synced 2026-03-31 17:15:39 +00:00
Compare commits
14 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42b1195b83 | ||
|
|
9f4e924592 | ||
|
|
795f304789 | ||
|
|
a101a55715 | ||
|
|
13a7c607e1 | ||
|
|
34390a0713 | ||
|
|
167f092a28 | ||
|
|
bacfce5f1b | ||
|
|
82c8564645 | ||
|
|
b1fce6a668 | ||
|
|
4f8fffed17 | ||
|
|
550efd120f | ||
|
|
3ffb314fa4 | ||
|
|
15035498b3 |
2 changed files with 119 additions and 71 deletions
|
|
@ -23,3 +23,7 @@ var_dump(
|
||||||
$master->getErrors()
|
$master->getErrors()
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Projects
|
||||||
|
|
||||||
|
* [HLState](https://github.com/YGGverse/HLState) - Web Monitor for Half-Life Servers
|
||||||
|
|
@ -4,77 +4,124 @@ 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 $_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,
|
||||||
int $port,
|
int $port = 27010,
|
||||||
int $timeout = 5
|
int $timeout = 5
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
$this->_socket = fsockopen(
|
$this->_host = $host;
|
||||||
"udp://{$host}",
|
$this->_port = $port;
|
||||||
$port,
|
$this->_timeout = $timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function _fclose(
|
||||||
|
mixed $socket
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (true === is_resource($socket))
|
||||||
|
{
|
||||||
|
fclose(
|
||||||
|
$socket
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
string $host = "0.0.0.0",
|
||||||
|
int $port = 0,
|
||||||
|
Game $game = Game::Valve,
|
||||||
|
Region $region = Region::World
|
||||||
|
): ?array
|
||||||
|
{
|
||||||
|
$family = filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? Family::IPv4 : Family::IPv6;
|
||||||
|
|
||||||
|
// Init connection
|
||||||
|
$socket = fsockopen(
|
||||||
|
"udp://{$this->_host}",
|
||||||
|
$this->_port,
|
||||||
$code,
|
$code,
|
||||||
$message,
|
$message,
|
||||||
$timeout
|
$this->_timeout
|
||||||
);
|
);
|
||||||
|
|
||||||
if (is_resource($this->_socket))
|
$master = "{$this->_host}:{$this->_port}";
|
||||||
|
|
||||||
|
// Is connected
|
||||||
|
if (true === is_resource($socket))
|
||||||
{
|
{
|
||||||
stream_set_timeout(
|
stream_set_timeout(
|
||||||
$this->_socket,
|
$socket,
|
||||||
$timeout
|
$this->_timeout
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$this->_errors[] = sprintf(
|
$this->_errors[] = sprintf(
|
||||||
_('Connection error: %s'),
|
_("Connection error for $master: %s"),
|
||||||
$message
|
$message
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __destruct()
|
$this->_fclose(
|
||||||
{
|
$socket
|
||||||
fclose(
|
);
|
||||||
$this->_socket
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getServersIPv6(
|
|
||||||
int $limit = 100,
|
|
||||||
string $region = "\xFF",
|
|
||||||
string $host = "0.0.0.0:0",
|
|
||||||
int $port = 0,
|
|
||||||
string $gamedir = "valve"
|
|
||||||
): ?array
|
|
||||||
{
|
|
||||||
// Is connected
|
|
||||||
if (false === is_resource($this->_socket))
|
|
||||||
{
|
|
||||||
$this->_errors[] = _('Socket connection error');
|
|
||||||
|
|
||||||
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->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(
|
||||||
|
$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 for $master");
|
||||||
|
|
||||||
|
$this->_fclose(
|
||||||
|
$socket
|
||||||
|
);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -84,62 +131,59 @@ class Master
|
||||||
|
|
||||||
for ($i = 0; $i < $limit; $i++)
|
for ($i = 0; $i < $limit; $i++)
|
||||||
{
|
{
|
||||||
// Get host
|
// Get host bytes
|
||||||
if (false === $host = fread($this->_socket, 16))
|
if (false === $host = fread($socket, $family->value))
|
||||||
|
{
|
||||||
|
$this->_errors[] = _("Invalid `host` fragment in packet at $i for $master");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// End of packet
|
||||||
|
if (true === str_ends_with(bin2hex($host), bin2hex("\0\0\0\0\0\0")))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is end of packet
|
// Get host string
|
||||||
if (true === str_starts_with($host, (string) 0))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip invalid host value
|
|
||||||
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($this->_socket, 2);
|
break;
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode first byte for port
|
// Get port bytes
|
||||||
if (false === $byte1 = fread($this->_socket, 1))
|
if (false === $p = fread($socket, 2))
|
||||||
{
|
{
|
||||||
// Shift port byte
|
$this->_errors[] = _("Invalid `port` fragment in packet at $i for $master");
|
||||||
fread($this->_socket, 1);
|
break;
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode second byte for port
|
// Get port value
|
||||||
if (false === $byte2 = fread($this->_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']
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->_fclose(
|
||||||
|
$socket
|
||||||
|
);
|
||||||
|
|
||||||
return $servers;
|
return $servers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue