mirror of
https://github.com/xPaw/PHP-Source-Query.git
synced 2026-06-10 23:03:15 +02:00
Bring in CS-Fixer, reformat.
This commit is contained in:
+125
-137
@@ -1,139 +1,127 @@
|
||||
<?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;
|
||||
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
use xPaw\SourceQuery\Exception\SocketException;
|
||||
|
||||
/**
|
||||
* Base socket interface
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
abstract class BaseSocket
|
||||
{
|
||||
/** @var ?resource */
|
||||
public $Socket;
|
||||
public int $Engine;
|
||||
|
||||
public string $Address;
|
||||
public int $Port;
|
||||
public int $Timeout;
|
||||
|
||||
public function __destruct( )
|
||||
{
|
||||
$this->Close( );
|
||||
}
|
||||
|
||||
abstract public function Close( ) : 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 Read( int $Length = 1400 ) : Buffer;
|
||||
|
||||
protected function ReadInternal( Buffer $Buffer, int $Length, callable $SherlockFunction ) : Buffer
|
||||
{
|
||||
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
|
||||
{
|
||||
// We don't have to do anything
|
||||
}
|
||||
else if( $Header === -2 ) // Split packet
|
||||
{
|
||||
$Packets = [];
|
||||
$IsCompressed = false;
|
||||
$ReadMore = false;
|
||||
$PacketChecksum = null;
|
||||
|
||||
do
|
||||
{
|
||||
$RequestID = $Buffer->GetLong( );
|
||||
|
||||
switch( $this->Engine )
|
||||
{
|
||||
case SourceQuery::GOLDSOURCE:
|
||||
{
|
||||
$PacketCountAndNumber = $Buffer->GetByte( );
|
||||
$PacketCount = $PacketCountAndNumber & 0xF;
|
||||
$PacketNumber = $PacketCountAndNumber >> 4;
|
||||
|
||||
break;
|
||||
}
|
||||
case SourceQuery::SOURCE:
|
||||
{
|
||||
$IsCompressed = ( $RequestID & 0x80000000 ) !== 0;
|
||||
$PacketCount = $Buffer->GetByte( );
|
||||
$PacketNumber = $Buffer->GetByte( ) + 1;
|
||||
|
||||
if( $IsCompressed )
|
||||
{
|
||||
$Buffer->GetLong( ); // Split size
|
||||
|
||||
$PacketChecksum = $Buffer->GetUnsignedLong( );
|
||||
}
|
||||
else
|
||||
{
|
||||
$Buffer->GetShort( ); // Split size
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE );
|
||||
}
|
||||
}
|
||||
|
||||
$Packets[ $PacketNumber ] = $Buffer->Get( );
|
||||
|
||||
$ReadMore = $PacketCount > sizeof( $Packets );
|
||||
}
|
||||
while( $ReadMore && $SherlockFunction( $Buffer, $Length ) );
|
||||
|
||||
$Data = implode( $Packets );
|
||||
|
||||
// TODO: Test this
|
||||
if( $IsCompressed )
|
||||
{
|
||||
// Let's make sure this function exists, it's not included in PHP by default
|
||||
if( !function_exists( 'bzdecompress' ) )
|
||||
{
|
||||
throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' );
|
||||
}
|
||||
|
||||
$Data = bzdecompress( $Data );
|
||||
|
||||
if( !is_string( $Data ) || crc32( $Data ) !== $PacketChecksum )
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @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;
|
||||
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
use xPaw\SourceQuery\Exception\SocketException;
|
||||
|
||||
/**
|
||||
* Base socket interface
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
abstract class BaseSocket
|
||||
{
|
||||
/** @var ?resource */
|
||||
public $Socket;
|
||||
public int $Engine;
|
||||
|
||||
public string $Address;
|
||||
public int $Port;
|
||||
public int $Timeout;
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->Close();
|
||||
}
|
||||
|
||||
abstract public function Close(): 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 Read(int $Length = 1400): Buffer;
|
||||
|
||||
protected function ReadInternal(Buffer $Buffer, int $Length, callable $SherlockFunction): Buffer
|
||||
{
|
||||
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
|
||||
// We don't have to do anything
|
||||
} elseif ($Header === -2) { // Split packet
|
||||
$Packets = [];
|
||||
$IsCompressed = false;
|
||||
$ReadMore = false;
|
||||
$PacketChecksum = null;
|
||||
|
||||
do {
|
||||
$RequestID = $Buffer->GetLong();
|
||||
|
||||
switch ($this->Engine) {
|
||||
case SourceQuery::GOLDSOURCE:
|
||||
{
|
||||
$PacketCountAndNumber = $Buffer->GetByte();
|
||||
$PacketCount = $PacketCountAndNumber & 0xF;
|
||||
$PacketNumber = $PacketCountAndNumber >> 4;
|
||||
|
||||
break;
|
||||
}
|
||||
case SourceQuery::SOURCE:
|
||||
{
|
||||
$IsCompressed = ($RequestID & 0x80000000) !== 0;
|
||||
$PacketCount = $Buffer->GetByte();
|
||||
$PacketNumber = $Buffer->GetByte() + 1;
|
||||
|
||||
if ($IsCompressed) {
|
||||
$Buffer->GetLong(); // Split size
|
||||
|
||||
$PacketChecksum = $Buffer->GetUnsignedLong();
|
||||
} else {
|
||||
$Buffer->GetShort(); // Split size
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new SocketException('Unknown engine.', SocketException::INVALID_ENGINE);
|
||||
}
|
||||
}
|
||||
|
||||
$Packets[ $PacketNumber ] = $Buffer->Get();
|
||||
|
||||
$ReadMore = $PacketCount > sizeof($Packets);
|
||||
} while ($ReadMore && $SherlockFunction($Buffer, $Length));
|
||||
|
||||
$Data = implode($Packets);
|
||||
|
||||
// TODO: Test this
|
||||
if ($IsCompressed) {
|
||||
// Let's make sure this function exists, it's not included in PHP by default
|
||||
if (!function_exists('bzdecompress')) {
|
||||
throw new \RuntimeException('Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.');
|
||||
}
|
||||
|
||||
$Data = bzdecompress($Data);
|
||||
|
||||
if (!is_string($Data) || crc32($Data) !== $PacketChecksum) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
+167
-173
@@ -1,177 +1,171 @@
|
||||
<?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;
|
||||
declare(strict_types=1);
|
||||
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class Buffer
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
*/
|
||||
class Buffer
|
||||
{
|
||||
/**
|
||||
* Buffer
|
||||
*/
|
||||
private string $Buffer = '';
|
||||
|
||||
/**
|
||||
* Buffer length
|
||||
*/
|
||||
private int $Length = 0;
|
||||
|
||||
/**
|
||||
* Current position in buffer
|
||||
*/
|
||||
private int $Position = 0;
|
||||
|
||||
/**
|
||||
* Sets buffer
|
||||
*/
|
||||
public function Set( string $Buffer ) : void
|
||||
{
|
||||
$this->Buffer = $Buffer;
|
||||
$this->Length = strlen( $Buffer );
|
||||
$this->Position = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get remaining bytes
|
||||
*
|
||||
* @return int Remaining bytes in buffer
|
||||
*/
|
||||
public function Remaining( ) : int
|
||||
{
|
||||
return $this->Length - $this->Position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets data from buffer
|
||||
*
|
||||
* @param int $Length Bytes to read
|
||||
*/
|
||||
public function Get( int $Length = -1 ) : string
|
||||
{
|
||||
if( $Length === 0 )
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
$Remaining = $this->Remaining( );
|
||||
|
||||
if( $Length === -1 )
|
||||
{
|
||||
$Length = $Remaining;
|
||||
}
|
||||
else if( $Length > $Remaining )
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
$Data = substr( $this->Buffer, $this->Position, $Length );
|
||||
|
||||
$this->Position += $Length;
|
||||
|
||||
return $Data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get byte from buffer
|
||||
*/
|
||||
public function GetByte( ) : int
|
||||
{
|
||||
return ord( $this->Get( 1 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get short from buffer
|
||||
*/
|
||||
public function GetShort( ) : int
|
||||
{
|
||||
if( $this->Remaining( ) < 2 )
|
||||
{
|
||||
throw new InvalidPacketException( 'Not enough data to unpack a short.', InvalidPacketException::BUFFER_EMPTY );
|
||||
}
|
||||
|
||||
$Data = unpack( 'v', $this->Get( 2 ) );
|
||||
|
||||
return (int)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get long from buffer
|
||||
*/
|
||||
public function GetLong( ) : int
|
||||
{
|
||||
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 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get float from buffer
|
||||
*/
|
||||
public function GetFloat( ) : float
|
||||
{
|
||||
if( $this->Remaining( ) < 4 )
|
||||
{
|
||||
throw new InvalidPacketException( 'Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY );
|
||||
}
|
||||
|
||||
$Data = unpack( 'f', $this->Get( 4 ) );
|
||||
|
||||
return (float)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unsigned long from buffer
|
||||
*/
|
||||
public function GetUnsignedLong( ) : int
|
||||
{
|
||||
if( $this->Remaining( ) < 4 )
|
||||
{
|
||||
throw new InvalidPacketException( 'Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY );
|
||||
}
|
||||
|
||||
$Data = unpack( 'V', $this->Get( 4 ) );
|
||||
|
||||
return (int)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Read one string from buffer ending with null byte
|
||||
*/
|
||||
public function GetString( ) : string
|
||||
{
|
||||
$ZeroBytePosition = strpos( $this->Buffer, "\0", $this->Position );
|
||||
|
||||
if( $ZeroBytePosition === false )
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
$String = $this->Get( $ZeroBytePosition - $this->Position );
|
||||
|
||||
$this->Position++;
|
||||
|
||||
return $String;
|
||||
}
|
||||
}
|
||||
namespace xPaw\SourceQuery;
|
||||
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
|
||||
/**
|
||||
* Class Buffer
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
*/
|
||||
final class Buffer
|
||||
{
|
||||
/**
|
||||
* Buffer
|
||||
*/
|
||||
private string $Buffer = '';
|
||||
|
||||
/**
|
||||
* Buffer length
|
||||
*/
|
||||
private int $Length = 0;
|
||||
|
||||
/**
|
||||
* Current position in buffer
|
||||
*/
|
||||
private int $Position = 0;
|
||||
|
||||
/**
|
||||
* Sets buffer
|
||||
*/
|
||||
public function Set(string $Buffer): void
|
||||
{
|
||||
$this->Buffer = $Buffer;
|
||||
$this->Length = strlen($Buffer);
|
||||
$this->Position = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get remaining bytes
|
||||
*
|
||||
* @return int Remaining bytes in buffer
|
||||
*/
|
||||
public function Remaining(): int
|
||||
{
|
||||
return $this->Length - $this->Position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets data from buffer
|
||||
*
|
||||
* @param int $Length Bytes to read
|
||||
*/
|
||||
public function Get(int $Length = -1): string
|
||||
{
|
||||
if ($Length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$Remaining = $this->Remaining();
|
||||
|
||||
if ($Length === -1) {
|
||||
$Length = $Remaining;
|
||||
} elseif ($Length > $Remaining) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$Data = substr($this->Buffer, $this->Position, $Length);
|
||||
|
||||
$this->Position += $Length;
|
||||
|
||||
return $Data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get byte from buffer
|
||||
*/
|
||||
public function GetByte(): int
|
||||
{
|
||||
return ord($this->Get(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get short from buffer
|
||||
*/
|
||||
public function GetShort(): int
|
||||
{
|
||||
if ($this->Remaining() < 2) {
|
||||
throw new InvalidPacketException('Not enough data to unpack a short.', InvalidPacketException::BUFFER_EMPTY);
|
||||
}
|
||||
|
||||
$Data = unpack('v', $this->Get(2));
|
||||
|
||||
return (int)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get long from buffer
|
||||
*/
|
||||
public function GetLong(): int
|
||||
{
|
||||
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 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get float from buffer
|
||||
*/
|
||||
public function GetFloat(): float
|
||||
{
|
||||
if ($this->Remaining() < 4) {
|
||||
throw new InvalidPacketException('Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY);
|
||||
}
|
||||
|
||||
$Data = unpack('f', $this->Get(4));
|
||||
|
||||
return (float)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unsigned long from buffer
|
||||
*/
|
||||
public function GetUnsignedLong(): int
|
||||
{
|
||||
if ($this->Remaining() < 4) {
|
||||
throw new InvalidPacketException('Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY);
|
||||
}
|
||||
|
||||
$Data = unpack('V', $this->Get(4));
|
||||
|
||||
return (int)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Read one string from buffer ending with null byte
|
||||
*/
|
||||
public function GetString(): string
|
||||
{
|
||||
$ZeroBytePosition = strpos($this->Buffer, "\0", $this->Position);
|
||||
|
||||
if ($ZeroBytePosition === false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$String = $this->Get($ZeroBytePosition - $this->Position);
|
||||
|
||||
$this->Position++;
|
||||
|
||||
return $String;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
<?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
|
||||
{
|
||||
const BAD_PASSWORD = 1;
|
||||
const BANNED = 2;
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
|
||||
final class AuthenticationException extends SourceQueryException
|
||||
{
|
||||
public const BAD_PASSWORD = 1;
|
||||
public const BANNED = 2;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
<?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
|
||||
{
|
||||
const TIMEOUT_NOT_INTEGER = 1;
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
|
||||
final class InvalidArgumentException extends SourceQueryException
|
||||
{
|
||||
public const TIMEOUT_NOT_INTEGER = 1;
|
||||
}
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
<?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
|
||||
{
|
||||
const PACKET_HEADER_MISMATCH = 1;
|
||||
const BUFFER_EMPTY = 2;
|
||||
const BUFFER_NOT_EMPTY = 3;
|
||||
const CHECKSUM_MISMATCH = 4;
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
|
||||
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
|
||||
/**
|
||||
* @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
|
||||
{
|
||||
const COULD_NOT_CREATE_SOCKET = 1;
|
||||
const NOT_CONNECTED = 2;
|
||||
const CONNECTION_FAILED = 3;
|
||||
const INVALID_ENGINE = 3;
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
|
||||
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
|
||||
/**
|
||||
* @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
|
||||
{
|
||||
// Base exception class
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
|
||||
abstract class SourceQueryException extends \Exception
|
||||
{
|
||||
// Base exception class
|
||||
}
|
||||
|
||||
+135
-142
@@ -1,145 +1,138 @@
|
||||
<?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;
|
||||
|
||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Class GoldSourceRcon
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
*/
|
||||
class GoldSourceRcon
|
||||
{
|
||||
/**
|
||||
* Points to socket class
|
||||
*
|
||||
* @var BaseSocket
|
||||
*/
|
||||
private $Socket;
|
||||
|
||||
private string $RconPassword = '';
|
||||
private string $RconChallenge = '';
|
||||
|
||||
public function __construct( BaseSocket $Socket )
|
||||
{
|
||||
$this->Socket = $Socket;
|
||||
}
|
||||
|
||||
public function Close( ) : void
|
||||
{
|
||||
$this->RconChallenge = '';
|
||||
$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 );
|
||||
|
||||
return $Length === fwrite( $this->Socket->Socket, $Command, $Length );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $Length
|
||||
* @throws AuthenticationException
|
||||
* @return Buffer
|
||||
*/
|
||||
public function Read( int $Length = 1400 ) : Buffer
|
||||
{
|
||||
// GoldSource RCON has same structure as Query
|
||||
$Buffer = $this->Socket->Read( );
|
||||
|
||||
$StringBuffer = '';
|
||||
$ReadMore = false;
|
||||
|
||||
// There is no indentifier of the end, so we just need to continue reading
|
||||
do
|
||||
{
|
||||
$ReadMore = $Buffer->Remaining( ) > 0;
|
||||
|
||||
if( $ReadMore )
|
||||
{
|
||||
if( $Buffer->GetByte( ) !== SourceQuery::S2A_RCON )
|
||||
{
|
||||
throw new InvalidPacketException( 'Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
}
|
||||
|
||||
$Packet = $Buffer->Get( );
|
||||
$StringBuffer .= $Packet;
|
||||
//$StringBuffer .= SubStr( $Packet, 0, -2 );
|
||||
|
||||
// Let's assume if this packet is not long enough, there are no more after this one
|
||||
$ReadMore = strlen( $Packet ) > 1000; // use 1300?
|
||||
|
||||
if( $ReadMore )
|
||||
{
|
||||
$Buffer = $this->Socket->Read( );
|
||||
}
|
||||
}
|
||||
}
|
||||
while( $ReadMore );
|
||||
|
||||
$Trimmed = trim( $StringBuffer );
|
||||
|
||||
if( $Trimmed === 'Bad rcon_password.' )
|
||||
{
|
||||
throw new AuthenticationException( $Trimmed, AuthenticationException::BAD_PASSWORD );
|
||||
}
|
||||
else if( $Trimmed === 'You have been banned from this server.' )
|
||||
{
|
||||
throw new AuthenticationException( $Trimmed, AuthenticationException::BANNED );
|
||||
}
|
||||
|
||||
$Buffer->Set( $Trimmed );
|
||||
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Command( string $Command ) : string
|
||||
{
|
||||
if( !$this->RconChallenge )
|
||||
{
|
||||
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" );
|
||||
$Buffer = $this->Read( );
|
||||
|
||||
return $Buffer->Get( );
|
||||
}
|
||||
|
||||
public function Authorize( string $Password ) : void
|
||||
{
|
||||
$this->RconPassword = $Password;
|
||||
|
||||
$this->Write( 0, 'challenge rcon' );
|
||||
$Buffer = $this->Socket->Read( );
|
||||
|
||||
if( $Buffer->Get( 14 ) !== 'challenge rcon' )
|
||||
{
|
||||
throw new AuthenticationException( 'Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD );
|
||||
}
|
||||
|
||||
$this->RconChallenge = trim( $Buffer->Get( ) );
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
|
||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
|
||||
/**
|
||||
* Class GoldSourceRcon
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses AuthenticationException
|
||||
* @uses InvalidPacketException
|
||||
*/
|
||||
final class GoldSourceRcon
|
||||
{
|
||||
/**
|
||||
* Points to socket class
|
||||
*
|
||||
* @var BaseSocket
|
||||
*/
|
||||
private $Socket;
|
||||
|
||||
private string $RconPassword = '';
|
||||
private string $RconChallenge = '';
|
||||
|
||||
public function __construct(BaseSocket $Socket)
|
||||
{
|
||||
$this->Socket = $Socket;
|
||||
}
|
||||
|
||||
public function Close(): void
|
||||
{
|
||||
$this->RconChallenge = '';
|
||||
$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);
|
||||
|
||||
return $Length === fwrite($this->Socket->Socket, $Command, $Length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $Length
|
||||
* @throws AuthenticationException
|
||||
* @return Buffer
|
||||
*/
|
||||
public function Read(int $Length = 1400): Buffer
|
||||
{
|
||||
// GoldSource RCON has same structure as Query
|
||||
$Buffer = $this->Socket->Read();
|
||||
|
||||
$StringBuffer = '';
|
||||
$ReadMore = false;
|
||||
|
||||
// There is no indentifier of the end, so we just need to continue reading
|
||||
do {
|
||||
$ReadMore = $Buffer->Remaining() > 0;
|
||||
|
||||
if ($ReadMore) {
|
||||
if ($Buffer->GetByte() !== SourceQuery::S2A_RCON) {
|
||||
throw new InvalidPacketException('Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH);
|
||||
}
|
||||
|
||||
$Packet = $Buffer->Get();
|
||||
$StringBuffer .= $Packet;
|
||||
//$StringBuffer .= SubStr( $Packet, 0, -2 );
|
||||
|
||||
// Let's assume if this packet is not long enough, there are no more after this one
|
||||
$ReadMore = strlen($Packet) > 1000; // use 1300?
|
||||
|
||||
if ($ReadMore) {
|
||||
$Buffer = $this->Socket->Read();
|
||||
}
|
||||
}
|
||||
} while ($ReadMore);
|
||||
|
||||
$Trimmed = trim($StringBuffer);
|
||||
|
||||
if ($Trimmed === 'Bad rcon_password.') {
|
||||
throw new AuthenticationException($Trimmed, AuthenticationException::BAD_PASSWORD);
|
||||
} elseif ($Trimmed === 'You have been banned from this server.') {
|
||||
throw new AuthenticationException($Trimmed, AuthenticationException::BANNED);
|
||||
}
|
||||
|
||||
$Buffer->Set($Trimmed);
|
||||
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Command(string $Command): string
|
||||
{
|
||||
if (!$this->RconChallenge) {
|
||||
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");
|
||||
$Buffer = $this->Read();
|
||||
|
||||
return $Buffer->Get();
|
||||
}
|
||||
|
||||
public function Authorize(string $Password): void
|
||||
{
|
||||
$this->RconPassword = $Password;
|
||||
|
||||
$this->Write(0, 'challenge rcon');
|
||||
$Buffer = $this->Socket->Read();
|
||||
|
||||
if ($Buffer->Get(14) !== 'challenge rcon') {
|
||||
throw new AuthenticationException('Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD);
|
||||
}
|
||||
|
||||
$this->RconChallenge = trim($Buffer->Get());
|
||||
}
|
||||
}
|
||||
|
||||
+92
-92
@@ -1,95 +1,95 @@
|
||||
<?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;
|
||||
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
use xPaw\SourceQuery\Exception\SocketException;
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Class Socket
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
class Socket extends BaseSocket
|
||||
{
|
||||
public function Close( ) : void
|
||||
{
|
||||
if( $this->Socket !== null )
|
||||
{
|
||||
fclose( $this->Socket );
|
||||
|
||||
$this->Socket = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void
|
||||
{
|
||||
$this->Timeout = $Timeout;
|
||||
$this->Engine = $Engine;
|
||||
$this->Port = $Port;
|
||||
$this->Address = $Address;
|
||||
|
||||
$Socket = @fsockopen( 'udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout );
|
||||
|
||||
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 );
|
||||
stream_set_blocking( $this->Socket, true );
|
||||
}
|
||||
|
||||
public function Write( int $Header, string $String = '' ) : bool
|
||||
{
|
||||
$Command = pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
|
||||
$Length = strlen( $Command );
|
||||
|
||||
return $Length === fwrite( $this->Socket, $Command, $Length );
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from socket and returns Buffer.
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
*
|
||||
* @return Buffer Buffer
|
||||
*/
|
||||
public function Read( int $Length = 1400 ) : Buffer
|
||||
{
|
||||
$Buffer = new Buffer( );
|
||||
$Buffer->Set( fread( $this->Socket, $Length ) );
|
||||
|
||||
$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
|
||||
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Sherlock( Buffer $Buffer, int $Length ) : bool
|
||||
{
|
||||
$Data = fread( $this->Socket, $Length );
|
||||
|
||||
if( strlen( $Data ) < 4 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$Buffer->Set( $Data );
|
||||
|
||||
return $Buffer->GetLong( ) === -2;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
use xPaw\SourceQuery\Exception\SocketException;
|
||||
|
||||
/**
|
||||
* Class Socket
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses InvalidPacketException
|
||||
* @uses SocketException
|
||||
*/
|
||||
final class Socket extends BaseSocket
|
||||
{
|
||||
public function Close(): void
|
||||
{
|
||||
if ($this->Socket !== null) {
|
||||
fclose($this->Socket);
|
||||
|
||||
$this->Socket = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function Open(string $Address, int $Port, int $Timeout, int $Engine): void
|
||||
{
|
||||
$this->Timeout = $Timeout;
|
||||
$this->Engine = $Engine;
|
||||
$this->Port = $Port;
|
||||
$this->Address = $Address;
|
||||
|
||||
$Socket = @fsockopen('udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout);
|
||||
|
||||
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);
|
||||
stream_set_blocking($this->Socket, true);
|
||||
}
|
||||
|
||||
public function Write(int $Header, string $String = ''): bool
|
||||
{
|
||||
$Command = pack('ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String);
|
||||
$Length = strlen($Command);
|
||||
|
||||
return $Length === fwrite($this->Socket, $Command, $Length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from socket and returns Buffer.
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
*
|
||||
* @return Buffer Buffer
|
||||
*/
|
||||
public function Read(int $Length = 1400): Buffer
|
||||
{
|
||||
$Buffer = new Buffer();
|
||||
$Buffer->Set(fread($this->Socket, $Length));
|
||||
|
||||
$this->ReadInternal($Buffer, $Length, [ $this, 'Sherlock' ]);
|
||||
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Sherlock(Buffer $Buffer, int $Length): bool
|
||||
{
|
||||
$Data = fread($this->Socket, $Length);
|
||||
|
||||
if (strlen($Data) < 4) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$Buffer->Set($Data);
|
||||
|
||||
return $Buffer->GetLong() === -2;
|
||||
}
|
||||
}
|
||||
|
||||
+529
-563
File diff suppressed because it is too large
Load Diff
+183
-196
@@ -1,199 +1,186 @@
|
||||
<?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;
|
||||
|
||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
use xPaw\SourceQuery\Exception\SocketException;
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Class SourceRcon
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
class SourceRcon
|
||||
{
|
||||
/**
|
||||
* Points to socket class
|
||||
*/
|
||||
private BaseSocket $Socket;
|
||||
|
||||
/** @var ?resource */
|
||||
private $RconSocket;
|
||||
private int $RconRequestId = 0;
|
||||
|
||||
public function __construct( BaseSocket $Socket )
|
||||
{
|
||||
$this->Socket = $Socket;
|
||||
}
|
||||
|
||||
public function Close( ) : void
|
||||
{
|
||||
if( $this->RconSocket )
|
||||
{
|
||||
fclose( $this->RconSocket );
|
||||
|
||||
$this->RconSocket = null;
|
||||
}
|
||||
|
||||
$this->RconRequestId = 0;
|
||||
}
|
||||
|
||||
public function Open( ) : void
|
||||
{
|
||||
if( !$this->RconSocket )
|
||||
{
|
||||
$RconSocket = @fsockopen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
|
||||
|
||||
if( $ErrNo || !$RconSocket )
|
||||
{
|
||||
throw new SocketException( 'Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED );
|
||||
}
|
||||
|
||||
$this->RconSocket = $RconSocket;
|
||||
stream_set_timeout( $this->RconSocket, $this->Socket->Timeout );
|
||||
stream_set_blocking( $this->RconSocket, true );
|
||||
}
|
||||
}
|
||||
|
||||
public function Write( int $Header, string $String = '' ) : bool
|
||||
{
|
||||
// Pack the packet together
|
||||
$Command = pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
|
||||
|
||||
// Prepend packet length
|
||||
$Command = pack( 'V', strlen( $Command ) ) . $Command;
|
||||
$Length = strlen( $Command );
|
||||
|
||||
return $Length === fwrite( $this->RconSocket, $Command, $Length );
|
||||
}
|
||||
|
||||
public function Read( ) : Buffer
|
||||
{
|
||||
$Buffer = new Buffer( );
|
||||
$Buffer->Set( fread( $this->RconSocket, 4 ) );
|
||||
|
||||
if( $Buffer->Remaining( ) < 4 )
|
||||
{
|
||||
throw new InvalidPacketException( 'Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY );
|
||||
}
|
||||
|
||||
$PacketSize = $Buffer->GetLong( );
|
||||
|
||||
$Buffer->Set( fread( $this->RconSocket, $PacketSize ) );
|
||||
|
||||
$Data = $Buffer->Get( );
|
||||
|
||||
$Remaining = $PacketSize - strlen( $Data );
|
||||
|
||||
while( $Remaining > 0 )
|
||||
{
|
||||
$Data2 = fread( $this->RconSocket, $Remaining );
|
||||
|
||||
$PacketSize = strlen( $Data2 );
|
||||
|
||||
if( $PacketSize === 0 )
|
||||
{
|
||||
throw new InvalidPacketException( 'Read ' . strlen( $Data ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
|
||||
}
|
||||
|
||||
$Data .= $Data2;
|
||||
$Remaining -= $PacketSize;
|
||||
}
|
||||
|
||||
$Buffer->Set( $Data );
|
||||
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Command( string $Command ) : string
|
||||
{
|
||||
$this->Write( SourceQuery::SERVERDATA_EXECCOMMAND, $Command );
|
||||
$Buffer = $this->Read( );
|
||||
|
||||
$Buffer->GetLong( ); // RequestID
|
||||
|
||||
$Type = $Buffer->GetLong( );
|
||||
|
||||
if( $Type === SourceQuery::SERVERDATA_AUTH_RESPONSE )
|
||||
{
|
||||
throw new AuthenticationException( 'Bad rcon_password.', AuthenticationException::BAD_PASSWORD );
|
||||
}
|
||||
else if( $Type !== SourceQuery::SERVERDATA_RESPONSE_VALUE )
|
||||
{
|
||||
throw new InvalidPacketException( 'Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
}
|
||||
|
||||
$Data = $Buffer->Get( );
|
||||
|
||||
// We do this stupid hack to handle split packets
|
||||
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
|
||||
if( strlen( $Data ) >= 4000 )
|
||||
{
|
||||
$this->Write( SourceQuery::SERVERDATA_REQUESTVALUE );
|
||||
|
||||
do
|
||||
{
|
||||
$Buffer = $this->Read( );
|
||||
|
||||
$Buffer->GetLong( ); // RequestID
|
||||
|
||||
if( $Buffer->GetLong( ) !== SourceQuery::SERVERDATA_RESPONSE_VALUE )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
$Data2 = $Buffer->Get( );
|
||||
|
||||
if( $Data2 === "\x00\x01\x00\x00\x00\x00" )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
$Data .= $Data2;
|
||||
}
|
||||
while( true );
|
||||
}
|
||||
|
||||
return rtrim( $Data, "\0" );
|
||||
}
|
||||
|
||||
public function Authorize( string $Password ) : void
|
||||
{
|
||||
$this->Write( SourceQuery::SERVERDATA_AUTH, $Password );
|
||||
$Buffer = $this->Read( );
|
||||
|
||||
$RequestID = $Buffer->GetLong( );
|
||||
$Type = $Buffer->GetLong( );
|
||||
|
||||
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
|
||||
// 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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
|
||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
use xPaw\SourceQuery\Exception\SocketException;
|
||||
|
||||
/**
|
||||
* Class SourceRcon
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses AuthenticationException
|
||||
* @uses InvalidPacketException
|
||||
* @uses SocketException
|
||||
*/
|
||||
final class SourceRcon
|
||||
{
|
||||
/**
|
||||
* Points to socket class
|
||||
*/
|
||||
private BaseSocket $Socket;
|
||||
|
||||
/** @var ?resource */
|
||||
private $RconSocket;
|
||||
private int $RconRequestId = 0;
|
||||
|
||||
public function __construct(BaseSocket $Socket)
|
||||
{
|
||||
$this->Socket = $Socket;
|
||||
}
|
||||
|
||||
public function Close(): void
|
||||
{
|
||||
if ($this->RconSocket) {
|
||||
fclose($this->RconSocket);
|
||||
|
||||
$this->RconSocket = null;
|
||||
}
|
||||
|
||||
$this->RconRequestId = 0;
|
||||
}
|
||||
|
||||
public function Open(): void
|
||||
{
|
||||
if (!$this->RconSocket) {
|
||||
$RconSocket = @fsockopen($this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout);
|
||||
|
||||
if ($ErrNo || !$RconSocket) {
|
||||
throw new SocketException('Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED);
|
||||
}
|
||||
|
||||
$this->RconSocket = $RconSocket;
|
||||
stream_set_timeout($this->RconSocket, $this->Socket->Timeout);
|
||||
stream_set_blocking($this->RconSocket, true);
|
||||
}
|
||||
}
|
||||
|
||||
public function Write(int $Header, string $String = ''): bool
|
||||
{
|
||||
// Pack the packet together
|
||||
$Command = pack('VV', ++$this->RconRequestId, $Header) . $String . "\x00\x00";
|
||||
|
||||
// Prepend packet length
|
||||
$Command = pack('V', strlen($Command)) . $Command;
|
||||
$Length = strlen($Command);
|
||||
|
||||
return $Length === fwrite($this->RconSocket, $Command, $Length);
|
||||
}
|
||||
|
||||
public function Read(): Buffer
|
||||
{
|
||||
$Buffer = new Buffer();
|
||||
$Buffer->Set(fread($this->RconSocket, 4));
|
||||
|
||||
if ($Buffer->Remaining() < 4) {
|
||||
throw new InvalidPacketException('Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY);
|
||||
}
|
||||
|
||||
$PacketSize = $Buffer->GetLong();
|
||||
|
||||
$Buffer->Set(fread($this->RconSocket, $PacketSize));
|
||||
|
||||
$Data = $Buffer->Get();
|
||||
|
||||
$Remaining = $PacketSize - strlen($Data);
|
||||
|
||||
while ($Remaining > 0) {
|
||||
$Data2 = fread($this->RconSocket, $Remaining);
|
||||
|
||||
$PacketSize = strlen($Data2);
|
||||
|
||||
if ($PacketSize === 0) {
|
||||
throw new InvalidPacketException('Read ' . strlen($Data) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY);
|
||||
}
|
||||
|
||||
$Data .= $Data2;
|
||||
$Remaining -= $PacketSize;
|
||||
}
|
||||
|
||||
$Buffer->Set($Data);
|
||||
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Command(string $Command): string
|
||||
{
|
||||
$this->Write(SourceQuery::SERVERDATA_EXECCOMMAND, $Command);
|
||||
$Buffer = $this->Read();
|
||||
|
||||
$Buffer->GetLong(); // RequestID
|
||||
|
||||
$Type = $Buffer->GetLong();
|
||||
|
||||
if ($Type === SourceQuery::SERVERDATA_AUTH_RESPONSE) {
|
||||
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);
|
||||
}
|
||||
|
||||
$Data = $Buffer->Get();
|
||||
|
||||
// We do this stupid hack to handle split packets
|
||||
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
|
||||
if (strlen($Data) >= 4000) {
|
||||
$this->Write(SourceQuery::SERVERDATA_REQUESTVALUE);
|
||||
|
||||
do {
|
||||
$Buffer = $this->Read();
|
||||
|
||||
$Buffer->GetLong(); // RequestID
|
||||
|
||||
if ($Buffer->GetLong() !== SourceQuery::SERVERDATA_RESPONSE_VALUE) {
|
||||
break;
|
||||
}
|
||||
|
||||
$Data2 = $Buffer->Get();
|
||||
|
||||
if ($Data2 === "\x00\x01\x00\x00\x00\x00") {
|
||||
break;
|
||||
}
|
||||
|
||||
$Data .= $Data2;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
return rtrim($Data, "\0");
|
||||
}
|
||||
|
||||
public function Authorize(string $Password): void
|
||||
{
|
||||
$this->Write(SourceQuery::SERVERDATA_AUTH, $Password);
|
||||
$Buffer = $this->Read();
|
||||
|
||||
$RequestID = $Buffer->GetLong();
|
||||
$Type = $Buffer->GetLong();
|
||||
|
||||
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+27
-24
@@ -1,27 +1,30 @@
|
||||
<?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';
|
||||
require_once __DIR__ . '/Exception/AuthenticationException.php';
|
||||
require_once __DIR__ . '/Exception/InvalidArgumentException.php';
|
||||
require_once __DIR__ . '/Exception/SocketException.php';
|
||||
require_once __DIR__ . '/Exception/InvalidPacketException.php';
|
||||
declare(strict_types=1);
|
||||
|
||||
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';
|
||||
/**
|
||||
* 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';
|
||||
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';
|
||||
|
||||
Reference in New Issue
Block a user