parent
3bfc073211
commit
e62d011689
@ -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/
|
||||||
|
@ -1,34 +1,32 @@
|
|||||||
<?php
|
<?php
|
||||||
require __DIR__ . '/../SourceQuery/bootstrap.php';
|
|
||||||
|
declare(strict_types=1);
|
||||||
use xPaw\SourceQuery\SourceQuery;
|
|
||||||
|
require __DIR__ . '/../SourceQuery/bootstrap.php';
|
||||||
// For the sake of this example
|
|
||||||
header( 'Content-Type: text/plain' );
|
use xPaw\SourceQuery\SourceQuery;
|
||||||
header( 'X-Content-Type-Options: nosniff' );
|
|
||||||
|
// For the sake of this example
|
||||||
// Edit this ->
|
header('Content-Type: text/plain');
|
||||||
define( 'SQ_SERVER_ADDR', 'localhost' );
|
header('X-Content-Type-Options: nosniff');
|
||||||
define( 'SQ_SERVER_PORT', 27015 );
|
|
||||||
define( 'SQ_TIMEOUT', 1 );
|
// Edit this ->
|
||||||
define( 'SQ_ENGINE', SourceQuery::SOURCE );
|
define('SQ_SERVER_ADDR', 'localhost');
|
||||||
// Edit this <-
|
define('SQ_SERVER_PORT', 27015);
|
||||||
|
define('SQ_TIMEOUT', 1);
|
||||||
$Query = new SourceQuery( );
|
define('SQ_ENGINE', SourceQuery::SOURCE);
|
||||||
|
// Edit this <-
|
||||||
try
|
|
||||||
{
|
$Query = new SourceQuery();
|
||||||
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
|
|
||||||
|
try {
|
||||||
print_r( $Query->GetInfo( ) );
|
$Query->Connect(SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE);
|
||||||
print_r( $Query->GetPlayers( ) );
|
|
||||||
print_r( $Query->GetRules( ) );
|
print_r($Query->GetInfo());
|
||||||
}
|
print_r($Query->GetPlayers());
|
||||||
catch( Exception $e )
|
print_r($Query->GetRules());
|
||||||
{
|
} catch (Exception $e) {
|
||||||
echo $e->getMessage( );
|
echo $e->getMessage();
|
||||||
}
|
} finally {
|
||||||
finally
|
$Query->Disconnect();
|
||||||
{
|
}
|
||||||
$Query->Disconnect( );
|
|
||||||
}
|
|
||||||
|
@ -1,34 +1,32 @@
|
|||||||
<?php
|
<?php
|
||||||
require __DIR__ . '/../SourceQuery/bootstrap.php';
|
|
||||||
|
declare(strict_types=1);
|
||||||
use xPaw\SourceQuery\SourceQuery;
|
|
||||||
|
require __DIR__ . '/../SourceQuery/bootstrap.php';
|
||||||
// For the sake of this example
|
|
||||||
header( 'Content-Type: text/plain' );
|
use xPaw\SourceQuery\SourceQuery;
|
||||||
header( 'X-Content-Type-Options: nosniff' );
|
|
||||||
|
// For the sake of this example
|
||||||
// Edit this ->
|
header('Content-Type: text/plain');
|
||||||
define( 'SQ_SERVER_ADDR', 'localhost' );
|
header('X-Content-Type-Options: nosniff');
|
||||||
define( 'SQ_SERVER_PORT', 27015 );
|
|
||||||
define( 'SQ_TIMEOUT', 1 );
|
// Edit this ->
|
||||||
define( 'SQ_ENGINE', SourceQuery::SOURCE );
|
define('SQ_SERVER_ADDR', 'localhost');
|
||||||
// Edit this <-
|
define('SQ_SERVER_PORT', 27015);
|
||||||
|
define('SQ_TIMEOUT', 1);
|
||||||
$Query = new SourceQuery( );
|
define('SQ_ENGINE', SourceQuery::SOURCE);
|
||||||
|
// Edit this <-
|
||||||
try
|
|
||||||
{
|
$Query = new SourceQuery();
|
||||||
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
|
|
||||||
|
try {
|
||||||
$Query->SetRconPassword( 'my_awesome_password' );
|
$Query->Connect(SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE);
|
||||||
|
|
||||||
var_dump( $Query->Rcon( 'say hello' ) );
|
$Query->SetRconPassword('my_awesome_password');
|
||||||
}
|
|
||||||
catch( Exception $e )
|
var_dump($Query->Rcon('say hello'));
|
||||||
{
|
} catch (Exception $e) {
|
||||||
echo $e->getMessage( );
|
echo $e->getMessage();
|
||||||
}
|
} finally {
|
||||||
finally
|
$Query->Disconnect();
|
||||||
{
|
}
|
||||||
$Query->Disconnect( );
|
|
||||||
}
|
|
||||||
|
@ -1,139 +1,127 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Pavel Djundik
|
declare(strict_types=1);
|
||||||
*
|
|
||||||
* @link https://xpaw.me
|
/**
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @license GNU Lesser General Public License, version 2.1
|
* @link https://xpaw.me
|
||||||
*
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
* @internal
|
*
|
||||||
*/
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
|
*
|
||||||
namespace xPaw\SourceQuery;
|
* @internal
|
||||||
|
*/
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
|
||||||
use xPaw\SourceQuery\Exception\SocketException;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
/**
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
* Base socket interface
|
use xPaw\SourceQuery\Exception\SocketException;
|
||||||
*
|
|
||||||
* @package xPaw\SourceQuery
|
/**
|
||||||
*
|
* Base socket interface
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
*
|
||||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
* @package xPaw\SourceQuery
|
||||||
*/
|
*
|
||||||
abstract class BaseSocket
|
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||||
{
|
* @uses xPaw\SourceQuery\Exception\SocketException
|
||||||
/** @var ?resource */
|
*/
|
||||||
public $Socket;
|
abstract class BaseSocket
|
||||||
public int $Engine;
|
{
|
||||||
|
/** @var ?resource */
|
||||||
public string $Address;
|
public $Socket;
|
||||||
public int $Port;
|
public int $Engine;
|
||||||
public int $Timeout;
|
|
||||||
|
public string $Address;
|
||||||
public function __destruct( )
|
public int $Port;
|
||||||
{
|
public int $Timeout;
|
||||||
$this->Close( );
|
|
||||||
}
|
public function __destruct()
|
||||||
|
{
|
||||||
abstract public function Close( ) : void;
|
$this->Close();
|
||||||
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 Read( int $Length = 1400 ) : Buffer;
|
abstract public function Close(): void;
|
||||||
|
abstract public function Open(string $Address, int $Port, int $Timeout, int $Engine): void;
|
||||||
protected function ReadInternal( Buffer $Buffer, int $Length, callable $SherlockFunction ) : Buffer
|
abstract public function Write(int $Header, string $String = ''): bool;
|
||||||
{
|
abstract public function Read(int $Length = 1400): Buffer;
|
||||||
if( $Buffer->Remaining( ) === 0 )
|
|
||||||
{
|
protected function ReadInternal(Buffer $Buffer, int $Length, callable $SherlockFunction): Buffer
|
||||||
throw new InvalidPacketException( 'Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY );
|
{
|
||||||
}
|
if ($Buffer->Remaining() === 0) {
|
||||||
|
throw new InvalidPacketException('Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY);
|
||||||
$Header = $Buffer->GetLong( );
|
}
|
||||||
|
|
||||||
if( $Header === -1 ) // Single packet
|
$Header = $Buffer->GetLong();
|
||||||
{
|
|
||||||
// We don't have to do anything
|
if ($Header === -1) { // Single packet
|
||||||
}
|
// We don't have to do anything
|
||||||
else if( $Header === -2 ) // Split packet
|
} elseif ($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) {
|
||||||
|
case SourceQuery::GOLDSOURCE:
|
||||||
switch( $this->Engine )
|
{
|
||||||
{
|
$PacketCountAndNumber = $Buffer->GetByte();
|
||||||
case SourceQuery::GOLDSOURCE:
|
$PacketCount = $PacketCountAndNumber & 0xF;
|
||||||
{
|
$PacketNumber = $PacketCountAndNumber >> 4;
|
||||||
$PacketCountAndNumber = $Buffer->GetByte( );
|
|
||||||
$PacketCount = $PacketCountAndNumber & 0xF;
|
break;
|
||||||
$PacketNumber = $PacketCountAndNumber >> 4;
|
}
|
||||||
|
case SourceQuery::SOURCE:
|
||||||
break;
|
{
|
||||||
}
|
$IsCompressed = ($RequestID & 0x80000000) !== 0;
|
||||||
case SourceQuery::SOURCE:
|
$PacketCount = $Buffer->GetByte();
|
||||||
{
|
$PacketNumber = $Buffer->GetByte() + 1;
|
||||||
$IsCompressed = ( $RequestID & 0x80000000 ) !== 0;
|
|
||||||
$PacketCount = $Buffer->GetByte( );
|
if ($IsCompressed) {
|
||||||
$PacketNumber = $Buffer->GetByte( ) + 1;
|
$Buffer->GetLong(); // Split size
|
||||||
|
|
||||||
if( $IsCompressed )
|
$PacketChecksum = $Buffer->GetUnsignedLong();
|
||||||
{
|
} else {
|
||||||
$Buffer->GetLong( ); // Split size
|
$Buffer->GetShort(); // Split size
|
||||||
|
}
|
||||||
$PacketChecksum = $Buffer->GetUnsignedLong( );
|
|
||||||
}
|
break;
|
||||||
else
|
}
|
||||||
{
|
default:
|
||||||
$Buffer->GetShort( ); // Split size
|
{
|
||||||
}
|
throw new SocketException('Unknown engine.', SocketException::INVALID_ENGINE);
|
||||||
|
}
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
default:
|
$Packets[ $PacketNumber ] = $Buffer->Get();
|
||||||
{
|
|
||||||
throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE );
|
$ReadMore = $PacketCount > sizeof($Packets);
|
||||||
}
|
} while ($ReadMore && $SherlockFunction($Buffer, $Length));
|
||||||
}
|
|
||||||
|
$Data = implode($Packets);
|
||||||
$Packets[ $PacketNumber ] = $Buffer->Get( );
|
|
||||||
|
// TODO: Test this
|
||||||
$ReadMore = $PacketCount > sizeof( $Packets );
|
if ($IsCompressed) {
|
||||||
}
|
// Let's make sure this function exists, it's not included in PHP by default
|
||||||
while( $ReadMore && $SherlockFunction( $Buffer, $Length ) );
|
if (!function_exists('bzdecompress')) {
|
||||||
|
throw new \RuntimeException('Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.');
|
||||||
$Data = implode( $Packets );
|
}
|
||||||
|
|
||||||
// TODO: Test this
|
$Data = bzdecompress($Data);
|
||||||
if( $IsCompressed )
|
|
||||||
{
|
if (!is_string($Data) || crc32($Data) !== $PacketChecksum) {
|
||||||
// Let's make sure this function exists, it's not included in PHP by default
|
throw new InvalidPacketException('CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH);
|
||||||
if( !function_exists( 'bzdecompress' ) )
|
}
|
||||||
{
|
}
|
||||||
throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' );
|
|
||||||
}
|
$Buffer->Set(substr($Data, 4));
|
||||||
|
} else {
|
||||||
$Data = bzdecompress( $Data );
|
throw new InvalidPacketException('Socket read: Raw packet header mismatch. (0x' . dechex($Header) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
|
||||||
|
}
|
||||||
if( !is_string( $Data ) || crc32( $Data ) !== $PacketChecksum )
|
|
||||||
{
|
return $Buffer;
|
||||||
throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH );
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$Buffer->Set( substr( $Data, 4 ) );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . dechex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
|
||||||
}
|
|
||||||
|
|
||||||
return $Buffer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,177 +1,171 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Pavel Djundik
|
declare(strict_types=1);
|
||||||
*
|
|
||||||
* @link https://xpaw.me
|
/**
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @license GNU Lesser General Public License, version 2.1
|
* @link https://xpaw.me
|
||||||
*
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
* @internal
|
*
|
||||||
*/
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
|
*
|
||||||
namespace xPaw\SourceQuery;
|
* @internal
|
||||||
|
*/
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
|
||||||
|
namespace xPaw\SourceQuery;
|
||||||
/**
|
|
||||||
* Class Buffer
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
*
|
|
||||||
* @package xPaw\SourceQuery
|
/**
|
||||||
*
|
* Class Buffer
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
*
|
||||||
*/
|
* @package xPaw\SourceQuery
|
||||||
class Buffer
|
*
|
||||||
{
|
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||||
/**
|
*/
|
||||||
* Buffer
|
final class Buffer
|
||||||
*/
|
{
|
||||||
private string $Buffer = '';
|
/**
|
||||||
|
* Buffer
|
||||||
/**
|
*/
|
||||||
* Buffer length
|
private string $Buffer = '';
|
||||||
*/
|
|
||||||
private int $Length = 0;
|
/**
|
||||||
|
* Buffer length
|
||||||
/**
|
*/
|
||||||
* Current position in buffer
|
private int $Length = 0;
|
||||||
*/
|
|
||||||
private int $Position = 0;
|
/**
|
||||||
|
* Current position in buffer
|
||||||
/**
|
*/
|
||||||
* Sets buffer
|
private int $Position = 0;
|
||||||
*/
|
|
||||||
public function Set( string $Buffer ) : void
|
/**
|
||||||
{
|
* Sets buffer
|
||||||
$this->Buffer = $Buffer;
|
*/
|
||||||
$this->Length = strlen( $Buffer );
|
public function Set(string $Buffer): void
|
||||||
$this->Position = 0;
|
{
|
||||||
}
|
$this->Buffer = $Buffer;
|
||||||
|
$this->Length = strlen($Buffer);
|
||||||
/**
|
$this->Position = 0;
|
||||||
* Get remaining bytes
|
}
|
||||||
*
|
|
||||||
* @return int Remaining bytes in buffer
|
/**
|
||||||
*/
|
* Get remaining bytes
|
||||||
public function Remaining( ) : int
|
*
|
||||||
{
|
* @return int Remaining bytes in buffer
|
||||||
return $this->Length - $this->Position;
|
*/
|
||||||
}
|
public function Remaining(): int
|
||||||
|
{
|
||||||
/**
|
return $this->Length - $this->Position;
|
||||||
* Gets data from buffer
|
}
|
||||||
*
|
|
||||||
* @param int $Length Bytes to read
|
/**
|
||||||
*/
|
* Gets data from buffer
|
||||||
public function Get( int $Length = -1 ) : string
|
*
|
||||||
{
|
* @param int $Length Bytes to read
|
||||||
if( $Length === 0 )
|
*/
|
||||||
{
|
public function Get(int $Length = -1): string
|
||||||
return '';
|
{
|
||||||
}
|
if ($Length === 0) {
|
||||||
|
return '';
|
||||||
$Remaining = $this->Remaining( );
|
}
|
||||||
|
|
||||||
if( $Length === -1 )
|
$Remaining = $this->Remaining();
|
||||||
{
|
|
||||||
$Length = $Remaining;
|
if ($Length === -1) {
|
||||||
}
|
$Length = $Remaining;
|
||||||
else if( $Length > $Remaining )
|
} elseif ($Length > $Remaining) {
|
||||||
{
|
return '';
|
||||||
return '';
|
}
|
||||||
}
|
|
||||||
|
$Data = substr($this->Buffer, $this->Position, $Length);
|
||||||
$Data = substr( $this->Buffer, $this->Position, $Length );
|
|
||||||
|
$this->Position += $Length;
|
||||||
$this->Position += $Length;
|
|
||||||
|
return $Data;
|
||||||
return $Data;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* 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 ];
|
|
||||||
}
|
/**
|
||||||
|
* Get long from buffer
|
||||||
/**
|
*/
|
||||||
* Get long from buffer
|
public function GetLong(): int
|
||||||
*/
|
{
|
||||||
public function GetLong( ) : int
|
if ($this->Remaining() < 4) {
|
||||||
{
|
throw new InvalidPacketException('Not enough data to unpack a long.', InvalidPacketException::BUFFER_EMPTY);
|
||||||
if( $this->Remaining( ) < 4 )
|
}
|
||||||
{
|
|
||||||
throw new InvalidPacketException( 'Not enough data to unpack a long.', InvalidPacketException::BUFFER_EMPTY );
|
$Data = unpack('l', $this->Get(4));
|
||||||
}
|
|
||||||
|
return (int)$Data[ 1 ];
|
||||||
$Data = unpack( 'l', $this->Get( 4 ) );
|
}
|
||||||
|
|
||||||
return (int)$Data[ 1 ];
|
/**
|
||||||
}
|
* Get float from buffer
|
||||||
|
*/
|
||||||
/**
|
public function GetFloat(): float
|
||||||
* Get float from buffer
|
{
|
||||||
*/
|
if ($this->Remaining() < 4) {
|
||||||
public function GetFloat( ) : float
|
throw new InvalidPacketException('Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY);
|
||||||
{
|
}
|
||||||
if( $this->Remaining( ) < 4 )
|
|
||||||
{
|
$Data = unpack('f', $this->Get(4));
|
||||||
throw new InvalidPacketException( 'Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY );
|
|
||||||
}
|
return (float)$Data[ 1 ];
|
||||||
|
}
|
||||||
$Data = unpack( 'f', $this->Get( 4 ) );
|
|
||||||
|
/**
|
||||||
return (float)$Data[ 1 ];
|
* Get unsigned long from buffer
|
||||||
}
|
*/
|
||||||
|
public function GetUnsignedLong(): int
|
||||||
/**
|
{
|
||||||
* Get unsigned long from buffer
|
if ($this->Remaining() < 4) {
|
||||||
*/
|
throw new InvalidPacketException('Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY);
|
||||||
public function GetUnsignedLong( ) : int
|
}
|
||||||
{
|
|
||||||
if( $this->Remaining( ) < 4 )
|
$Data = unpack('V', $this->Get(4));
|
||||||
{
|
|
||||||
throw new InvalidPacketException( 'Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY );
|
return (int)$Data[ 1 ];
|
||||||
}
|
}
|
||||||
|
|
||||||
$Data = unpack( 'V', $this->Get( 4 ) );
|
/**
|
||||||
|
* Read one string from buffer ending with null byte
|
||||||
return (int)$Data[ 1 ];
|
*/
|
||||||
}
|
public function GetString(): string
|
||||||
|
{
|
||||||
/**
|
$ZeroBytePosition = strpos($this->Buffer, "\0", $this->Position);
|
||||||
* Read one string from buffer ending with null byte
|
|
||||||
*/
|
if ($ZeroBytePosition === false) {
|
||||||
public function GetString( ) : string
|
return '';
|
||||||
{
|
}
|
||||||
$ZeroBytePosition = strpos( $this->Buffer, "\0", $this->Position );
|
|
||||||
|
$String = $this->Get($ZeroBytePosition - $this->Position);
|
||||||
if( $ZeroBytePosition === false )
|
|
||||||
{
|
$this->Position++;
|
||||||
return '';
|
|
||||||
}
|
return $String;
|
||||||
|
}
|
||||||
$String = $this->Get( $ZeroBytePosition - $this->Position );
|
}
|
||||||
|
|
||||||
$this->Position++;
|
|
||||||
|
|
||||||
return $String;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,19 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Pavel Djundik
|
|
||||||
*
|
|
||||||
* @link https://xpaw.me
|
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
|
||||||
*
|
|
||||||
* @license GNU Lesser General Public License, version 2.1
|
|
||||||
*
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace xPaw\SourceQuery\Exception;
|
declare(strict_types=1);
|
||||||
|
|
||||||
class AuthenticationException extends SourceQueryException
|
/**
|
||||||
{
|
* @author Pavel Djundik
|
||||||
const BAD_PASSWORD = 1;
|
*
|
||||||
const BANNED = 2;
|
* @link https://xpaw.me
|
||||||
}
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
|
*
|
||||||
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace xPaw\SourceQuery\Exception;
|
||||||
|
|
||||||
|
final class AuthenticationException extends SourceQueryException
|
||||||
|
{
|
||||||
|
public const BAD_PASSWORD = 1;
|
||||||
|
public const BANNED = 2;
|
||||||
|
}
|
||||||
|
@ -1,18 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Pavel Djundik
|
|
||||||
*
|
|
||||||
* @link https://xpaw.me
|
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
|
||||||
*
|
|
||||||
* @license GNU Lesser General Public License, version 2.1
|
|
||||||
*
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace xPaw\SourceQuery\Exception;
|
declare(strict_types=1);
|
||||||
|
|
||||||
class InvalidArgumentException extends SourceQueryException
|
/**
|
||||||
{
|
* @author Pavel Djundik
|
||||||
const TIMEOUT_NOT_INTEGER = 1;
|
*
|
||||||
}
|
* @link https://xpaw.me
|
||||||
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
|
*
|
||||||
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace xPaw\SourceQuery\Exception;
|
||||||
|
|
||||||
|
final class InvalidArgumentException extends SourceQueryException
|
||||||
|
{
|
||||||
|
public const TIMEOUT_NOT_INTEGER = 1;
|
||||||
|
}
|
||||||
|
@ -1,21 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Pavel Djundik
|
|
||||||
*
|
|
||||||
* @link https://xpaw.me
|
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
|
||||||
*
|
|
||||||
* @license GNU Lesser General Public License, version 2.1
|
|
||||||
*
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace xPaw\SourceQuery\Exception;
|
declare(strict_types=1);
|
||||||
|
|
||||||
class InvalidPacketException extends SourceQueryException
|
/**
|
||||||
{
|
* @author Pavel Djundik
|
||||||
const PACKET_HEADER_MISMATCH = 1;
|
*
|
||||||
const BUFFER_EMPTY = 2;
|
* @link https://xpaw.me
|
||||||
const BUFFER_NOT_EMPTY = 3;
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
const CHECKSUM_MISMATCH = 4;
|
*
|
||||||
}
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace xPaw\SourceQuery\Exception;
|
||||||
|
|
||||||
|
final class InvalidPacketException extends SourceQueryException
|
||||||
|
{
|
||||||
|
public const PACKET_HEADER_MISMATCH = 1;
|
||||||
|
public const BUFFER_EMPTY = 2;
|
||||||
|
public const BUFFER_NOT_EMPTY = 3;
|
||||||
|
public const CHECKSUM_MISMATCH = 4;
|
||||||
|
}
|
||||||
|
@ -1,21 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Pavel Djundik
|
|
||||||
*
|
|
||||||
* @link https://xpaw.me
|
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
|
||||||
*
|
|
||||||
* @license GNU Lesser General Public License, version 2.1
|
|
||||||
*
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace xPaw\SourceQuery\Exception;
|
declare(strict_types=1);
|
||||||
|
|
||||||
class SocketException extends SourceQueryException
|
/**
|
||||||
{
|
* @author Pavel Djundik
|
||||||
const COULD_NOT_CREATE_SOCKET = 1;
|
*
|
||||||
const NOT_CONNECTED = 2;
|
* @link https://xpaw.me
|
||||||
const CONNECTION_FAILED = 3;
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
const INVALID_ENGINE = 3;
|
*
|
||||||
}
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace xPaw\SourceQuery\Exception;
|
||||||
|
|
||||||
|
final class SocketException extends SourceQueryException
|
||||||
|
{
|
||||||
|
public const COULD_NOT_CREATE_SOCKET = 1;
|
||||||
|
public const NOT_CONNECTED = 2;
|
||||||
|
public const CONNECTION_FAILED = 3;
|
||||||
|
public const INVALID_ENGINE = 3;
|
||||||
|
}
|
||||||
|
@ -1,18 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Pavel Djundik
|
|
||||||
*
|
|
||||||
* @link https://xpaw.me
|
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
|
||||||
*
|
|
||||||
* @license GNU Lesser General Public License, version 2.1
|
|
||||||
*
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace xPaw\SourceQuery\Exception;
|
declare(strict_types=1);
|
||||||
|
|
||||||
abstract class SourceQueryException extends \Exception
|
/**
|
||||||
{
|
* @author Pavel Djundik
|
||||||
// Base exception class
|
*
|
||||||
}
|
* @link https://xpaw.me
|
||||||
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
|
*
|
||||||
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace xPaw\SourceQuery\Exception;
|
||||||
|
|
||||||
|
abstract class SourceQueryException extends \Exception
|
||||||
|
{
|
||||||
|
// Base exception class
|
||||||
|
}
|
||||||
|
@ -1,145 +1,138 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Pavel Djundik
|
declare(strict_types=1);
|
||||||
*
|
|
||||||
* @link https://xpaw.me
|
/**
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @license GNU Lesser General Public License, version 2.1
|
* @link https://xpaw.me
|
||||||
*
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
* @internal
|
*
|
||||||
*/
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
|
*
|
||||||
namespace xPaw\SourceQuery;
|
* @internal
|
||||||
|
*/
|
||||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
/**
|
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||||
* Class GoldSourceRcon
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
*
|
|
||||||
* @package xPaw\SourceQuery
|
/**
|
||||||
*
|
* Class GoldSourceRcon
|
||||||
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
*
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
* @package xPaw\SourceQuery
|
||||||
*/
|
*
|
||||||
class GoldSourceRcon
|
* @uses AuthenticationException
|
||||||
{
|
* @uses InvalidPacketException
|
||||||
/**
|
*/
|
||||||
* Points to socket class
|
final class GoldSourceRcon
|
||||||
*
|
{
|
||||||
* @var BaseSocket
|
/**
|
||||||
*/
|
* Points to socket class
|
||||||
private $Socket;
|
*
|
||||||
|
* @var BaseSocket
|
||||||
private string $RconPassword = '';
|
*/
|
||||||
private string $RconChallenge = '';
|
private $Socket;
|
||||||
|
|
||||||
public function __construct( BaseSocket $Socket )
|
private string $RconPassword = '';
|
||||||
{
|
private string $RconChallenge = '';
|
||||||
$this->Socket = $Socket;
|
|
||||||
}
|
public function __construct(BaseSocket $Socket)
|
||||||
|
{
|
||||||
public function Close( ) : void
|
$this->Socket = $Socket;
|
||||||
{
|
}
|
||||||
$this->RconChallenge = '';
|
|
||||||
$this->RconPassword = '';
|
public function Close(): void
|
||||||
}
|
{
|
||||||
|
$this->RconChallenge = '';
|
||||||
public function Open( ) : void
|
$this->RconPassword = '';
|
||||||
{
|
}
|
||||||
//
|
|
||||||
}
|
public function Open(): void
|
||||||
|
{
|
||||||
public function Write( int $Header, string $String = '' ) : bool
|
//
|
||||||
{
|
}
|
||||||
$Command = pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
|
|
||||||
$Length = strlen( $Command );
|
public function Write(int $Header, string $String = ''): bool
|
||||||
|
{
|
||||||
return $Length === fwrite( $this->Socket->Socket, $Command, $Length );
|
$Command = pack('cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String);
|
||||||
}
|
$Length = strlen($Command);
|
||||||
|
|
||||||
/**
|
return $Length === fwrite($this->Socket->Socket, $Command, $Length);
|
||||||
* @param int $Length
|
}
|
||||||
* @throws AuthenticationException
|
|
||||||
* @return Buffer
|
/**
|
||||||
*/
|
* @param int $Length
|
||||||
public function Read( int $Length = 1400 ) : Buffer
|
* @throws AuthenticationException
|
||||||
{
|
* @return Buffer
|
||||||
// GoldSource RCON has same structure as Query
|
*/
|
||||||
$Buffer = $this->Socket->Read( );
|
public function Read(int $Length = 1400): Buffer
|
||||||
|
{
|
||||||
$StringBuffer = '';
|
// GoldSource RCON has same structure as Query
|
||||||
$ReadMore = false;
|
$Buffer = $this->Socket->Read();
|
||||||
|
|
||||||
// There is no indentifier of the end, so we just need to continue reading
|
$StringBuffer = '';
|
||||||
do
|
$ReadMore = false;
|
||||||
{
|
|
||||||
$ReadMore = $Buffer->Remaining( ) > 0;
|
// There is no indentifier of the end, so we just need to continue reading
|
||||||
|
do {
|
||||||
if( $ReadMore )
|
$ReadMore = $Buffer->Remaining() > 0;
|
||||||
{
|
|
||||||
if( $Buffer->GetByte( ) !== SourceQuery::S2A_RCON )
|
if ($ReadMore) {
|
||||||
{
|
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.') {
|
||||||
|
throw new AuthenticationException($Trimmed, AuthenticationException::BAD_PASSWORD);
|
||||||
if( $Trimmed === 'Bad rcon_password.' )
|
} elseif ($Trimmed === 'You have been banned from this server.') {
|
||||||
{
|
throw new AuthenticationException($Trimmed, AuthenticationException::BANNED);
|
||||||
throw new AuthenticationException( $Trimmed, AuthenticationException::BAD_PASSWORD );
|
}
|
||||||
}
|
|
||||||
else if( $Trimmed === 'You have been banned from this server.' )
|
$Buffer->Set($Trimmed);
|
||||||
{
|
|
||||||
throw new AuthenticationException( $Trimmed, AuthenticationException::BANNED );
|
return $Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
$Buffer->Set( $Trimmed );
|
public function Command(string $Command): string
|
||||||
|
{
|
||||||
return $Buffer;
|
if (!$this->RconChallenge) {
|
||||||
}
|
throw new AuthenticationException('Tried to execute a RCON command before successful authorization.', AuthenticationException::BAD_PASSWORD);
|
||||||
|
}
|
||||||
public function Command( string $Command ) : string
|
|
||||||
{
|
$this->Write(0, 'rcon ' . $this->RconChallenge . ' "' . $this->RconPassword . '" ' . $Command . "\0");
|
||||||
if( !$this->RconChallenge )
|
$Buffer = $this->Read();
|
||||||
{
|
|
||||||
throw new AuthenticationException( 'Tried to execute a RCON command before successful authorization.', AuthenticationException::BAD_PASSWORD );
|
return $Buffer->Get();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Write( 0, 'rcon ' . $this->RconChallenge . ' "' . $this->RconPassword . '" ' . $Command . "\0" );
|
public function Authorize(string $Password): void
|
||||||
$Buffer = $this->Read( );
|
{
|
||||||
|
$this->RconPassword = $Password;
|
||||||
return $Buffer->Get( );
|
|
||||||
}
|
$this->Write(0, 'challenge rcon');
|
||||||
|
$Buffer = $this->Socket->Read();
|
||||||
public function Authorize( string $Password ) : void
|
|
||||||
{
|
if ($Buffer->Get(14) !== 'challenge rcon') {
|
||||||
$this->RconPassword = $Password;
|
throw new AuthenticationException('Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD);
|
||||||
|
}
|
||||||
$this->Write( 0, 'challenge rcon' );
|
|
||||||
$Buffer = $this->Socket->Read( );
|
$this->RconChallenge = trim($Buffer->Get());
|
||||||
|
}
|
||||||
if( $Buffer->Get( 14 ) !== 'challenge rcon' )
|
}
|
||||||
{
|
|
||||||
throw new AuthenticationException( 'Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD );
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->RconChallenge = trim( $Buffer->Get( ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,95 +1,95 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Pavel Djundik
|
declare(strict_types=1);
|
||||||
*
|
|
||||||
* @link https://xpaw.me
|
/**
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @license GNU Lesser General Public License, version 2.1
|
* @link https://xpaw.me
|
||||||
*
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
* @internal
|
*
|
||||||
*/
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
|
*
|
||||||
namespace xPaw\SourceQuery;
|
* @internal
|
||||||
|
*/
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
|
||||||
use xPaw\SourceQuery\Exception\SocketException;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
/**
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
* Class Socket
|
use xPaw\SourceQuery\Exception\SocketException;
|
||||||
*
|
|
||||||
* @package xPaw\SourceQuery
|
/**
|
||||||
*
|
* Class Socket
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
*
|
||||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
* @package xPaw\SourceQuery
|
||||||
*/
|
*
|
||||||
class Socket extends BaseSocket
|
* @uses InvalidPacketException
|
||||||
{
|
* @uses SocketException
|
||||||
public function Close( ) : void
|
*/
|
||||||
{
|
final class Socket extends BaseSocket
|
||||||
if( $this->Socket !== null )
|
{
|
||||||
{
|
public function Close(): void
|
||||||
fclose( $this->Socket );
|
{
|
||||||
|
if ($this->Socket !== null) {
|
||||||
$this->Socket = null;
|
fclose($this->Socket);
|
||||||
}
|
|
||||||
}
|
$this->Socket = null;
|
||||||
|
}
|
||||||
public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void
|
}
|
||||||
{
|
|
||||||
$this->Timeout = $Timeout;
|
public function Open(string $Address, int $Port, int $Timeout, int $Engine): void
|
||||||
$this->Engine = $Engine;
|
{
|
||||||
$this->Port = $Port;
|
$this->Timeout = $Timeout;
|
||||||
$this->Address = $Address;
|
$this->Engine = $Engine;
|
||||||
|
$this->Port = $Port;
|
||||||
$Socket = @fsockopen( 'udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout );
|
$this->Address = $Address;
|
||||||
|
|
||||||
if( $ErrNo || $Socket === false )
|
$Socket = @fsockopen('udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout);
|
||||||
{
|
|
||||||
throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET );
|
if ($ErrNo || $Socket === false) {
|
||||||
}
|
throw new SocketException('Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET);
|
||||||
|
}
|
||||||
$this->Socket = $Socket;
|
|
||||||
stream_set_timeout( $this->Socket, $Timeout );
|
$this->Socket = $Socket;
|
||||||
stream_set_blocking( $this->Socket, true );
|
stream_set_timeout($this->Socket, $Timeout);
|
||||||
}
|
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 );
|
{
|
||||||
$Length = strlen( $Command );
|
$Command = pack('ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String);
|
||||||
|
$Length = strlen($Command);
|
||||||
return $Length === fwrite( $this->Socket, $Command, $Length );
|
|
||||||
}
|
return $Length === fwrite($this->Socket, $Command, $Length);
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Reads from socket and returns Buffer.
|
/**
|
||||||
*
|
* Reads from socket and returns Buffer.
|
||||||
* @throws InvalidPacketException
|
*
|
||||||
*
|
* @throws InvalidPacketException
|
||||||
* @return Buffer Buffer
|
*
|
||||||
*/
|
* @return Buffer Buffer
|
||||||
public function Read( int $Length = 1400 ) : Buffer
|
*/
|
||||||
{
|
public function Read(int $Length = 1400): Buffer
|
||||||
$Buffer = new Buffer( );
|
{
|
||||||
$Buffer->Set( fread( $this->Socket, $Length ) );
|
$Buffer = new Buffer();
|
||||||
|
$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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,199 +1,186 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Pavel Djundik
|
declare(strict_types=1);
|
||||||
*
|
|
||||||
* @link https://xpaw.me
|
/**
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @license GNU Lesser General Public License, version 2.1
|
* @link https://xpaw.me
|
||||||
*
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
* @internal
|
*
|
||||||
*/
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
|
*
|
||||||
namespace xPaw\SourceQuery;
|
* @internal
|
||||||
|
*/
|
||||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
namespace xPaw\SourceQuery;
|
||||||
use xPaw\SourceQuery\Exception\SocketException;
|
|
||||||
|
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||||
/**
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
* Class SourceRcon
|
use xPaw\SourceQuery\Exception\SocketException;
|
||||||
*
|
|
||||||
* @package xPaw\SourceQuery
|
/**
|
||||||
*
|
* Class SourceRcon
|
||||||
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
*
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
* @package xPaw\SourceQuery
|
||||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
*
|
||||||
*/
|
* @uses AuthenticationException
|
||||||
class SourceRcon
|
* @uses InvalidPacketException
|
||||||
{
|
* @uses SocketException
|
||||||
/**
|
*/
|
||||||
* Points to socket class
|
final class SourceRcon
|
||||||
*/
|
{
|
||||||
private BaseSocket $Socket;
|
/**
|
||||||
|
* Points to socket class
|
||||||
/** @var ?resource */
|
*/
|
||||||
private $RconSocket;
|
private BaseSocket $Socket;
|
||||||
private int $RconRequestId = 0;
|
|
||||||
|
/** @var ?resource */
|
||||||
public function __construct( BaseSocket $Socket )
|
private $RconSocket;
|
||||||
{
|
private int $RconRequestId = 0;
|
||||||
$this->Socket = $Socket;
|
|
||||||
}
|
public function __construct(BaseSocket $Socket)
|
||||||
|
{
|
||||||
public function Close( ) : void
|
$this->Socket = $Socket;
|
||||||
{
|
}
|
||||||
if( $this->RconSocket )
|
|
||||||
{
|
public function Close(): void
|
||||||
fclose( $this->RconSocket );
|
{
|
||||||
|
if ($this->RconSocket) {
|
||||||
$this->RconSocket = null;
|
fclose($this->RconSocket);
|
||||||
}
|
|
||||||
|
$this->RconSocket = null;
|
||||||
$this->RconRequestId = 0;
|
}
|
||||||
}
|
|
||||||
|
$this->RconRequestId = 0;
|
||||||
public function Open( ) : void
|
}
|
||||||
{
|
|
||||||
if( !$this->RconSocket )
|
public function Open(): void
|
||||||
{
|
{
|
||||||
$RconSocket = @fsockopen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
|
if (!$this->RconSocket) {
|
||||||
|
$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) {
|
||||||
|
throw new InvalidPacketException('Read ' . strlen($Data) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY);
|
||||||
if( $PacketSize === 0 )
|
}
|
||||||
{
|
|
||||||
throw new InvalidPacketException( 'Read ' . strlen( $Data ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
|
$Data .= $Data2;
|
||||||
}
|
$Remaining -= $PacketSize;
|
||||||
|
}
|
||||||
$Data .= $Data2;
|
|
||||||
$Remaining -= $PacketSize;
|
$Buffer->Set($Data);
|
||||||
}
|
|
||||||
|
return $Buffer;
|
||||||
$Buffer->Set( $Data );
|
}
|
||||||
|
|
||||||
return $Buffer;
|
public function Command(string $Command): string
|
||||||
}
|
{
|
||||||
|
$this->Write(SourceQuery::SERVERDATA_EXECCOMMAND, $Command);
|
||||||
public function Command( string $Command ) : string
|
$Buffer = $this->Read();
|
||||||
{
|
|
||||||
$this->Write( SourceQuery::SERVERDATA_EXECCOMMAND, $Command );
|
$Buffer->GetLong(); // RequestID
|
||||||
$Buffer = $this->Read( );
|
|
||||||
|
$Type = $Buffer->GetLong();
|
||||||
$Buffer->GetLong( ); // RequestID
|
|
||||||
|
if ($Type === SourceQuery::SERVERDATA_AUTH_RESPONSE) {
|
||||||
$Type = $Buffer->GetLong( );
|
throw new AuthenticationException('Bad rcon_password.', AuthenticationException::BAD_PASSWORD);
|
||||||
|
} elseif ($Type !== SourceQuery::SERVERDATA_RESPONSE_VALUE) {
|
||||||
if( $Type === SourceQuery::SERVERDATA_AUTH_RESPONSE )
|
throw new InvalidPacketException('Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH);
|
||||||
{
|
}
|
||||||
throw new AuthenticationException( 'Bad rcon_password.', AuthenticationException::BAD_PASSWORD );
|
|
||||||
}
|
$Data = $Buffer->Get();
|
||||||
else if( $Type !== SourceQuery::SERVERDATA_RESPONSE_VALUE )
|
|
||||||
{
|
// We do this stupid hack to handle split packets
|
||||||
throw new InvalidPacketException( 'Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
|
||||||
}
|
if (strlen($Data) >= 4000) {
|
||||||
|
$this->Write(SourceQuery::SERVERDATA_REQUESTVALUE);
|
||||||
$Data = $Buffer->Get( );
|
|
||||||
|
do {
|
||||||
// We do this stupid hack to handle split packets
|
$Buffer = $this->Read();
|
||||||
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
|
|
||||||
if( strlen( $Data ) >= 4000 )
|
$Buffer->GetLong(); // RequestID
|
||||||
{
|
|
||||||
$this->Write( SourceQuery::SERVERDATA_REQUESTVALUE );
|
if ($Buffer->GetLong() !== SourceQuery::SERVERDATA_RESPONSE_VALUE) {
|
||||||
|
break;
|
||||||
do
|
}
|
||||||
{
|
|
||||||
$Buffer = $this->Read( );
|
$Data2 = $Buffer->Get();
|
||||||
|
|
||||||
$Buffer->GetLong( ); // RequestID
|
if ($Data2 === "\x00\x01\x00\x00\x00\x00") {
|
||||||
|
break;
|
||||||
if( $Buffer->GetLong( ) !== SourceQuery::SERVERDATA_RESPONSE_VALUE )
|
}
|
||||||
{
|
|
||||||
break;
|
$Data .= $Data2;
|
||||||
}
|
} while (true);
|
||||||
|
}
|
||||||
$Data2 = $Buffer->Get( );
|
|
||||||
|
return rtrim($Data, "\0");
|
||||||
if( $Data2 === "\x00\x01\x00\x00\x00\x00" )
|
}
|
||||||
{
|
|
||||||
break;
|
public function Authorize(string $Password): void
|
||||||
}
|
{
|
||||||
|
$this->Write(SourceQuery::SERVERDATA_AUTH, $Password);
|
||||||
$Data .= $Data2;
|
$Buffer = $this->Read();
|
||||||
}
|
|
||||||
while( true );
|
$RequestID = $Buffer->GetLong();
|
||||||
}
|
$Type = $Buffer->GetLong();
|
||||||
|
|
||||||
return rtrim( $Data, "\0" );
|
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
|
||||||
}
|
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
|
||||||
|
|
||||||
public function Authorize( string $Password ) : void
|
if ($Type === SourceQuery::SERVERDATA_RESPONSE_VALUE) {
|
||||||
{
|
$Buffer = $this->Read();
|
||||||
$this->Write( SourceQuery::SERVERDATA_AUTH, $Password );
|
|
||||||
$Buffer = $this->Read( );
|
$RequestID = $Buffer->GetLong();
|
||||||
|
$Type = $Buffer->GetLong();
|
||||||
$RequestID = $Buffer->GetLong( );
|
}
|
||||||
$Type = $Buffer->GetLong( );
|
|
||||||
|
if ($RequestID === -1 || $Type !== SourceQuery::SERVERDATA_AUTH_RESPONSE) {
|
||||||
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
|
throw new AuthenticationException('RCON authorization failed.', AuthenticationException::BAD_PASSWORD);
|
||||||
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
|
}
|
||||||
|
}
|
||||||
if( $Type === SourceQuery::SERVERDATA_RESPONSE_VALUE )
|
}
|
||||||
{
|
|
||||||
$Buffer = $this->Read( );
|
|
||||||
|
|
||||||
$RequestID = $Buffer->GetLong( );
|
|
||||||
$Type = $Buffer->GetLong( );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( $RequestID === -1 || $Type !== SourceQuery::SERVERDATA_AUTH_RESPONSE )
|
|
||||||
{
|
|
||||||
throw new AuthenticationException( 'RCON authorization failed.', AuthenticationException::BAD_PASSWORD );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,27 +1,30 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Library to query servers that implement Source Engine Query protocol.
|
|
||||||
*
|
|
||||||
* Special thanks to koraktor for his awesome Steam Condenser class,
|
|
||||||
* I used it as a reference at some points.
|
|
||||||
*
|
|
||||||
* @author Pavel Djundik
|
|
||||||
*
|
|
||||||
* @link https://xpaw.me
|
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
|
||||||
*
|
|
||||||
* @license GNU Lesser General Public License, version 2.1
|
|
||||||
*/
|
|
||||||
|
|
||||||
require_once __DIR__ . '/Exception/SourceQueryException.php';
|
declare(strict_types=1);
|
||||||
require_once __DIR__ . '/Exception/AuthenticationException.php';
|
|
||||||
require_once __DIR__ . '/Exception/InvalidArgumentException.php';
|
|
||||||
require_once __DIR__ . '/Exception/SocketException.php';
|
|
||||||
require_once __DIR__ . '/Exception/InvalidPacketException.php';
|
|
||||||
|
|
||||||
require_once __DIR__ . '/Buffer.php';
|
/**
|
||||||
require_once __DIR__ . '/BaseSocket.php';
|
* Library to query servers that implement Source Engine Query protocol.
|
||||||
require_once __DIR__ . '/Socket.php';
|
*
|
||||||
require_once __DIR__ . '/SourceRcon.php';
|
* Special thanks to koraktor for his awesome Steam Condenser class,
|
||||||
require_once __DIR__ . '/GoldSourceRcon.php';
|
* I used it as a reference at some points.
|
||||||
require_once __DIR__ . '/SourceQuery.php';
|
*
|
||||||
|
* @author Pavel Djundik
|
||||||
|
*
|
||||||
|
* @link https://xpaw.me
|
||||||
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
|
*
|
||||||
|
* @license GNU Lesser General Public License, version 2.1
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once __DIR__ . '/Exception/SourceQueryException.php';
|
||||||
|
require_once __DIR__ . '/Exception/AuthenticationException.php';
|
||||||
|
require_once __DIR__ . '/Exception/InvalidArgumentException.php';
|
||||||
|
require_once __DIR__ . '/Exception/SocketException.php';
|
||||||
|
require_once __DIR__ . '/Exception/InvalidPacketException.php';
|
||||||
|
|
||||||
|
require_once __DIR__ . '/Buffer.php';
|
||||||
|
require_once __DIR__ . '/BaseSocket.php';
|
||||||
|
require_once __DIR__ . '/Socket.php';
|
||||||
|
require_once __DIR__ . '/SourceRcon.php';
|
||||||
|
require_once __DIR__ . '/GoldSourceRcon.php';
|
||||||
|
require_once __DIR__ . '/SourceQuery.php';
|
||||||
|
@ -1,328 +1,322 @@
|
|||||||
<?php
|
<?php
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
use xPaw\SourceQuery\BaseSocket;
|
declare(strict_types=1);
|
||||||
use xPaw\SourceQuery\SourceQuery;
|
|
||||||
use xPaw\SourceQuery\Buffer;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use xPaw\SourceQuery\BaseSocket;
|
||||||
class TestableSocket extends BaseSocket
|
use xPaw\SourceQuery\SourceQuery;
|
||||||
{
|
use xPaw\SourceQuery\Buffer;
|
||||||
/** @var \SplQueue<string> */
|
|
||||||
private \SplQueue $PacketQueue;
|
final class TestableSocket extends BaseSocket
|
||||||
|
{
|
||||||
public function __construct( )
|
/** @var \SplQueue<string> */
|
||||||
{
|
private \SplQueue $PacketQueue;
|
||||||
/** @var \SplQueue<string> */
|
|
||||||
$this->PacketQueue = new \SplQueue();
|
public function __construct()
|
||||||
$this->PacketQueue->setIteratorMode( \SplDoublyLinkedList::IT_MODE_DELETE );
|
{
|
||||||
|
$this->PacketQueue = new \SplQueue();
|
||||||
}
|
$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->Engine = $Engine;
|
$this->Timeout = $Timeout;
|
||||||
$this->Port = $Port;
|
$this->Engine = $Engine;
|
||||||
$this->Address = $Address;
|
$this->Port = $Port;
|
||||||
}
|
$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->Set( $this->PacketQueue->shift() );
|
$Buffer = new Buffer();
|
||||||
|
$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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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();
|
|
||||||
|
self::assertEquals($ExpectedOutput, $RealOutput);
|
||||||
$this->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))),
|
||||||
[
|
json_decode(file_get_contents(str_replace('.raw', '.json', $File)), true)
|
||||||
hex2bin( trim( file_get_contents( $File ) ) ),
|
];
|
||||||
json_decode( file_get_contents( str_replace( '.raw', '.json', $File ) ), true )
|
}
|
||||||
];
|
|
||||||
}
|
return $DataProvider;
|
||||||
|
}
|
||||||
return $DataProvider;
|
|
||||||
}
|
/**
|
||||||
|
* @dataProvider BadPacketProvider
|
||||||
/**
|
*/
|
||||||
* @dataProvider BadPacketProvider
|
public function testBadGetInfo(string $Data): void
|
||||||
*/
|
{
|
||||||
public function testBadGetInfo( string $Data ) : void
|
$this->expectException(xPaw\SourceQuery\Exception\InvalidPacketException::class);
|
||||||
{
|
$this->Socket->Queue($Data);
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
|
||||||
$this->Socket->Queue( $Data );
|
$this->SourceQuery->GetInfo();
|
||||||
|
}
|
||||||
$this->SourceQuery->GetInfo();
|
|
||||||
}
|
/**
|
||||||
|
* @dataProvider BadPacketProvider
|
||||||
/**
|
*/
|
||||||
* @dataProvider BadPacketProvider
|
public function testBadGetChallengeViaPlayers(string $Data): void
|
||||||
*/
|
{
|
||||||
public function testBadGetChallengeViaPlayers( string $Data ) : void
|
$this->expectException(xPaw\SourceQuery\Exception\InvalidPacketException::class);
|
||||||
{
|
$this->Socket->Queue($Data);
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
|
||||||
$this->Socket->Queue( $Data );
|
$this->SourceQuery->GetPlayers();
|
||||||
|
}
|
||||||
$this->SourceQuery->GetPlayers();
|
|
||||||
}
|
/**
|
||||||
|
* @dataProvider BadPacketProvider
|
||||||
/**
|
*/
|
||||||
* @dataProvider BadPacketProvider
|
public function testBadGetPlayersAfterCorrectChallenge(string $Data): void
|
||||||
*/
|
{
|
||||||
public function testBadGetPlayersAfterCorrectChallenge( string $Data ) : void
|
$this->expectException(xPaw\SourceQuery\Exception\InvalidPacketException::class);
|
||||||
{
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11");
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
$this->Socket->Queue($Data);
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11" );
|
|
||||||
$this->Socket->Queue( $Data );
|
$this->SourceQuery->GetPlayers();
|
||||||
|
}
|
||||||
$this->SourceQuery->GetPlayers();
|
|
||||||
}
|
/**
|
||||||
|
* @dataProvider BadPacketProvider
|
||||||
/**
|
*/
|
||||||
* @dataProvider BadPacketProvider
|
public function testBadGetRulesAfterCorrectChallenge(string $Data): void
|
||||||
*/
|
{
|
||||||
public function testBadGetRulesAfterCorrectChallenge( string $Data ) : void
|
$this->expectException(xPaw\SourceQuery\Exception\InvalidPacketException::class);
|
||||||
{
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11");
|
||||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
$this->Socket->Queue($Data);
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11" );
|
|
||||||
$this->Socket->Queue( $Data );
|
$this->SourceQuery->GetRules();
|
||||||
|
}
|
||||||
$this->SourceQuery->GetRules();
|
|
||||||
}
|
public function BadPacketProvider(): array
|
||||||
|
{
|
||||||
public function BadPacketProvider( ) : array
|
return
|
||||||
{
|
[
|
||||||
return
|
[ "" ],
|
||||||
[
|
[ "\xff\xff\xff\xff" ], // No type
|
||||||
[ "" ],
|
[ "\xff\xff\xff\xff\x49" ], // Correct type, but no data after
|
||||||
[ "\xff\xff\xff\xff" ], // No type
|
[ "\xff\xff\xff\xff\x6D" ], // Old info packet, but tests are done for source
|
||||||
[ "\xff\xff\xff\xff\x49" ], // Correct type, but no data after
|
[ "\xff\xff\xff\xff\x11" ], // Wrong type
|
||||||
[ "\xff\xff\xff\xff\x6D" ], // Old info packet, but tests are done for source
|
[ "\x11\x11\x11\x11" ], // Wrong header
|
||||||
[ "\xff\xff\xff\xff\x11" ], // Wrong type
|
[ "\xff" ], // Should be 4 bytes, but it's 1
|
||||||
[ "\x11\x11\x11\x11" ], // Wrong header
|
];
|
||||||
[ "\xff" ], // Should be 4 bytes, but it's 1
|
}
|
||||||
];
|
|
||||||
}
|
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\x45\x01\x00ayy\x00lmao\x00");
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11" );
|
self::assertEquals([ 'ayy' => 'lmao' ], $this->SourceQuery->GetRules());
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x45\x01\x00ayy\x00lmao\x00" );
|
|
||||||
$this->assertEquals( [ 'ayy' => 'lmao' ], $this->SourceQuery->GetRules() );
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x45\x01\x00wow\x00much\x00");
|
||||||
|
self::assertEquals([ 'wow' => 'much' ], $this->SourceQuery->GetRules());
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x45\x01\x00wow\x00much\x00" );
|
}
|
||||||
$this->assertEquals( [ 'wow' => 'much' ], $this->SourceQuery->GetRules() );
|
|
||||||
}
|
/**
|
||||||
|
* @dataProvider RulesProvider
|
||||||
/**
|
* @param array<string> $RawInput
|
||||||
* @dataProvider RulesProvider
|
*/
|
||||||
* @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) {
|
||||||
|
$this->Socket->Queue(hex2bin($Packet));
|
||||||
foreach( $RawInput as $Packet )
|
}
|
||||||
{
|
|
||||||
$this->Socket->Queue( hex2bin( $Packet ) );
|
$RealOutput = $this->SourceQuery->GetRules();
|
||||||
}
|
|
||||||
|
self::assertEquals($ExpectedOutput, $RealOutput);
|
||||||
$RealOutput = $this->SourceQuery->GetRules();
|
}
|
||||||
|
|
||||||
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
public function RulesProvider(): array
|
||||||
}
|
{
|
||||||
|
$DataProvider = [];
|
||||||
public function RulesProvider() : array
|
|
||||||
{
|
$Files = glob(__DIR__ . '/Rules/*.raw', GLOB_ERR);
|
||||||
$DataProvider = [];
|
|
||||||
|
foreach ($Files as $File) {
|
||||||
$Files = glob( __DIR__ . '/Rules/*.raw', GLOB_ERR );
|
$DataProvider[] =
|
||||||
|
[
|
||||||
foreach( $Files as $File )
|
file($File, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES),
|
||||||
{
|
json_decode(file_get_contents(str_replace('.raw', '.json', $File)), true)
|
||||||
$DataProvider[] =
|
];
|
||||||
[
|
}
|
||||||
file( $File, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES ),
|
|
||||||
json_decode( file_get_contents( str_replace( '.raw', '.json', $File ) ), true )
|
return $DataProvider;
|
||||||
];
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
return $DataProvider;
|
* @dataProvider PlayersProvider
|
||||||
}
|
* @param array<string> $RawInput
|
||||||
|
*/
|
||||||
/**
|
public function testGetPlayers(array $RawInput, array $ExpectedOutput): void
|
||||||
* @dataProvider PlayersProvider
|
{
|
||||||
* @param array<string> $RawInput
|
$this->Socket->Queue(hex2bin("ffffffff4104fce20e")); // Challenge
|
||||||
*/
|
|
||||||
public function testGetPlayers( array $RawInput, array $ExpectedOutput ) : void
|
foreach ($RawInput as $Packet) {
|
||||||
{
|
$this->Socket->Queue(hex2bin($Packet));
|
||||||
$this->Socket->Queue( hex2bin( "ffffffff4104fce20e" ) ); // Challenge
|
}
|
||||||
|
|
||||||
foreach( $RawInput as $Packet )
|
$RealOutput = $this->SourceQuery->GetPlayers();
|
||||||
{
|
|
||||||
$this->Socket->Queue( hex2bin( $Packet ) );
|
self::assertEquals($ExpectedOutput, $RealOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
$RealOutput = $this->SourceQuery->GetPlayers();
|
public function PlayersProvider(): array
|
||||||
|
{
|
||||||
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
$DataProvider = [];
|
||||||
}
|
|
||||||
|
$Files = glob(__DIR__ . '/Players/*.raw', GLOB_ERR);
|
||||||
public function PlayersProvider() : array
|
|
||||||
{
|
foreach ($Files as $File) {
|
||||||
$DataProvider = [];
|
$DataProvider[] =
|
||||||
|
[
|
||||||
$Files = glob( __DIR__ . '/Players/*.raw', GLOB_ERR );
|
file($File, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES),
|
||||||
|
json_decode(file_get_contents(str_replace('.raw', '.json', $File)), true)
|
||||||
foreach( $Files as $File )
|
];
|
||||||
{
|
}
|
||||||
$DataProvider[] =
|
|
||||||
[
|
return $DataProvider;
|
||||||
file( $File, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES ),
|
}
|
||||||
json_decode( file_get_contents( str_replace( '.raw', '.json', $File ) ), true )
|
|
||||||
];
|
public function testPing(): void
|
||||||
}
|
{
|
||||||
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x6A\x00");
|
||||||
return $DataProvider;
|
self::assertTrue($this->SourceQuery->Ping());
|
||||||
}
|
|
||||||
|
$this->Socket->Queue("\xFF\xFF\xFF\xFF\xEE");
|
||||||
public function testPing() : void
|
self::assertFalse($this->SourceQuery->Ping());
|
||||||
{
|
}
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x6A\x00");
|
}
|
||||||
$this->assertTrue( $this->SourceQuery->Ping() );
|
|
||||||
|
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\xEE");
|
|
||||||
$this->assertFalse( $this->SourceQuery->Ping() );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in new issue