mirror of
https://github.com/xPaw/PHP-Source-Query.git
synced 2026-06-11 00:13:14 +02:00
Bring in CS-Fixer, reformat.
This commit is contained in:
@@ -1,8 +0,0 @@
|
|||||||
# http://editorconfig.org
|
|
||||||
|
|
||||||
root = true
|
|
||||||
|
|
||||||
[*]
|
|
||||||
charset = utf-8
|
|
||||||
indent_style = tab
|
|
||||||
insert_final_newline = true
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
* text=auto
|
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
vendor/
|
vendor/
|
||||||
composer.lock
|
composer.lock
|
||||||
|
.php-cs-fixer.cache
|
||||||
|
.idea/
|
||||||
|
|||||||
+25
-27
@@ -1,34 +1,32 @@
|
|||||||
<?php
|
<?php
|
||||||
require __DIR__ . '/../SourceQuery/bootstrap.php';
|
|
||||||
|
|
||||||
use xPaw\SourceQuery\SourceQuery;
|
declare(strict_types=1);
|
||||||
|
|
||||||
// For the sake of this example
|
require __DIR__ . '/../SourceQuery/bootstrap.php';
|
||||||
header( 'Content-Type: text/plain' );
|
|
||||||
header( 'X-Content-Type-Options: nosniff' );
|
|
||||||
|
|
||||||
// Edit this ->
|
use xPaw\SourceQuery\SourceQuery;
|
||||||
define( 'SQ_SERVER_ADDR', 'localhost' );
|
|
||||||
define( 'SQ_SERVER_PORT', 27015 );
|
|
||||||
define( 'SQ_TIMEOUT', 1 );
|
|
||||||
define( 'SQ_ENGINE', SourceQuery::SOURCE );
|
|
||||||
// Edit this <-
|
|
||||||
|
|
||||||
$Query = new SourceQuery( );
|
// For the sake of this example
|
||||||
|
header('Content-Type: text/plain');
|
||||||
|
header('X-Content-Type-Options: nosniff');
|
||||||
|
|
||||||
try
|
// Edit this ->
|
||||||
{
|
define('SQ_SERVER_ADDR', 'localhost');
|
||||||
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
|
define('SQ_SERVER_PORT', 27015);
|
||||||
|
define('SQ_TIMEOUT', 1);
|
||||||
|
define('SQ_ENGINE', SourceQuery::SOURCE);
|
||||||
|
// Edit this <-
|
||||||
|
|
||||||
print_r( $Query->GetInfo( ) );
|
$Query = new SourceQuery();
|
||||||
print_r( $Query->GetPlayers( ) );
|
|
||||||
print_r( $Query->GetRules( ) );
|
try {
|
||||||
}
|
$Query->Connect(SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE);
|
||||||
catch( Exception $e )
|
|
||||||
{
|
print_r($Query->GetInfo());
|
||||||
echo $e->getMessage( );
|
print_r($Query->GetPlayers());
|
||||||
}
|
print_r($Query->GetRules());
|
||||||
finally
|
} catch (Exception $e) {
|
||||||
{
|
echo $e->getMessage();
|
||||||
$Query->Disconnect( );
|
} finally {
|
||||||
}
|
$Query->Disconnect();
|
||||||
|
}
|
||||||
|
|||||||
+24
-26
@@ -1,34 +1,32 @@
|
|||||||
<?php
|
<?php
|
||||||
require __DIR__ . '/../SourceQuery/bootstrap.php';
|
|
||||||
|
|
||||||
use xPaw\SourceQuery\SourceQuery;
|
declare(strict_types=1);
|
||||||
|
|
||||||
// For the sake of this example
|
require __DIR__ . '/../SourceQuery/bootstrap.php';
|
||||||
header( 'Content-Type: text/plain' );
|
|
||||||
header( 'X-Content-Type-Options: nosniff' );
|
|
||||||
|
|
||||||
// Edit this ->
|
use xPaw\SourceQuery\SourceQuery;
|
||||||
define( 'SQ_SERVER_ADDR', 'localhost' );
|
|
||||||
define( 'SQ_SERVER_PORT', 27015 );
|
|
||||||
define( 'SQ_TIMEOUT', 1 );
|
|
||||||
define( 'SQ_ENGINE', SourceQuery::SOURCE );
|
|
||||||
// Edit this <-
|
|
||||||
|
|
||||||
$Query = new SourceQuery( );
|
// For the sake of this example
|
||||||
|
header('Content-Type: text/plain');
|
||||||
|
header('X-Content-Type-Options: nosniff');
|
||||||
|
|
||||||
try
|
// Edit this ->
|
||||||
{
|
define('SQ_SERVER_ADDR', 'localhost');
|
||||||
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
|
define('SQ_SERVER_PORT', 27015);
|
||||||
|
define('SQ_TIMEOUT', 1);
|
||||||
|
define('SQ_ENGINE', SourceQuery::SOURCE);
|
||||||
|
// Edit this <-
|
||||||
|
|
||||||
$Query->SetRconPassword( 'my_awesome_password' );
|
$Query = new SourceQuery();
|
||||||
|
|
||||||
var_dump( $Query->Rcon( 'say hello' ) );
|
try {
|
||||||
}
|
$Query->Connect(SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE);
|
||||||
catch( Exception $e )
|
|
||||||
{
|
$Query->SetRconPassword('my_awesome_password');
|
||||||
echo $e->getMessage( );
|
|
||||||
}
|
var_dump($Query->Rcon('say hello'));
|
||||||
finally
|
} catch (Exception $e) {
|
||||||
{
|
echo $e->getMessage();
|
||||||
$Query->Disconnect( );
|
} finally {
|
||||||
}
|
$Query->Disconnect();
|
||||||
|
}
|
||||||
|
|||||||
+49
-58
@@ -1,43 +1,42 @@
|
|||||||
<?php
|
<?php
|
||||||
require __DIR__ . '/../SourceQuery/bootstrap.php';
|
|
||||||
|
|
||||||
use xPaw\SourceQuery\SourceQuery;
|
declare(strict_types=1);
|
||||||
|
|
||||||
// Edit this ->
|
require __DIR__ . '/../SourceQuery/bootstrap.php';
|
||||||
define( 'SQ_SERVER_ADDR', 'localhost' );
|
|
||||||
define( 'SQ_SERVER_PORT', 27015 );
|
|
||||||
define( 'SQ_TIMEOUT', 3 );
|
|
||||||
define( 'SQ_ENGINE', SourceQuery::SOURCE );
|
|
||||||
// Edit this <-
|
|
||||||
|
|
||||||
$Timer = microtime( true );
|
use xPaw\SourceQuery\SourceQuery;
|
||||||
|
|
||||||
$Query = new SourceQuery( );
|
// Edit this ->
|
||||||
|
define('SQ_SERVER_ADDR', 'localhost');
|
||||||
|
define('SQ_SERVER_PORT', 27015);
|
||||||
|
define('SQ_TIMEOUT', 3);
|
||||||
|
define('SQ_ENGINE', SourceQuery::SOURCE);
|
||||||
|
// Edit this <-
|
||||||
|
|
||||||
$Info = [];
|
$Timer = microtime(true);
|
||||||
$Rules = [];
|
|
||||||
$Players = [];
|
|
||||||
$Exception = null;
|
|
||||||
|
|
||||||
try
|
$Query = new SourceQuery();
|
||||||
{
|
|
||||||
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
|
$Info = [];
|
||||||
|
$Rules = [];
|
||||||
|
$Players = [];
|
||||||
|
$Exception = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$Query->Connect(SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE);
|
||||||
//$Query->SetUseOldGetChallengeMethod( true ); // Use this when players/rules retrieval fails on games like Starbound
|
//$Query->SetUseOldGetChallengeMethod( true ); // Use this when players/rules retrieval fails on games like Starbound
|
||||||
|
|
||||||
$Info = $Query->GetInfo( );
|
$Info = $Query->GetInfo();
|
||||||
$Players = $Query->GetPlayers( );
|
$Players = $Query->GetPlayers();
|
||||||
$Rules = $Query->GetRules( );
|
$Rules = $Query->GetRules();
|
||||||
}
|
} catch (Exception $e) {
|
||||||
catch( Exception $e )
|
|
||||||
{
|
|
||||||
$Exception = $e;
|
$Exception = $e;
|
||||||
}
|
} finally {
|
||||||
finally
|
$Query->Disconnect();
|
||||||
{
|
}
|
||||||
$Query->Disconnect( );
|
|
||||||
}
|
$Timer = number_format(microtime(true) - $Timer, 4, '.', '');
|
||||||
|
|
||||||
$Timer = number_format( microtime( true ) - $Timer, 4, '.', '' );
|
|
||||||
?>
|
?>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
@@ -88,9 +87,9 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<?php if( $Exception !== null ): ?>
|
<?php if ($Exception !== null): ?>
|
||||||
<div class="panel panel-error">
|
<div class="panel panel-error">
|
||||||
<pre class="panel-body"><?php echo htmlspecialchars( $Exception->__toString( ) ); ?></pre>
|
<pre class="panel-body"><?php echo htmlspecialchars($Exception->__toString()); ?></pre>
|
||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
@@ -103,30 +102,22 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<?php if( !empty( $Info ) ): ?>
|
<?php if (!empty($Info)): ?>
|
||||||
<?php foreach( $Info as $InfoKey => $InfoValue ): ?>
|
<?php foreach ($Info as $InfoKey => $InfoValue): ?>
|
||||||
<tr>
|
<tr>
|
||||||
<td><?php echo htmlspecialchars( $InfoKey ); ?></td>
|
<td><?php echo htmlspecialchars($InfoKey); ?></td>
|
||||||
<td><?php
|
<td><?php
|
||||||
if( is_array( $InfoValue ) )
|
if (is_array($InfoValue)) {
|
||||||
{
|
|
||||||
echo "<pre>";
|
echo "<pre>";
|
||||||
print_r( $InfoValue );
|
print_r($InfoValue);
|
||||||
echo "</pre>";
|
echo "</pre>";
|
||||||
}
|
} else {
|
||||||
else
|
if ($InfoValue === true) {
|
||||||
{
|
|
||||||
if( $InfoValue === true )
|
|
||||||
{
|
|
||||||
echo 'true';
|
echo 'true';
|
||||||
}
|
} elseif ($InfoValue === false) {
|
||||||
else if( $InfoValue === false )
|
|
||||||
{
|
|
||||||
echo 'false';
|
echo 'false';
|
||||||
}
|
} else {
|
||||||
else
|
echo htmlspecialchars($InfoValue);
|
||||||
{
|
|
||||||
echo htmlspecialchars( $InfoValue );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?></td>
|
?></td>
|
||||||
@@ -144,16 +135,16 @@
|
|||||||
<table class="table table-bordered table-striped">
|
<table class="table table-bordered table-striped">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Player <span class="label label-info"><?php echo count( $Players ); ?></span></th>
|
<th>Player <span class="label label-info"><?php echo count($Players); ?></span></th>
|
||||||
<th class="frags-column">Frags</th>
|
<th class="frags-column">Frags</th>
|
||||||
<th class="frags-column">Time</th>
|
<th class="frags-column">Time</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<?php if( !empty( $Players ) ): ?>
|
<?php if (!empty($Players)): ?>
|
||||||
<?php foreach( $Players as $Player ): ?>
|
<?php foreach ($Players as $Player): ?>
|
||||||
<tr>
|
<tr>
|
||||||
<td><?php echo htmlspecialchars( $Player[ 'Name' ] ); ?></td>
|
<td><?php echo htmlspecialchars($Player[ 'Name' ]); ?></td>
|
||||||
<td><?php echo $Player[ 'Frags' ]; ?></td>
|
<td><?php echo $Player[ 'Frags' ]; ?></td>
|
||||||
<td><?php echo $Player[ 'TimeF' ]; ?></td>
|
<td><?php echo $Player[ 'TimeF' ]; ?></td>
|
||||||
</tr>
|
</tr>
|
||||||
@@ -172,15 +163,15 @@
|
|||||||
<table class="table table-bordered table-striped">
|
<table class="table table-bordered table-striped">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th colspan="2">Rules <span class="label label-info"><?php echo count( $Rules ); ?></span></th>
|
<th colspan="2">Rules <span class="label label-info"><?php echo count($Rules); ?></span></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<?php if( !empty( $Rules ) ): ?>
|
<?php if (!empty($Rules)): ?>
|
||||||
<?php foreach( $Rules as $Rule => $Value ): ?>
|
<?php foreach ($Rules as $Rule => $Value): ?>
|
||||||
<tr>
|
<tr>
|
||||||
<td><?php echo htmlspecialchars( $Rule ); ?></td>
|
<td><?php echo htmlspecialchars($Rule); ?></td>
|
||||||
<td><?php echo htmlspecialchars( $Value ); ?></td>
|
<td><?php echo htmlspecialchars($Value); ?></td>
|
||||||
</tr>
|
</tr>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
|
|||||||
+49
-61
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* @author Pavel Djundik
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
@@ -10,12 +13,12 @@
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace xPaw\SourceQuery;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
use xPaw\SourceQuery\Exception\SocketException;
|
use xPaw\SourceQuery\Exception\SocketException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base socket interface
|
* Base socket interface
|
||||||
*
|
*
|
||||||
* @package xPaw\SourceQuery
|
* @package xPaw\SourceQuery
|
||||||
@@ -23,8 +26,8 @@
|
|||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
* @uses xPaw\SourceQuery\Exception\SocketException
|
||||||
*/
|
*/
|
||||||
abstract class BaseSocket
|
abstract class BaseSocket
|
||||||
{
|
{
|
||||||
/** @var ?resource */
|
/** @var ?resource */
|
||||||
public $Socket;
|
public $Socket;
|
||||||
public int $Engine;
|
public int $Engine;
|
||||||
@@ -33,45 +36,39 @@
|
|||||||
public int $Port;
|
public int $Port;
|
||||||
public int $Timeout;
|
public int $Timeout;
|
||||||
|
|
||||||
public function __destruct( )
|
public function __destruct()
|
||||||
{
|
{
|
||||||
$this->Close( );
|
$this->Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public function Close( ) : void;
|
abstract public function Close(): void;
|
||||||
abstract public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void;
|
abstract public function Open(string $Address, int $Port, int $Timeout, int $Engine): void;
|
||||||
abstract public function Write( int $Header, string $String = '' ) : bool;
|
abstract public function Write(int $Header, string $String = ''): bool;
|
||||||
abstract public function Read( int $Length = 1400 ) : Buffer;
|
abstract public function Read(int $Length = 1400): Buffer;
|
||||||
|
|
||||||
protected function ReadInternal( Buffer $Buffer, int $Length, callable $SherlockFunction ) : Buffer
|
protected function ReadInternal(Buffer $Buffer, int $Length, callable $SherlockFunction): Buffer
|
||||||
{
|
{
|
||||||
if( $Buffer->Remaining( ) === 0 )
|
if ($Buffer->Remaining() === 0) {
|
||||||
{
|
throw new InvalidPacketException('Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY);
|
||||||
throw new InvalidPacketException( 'Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Header = $Buffer->GetLong( );
|
$Header = $Buffer->GetLong();
|
||||||
|
|
||||||
if( $Header === -1 ) // Single packet
|
if ($Header === -1) { // Single packet
|
||||||
{
|
|
||||||
// We don't have to do anything
|
// We don't have to do anything
|
||||||
}
|
} elseif ($Header === -2) { // Split packet
|
||||||
else if( $Header === -2 ) // Split packet
|
|
||||||
{
|
|
||||||
$Packets = [];
|
$Packets = [];
|
||||||
$IsCompressed = false;
|
$IsCompressed = false;
|
||||||
$ReadMore = false;
|
$ReadMore = false;
|
||||||
$PacketChecksum = null;
|
$PacketChecksum = null;
|
||||||
|
|
||||||
do
|
do {
|
||||||
{
|
$RequestID = $Buffer->GetLong();
|
||||||
$RequestID = $Buffer->GetLong( );
|
|
||||||
|
|
||||||
switch( $this->Engine )
|
switch ($this->Engine) {
|
||||||
{
|
|
||||||
case SourceQuery::GOLDSOURCE:
|
case SourceQuery::GOLDSOURCE:
|
||||||
{
|
{
|
||||||
$PacketCountAndNumber = $Buffer->GetByte( );
|
$PacketCountAndNumber = $Buffer->GetByte();
|
||||||
$PacketCount = $PacketCountAndNumber & 0xF;
|
$PacketCount = $PacketCountAndNumber & 0xF;
|
||||||
$PacketNumber = $PacketCountAndNumber >> 4;
|
$PacketNumber = $PacketCountAndNumber >> 4;
|
||||||
|
|
||||||
@@ -79,61 +76,52 @@
|
|||||||
}
|
}
|
||||||
case SourceQuery::SOURCE:
|
case SourceQuery::SOURCE:
|
||||||
{
|
{
|
||||||
$IsCompressed = ( $RequestID & 0x80000000 ) !== 0;
|
$IsCompressed = ($RequestID & 0x80000000) !== 0;
|
||||||
$PacketCount = $Buffer->GetByte( );
|
$PacketCount = $Buffer->GetByte();
|
||||||
$PacketNumber = $Buffer->GetByte( ) + 1;
|
$PacketNumber = $Buffer->GetByte() + 1;
|
||||||
|
|
||||||
if( $IsCompressed )
|
if ($IsCompressed) {
|
||||||
{
|
$Buffer->GetLong(); // Split size
|
||||||
$Buffer->GetLong( ); // Split size
|
|
||||||
|
|
||||||
$PacketChecksum = $Buffer->GetUnsignedLong( );
|
$PacketChecksum = $Buffer->GetUnsignedLong();
|
||||||
}
|
} else {
|
||||||
else
|
$Buffer->GetShort(); // Split size
|
||||||
{
|
|
||||||
$Buffer->GetShort( ); // Split size
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE );
|
throw new SocketException('Unknown engine.', SocketException::INVALID_ENGINE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$Packets[ $PacketNumber ] = $Buffer->Get( );
|
$Packets[ $PacketNumber ] = $Buffer->Get();
|
||||||
|
|
||||||
$ReadMore = $PacketCount > sizeof( $Packets );
|
$ReadMore = $PacketCount > sizeof($Packets);
|
||||||
}
|
} while ($ReadMore && $SherlockFunction($Buffer, $Length));
|
||||||
while( $ReadMore && $SherlockFunction( $Buffer, $Length ) );
|
|
||||||
|
|
||||||
$Data = implode( $Packets );
|
$Data = implode($Packets);
|
||||||
|
|
||||||
// TODO: Test this
|
// TODO: Test this
|
||||||
if( $IsCompressed )
|
if ($IsCompressed) {
|
||||||
{
|
|
||||||
// Let's make sure this function exists, it's not included in PHP by default
|
// Let's make sure this function exists, it's not included in PHP by default
|
||||||
if( !function_exists( 'bzdecompress' ) )
|
if (!function_exists('bzdecompress')) {
|
||||||
{
|
throw new \RuntimeException('Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.');
|
||||||
throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Data = bzdecompress( $Data );
|
$Data = bzdecompress($Data);
|
||||||
|
|
||||||
if( !is_string( $Data ) || crc32( $Data ) !== $PacketChecksum )
|
if (!is_string($Data) || crc32($Data) !== $PacketChecksum) {
|
||||||
{
|
throw new InvalidPacketException('CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH);
|
||||||
throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$Buffer->Set( substr( $Data, 4 ) );
|
$Buffer->Set(substr($Data, 4));
|
||||||
}
|
} else {
|
||||||
else
|
throw new InvalidPacketException('Socket read: Raw packet header mismatch. (0x' . dechex($Header) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
|
||||||
{
|
|
||||||
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . dechex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $Buffer;
|
return $Buffer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+41
-47
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* @author Pavel Djundik
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
@@ -10,19 +13,19 @@
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace xPaw\SourceQuery;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Buffer
|
* Class Buffer
|
||||||
*
|
*
|
||||||
* @package xPaw\SourceQuery
|
* @package xPaw\SourceQuery
|
||||||
*
|
*
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||||
*/
|
*/
|
||||||
class Buffer
|
final class Buffer
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Buffer
|
* Buffer
|
||||||
*/
|
*/
|
||||||
@@ -41,10 +44,10 @@
|
|||||||
/**
|
/**
|
||||||
* Sets buffer
|
* Sets buffer
|
||||||
*/
|
*/
|
||||||
public function Set( string $Buffer ) : void
|
public function Set(string $Buffer): void
|
||||||
{
|
{
|
||||||
$this->Buffer = $Buffer;
|
$this->Buffer = $Buffer;
|
||||||
$this->Length = strlen( $Buffer );
|
$this->Length = strlen($Buffer);
|
||||||
$this->Position = 0;
|
$this->Position = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +56,7 @@
|
|||||||
*
|
*
|
||||||
* @return int Remaining bytes in buffer
|
* @return int Remaining bytes in buffer
|
||||||
*/
|
*/
|
||||||
public function Remaining( ) : int
|
public function Remaining(): int
|
||||||
{
|
{
|
||||||
return $this->Length - $this->Position;
|
return $this->Length - $this->Position;
|
||||||
}
|
}
|
||||||
@@ -63,25 +66,21 @@
|
|||||||
*
|
*
|
||||||
* @param int $Length Bytes to read
|
* @param int $Length Bytes to read
|
||||||
*/
|
*/
|
||||||
public function Get( int $Length = -1 ) : string
|
public function Get(int $Length = -1): string
|
||||||
{
|
|
||||||
if( $Length === 0 )
|
|
||||||
{
|
{
|
||||||
|
if ($Length === 0) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$Remaining = $this->Remaining( );
|
$Remaining = $this->Remaining();
|
||||||
|
|
||||||
if( $Length === -1 )
|
if ($Length === -1) {
|
||||||
{
|
|
||||||
$Length = $Remaining;
|
$Length = $Remaining;
|
||||||
}
|
} elseif ($Length > $Remaining) {
|
||||||
else if( $Length > $Remaining )
|
|
||||||
{
|
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$Data = substr( $this->Buffer, $this->Position, $Length );
|
$Data = substr($this->Buffer, $this->Position, $Length);
|
||||||
|
|
||||||
$this->Position += $Length;
|
$this->Position += $Length;
|
||||||
|
|
||||||
@@ -91,22 +90,21 @@
|
|||||||
/**
|
/**
|
||||||
* Get byte from buffer
|
* Get byte from buffer
|
||||||
*/
|
*/
|
||||||
public function GetByte( ) : int
|
public function GetByte(): int
|
||||||
{
|
{
|
||||||
return ord( $this->Get( 1 ) );
|
return ord($this->Get(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get short from buffer
|
* Get short from buffer
|
||||||
*/
|
*/
|
||||||
public function GetShort( ) : int
|
public function GetShort(): int
|
||||||
{
|
{
|
||||||
if( $this->Remaining( ) < 2 )
|
if ($this->Remaining() < 2) {
|
||||||
{
|
throw new InvalidPacketException('Not enough data to unpack a short.', InvalidPacketException::BUFFER_EMPTY);
|
||||||
throw new InvalidPacketException( 'Not enough data to unpack a short.', InvalidPacketException::BUFFER_EMPTY );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Data = unpack( 'v', $this->Get( 2 ) );
|
$Data = unpack('v', $this->Get(2));
|
||||||
|
|
||||||
return (int)$Data[ 1 ];
|
return (int)$Data[ 1 ];
|
||||||
}
|
}
|
||||||
@@ -114,14 +112,13 @@
|
|||||||
/**
|
/**
|
||||||
* Get long from buffer
|
* Get long from buffer
|
||||||
*/
|
*/
|
||||||
public function GetLong( ) : int
|
public function GetLong(): int
|
||||||
{
|
{
|
||||||
if( $this->Remaining( ) < 4 )
|
if ($this->Remaining() < 4) {
|
||||||
{
|
throw new InvalidPacketException('Not enough data to unpack a long.', InvalidPacketException::BUFFER_EMPTY);
|
||||||
throw new InvalidPacketException( 'Not enough data to unpack a long.', InvalidPacketException::BUFFER_EMPTY );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Data = unpack( 'l', $this->Get( 4 ) );
|
$Data = unpack('l', $this->Get(4));
|
||||||
|
|
||||||
return (int)$Data[ 1 ];
|
return (int)$Data[ 1 ];
|
||||||
}
|
}
|
||||||
@@ -129,14 +126,13 @@
|
|||||||
/**
|
/**
|
||||||
* Get float from buffer
|
* Get float from buffer
|
||||||
*/
|
*/
|
||||||
public function GetFloat( ) : float
|
public function GetFloat(): float
|
||||||
{
|
{
|
||||||
if( $this->Remaining( ) < 4 )
|
if ($this->Remaining() < 4) {
|
||||||
{
|
throw new InvalidPacketException('Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY);
|
||||||
throw new InvalidPacketException( 'Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Data = unpack( 'f', $this->Get( 4 ) );
|
$Data = unpack('f', $this->Get(4));
|
||||||
|
|
||||||
return (float)$Data[ 1 ];
|
return (float)$Data[ 1 ];
|
||||||
}
|
}
|
||||||
@@ -144,14 +140,13 @@
|
|||||||
/**
|
/**
|
||||||
* Get unsigned long from buffer
|
* Get unsigned long from buffer
|
||||||
*/
|
*/
|
||||||
public function GetUnsignedLong( ) : int
|
public function GetUnsignedLong(): int
|
||||||
{
|
{
|
||||||
if( $this->Remaining( ) < 4 )
|
if ($this->Remaining() < 4) {
|
||||||
{
|
throw new InvalidPacketException('Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY);
|
||||||
throw new InvalidPacketException( 'Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Data = unpack( 'V', $this->Get( 4 ) );
|
$Data = unpack('V', $this->Get(4));
|
||||||
|
|
||||||
return (int)$Data[ 1 ];
|
return (int)$Data[ 1 ];
|
||||||
}
|
}
|
||||||
@@ -159,19 +154,18 @@
|
|||||||
/**
|
/**
|
||||||
* Read one string from buffer ending with null byte
|
* Read one string from buffer ending with null byte
|
||||||
*/
|
*/
|
||||||
public function GetString( ) : string
|
public function GetString(): string
|
||||||
{
|
{
|
||||||
$ZeroBytePosition = strpos( $this->Buffer, "\0", $this->Position );
|
$ZeroBytePosition = strpos($this->Buffer, "\0", $this->Position);
|
||||||
|
|
||||||
if( $ZeroBytePosition === false )
|
if ($ZeroBytePosition === false) {
|
||||||
{
|
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$String = $this->Get( $ZeroBytePosition - $this->Position );
|
$String = $this->Get($ZeroBytePosition - $this->Position);
|
||||||
|
|
||||||
$this->Position++;
|
$this->Position++;
|
||||||
|
|
||||||
return $String;
|
return $String;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* @author Pavel Djundik
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
@@ -10,10 +13,10 @@
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace xPaw\SourceQuery\Exception;
|
namespace xPaw\SourceQuery\Exception;
|
||||||
|
|
||||||
class AuthenticationException extends SourceQueryException
|
final class AuthenticationException extends SourceQueryException
|
||||||
{
|
{
|
||||||
const BAD_PASSWORD = 1;
|
public const BAD_PASSWORD = 1;
|
||||||
const BANNED = 2;
|
public const BANNED = 2;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* @author Pavel Djundik
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
@@ -10,9 +13,9 @@
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace xPaw\SourceQuery\Exception;
|
namespace xPaw\SourceQuery\Exception;
|
||||||
|
|
||||||
class InvalidArgumentException extends SourceQueryException
|
final class InvalidArgumentException extends SourceQueryException
|
||||||
{
|
{
|
||||||
const TIMEOUT_NOT_INTEGER = 1;
|
public const TIMEOUT_NOT_INTEGER = 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* @author Pavel Djundik
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
@@ -10,12 +13,12 @@
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace xPaw\SourceQuery\Exception;
|
namespace xPaw\SourceQuery\Exception;
|
||||||
|
|
||||||
class InvalidPacketException extends SourceQueryException
|
final class InvalidPacketException extends SourceQueryException
|
||||||
{
|
{
|
||||||
const PACKET_HEADER_MISMATCH = 1;
|
public const PACKET_HEADER_MISMATCH = 1;
|
||||||
const BUFFER_EMPTY = 2;
|
public const BUFFER_EMPTY = 2;
|
||||||
const BUFFER_NOT_EMPTY = 3;
|
public const BUFFER_NOT_EMPTY = 3;
|
||||||
const CHECKSUM_MISMATCH = 4;
|
public const CHECKSUM_MISMATCH = 4;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* @author Pavel Djundik
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
@@ -10,12 +13,12 @@
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace xPaw\SourceQuery\Exception;
|
namespace xPaw\SourceQuery\Exception;
|
||||||
|
|
||||||
class SocketException extends SourceQueryException
|
final class SocketException extends SourceQueryException
|
||||||
{
|
{
|
||||||
const COULD_NOT_CREATE_SOCKET = 1;
|
public const COULD_NOT_CREATE_SOCKET = 1;
|
||||||
const NOT_CONNECTED = 2;
|
public const NOT_CONNECTED = 2;
|
||||||
const CONNECTION_FAILED = 3;
|
public const CONNECTION_FAILED = 3;
|
||||||
const INVALID_ENGINE = 3;
|
public const INVALID_ENGINE = 3;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* @author Pavel Djundik
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
@@ -10,9 +13,9 @@
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace xPaw\SourceQuery\Exception;
|
namespace xPaw\SourceQuery\Exception;
|
||||||
|
|
||||||
abstract class SourceQueryException extends \Exception
|
abstract class SourceQueryException extends \Exception
|
||||||
{
|
{
|
||||||
// Base exception class
|
// Base exception class
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* @author Pavel Djundik
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
@@ -10,21 +13,21 @@
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace xPaw\SourceQuery;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class GoldSourceRcon
|
* Class GoldSourceRcon
|
||||||
*
|
*
|
||||||
* @package xPaw\SourceQuery
|
* @package xPaw\SourceQuery
|
||||||
*
|
*
|
||||||
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
* @uses AuthenticationException
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
* @uses InvalidPacketException
|
||||||
*/
|
*/
|
||||||
class GoldSourceRcon
|
final class GoldSourceRcon
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Points to socket class
|
* Points to socket class
|
||||||
*
|
*
|
||||||
@@ -35,28 +38,28 @@
|
|||||||
private string $RconPassword = '';
|
private string $RconPassword = '';
|
||||||
private string $RconChallenge = '';
|
private string $RconChallenge = '';
|
||||||
|
|
||||||
public function __construct( BaseSocket $Socket )
|
public function __construct(BaseSocket $Socket)
|
||||||
{
|
{
|
||||||
$this->Socket = $Socket;
|
$this->Socket = $Socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Close( ) : void
|
public function Close(): void
|
||||||
{
|
{
|
||||||
$this->RconChallenge = '';
|
$this->RconChallenge = '';
|
||||||
$this->RconPassword = '';
|
$this->RconPassword = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Open( ) : void
|
public function Open(): void
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Write( int $Header, string $String = '' ) : bool
|
public function Write(int $Header, string $String = ''): bool
|
||||||
{
|
{
|
||||||
$Command = pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
|
$Command = pack('cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String);
|
||||||
$Length = strlen( $Command );
|
$Length = strlen($Command);
|
||||||
|
|
||||||
return $Length === fwrite( $this->Socket->Socket, $Command, $Length );
|
return $Length === fwrite($this->Socket->Socket, $Command, $Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,82 +67,72 @@
|
|||||||
* @throws AuthenticationException
|
* @throws AuthenticationException
|
||||||
* @return Buffer
|
* @return Buffer
|
||||||
*/
|
*/
|
||||||
public function Read( int $Length = 1400 ) : Buffer
|
public function Read(int $Length = 1400): Buffer
|
||||||
{
|
{
|
||||||
// GoldSource RCON has same structure as Query
|
// GoldSource RCON has same structure as Query
|
||||||
$Buffer = $this->Socket->Read( );
|
$Buffer = $this->Socket->Read();
|
||||||
|
|
||||||
$StringBuffer = '';
|
$StringBuffer = '';
|
||||||
$ReadMore = false;
|
$ReadMore = false;
|
||||||
|
|
||||||
// There is no indentifier of the end, so we just need to continue reading
|
// There is no indentifier of the end, so we just need to continue reading
|
||||||
do
|
do {
|
||||||
{
|
$ReadMore = $Buffer->Remaining() > 0;
|
||||||
$ReadMore = $Buffer->Remaining( ) > 0;
|
|
||||||
|
|
||||||
if( $ReadMore )
|
if ($ReadMore) {
|
||||||
{
|
if ($Buffer->GetByte() !== SourceQuery::S2A_RCON) {
|
||||||
if( $Buffer->GetByte( ) !== SourceQuery::S2A_RCON )
|
throw new InvalidPacketException('Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH);
|
||||||
{
|
|
||||||
throw new InvalidPacketException( 'Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Packet = $Buffer->Get( );
|
$Packet = $Buffer->Get();
|
||||||
$StringBuffer .= $Packet;
|
$StringBuffer .= $Packet;
|
||||||
//$StringBuffer .= SubStr( $Packet, 0, -2 );
|
//$StringBuffer .= SubStr( $Packet, 0, -2 );
|
||||||
|
|
||||||
// Let's assume if this packet is not long enough, there are no more after this one
|
// Let's assume if this packet is not long enough, there are no more after this one
|
||||||
$ReadMore = strlen( $Packet ) > 1000; // use 1300?
|
$ReadMore = strlen($Packet) > 1000; // use 1300?
|
||||||
|
|
||||||
if( $ReadMore )
|
if ($ReadMore) {
|
||||||
{
|
$Buffer = $this->Socket->Read();
|
||||||
$Buffer = $this->Socket->Read( );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} while ($ReadMore);
|
||||||
while( $ReadMore );
|
|
||||||
|
|
||||||
$Trimmed = trim( $StringBuffer );
|
$Trimmed = trim($StringBuffer);
|
||||||
|
|
||||||
if( $Trimmed === 'Bad rcon_password.' )
|
if ($Trimmed === 'Bad rcon_password.') {
|
||||||
{
|
throw new AuthenticationException($Trimmed, AuthenticationException::BAD_PASSWORD);
|
||||||
throw new AuthenticationException( $Trimmed, AuthenticationException::BAD_PASSWORD );
|
} elseif ($Trimmed === 'You have been banned from this server.') {
|
||||||
}
|
throw new AuthenticationException($Trimmed, AuthenticationException::BANNED);
|
||||||
else if( $Trimmed === 'You have been banned from this server.' )
|
|
||||||
{
|
|
||||||
throw new AuthenticationException( $Trimmed, AuthenticationException::BANNED );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Buffer->Set( $Trimmed );
|
$Buffer->Set($Trimmed);
|
||||||
|
|
||||||
return $Buffer;
|
return $Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Command( string $Command ) : string
|
public function Command(string $Command): string
|
||||||
{
|
{
|
||||||
if( !$this->RconChallenge )
|
if (!$this->RconChallenge) {
|
||||||
{
|
throw new AuthenticationException('Tried to execute a RCON command before successful authorization.', AuthenticationException::BAD_PASSWORD);
|
||||||
throw new AuthenticationException( 'Tried to execute a RCON command before successful authorization.', AuthenticationException::BAD_PASSWORD );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Write( 0, 'rcon ' . $this->RconChallenge . ' "' . $this->RconPassword . '" ' . $Command . "\0" );
|
$this->Write(0, 'rcon ' . $this->RconChallenge . ' "' . $this->RconPassword . '" ' . $Command . "\0");
|
||||||
$Buffer = $this->Read( );
|
$Buffer = $this->Read();
|
||||||
|
|
||||||
return $Buffer->Get( );
|
return $Buffer->Get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Authorize( string $Password ) : void
|
public function Authorize(string $Password): void
|
||||||
{
|
{
|
||||||
$this->RconPassword = $Password;
|
$this->RconPassword = $Password;
|
||||||
|
|
||||||
$this->Write( 0, 'challenge rcon' );
|
$this->Write(0, 'challenge rcon');
|
||||||
$Buffer = $this->Socket->Read( );
|
$Buffer = $this->Socket->Read();
|
||||||
|
|
||||||
if( $Buffer->Get( 14 ) !== 'challenge rcon' )
|
if ($Buffer->Get(14) !== 'challenge rcon') {
|
||||||
{
|
throw new AuthenticationException('Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD);
|
||||||
throw new AuthenticationException( 'Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->RconChallenge = trim( $Buffer->Get( ) );
|
$this->RconChallenge = trim($Buffer->Get());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
+35
-35
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* @author Pavel Djundik
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
@@ -10,56 +13,54 @@
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace xPaw\SourceQuery;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
use xPaw\SourceQuery\Exception\SocketException;
|
use xPaw\SourceQuery\Exception\SocketException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Socket
|
* Class Socket
|
||||||
*
|
*
|
||||||
* @package xPaw\SourceQuery
|
* @package xPaw\SourceQuery
|
||||||
*
|
*
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
* @uses InvalidPacketException
|
||||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
* @uses SocketException
|
||||||
*/
|
*/
|
||||||
class Socket extends BaseSocket
|
final class Socket extends BaseSocket
|
||||||
|
{
|
||||||
|
public function Close(): void
|
||||||
{
|
{
|
||||||
public function Close( ) : void
|
if ($this->Socket !== null) {
|
||||||
{
|
fclose($this->Socket);
|
||||||
if( $this->Socket !== null )
|
|
||||||
{
|
|
||||||
fclose( $this->Socket );
|
|
||||||
|
|
||||||
$this->Socket = null;
|
$this->Socket = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void
|
public function Open(string $Address, int $Port, int $Timeout, int $Engine): void
|
||||||
{
|
{
|
||||||
$this->Timeout = $Timeout;
|
$this->Timeout = $Timeout;
|
||||||
$this->Engine = $Engine;
|
$this->Engine = $Engine;
|
||||||
$this->Port = $Port;
|
$this->Port = $Port;
|
||||||
$this->Address = $Address;
|
$this->Address = $Address;
|
||||||
|
|
||||||
$Socket = @fsockopen( 'udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout );
|
$Socket = @fsockopen('udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout);
|
||||||
|
|
||||||
if( $ErrNo || $Socket === false )
|
if ($ErrNo || $Socket === false) {
|
||||||
{
|
throw new SocketException('Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET);
|
||||||
throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Socket = $Socket;
|
$this->Socket = $Socket;
|
||||||
stream_set_timeout( $this->Socket, $Timeout );
|
stream_set_timeout($this->Socket, $Timeout);
|
||||||
stream_set_blocking( $this->Socket, true );
|
stream_set_blocking($this->Socket, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Write( int $Header, string $String = '' ) : bool
|
public function Write(int $Header, string $String = ''): bool
|
||||||
{
|
{
|
||||||
$Command = pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
|
$Command = pack('ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String);
|
||||||
$Length = strlen( $Command );
|
$Length = strlen($Command);
|
||||||
|
|
||||||
return $Length === fwrite( $this->Socket, $Command, $Length );
|
return $Length === fwrite($this->Socket, $Command, $Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,27 +70,26 @@
|
|||||||
*
|
*
|
||||||
* @return Buffer Buffer
|
* @return Buffer Buffer
|
||||||
*/
|
*/
|
||||||
public function Read( int $Length = 1400 ) : Buffer
|
public function Read(int $Length = 1400): Buffer
|
||||||
{
|
{
|
||||||
$Buffer = new Buffer( );
|
$Buffer = new Buffer();
|
||||||
$Buffer->Set( fread( $this->Socket, $Length ) );
|
$Buffer->Set(fread($this->Socket, $Length));
|
||||||
|
|
||||||
$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
|
$this->ReadInternal($Buffer, $Length, [ $this, 'Sherlock' ]);
|
||||||
|
|
||||||
return $Buffer;
|
return $Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Sherlock( Buffer $Buffer, int $Length ) : bool
|
public function Sherlock(Buffer $Buffer, int $Length): bool
|
||||||
{
|
{
|
||||||
$Data = fread( $this->Socket, $Length );
|
$Data = fread($this->Socket, $Length);
|
||||||
|
|
||||||
if( strlen( $Data ) < 4 )
|
if (strlen($Data) < 4) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$Buffer->Set( $Data );
|
$Buffer->Set($Data);
|
||||||
|
|
||||||
return $Buffer->GetLong( ) === -2;
|
return $Buffer->GetLong() === -2;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
+197
-231
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* This class provides the public interface to the PHP-Source-Query library.
|
* This class provides the public interface to the PHP-Source-Query library.
|
||||||
*
|
*
|
||||||
* @author Pavel Djundik
|
* @author Pavel Djundik
|
||||||
@@ -10,63 +13,63 @@
|
|||||||
* @license GNU Lesser General Public License, version 2.1
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace xPaw\SourceQuery;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||||
use xPaw\SourceQuery\Exception\InvalidArgumentException;
|
use xPaw\SourceQuery\Exception\InvalidArgumentException;
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
use xPaw\SourceQuery\Exception\SocketException;
|
use xPaw\SourceQuery\Exception\SocketException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SourceQuery
|
* Class SourceQuery
|
||||||
*
|
*
|
||||||
* @package xPaw\SourceQuery
|
* @package xPaw\SourceQuery
|
||||||
*
|
*
|
||||||
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
* @uses AuthenticationException
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidArgumentException
|
* @uses InvalidArgumentException
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
* @uses InvalidPacketException
|
||||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
* @uses SocketException
|
||||||
*/
|
*/
|
||||||
class SourceQuery
|
final class SourceQuery
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Engines
|
* Engines
|
||||||
*/
|
*/
|
||||||
const GOLDSOURCE = 0;
|
public const GOLDSOURCE = 0;
|
||||||
const SOURCE = 1;
|
public const SOURCE = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Packets sent
|
* Packets sent
|
||||||
*/
|
*/
|
||||||
const A2A_PING = 0x69;
|
private const A2A_PING = 0x69;
|
||||||
const A2S_INFO = 0x54;
|
private const A2S_INFO = 0x54;
|
||||||
const A2S_PLAYER = 0x55;
|
private const A2S_PLAYER = 0x55;
|
||||||
const A2S_RULES = 0x56;
|
private const A2S_RULES = 0x56;
|
||||||
const A2S_SERVERQUERY_GETCHALLENGE = 0x57;
|
private const A2S_SERVERQUERY_GETCHALLENGE = 0x57;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Packets received
|
* Packets received
|
||||||
*/
|
*/
|
||||||
const A2A_ACK = 0x6A;
|
private const A2A_ACK = 0x6A;
|
||||||
const S2C_CHALLENGE = 0x41;
|
private const S2C_CHALLENGE = 0x41;
|
||||||
const S2A_INFO_SRC = 0x49;
|
private const S2A_INFO_SRC = 0x49;
|
||||||
const S2A_INFO_OLD = 0x6D; // Old GoldSource, HLTV uses it (actually called S2A_INFO_DETAILED)
|
private const S2A_INFO_OLD = 0x6D; // Old GoldSource, HLTV uses it (actually called S2A_INFO_DETAILED)
|
||||||
const S2A_PLAYER = 0x44;
|
private const S2A_PLAYER = 0x44;
|
||||||
const S2A_RULES = 0x45;
|
private const S2A_RULES = 0x45;
|
||||||
const S2A_RCON = 0x6C;
|
public const S2A_RCON = 0x6C;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Source rcon sent
|
* Source rcon sent
|
||||||
*/
|
*/
|
||||||
const SERVERDATA_REQUESTVALUE = 0;
|
public const SERVERDATA_REQUESTVALUE = 0;
|
||||||
const SERVERDATA_EXECCOMMAND = 2;
|
public const SERVERDATA_EXECCOMMAND = 2;
|
||||||
const SERVERDATA_AUTH = 3;
|
public const SERVERDATA_AUTH = 3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Source rcon received
|
* Source rcon received
|
||||||
*/
|
*/
|
||||||
const SERVERDATA_RESPONSE_VALUE = 0;
|
public const SERVERDATA_RESPONSE_VALUE = 0;
|
||||||
const SERVERDATA_AUTH_RESPONSE = 2;
|
public const SERVERDATA_AUTH_RESPONSE = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Points to rcon class
|
* Points to rcon class
|
||||||
@@ -95,14 +98,14 @@
|
|||||||
*/
|
*/
|
||||||
private bool $UseOldGetChallengeMethod = false;
|
private bool $UseOldGetChallengeMethod = false;
|
||||||
|
|
||||||
public function __construct( BaseSocket $Socket = null )
|
public function __construct(BaseSocket $Socket = null)
|
||||||
{
|
{
|
||||||
$this->Socket = $Socket ?: new Socket( );
|
$this->Socket = $Socket ?: new Socket();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct( )
|
public function __destruct()
|
||||||
{
|
{
|
||||||
$this->Disconnect( );
|
$this->Disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -116,16 +119,15 @@
|
|||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
* @throws SocketException
|
* @throws SocketException
|
||||||
*/
|
*/
|
||||||
public function Connect( string $Address, int $Port, int $Timeout = 3, int $Engine = self::SOURCE ) : void
|
public function Connect(string $Address, int $Port, int $Timeout = 3, int $Engine = self::SOURCE): void
|
||||||
{
|
{
|
||||||
$this->Disconnect( );
|
$this->Disconnect();
|
||||||
|
|
||||||
if( $Timeout < 0 )
|
if ($Timeout < 0) {
|
||||||
{
|
throw new InvalidArgumentException('Timeout must be a positive integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER);
|
||||||
throw new InvalidArgumentException( 'Timeout must be a positive integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Socket->Open( $Address, $Port, $Timeout, $Engine );
|
$this->Socket->Open($Address, $Port, $Timeout, $Engine);
|
||||||
|
|
||||||
$this->Connected = true;
|
$this->Connected = true;
|
||||||
}
|
}
|
||||||
@@ -137,7 +139,7 @@
|
|||||||
*
|
*
|
||||||
* @returns bool Previous value
|
* @returns bool Previous value
|
||||||
*/
|
*/
|
||||||
public function SetUseOldGetChallengeMethod( bool $Value ) : bool
|
public function SetUseOldGetChallengeMethod(bool $Value): bool
|
||||||
{
|
{
|
||||||
$Previous = $this->UseOldGetChallengeMethod;
|
$Previous = $this->UseOldGetChallengeMethod;
|
||||||
|
|
||||||
@@ -149,16 +151,15 @@
|
|||||||
/**
|
/**
|
||||||
* Closes all open connections
|
* Closes all open connections
|
||||||
*/
|
*/
|
||||||
public function Disconnect( ) : void
|
public function Disconnect(): void
|
||||||
{
|
{
|
||||||
$this->Connected = false;
|
$this->Connected = false;
|
||||||
$this->Challenge = '';
|
$this->Challenge = '';
|
||||||
|
|
||||||
$this->Socket->Close( );
|
$this->Socket->Close();
|
||||||
|
|
||||||
if( $this->Rcon )
|
if ($this->Rcon) {
|
||||||
{
|
$this->Rcon->Close();
|
||||||
$this->Rcon->Close( );
|
|
||||||
|
|
||||||
$this->Rcon = null;
|
$this->Rcon = null;
|
||||||
}
|
}
|
||||||
@@ -173,17 +174,16 @@
|
|||||||
*
|
*
|
||||||
* @return bool True on success, false on failure
|
* @return bool True on success, false on failure
|
||||||
*/
|
*/
|
||||||
public function Ping( ) : bool
|
public function Ping(): bool
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if (!$this->Connected) {
|
||||||
{
|
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
|
||||||
throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Socket->Write( self::A2A_PING );
|
$this->Socket->Write(self::A2A_PING);
|
||||||
$Buffer = $this->Socket->Read( );
|
$Buffer = $this->Socket->Read();
|
||||||
|
|
||||||
return $Buffer->GetByte( ) === self::A2A_ACK;
|
return $Buffer->GetByte() === self::A2A_ACK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -194,171 +194,151 @@
|
|||||||
*
|
*
|
||||||
* @return array Returns an array with information on success
|
* @return array Returns an array with information on success
|
||||||
*/
|
*/
|
||||||
public function GetInfo( ) : array
|
public function GetInfo(): array
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if (!$this->Connected) {
|
||||||
{
|
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
|
||||||
throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if( $this->Challenge )
|
if ($this->Challenge) {
|
||||||
{
|
$this->Socket->Write(self::A2S_INFO, "Source Engine Query\0" . $this->Challenge);
|
||||||
$this->Socket->Write( self::A2S_INFO, "Source Engine Query\0" . $this->Challenge );
|
} else {
|
||||||
}
|
$this->Socket->Write(self::A2S_INFO, "Source Engine Query\0");
|
||||||
else
|
|
||||||
{
|
|
||||||
$this->Socket->Write( self::A2S_INFO, "Source Engine Query\0" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Buffer = $this->Socket->Read( );
|
$Buffer = $this->Socket->Read();
|
||||||
$Type = $Buffer->GetByte( );
|
$Type = $Buffer->GetByte();
|
||||||
$Server = [];
|
$Server = [];
|
||||||
|
|
||||||
if( $Type === self::S2C_CHALLENGE )
|
if ($Type === self::S2C_CHALLENGE) {
|
||||||
{
|
$this->Challenge = $Buffer->Get(4);
|
||||||
$this->Challenge = $Buffer->Get( 4 );
|
|
||||||
|
|
||||||
$this->Socket->Write( self::A2S_INFO, "Source Engine Query\0" . $this->Challenge );
|
$this->Socket->Write(self::A2S_INFO, "Source Engine Query\0" . $this->Challenge);
|
||||||
$Buffer = $this->Socket->Read( );
|
$Buffer = $this->Socket->Read();
|
||||||
$Type = $Buffer->GetByte( );
|
$Type = $Buffer->GetByte();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Old GoldSource protocol, HLTV still uses it
|
// Old GoldSource protocol, HLTV still uses it
|
||||||
if( $Type === self::S2A_INFO_OLD && $this->Socket->Engine === self::GOLDSOURCE )
|
if ($Type === self::S2A_INFO_OLD && $this->Socket->Engine === self::GOLDSOURCE) {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* If we try to read data again, and we get the result with type S2A_INFO (0x49)
|
* If we try to read data again, and we get the result with type S2A_INFO (0x49)
|
||||||
* That means this server is running dproto,
|
* That means this server is running dproto,
|
||||||
* Because it sends answer for both protocols
|
* Because it sends answer for both protocols
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$Server[ 'Address' ] = $Buffer->GetString( );
|
$Server[ 'Address' ] = $Buffer->GetString();
|
||||||
$Server[ 'HostName' ] = $Buffer->GetString( );
|
$Server[ 'HostName' ] = $Buffer->GetString();
|
||||||
$Server[ 'Map' ] = $Buffer->GetString( );
|
$Server[ 'Map' ] = $Buffer->GetString();
|
||||||
$Server[ 'ModDir' ] = $Buffer->GetString( );
|
$Server[ 'ModDir' ] = $Buffer->GetString();
|
||||||
$Server[ 'ModDesc' ] = $Buffer->GetString( );
|
$Server[ 'ModDesc' ] = $Buffer->GetString();
|
||||||
$Server[ 'Players' ] = $Buffer->GetByte( );
|
$Server[ 'Players' ] = $Buffer->GetByte();
|
||||||
$Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
|
$Server[ 'MaxPlayers' ] = $Buffer->GetByte();
|
||||||
$Server[ 'Protocol' ] = $Buffer->GetByte( );
|
$Server[ 'Protocol' ] = $Buffer->GetByte();
|
||||||
$Server[ 'Dedicated' ] = chr( $Buffer->GetByte( ) );
|
$Server[ 'Dedicated' ] = chr($Buffer->GetByte());
|
||||||
$Server[ 'Os' ] = chr( $Buffer->GetByte( ) );
|
$Server[ 'Os' ] = chr($Buffer->GetByte());
|
||||||
$Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
|
$Server[ 'Password' ] = $Buffer->GetByte() === 1;
|
||||||
$Server[ 'IsMod' ] = $Buffer->GetByte( ) === 1;
|
$Server[ 'IsMod' ] = $Buffer->GetByte() === 1;
|
||||||
|
|
||||||
if( $Server[ 'IsMod' ] )
|
if ($Server[ 'IsMod' ]) {
|
||||||
{
|
|
||||||
$Mod = [];
|
$Mod = [];
|
||||||
$Mod[ 'Url' ] = $Buffer->GetString( );
|
$Mod[ 'Url' ] = $Buffer->GetString();
|
||||||
$Mod[ 'Download' ] = $Buffer->GetString( );
|
$Mod[ 'Download' ] = $Buffer->GetString();
|
||||||
$Buffer->Get( 1 ); // NULL byte
|
$Buffer->Get(1); // NULL byte
|
||||||
$Mod[ 'Version' ] = $Buffer->GetLong( );
|
$Mod[ 'Version' ] = $Buffer->GetLong();
|
||||||
$Mod[ 'Size' ] = $Buffer->GetLong( );
|
$Mod[ 'Size' ] = $Buffer->GetLong();
|
||||||
$Mod[ 'ServerSide' ] = $Buffer->GetByte( ) === 1;
|
$Mod[ 'ServerSide' ] = $Buffer->GetByte() === 1;
|
||||||
$Mod[ 'CustomDLL' ] = $Buffer->GetByte( ) === 1;
|
$Mod[ 'CustomDLL' ] = $Buffer->GetByte() === 1;
|
||||||
$Server[ 'Mod' ] = $Mod;
|
$Server[ 'Mod' ] = $Mod;
|
||||||
}
|
}
|
||||||
|
|
||||||
$Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
|
$Server[ 'Secure' ] = $Buffer->GetByte() === 1;
|
||||||
$Server[ 'Bots' ] = $Buffer->GetByte( );
|
$Server[ 'Bots' ] = $Buffer->GetByte();
|
||||||
|
|
||||||
return $Server;
|
return $Server;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( $Type !== self::S2A_INFO_SRC )
|
if ($Type !== self::S2A_INFO_SRC) {
|
||||||
{
|
throw new InvalidPacketException('GetInfo: Packet header mismatch. (0x' . dechex($Type) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
|
||||||
throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Server[ 'Protocol' ] = $Buffer->GetByte( );
|
$Server[ 'Protocol' ] = $Buffer->GetByte();
|
||||||
$Server[ 'HostName' ] = $Buffer->GetString( );
|
$Server[ 'HostName' ] = $Buffer->GetString();
|
||||||
$Server[ 'Map' ] = $Buffer->GetString( );
|
$Server[ 'Map' ] = $Buffer->GetString();
|
||||||
$Server[ 'ModDir' ] = $Buffer->GetString( );
|
$Server[ 'ModDir' ] = $Buffer->GetString();
|
||||||
$Server[ 'ModDesc' ] = $Buffer->GetString( );
|
$Server[ 'ModDesc' ] = $Buffer->GetString();
|
||||||
$Server[ 'AppID' ] = $Buffer->GetShort( );
|
$Server[ 'AppID' ] = $Buffer->GetShort();
|
||||||
$Server[ 'Players' ] = $Buffer->GetByte( );
|
$Server[ 'Players' ] = $Buffer->GetByte();
|
||||||
$Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
|
$Server[ 'MaxPlayers' ] = $Buffer->GetByte();
|
||||||
$Server[ 'Bots' ] = $Buffer->GetByte( );
|
$Server[ 'Bots' ] = $Buffer->GetByte();
|
||||||
$Server[ 'Dedicated' ] = chr( $Buffer->GetByte( ) );
|
$Server[ 'Dedicated' ] = chr($Buffer->GetByte());
|
||||||
$Server[ 'Os' ] = chr( $Buffer->GetByte( ) );
|
$Server[ 'Os' ] = chr($Buffer->GetByte());
|
||||||
$Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
|
$Server[ 'Password' ] = $Buffer->GetByte() === 1;
|
||||||
$Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
|
$Server[ 'Secure' ] = $Buffer->GetByte() === 1;
|
||||||
|
|
||||||
// The Ship (they violate query protocol spec by modifying the response)
|
// The Ship (they violate query protocol spec by modifying the response)
|
||||||
if( $Server[ 'AppID' ] === 2400 )
|
if ($Server[ 'AppID' ] === 2400) {
|
||||||
{
|
$Server[ 'GameMode' ] = $Buffer->GetByte();
|
||||||
$Server[ 'GameMode' ] = $Buffer->GetByte( );
|
$Server[ 'WitnessCount' ] = $Buffer->GetByte();
|
||||||
$Server[ 'WitnessCount' ] = $Buffer->GetByte( );
|
$Server[ 'WitnessTime' ] = $Buffer->GetByte();
|
||||||
$Server[ 'WitnessTime' ] = $Buffer->GetByte( );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Server[ 'Version' ] = $Buffer->GetString( );
|
$Server[ 'Version' ] = $Buffer->GetString();
|
||||||
|
|
||||||
// Extra Data Flags
|
// Extra Data Flags
|
||||||
if( $Buffer->Remaining( ) > 0 )
|
if ($Buffer->Remaining() > 0) {
|
||||||
{
|
$Server[ 'ExtraDataFlags' ] = $Flags = $Buffer->GetByte();
|
||||||
$Server[ 'ExtraDataFlags' ] = $Flags = $Buffer->GetByte( );
|
|
||||||
|
|
||||||
// S2A_EXTRA_DATA_HAS_GAME_PORT - Next 2 bytes include the game port.
|
// S2A_EXTRA_DATA_HAS_GAME_PORT - Next 2 bytes include the game port.
|
||||||
if( $Flags & 0x80 )
|
if ($Flags & 0x80) {
|
||||||
{
|
$Server[ 'GamePort' ] = $Buffer->GetShort();
|
||||||
$Server[ 'GamePort' ] = $Buffer->GetShort( );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// S2A_EXTRA_DATA_HAS_STEAMID - Next 8 bytes are the steamID
|
// S2A_EXTRA_DATA_HAS_STEAMID - Next 8 bytes are the steamID
|
||||||
// Want to play around with this?
|
// Want to play around with this?
|
||||||
// You can use https://github.com/xPaw/SteamID.php
|
// You can use https://github.com/xPaw/SteamID.php
|
||||||
if( $Flags & 0x10 )
|
if ($Flags & 0x10) {
|
||||||
{
|
$SteamIDLower = $Buffer->GetUnsignedLong();
|
||||||
$SteamIDLower = $Buffer->GetUnsignedLong( );
|
$SteamIDInstance = $Buffer->GetUnsignedLong(); // This gets shifted by 32 bits, which should be steamid instance
|
||||||
$SteamIDInstance = $Buffer->GetUnsignedLong( ); // This gets shifted by 32 bits, which should be steamid instance
|
|
||||||
$SteamID = 0;
|
$SteamID = 0;
|
||||||
|
|
||||||
if( PHP_INT_SIZE === 4 )
|
if (PHP_INT_SIZE === 4) {
|
||||||
{
|
if (extension_loaded('gmp')) {
|
||||||
if( extension_loaded( 'gmp' ) )
|
$SteamIDLower = gmp_abs($SteamIDLower);
|
||||||
{
|
$SteamIDInstance = gmp_abs($SteamIDInstance);
|
||||||
$SteamIDLower = gmp_abs( $SteamIDLower );
|
$SteamID = gmp_strval(gmp_or($SteamIDLower, gmp_mul($SteamIDInstance, gmp_pow(2, 32))));
|
||||||
$SteamIDInstance = gmp_abs( $SteamIDInstance );
|
} else {
|
||||||
$SteamID = gmp_strval( gmp_or( $SteamIDLower, gmp_mul( $SteamIDInstance, gmp_pow( 2, 32 ) ) ) );
|
throw new \RuntimeException('Either 64-bit PHP installation or "gmp" module is required to correctly parse server\'s steamid.');
|
||||||
}
|
}
|
||||||
else
|
} else {
|
||||||
{
|
$SteamID = $SteamIDLower | ($SteamIDInstance << 32);
|
||||||
throw new \RuntimeException( 'Either 64-bit PHP installation or "gmp" module is required to correctly parse server\'s steamid.' );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$SteamID = $SteamIDLower | ( $SteamIDInstance << 32 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Server[ 'SteamID' ] = $SteamID;
|
$Server[ 'SteamID' ] = $SteamID;
|
||||||
|
|
||||||
unset( $SteamIDLower, $SteamIDInstance, $SteamID );
|
unset($SteamIDLower, $SteamIDInstance, $SteamID);
|
||||||
}
|
}
|
||||||
|
|
||||||
// S2A_EXTRA_DATA_HAS_SPECTATOR_DATA - Next 2 bytes include the spectator port, then the spectator server name.
|
// S2A_EXTRA_DATA_HAS_SPECTATOR_DATA - Next 2 bytes include the spectator port, then the spectator server name.
|
||||||
if( $Flags & 0x40 )
|
if ($Flags & 0x40) {
|
||||||
{
|
$Server[ 'SpecPort' ] = $Buffer->GetShort();
|
||||||
$Server[ 'SpecPort' ] = $Buffer->GetShort( );
|
$Server[ 'SpecName' ] = $Buffer->GetString();
|
||||||
$Server[ 'SpecName' ] = $Buffer->GetString( );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// S2A_EXTRA_DATA_HAS_GAMETAG_DATA - Next bytes are the game tag string
|
// S2A_EXTRA_DATA_HAS_GAMETAG_DATA - Next bytes are the game tag string
|
||||||
if( $Flags & 0x20 )
|
if ($Flags & 0x20) {
|
||||||
{
|
$Server[ 'GameTags' ] = $Buffer->GetString();
|
||||||
$Server[ 'GameTags' ] = $Buffer->GetString( );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// S2A_EXTRA_DATA_GAMEID - Next 8 bytes are the gameID of the server
|
// S2A_EXTRA_DATA_GAMEID - Next 8 bytes are the gameID of the server
|
||||||
if( $Flags & 0x01 )
|
if ($Flags & 0x01) {
|
||||||
{
|
$Server[ 'GameID' ] = $Buffer->GetUnsignedLong() | ($Buffer->GetUnsignedLong() << 32);
|
||||||
$Server[ 'GameID' ] = $Buffer->GetUnsignedLong( ) | ( $Buffer->GetUnsignedLong( ) << 32 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if( $Buffer->Remaining( ) > 0 )
|
if ($Buffer->Remaining() > 0) {
|
||||||
{
|
throw new InvalidPacketException(
|
||||||
throw new InvalidPacketException( 'GetInfo: unread data? ' . $Buffer->Remaining( ) . ' bytes remaining in the buffer. Please report it to the library developer.',
|
'GetInfo: unread data? ' . $Buffer->Remaining() . ' bytes remaining in the buffer. Please report it to the library developer.',
|
||||||
InvalidPacketException::BUFFER_NOT_EMPTY );
|
InvalidPacketException::BUFFER_NOT_EMPTY
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -373,37 +353,34 @@
|
|||||||
*
|
*
|
||||||
* @return array Returns an array with players on success
|
* @return array Returns an array with players on success
|
||||||
*/
|
*/
|
||||||
public function GetPlayers( ) : array
|
public function GetPlayers(): array
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if (!$this->Connected) {
|
||||||
{
|
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
|
||||||
throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->GetChallenge( self::A2S_PLAYER, self::S2A_PLAYER );
|
$this->GetChallenge(self::A2S_PLAYER, self::S2A_PLAYER);
|
||||||
|
|
||||||
$this->Socket->Write( self::A2S_PLAYER, $this->Challenge );
|
$this->Socket->Write(self::A2S_PLAYER, $this->Challenge);
|
||||||
$Buffer = $this->Socket->Read( 14000 ); // Moronic Arma 3 developers do not split their packets, so we have to read more data
|
$Buffer = $this->Socket->Read(14000); // Moronic Arma 3 developers do not split their packets, so we have to read more data
|
||||||
// This violates the protocol spec, and they probably should fix it: https://developer.valvesoftware.com/wiki/Server_queries#Protocol
|
// This violates the protocol spec, and they probably should fix it: https://developer.valvesoftware.com/wiki/Server_queries#Protocol
|
||||||
|
|
||||||
$Type = $Buffer->GetByte( );
|
$Type = $Buffer->GetByte();
|
||||||
|
|
||||||
if( $Type !== self::S2A_PLAYER )
|
if ($Type !== self::S2A_PLAYER) {
|
||||||
{
|
throw new InvalidPacketException('GetPlayers: Packet header mismatch. (0x' . dechex($Type) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
|
||||||
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Players = [];
|
$Players = [];
|
||||||
$Count = $Buffer->GetByte( );
|
$Count = $Buffer->GetByte();
|
||||||
|
|
||||||
while( $Count-- > 0 && $Buffer->Remaining( ) > 0 )
|
while ($Count-- > 0 && $Buffer->Remaining() > 0) {
|
||||||
{
|
|
||||||
$Player = [];
|
$Player = [];
|
||||||
$Player[ 'Id' ] = $Buffer->GetByte( ); // PlayerID, is it just always 0?
|
$Player[ 'Id' ] = $Buffer->GetByte(); // PlayerID, is it just always 0?
|
||||||
$Player[ 'Name' ] = $Buffer->GetString( );
|
$Player[ 'Name' ] = $Buffer->GetString();
|
||||||
$Player[ 'Frags' ] = $Buffer->GetLong( );
|
$Player[ 'Frags' ] = $Buffer->GetLong();
|
||||||
$Player[ 'Time' ] = (int)$Buffer->GetFloat( );
|
$Player[ 'Time' ] = (int)$Buffer->GetFloat();
|
||||||
$Player[ 'TimeF' ] = gmdate( ( $Player[ 'Time' ] > 3600 ? 'H:i:s' : 'i:s' ), $Player[ 'Time' ] );
|
$Player[ 'TimeF' ] = gmdate(($Player[ 'Time' ] > 3600 ? 'H:i:s' : 'i:s'), $Player[ 'Time' ]);
|
||||||
|
|
||||||
$Players[ ] = $Player;
|
$Players[ ] = $Player;
|
||||||
}
|
}
|
||||||
@@ -419,35 +396,31 @@
|
|||||||
*
|
*
|
||||||
* @return array Returns an array with rules on success
|
* @return array Returns an array with rules on success
|
||||||
*/
|
*/
|
||||||
public function GetRules( ) : array
|
public function GetRules(): array
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if (!$this->Connected) {
|
||||||
{
|
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
|
||||||
throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->GetChallenge( self::A2S_RULES, self::S2A_RULES );
|
$this->GetChallenge(self::A2S_RULES, self::S2A_RULES);
|
||||||
|
|
||||||
$this->Socket->Write( self::A2S_RULES, $this->Challenge );
|
$this->Socket->Write(self::A2S_RULES, $this->Challenge);
|
||||||
$Buffer = $this->Socket->Read( );
|
$Buffer = $this->Socket->Read();
|
||||||
|
|
||||||
$Type = $Buffer->GetByte( );
|
$Type = $Buffer->GetByte();
|
||||||
|
|
||||||
if( $Type !== self::S2A_RULES )
|
if ($Type !== self::S2A_RULES) {
|
||||||
{
|
throw new InvalidPacketException('GetRules: Packet header mismatch. (0x' . dechex($Type) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
|
||||||
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Rules = [];
|
$Rules = [];
|
||||||
$Count = $Buffer->GetShort( );
|
$Count = $Buffer->GetShort();
|
||||||
|
|
||||||
while( $Count-- > 0 && $Buffer->Remaining( ) > 0 )
|
while ($Count-- > 0 && $Buffer->Remaining() > 0) {
|
||||||
{
|
$Rule = $Buffer->GetString();
|
||||||
$Rule = $Buffer->GetString( );
|
$Value = $Buffer->GetString();
|
||||||
$Value = $Buffer->GetString( );
|
|
||||||
|
|
||||||
if( !empty( $Rule ) )
|
if (!empty($Rule)) {
|
||||||
{
|
|
||||||
$Rules[ $Rule ] = $Value;
|
$Rules[ $Rule ] = $Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -460,28 +433,25 @@
|
|||||||
*
|
*
|
||||||
* @throws InvalidPacketException
|
* @throws InvalidPacketException
|
||||||
*/
|
*/
|
||||||
private function GetChallenge( int $Header, int $ExpectedResult ) : void
|
private function GetChallenge(int $Header, int $ExpectedResult): void
|
||||||
{
|
|
||||||
if( $this->Challenge )
|
|
||||||
{
|
{
|
||||||
|
if ($this->Challenge) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( $this->UseOldGetChallengeMethod )
|
if ($this->UseOldGetChallengeMethod) {
|
||||||
{
|
|
||||||
$Header = self::A2S_SERVERQUERY_GETCHALLENGE;
|
$Header = self::A2S_SERVERQUERY_GETCHALLENGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Socket->Write( $Header, "\xFF\xFF\xFF\xFF" );
|
$this->Socket->Write($Header, "\xFF\xFF\xFF\xFF");
|
||||||
$Buffer = $this->Socket->Read( );
|
$Buffer = $this->Socket->Read();
|
||||||
|
|
||||||
$Type = $Buffer->GetByte( );
|
$Type = $Buffer->GetByte();
|
||||||
|
|
||||||
switch( $Type )
|
switch ($Type) {
|
||||||
{
|
|
||||||
case self::S2C_CHALLENGE:
|
case self::S2C_CHALLENGE:
|
||||||
{
|
{
|
||||||
$this->Challenge = $Buffer->Get( 4 );
|
$this->Challenge = $Buffer->Get(4);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -493,11 +463,11 @@
|
|||||||
}
|
}
|
||||||
case 0:
|
case 0:
|
||||||
{
|
{
|
||||||
throw new InvalidPacketException( 'GetChallenge: Failed to get challenge.' );
|
throw new InvalidPacketException('GetChallenge: Failed to get challenge.');
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
throw new InvalidPacketException('GetChallenge: Packet header mismatch. (0x' . dechex($Type) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -511,35 +481,33 @@
|
|||||||
* @throws InvalidPacketException
|
* @throws InvalidPacketException
|
||||||
* @throws SocketException
|
* @throws SocketException
|
||||||
*/
|
*/
|
||||||
public function SetRconPassword( string $Password ) : void
|
public function SetRconPassword(string $Password): void
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if (!$this->Connected) {
|
||||||
{
|
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
|
||||||
throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch( $this->Socket->Engine )
|
switch ($this->Socket->Engine) {
|
||||||
{
|
|
||||||
case SourceQuery::GOLDSOURCE:
|
case SourceQuery::GOLDSOURCE:
|
||||||
{
|
{
|
||||||
$this->Rcon = new GoldSourceRcon( $this->Socket );
|
$this->Rcon = new GoldSourceRcon($this->Socket);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SourceQuery::SOURCE:
|
case SourceQuery::SOURCE:
|
||||||
{
|
{
|
||||||
$this->Rcon = new SourceRcon( $this->Socket );
|
$this->Rcon = new SourceRcon($this->Socket);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE );
|
throw new SocketException('Unknown engine.', SocketException::INVALID_ENGINE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Rcon->Open( );
|
$this->Rcon->Open();
|
||||||
$this->Rcon->Authorize( $Password );
|
$this->Rcon->Authorize($Password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -553,18 +521,16 @@
|
|||||||
*
|
*
|
||||||
* @return string Answer from server in string
|
* @return string Answer from server in string
|
||||||
*/
|
*/
|
||||||
public function Rcon( string $Command ) : string
|
public function Rcon(string $Command): string
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if (!$this->Connected) {
|
||||||
{
|
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
|
||||||
throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if( $this->Rcon === null )
|
if ($this->Rcon === null) {
|
||||||
{
|
throw new SocketException('You must set a RCON password before trying to execute a RCON command.', SocketException::NOT_CONNECTED);
|
||||||
throw new SocketException( 'You must set a RCON password before trying to execute a RCON command.', SocketException::NOT_CONNECTED );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->Rcon->Command( $Command );
|
return $this->Rcon->Command($Command);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
+77
-90
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* @author Pavel Djundik
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
@@ -10,23 +13,23 @@
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace xPaw\SourceQuery;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
use xPaw\SourceQuery\Exception\SocketException;
|
use xPaw\SourceQuery\Exception\SocketException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SourceRcon
|
* Class SourceRcon
|
||||||
*
|
*
|
||||||
* @package xPaw\SourceQuery
|
* @package xPaw\SourceQuery
|
||||||
*
|
*
|
||||||
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
* @uses AuthenticationException
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
* @uses InvalidPacketException
|
||||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
* @uses SocketException
|
||||||
*/
|
*/
|
||||||
class SourceRcon
|
final class SourceRcon
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Points to socket class
|
* Points to socket class
|
||||||
*/
|
*/
|
||||||
@@ -36,16 +39,15 @@
|
|||||||
private $RconSocket;
|
private $RconSocket;
|
||||||
private int $RconRequestId = 0;
|
private int $RconRequestId = 0;
|
||||||
|
|
||||||
public function __construct( BaseSocket $Socket )
|
public function __construct(BaseSocket $Socket)
|
||||||
{
|
{
|
||||||
$this->Socket = $Socket;
|
$this->Socket = $Socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Close( ) : void
|
public function Close(): void
|
||||||
{
|
{
|
||||||
if( $this->RconSocket )
|
if ($this->RconSocket) {
|
||||||
{
|
fclose($this->RconSocket);
|
||||||
fclose( $this->RconSocket );
|
|
||||||
|
|
||||||
$this->RconSocket = null;
|
$this->RconSocket = null;
|
||||||
}
|
}
|
||||||
@@ -53,147 +55,132 @@
|
|||||||
$this->RconRequestId = 0;
|
$this->RconRequestId = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Open( ) : void
|
public function Open(): void
|
||||||
{
|
{
|
||||||
if( !$this->RconSocket )
|
if (!$this->RconSocket) {
|
||||||
{
|
$RconSocket = @fsockopen($this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout);
|
||||||
$RconSocket = @fsockopen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
|
|
||||||
|
|
||||||
if( $ErrNo || !$RconSocket )
|
if ($ErrNo || !$RconSocket) {
|
||||||
{
|
throw new SocketException('Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED);
|
||||||
throw new SocketException( 'Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->RconSocket = $RconSocket;
|
$this->RconSocket = $RconSocket;
|
||||||
stream_set_timeout( $this->RconSocket, $this->Socket->Timeout );
|
stream_set_timeout($this->RconSocket, $this->Socket->Timeout);
|
||||||
stream_set_blocking( $this->RconSocket, true );
|
stream_set_blocking($this->RconSocket, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Write( int $Header, string $String = '' ) : bool
|
public function Write(int $Header, string $String = ''): bool
|
||||||
{
|
{
|
||||||
// Pack the packet together
|
// Pack the packet together
|
||||||
$Command = pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
|
$Command = pack('VV', ++$this->RconRequestId, $Header) . $String . "\x00\x00";
|
||||||
|
|
||||||
// Prepend packet length
|
// Prepend packet length
|
||||||
$Command = pack( 'V', strlen( $Command ) ) . $Command;
|
$Command = pack('V', strlen($Command)) . $Command;
|
||||||
$Length = strlen( $Command );
|
$Length = strlen($Command);
|
||||||
|
|
||||||
return $Length === fwrite( $this->RconSocket, $Command, $Length );
|
return $Length === fwrite($this->RconSocket, $Command, $Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Read( ) : Buffer
|
public function Read(): Buffer
|
||||||
{
|
{
|
||||||
$Buffer = new Buffer( );
|
$Buffer = new Buffer();
|
||||||
$Buffer->Set( fread( $this->RconSocket, 4 ) );
|
$Buffer->Set(fread($this->RconSocket, 4));
|
||||||
|
|
||||||
if( $Buffer->Remaining( ) < 4 )
|
if ($Buffer->Remaining() < 4) {
|
||||||
{
|
throw new InvalidPacketException('Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY);
|
||||||
throw new InvalidPacketException( 'Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$PacketSize = $Buffer->GetLong( );
|
$PacketSize = $Buffer->GetLong();
|
||||||
|
|
||||||
$Buffer->Set( fread( $this->RconSocket, $PacketSize ) );
|
$Buffer->Set(fread($this->RconSocket, $PacketSize));
|
||||||
|
|
||||||
$Data = $Buffer->Get( );
|
$Data = $Buffer->Get();
|
||||||
|
|
||||||
$Remaining = $PacketSize - strlen( $Data );
|
$Remaining = $PacketSize - strlen($Data);
|
||||||
|
|
||||||
while( $Remaining > 0 )
|
while ($Remaining > 0) {
|
||||||
{
|
$Data2 = fread($this->RconSocket, $Remaining);
|
||||||
$Data2 = fread( $this->RconSocket, $Remaining );
|
|
||||||
|
|
||||||
$PacketSize = strlen( $Data2 );
|
$PacketSize = strlen($Data2);
|
||||||
|
|
||||||
if( $PacketSize === 0 )
|
if ($PacketSize === 0) {
|
||||||
{
|
throw new InvalidPacketException('Read ' . strlen($Data) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY);
|
||||||
throw new InvalidPacketException( 'Read ' . strlen( $Data ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Data .= $Data2;
|
$Data .= $Data2;
|
||||||
$Remaining -= $PacketSize;
|
$Remaining -= $PacketSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
$Buffer->Set( $Data );
|
$Buffer->Set($Data);
|
||||||
|
|
||||||
return $Buffer;
|
return $Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Command( string $Command ) : string
|
public function Command(string $Command): string
|
||||||
{
|
{
|
||||||
$this->Write( SourceQuery::SERVERDATA_EXECCOMMAND, $Command );
|
$this->Write(SourceQuery::SERVERDATA_EXECCOMMAND, $Command);
|
||||||
$Buffer = $this->Read( );
|
$Buffer = $this->Read();
|
||||||
|
|
||||||
$Buffer->GetLong( ); // RequestID
|
$Buffer->GetLong(); // RequestID
|
||||||
|
|
||||||
$Type = $Buffer->GetLong( );
|
$Type = $Buffer->GetLong();
|
||||||
|
|
||||||
if( $Type === SourceQuery::SERVERDATA_AUTH_RESPONSE )
|
if ($Type === SourceQuery::SERVERDATA_AUTH_RESPONSE) {
|
||||||
{
|
throw new AuthenticationException('Bad rcon_password.', AuthenticationException::BAD_PASSWORD);
|
||||||
throw new AuthenticationException( 'Bad rcon_password.', AuthenticationException::BAD_PASSWORD );
|
} elseif ($Type !== SourceQuery::SERVERDATA_RESPONSE_VALUE) {
|
||||||
}
|
throw new InvalidPacketException('Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH);
|
||||||
else if( $Type !== SourceQuery::SERVERDATA_RESPONSE_VALUE )
|
|
||||||
{
|
|
||||||
throw new InvalidPacketException( 'Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$Data = $Buffer->Get( );
|
$Data = $Buffer->Get();
|
||||||
|
|
||||||
// We do this stupid hack to handle split packets
|
// We do this stupid hack to handle split packets
|
||||||
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
|
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
|
||||||
if( strlen( $Data ) >= 4000 )
|
if (strlen($Data) >= 4000) {
|
||||||
{
|
$this->Write(SourceQuery::SERVERDATA_REQUESTVALUE);
|
||||||
$this->Write( SourceQuery::SERVERDATA_REQUESTVALUE );
|
|
||||||
|
|
||||||
do
|
do {
|
||||||
{
|
$Buffer = $this->Read();
|
||||||
$Buffer = $this->Read( );
|
|
||||||
|
|
||||||
$Buffer->GetLong( ); // RequestID
|
$Buffer->GetLong(); // RequestID
|
||||||
|
|
||||||
if( $Buffer->GetLong( ) !== SourceQuery::SERVERDATA_RESPONSE_VALUE )
|
if ($Buffer->GetLong() !== SourceQuery::SERVERDATA_RESPONSE_VALUE) {
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$Data2 = $Buffer->Get( );
|
$Data2 = $Buffer->Get();
|
||||||
|
|
||||||
if( $Data2 === "\x00\x01\x00\x00\x00\x00" )
|
if ($Data2 === "\x00\x01\x00\x00\x00\x00") {
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$Data .= $Data2;
|
$Data .= $Data2;
|
||||||
}
|
} while (true);
|
||||||
while( true );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return rtrim( $Data, "\0" );
|
return rtrim($Data, "\0");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Authorize( string $Password ) : void
|
public function Authorize(string $Password): void
|
||||||
{
|
{
|
||||||
$this->Write( SourceQuery::SERVERDATA_AUTH, $Password );
|
$this->Write(SourceQuery::SERVERDATA_AUTH, $Password);
|
||||||
$Buffer = $this->Read( );
|
$Buffer = $this->Read();
|
||||||
|
|
||||||
$RequestID = $Buffer->GetLong( );
|
$RequestID = $Buffer->GetLong();
|
||||||
$Type = $Buffer->GetLong( );
|
$Type = $Buffer->GetLong();
|
||||||
|
|
||||||
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
|
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
|
||||||
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
|
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
|
||||||
|
|
||||||
if( $Type === SourceQuery::SERVERDATA_RESPONSE_VALUE )
|
if ($Type === SourceQuery::SERVERDATA_RESPONSE_VALUE) {
|
||||||
{
|
$Buffer = $this->Read();
|
||||||
$Buffer = $this->Read( );
|
|
||||||
|
|
||||||
$RequestID = $Buffer->GetLong( );
|
$RequestID = $Buffer->GetLong();
|
||||||
$Type = $Buffer->GetLong( );
|
$Type = $Buffer->GetLong();
|
||||||
}
|
}
|
||||||
|
|
||||||
if( $RequestID === -1 || $Type !== SourceQuery::SERVERDATA_AUTH_RESPONSE )
|
if ($RequestID === -1 || $Type !== SourceQuery::SERVERDATA_AUTH_RESPONSE) {
|
||||||
{
|
throw new AuthenticationException('RCON authorization failed.', AuthenticationException::BAD_PASSWORD);
|
||||||
throw new AuthenticationException( 'RCON authorization failed.', AuthenticationException::BAD_PASSWORD );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
+15
-12
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
* Library to query servers that implement Source Engine Query protocol.
|
* Library to query servers that implement Source Engine Query protocol.
|
||||||
*
|
*
|
||||||
* Special thanks to koraktor for his awesome Steam Condenser class,
|
* Special thanks to koraktor for his awesome Steam Condenser class,
|
||||||
@@ -13,15 +16,15 @@
|
|||||||
* @license GNU Lesser General Public License, version 2.1
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once __DIR__ . '/Exception/SourceQueryException.php';
|
require_once __DIR__ . '/Exception/SourceQueryException.php';
|
||||||
require_once __DIR__ . '/Exception/AuthenticationException.php';
|
require_once __DIR__ . '/Exception/AuthenticationException.php';
|
||||||
require_once __DIR__ . '/Exception/InvalidArgumentException.php';
|
require_once __DIR__ . '/Exception/InvalidArgumentException.php';
|
||||||
require_once __DIR__ . '/Exception/SocketException.php';
|
require_once __DIR__ . '/Exception/SocketException.php';
|
||||||
require_once __DIR__ . '/Exception/InvalidPacketException.php';
|
require_once __DIR__ . '/Exception/InvalidPacketException.php';
|
||||||
|
|
||||||
require_once __DIR__ . '/Buffer.php';
|
require_once __DIR__ . '/Buffer.php';
|
||||||
require_once __DIR__ . '/BaseSocket.php';
|
require_once __DIR__ . '/BaseSocket.php';
|
||||||
require_once __DIR__ . '/Socket.php';
|
require_once __DIR__ . '/Socket.php';
|
||||||
require_once __DIR__ . '/SourceRcon.php';
|
require_once __DIR__ . '/SourceRcon.php';
|
||||||
require_once __DIR__ . '/GoldSourceRcon.php';
|
require_once __DIR__ . '/GoldSourceRcon.php';
|
||||||
require_once __DIR__ . '/SourceQuery.php';
|
require_once __DIR__ . '/SourceQuery.php';
|
||||||
|
|||||||
+106
-112
@@ -1,33 +1,34 @@
|
|||||||
<?php
|
<?php
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
use xPaw\SourceQuery\BaseSocket;
|
|
||||||
use xPaw\SourceQuery\SourceQuery;
|
|
||||||
use xPaw\SourceQuery\Buffer;
|
|
||||||
|
|
||||||
class TestableSocket extends BaseSocket
|
declare(strict_types=1);
|
||||||
{
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use xPaw\SourceQuery\BaseSocket;
|
||||||
|
use xPaw\SourceQuery\SourceQuery;
|
||||||
|
use xPaw\SourceQuery\Buffer;
|
||||||
|
|
||||||
|
final class TestableSocket extends BaseSocket
|
||||||
|
{
|
||||||
/** @var \SplQueue<string> */
|
/** @var \SplQueue<string> */
|
||||||
private \SplQueue $PacketQueue;
|
private \SplQueue $PacketQueue;
|
||||||
|
|
||||||
public function __construct( )
|
public function __construct()
|
||||||
{
|
{
|
||||||
/** @var \SplQueue<string> */
|
|
||||||
$this->PacketQueue = new \SplQueue();
|
$this->PacketQueue = new \SplQueue();
|
||||||
$this->PacketQueue->setIteratorMode( \SplDoublyLinkedList::IT_MODE_DELETE );
|
$this->PacketQueue->setIteratorMode(\SplDoublyLinkedList::IT_MODE_DELETE);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Queue( string $Data ) : void
|
public function Queue(string $Data): void
|
||||||
{
|
{
|
||||||
$this->PacketQueue->push( $Data );
|
$this->PacketQueue->push($Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Close( ) : void
|
public function Close(): void
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void
|
public function Open(string $Address, int $Port, int $Timeout, int $Engine): void
|
||||||
{
|
{
|
||||||
$this->Timeout = $Timeout;
|
$this->Timeout = $Timeout;
|
||||||
$this->Engine = $Engine;
|
$this->Engine = $Engine;
|
||||||
@@ -35,77 +36,76 @@
|
|||||||
$this->Address = $Address;
|
$this->Address = $Address;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Write( int $Header, string $String = '' ) : bool
|
public function Write(int $Header, string $String = ''): bool
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Read( int $Length = 1400 ) : Buffer
|
public function Read(int $Length = 1400): Buffer
|
||||||
{
|
{
|
||||||
$Buffer = new Buffer( );
|
$Buffer = new Buffer();
|
||||||
$Buffer->Set( $this->PacketQueue->shift() );
|
$Buffer->Set($this->PacketQueue->shift());
|
||||||
|
|
||||||
$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
|
$this->ReadInternal($Buffer, $Length, [ $this, 'Sherlock' ]);
|
||||||
|
|
||||||
return $Buffer;
|
return $Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Sherlock( Buffer $Buffer, int $Length ) : bool
|
public function Sherlock(Buffer $Buffer, int $Length): bool
|
||||||
{
|
|
||||||
if( $this->PacketQueue->isEmpty() )
|
|
||||||
{
|
{
|
||||||
|
if ($this->PacketQueue->isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$Buffer->Set( $this->PacketQueue->shift() );
|
$Buffer->Set($this->PacketQueue->shift());
|
||||||
|
|
||||||
return $Buffer->GetLong( ) === -2;
|
return $Buffer->GetLong() === -2;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Tests extends \PHPUnit\Framework\TestCase
|
final class Tests extends TestCase
|
||||||
{
|
{
|
||||||
private TestableSocket $Socket;
|
private TestableSocket $Socket;
|
||||||
private SourceQuery $SourceQuery;
|
private SourceQuery $SourceQuery;
|
||||||
|
|
||||||
public function setUp() : void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$this->Socket = new TestableSocket();
|
$this->Socket = new TestableSocket();
|
||||||
$this->SourceQuery = new SourceQuery( $this->Socket );
|
$this->SourceQuery = new SourceQuery($this->Socket);
|
||||||
$this->SourceQuery->Connect( '', 2 );
|
$this->SourceQuery->Connect('', 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tearDown() : void
|
public function tearDown(): void
|
||||||
{
|
{
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
|
|
||||||
unset( $this->Socket, $this->SourceQuery );
|
unset($this->Socket, $this->SourceQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInvalidTimeout() : void
|
public function testInvalidTimeout(): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidArgumentException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\InvalidArgumentException::class);
|
||||||
$SourceQuery = new SourceQuery( );
|
$SourceQuery = new SourceQuery();
|
||||||
$SourceQuery->Connect( '', 2, -1 );
|
$SourceQuery->Connect('', 2, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNotConnectedGetInfo() : void
|
public function testNotConnectedGetInfo(): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\SocketException::class);
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->GetInfo();
|
$this->SourceQuery->GetInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNotConnectedPing() : void
|
public function testNotConnectedPing(): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\SocketException::class);
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->Ping();
|
$this->SourceQuery->Ping();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNotConnectedGetPlayers() : void
|
public function testNotConnectedGetPlayers(): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\SocketException::class);
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->GetPlayers();
|
$this->SourceQuery->GetPlayers();
|
||||||
}
|
}
|
||||||
@@ -113,62 +113,60 @@
|
|||||||
/**
|
/**
|
||||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
||||||
*/
|
*/
|
||||||
public function testNotConnectedGetRules() : void
|
public function testNotConnectedGetRules(): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\SocketException::class);
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->GetRules();
|
$this->SourceQuery->GetRules();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNotConnectedSetRconPassword() : void
|
public function testNotConnectedSetRconPassword(): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\SocketException::class);
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->SetRconPassword('a');
|
$this->SourceQuery->SetRconPassword('a');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNotConnectedRcon() : void
|
public function testNotConnectedRcon(): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\SocketException::class);
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->Rcon('a');
|
$this->SourceQuery->Rcon('a');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRconWithoutPassword() : void
|
public function testRconWithoutPassword(): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\SocketException::class);
|
||||||
$this->SourceQuery->Rcon('a');
|
$this->SourceQuery->Rcon('a');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider InfoProvider
|
* @dataProvider InfoProvider
|
||||||
*/
|
*/
|
||||||
public function testGetInfo( string $RawInput, array $ExpectedOutput ) : void
|
public function testGetInfo(string $RawInput, array $ExpectedOutput): void
|
||||||
{
|
|
||||||
if( isset( $ExpectedOutput[ 'IsMod' ] ) )
|
|
||||||
{
|
{
|
||||||
|
if (isset($ExpectedOutput[ 'IsMod' ])) {
|
||||||
$this->Socket->Engine = SourceQuery::GOLDSOURCE;
|
$this->Socket->Engine = SourceQuery::GOLDSOURCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Socket->Queue( $RawInput );
|
$this->Socket->Queue($RawInput);
|
||||||
|
|
||||||
$RealOutput = $this->SourceQuery->GetInfo();
|
$RealOutput = $this->SourceQuery->GetInfo();
|
||||||
|
|
||||||
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
self::assertEquals($ExpectedOutput, $RealOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function InfoProvider() : array
|
public function InfoProvider(): array
|
||||||
{
|
{
|
||||||
$DataProvider = [];
|
$DataProvider = [];
|
||||||
|
|
||||||
$Files = glob( __DIR__ . '/Info/*.raw', GLOB_ERR );
|
$Files = glob(__DIR__ . '/Info/*.raw', GLOB_ERR);
|
||||||
|
|
||||||
foreach( $Files as $File )
|
foreach ($Files as $File) {
|
||||||
{
|
|
||||||
$DataProvider[] =
|
$DataProvider[] =
|
||||||
[
|
[
|
||||||
hex2bin( trim( file_get_contents( $File ) ) ),
|
hex2bin(trim(file_get_contents($File))),
|
||||||
json_decode( file_get_contents( str_replace( '.raw', '.json', $File ) ), true )
|
json_decode(file_get_contents(str_replace('.raw', '.json', $File)), true)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,10 +176,10 @@
|
|||||||
/**
|
/**
|
||||||
* @dataProvider BadPacketProvider
|
* @dataProvider BadPacketProvider
|
||||||
*/
|
*/
|
||||||
public function testBadGetInfo( string $Data ) : void
|
public function testBadGetInfo(string $Data): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\InvalidPacketException::class);
|
||||||
$this->Socket->Queue( $Data );
|
$this->Socket->Queue($Data);
|
||||||
|
|
||||||
$this->SourceQuery->GetInfo();
|
$this->SourceQuery->GetInfo();
|
||||||
}
|
}
|
||||||
@@ -189,10 +187,10 @@
|
|||||||
/**
|
/**
|
||||||
* @dataProvider BadPacketProvider
|
* @dataProvider BadPacketProvider
|
||||||
*/
|
*/
|
||||||
public function testBadGetChallengeViaPlayers( string $Data ) : void
|
public function testBadGetChallengeViaPlayers(string $Data): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\InvalidPacketException::class);
|
||||||
$this->Socket->Queue( $Data );
|
$this->Socket->Queue($Data);
|
||||||
|
|
||||||
$this->SourceQuery->GetPlayers();
|
$this->SourceQuery->GetPlayers();
|
||||||
}
|
}
|
||||||
@@ -200,11 +198,11 @@
|
|||||||
/**
|
/**
|
||||||
* @dataProvider BadPacketProvider
|
* @dataProvider BadPacketProvider
|
||||||
*/
|
*/
|
||||||
public function testBadGetPlayersAfterCorrectChallenge( string $Data ) : void
|
public function testBadGetPlayersAfterCorrectChallenge(string $Data): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\InvalidPacketException::class);
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11" );
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11");
|
||||||
$this->Socket->Queue( $Data );
|
$this->Socket->Queue($Data);
|
||||||
|
|
||||||
$this->SourceQuery->GetPlayers();
|
$this->SourceQuery->GetPlayers();
|
||||||
}
|
}
|
||||||
@@ -212,16 +210,16 @@
|
|||||||
/**
|
/**
|
||||||
* @dataProvider BadPacketProvider
|
* @dataProvider BadPacketProvider
|
||||||
*/
|
*/
|
||||||
public function testBadGetRulesAfterCorrectChallenge( string $Data ) : void
|
public function testBadGetRulesAfterCorrectChallenge(string $Data): void
|
||||||
{
|
{
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
$this->expectException(xPaw\SourceQuery\Exception\InvalidPacketException::class);
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11" );
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11");
|
||||||
$this->Socket->Queue( $Data );
|
$this->Socket->Queue($Data);
|
||||||
|
|
||||||
$this->SourceQuery->GetRules();
|
$this->SourceQuery->GetRules();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function BadPacketProvider( ) : array
|
public function BadPacketProvider(): array
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
@@ -235,46 +233,44 @@
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetChallengeTwice( ) : void
|
public function testGetChallengeTwice(): void
|
||||||
{
|
{
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11" );
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11");
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x45\x01\x00ayy\x00lmao\x00" );
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x45\x01\x00ayy\x00lmao\x00");
|
||||||
$this->assertEquals( [ 'ayy' => 'lmao' ], $this->SourceQuery->GetRules() );
|
self::assertEquals([ 'ayy' => 'lmao' ], $this->SourceQuery->GetRules());
|
||||||
|
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x45\x01\x00wow\x00much\x00" );
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x45\x01\x00wow\x00much\x00");
|
||||||
$this->assertEquals( [ 'wow' => 'much' ], $this->SourceQuery->GetRules() );
|
self::assertEquals([ 'wow' => 'much' ], $this->SourceQuery->GetRules());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider RulesProvider
|
* @dataProvider RulesProvider
|
||||||
* @param array<string> $RawInput
|
* @param array<string> $RawInput
|
||||||
*/
|
*/
|
||||||
public function testGetRules( array $RawInput, array $ExpectedOutput ) : void
|
public function testGetRules(array $RawInput, array $ExpectedOutput): void
|
||||||
{
|
{
|
||||||
$this->Socket->Queue( hex2bin( "ffffffff4104fce20e" ) ); // Challenge
|
$this->Socket->Queue(hex2bin("ffffffff4104fce20e")); // Challenge
|
||||||
|
|
||||||
foreach( $RawInput as $Packet )
|
foreach ($RawInput as $Packet) {
|
||||||
{
|
$this->Socket->Queue(hex2bin($Packet));
|
||||||
$this->Socket->Queue( hex2bin( $Packet ) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$RealOutput = $this->SourceQuery->GetRules();
|
$RealOutput = $this->SourceQuery->GetRules();
|
||||||
|
|
||||||
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
self::assertEquals($ExpectedOutput, $RealOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function RulesProvider() : array
|
public function RulesProvider(): array
|
||||||
{
|
{
|
||||||
$DataProvider = [];
|
$DataProvider = [];
|
||||||
|
|
||||||
$Files = glob( __DIR__ . '/Rules/*.raw', GLOB_ERR );
|
$Files = glob(__DIR__ . '/Rules/*.raw', GLOB_ERR);
|
||||||
|
|
||||||
foreach( $Files as $File )
|
foreach ($Files as $File) {
|
||||||
{
|
|
||||||
$DataProvider[] =
|
$DataProvider[] =
|
||||||
[
|
[
|
||||||
file( $File, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES ),
|
file($File, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES),
|
||||||
json_decode( file_get_contents( str_replace( '.raw', '.json', $File ) ), true )
|
json_decode(file_get_contents(str_replace('.raw', '.json', $File)), true)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,44 +281,42 @@
|
|||||||
* @dataProvider PlayersProvider
|
* @dataProvider PlayersProvider
|
||||||
* @param array<string> $RawInput
|
* @param array<string> $RawInput
|
||||||
*/
|
*/
|
||||||
public function testGetPlayers( array $RawInput, array $ExpectedOutput ) : void
|
public function testGetPlayers(array $RawInput, array $ExpectedOutput): void
|
||||||
{
|
{
|
||||||
$this->Socket->Queue( hex2bin( "ffffffff4104fce20e" ) ); // Challenge
|
$this->Socket->Queue(hex2bin("ffffffff4104fce20e")); // Challenge
|
||||||
|
|
||||||
foreach( $RawInput as $Packet )
|
foreach ($RawInput as $Packet) {
|
||||||
{
|
$this->Socket->Queue(hex2bin($Packet));
|
||||||
$this->Socket->Queue( hex2bin( $Packet ) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$RealOutput = $this->SourceQuery->GetPlayers();
|
$RealOutput = $this->SourceQuery->GetPlayers();
|
||||||
|
|
||||||
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
self::assertEquals($ExpectedOutput, $RealOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function PlayersProvider() : array
|
public function PlayersProvider(): array
|
||||||
{
|
{
|
||||||
$DataProvider = [];
|
$DataProvider = [];
|
||||||
|
|
||||||
$Files = glob( __DIR__ . '/Players/*.raw', GLOB_ERR );
|
$Files = glob(__DIR__ . '/Players/*.raw', GLOB_ERR);
|
||||||
|
|
||||||
foreach( $Files as $File )
|
foreach ($Files as $File) {
|
||||||
{
|
|
||||||
$DataProvider[] =
|
$DataProvider[] =
|
||||||
[
|
[
|
||||||
file( $File, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES ),
|
file($File, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES),
|
||||||
json_decode( file_get_contents( str_replace( '.raw', '.json', $File ) ), true )
|
json_decode(file_get_contents(str_replace('.raw', '.json', $File)), true)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $DataProvider;
|
return $DataProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPing() : void
|
public function testPing(): void
|
||||||
{
|
{
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x6A\x00");
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x6A\x00");
|
||||||
$this->assertTrue( $this->SourceQuery->Ping() );
|
self::assertTrue($this->SourceQuery->Ping());
|
||||||
|
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\xEE");
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\xEE");
|
||||||
$this->assertFalse( $this->SourceQuery->Ping() );
|
self::assertFalse($this->SourceQuery->Ping());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
+2
-1
@@ -24,7 +24,8 @@
|
|||||||
{
|
{
|
||||||
"phpunit/phpunit": "^9.5",
|
"phpunit/phpunit": "^9.5",
|
||||||
"vimeo/psalm": "^4.7",
|
"vimeo/psalm": "^4.7",
|
||||||
"phpstan/phpstan": "^0.12.83"
|
"phpstan/phpstan": "^0.12.83",
|
||||||
|
"friendsofphp/php-cs-fixer": "^3.0"
|
||||||
},
|
},
|
||||||
"autoload":
|
"autoload":
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user