mirror of
https://github.com/xPaw/PHP-Source-Query.git
synced 2026-07-14 07:54:49 +02:00
Compare commits
24 Commits
e00235c29a
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 48307b5556 | |||
| 76edb8b2d7 | |||
| 8511444465 | |||
| 3bfc073211 | |||
| 890d98da92 | |||
| 7a7e6b0d67 | |||
| 904e547fe4 | |||
| cd3624704e | |||
| e96807bb24 | |||
| 7f2e4484d5 | |||
| 673e572233 | |||
| 7c8e5add77 | |||
| fbabd440da | |||
| bbb6c4c23e | |||
| 9da781a993 | |||
| b4601cdf86 | |||
| 7231921b0b | |||
| 4f88fb0c76 | |||
| 7414334804 | |||
| 8a2c16123c | |||
| 528d0ef4fc | |||
| 914ec4b79a | |||
| b87c9faab9 | |||
| 1078e47b7e |
@@ -7,10 +7,14 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
php: [7.2, 7.3]
|
||||
php: ['7.4', '8.0']
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Install dependencies
|
||||
run: composer install --no-interaction --no-progress
|
||||
- name: Run tests
|
||||
run: php${{ matrix.php }} vendor/bin/phpunit --configuration Tests/phpunit.xml --verbose --fail-on-warning
|
||||
- name: Run phpstan
|
||||
run: php${{ matrix.php }} vendor/bin/phpstan
|
||||
- name: Run psalm
|
||||
run: php${{ matrix.php }} vendor/bin/psalm
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
use xPaw\SourceQuery\SourceQuery;
|
||||
|
||||
// For the sake of this example
|
||||
Header( 'Content-Type: text/plain' );
|
||||
Header( 'X-Content-Type-Options: nosniff' );
|
||||
header( 'Content-Type: text/plain' );
|
||||
header( 'X-Content-Type-Options: nosniff' );
|
||||
|
||||
// Edit this ->
|
||||
define( 'SQ_SERVER_ADDR', 'localhost' );
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
use xPaw\SourceQuery\SourceQuery;
|
||||
|
||||
// For the sake of this example
|
||||
Header( 'Content-Type: text/plain' );
|
||||
Header( 'X-Content-Type-Options: nosniff' );
|
||||
header( 'Content-Type: text/plain' );
|
||||
header( 'X-Content-Type-Options: nosniff' );
|
||||
|
||||
// Edit this ->
|
||||
define( 'SQ_SERVER_ADDR', 'localhost' );
|
||||
|
||||
+13
-15
@@ -10,13 +10,14 @@
|
||||
define( 'SQ_ENGINE', SourceQuery::SOURCE );
|
||||
// Edit this <-
|
||||
|
||||
$Timer = MicroTime( true );
|
||||
$Timer = microtime( true );
|
||||
|
||||
$Query = new SourceQuery( );
|
||||
|
||||
$Info = Array( );
|
||||
$Rules = Array( );
|
||||
$Players = Array( );
|
||||
$Info = [];
|
||||
$Rules = [];
|
||||
$Players = [];
|
||||
$Exception = null;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -36,7 +37,7 @@
|
||||
$Query->Disconnect( );
|
||||
}
|
||||
|
||||
$Timer = Number_Format( MicroTime( true ) - $Timer, 4, '.', '' );
|
||||
$Timer = number_format( microtime( true ) - $Timer, 4, '.', '' );
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
@@ -87,13 +88,11 @@
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<?php if( isset( $Exception ) ): ?>
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-heading"><?php echo Get_Class( $Exception ); ?> at line <?php echo $Exception->getLine( ); ?></div>
|
||||
<p><b><?php echo htmlspecialchars( $Exception->getMessage( ) ); ?></b></p>
|
||||
<p><?php echo nl2br( $e->getTraceAsString(), false ); ?></p>
|
||||
<?php if( $Exception !== null ): ?>
|
||||
<div class="panel panel-error">
|
||||
<pre class="panel-body"><?php echo htmlspecialchars( $Exception->__toString( ) ); ?></pre>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<?php endif; ?>
|
||||
<div class="row">
|
||||
<div class="col-sm-6">
|
||||
<table class="table table-bordered table-striped">
|
||||
@@ -104,12 +103,12 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if( Is_Array( $Info ) ): ?>
|
||||
<?php if( !empty( $Info ) ): ?>
|
||||
<?php foreach( $Info as $InfoKey => $InfoValue ): ?>
|
||||
<tr>
|
||||
<td><?php echo htmlspecialchars( $InfoKey ); ?></td>
|
||||
<td><?php
|
||||
if( Is_Array( $InfoValue ) )
|
||||
if( is_array( $InfoValue ) )
|
||||
{
|
||||
echo "<pre>";
|
||||
print_r( $InfoValue );
|
||||
@@ -177,7 +176,7 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if( Is_Array( $Rules ) ): ?>
|
||||
<?php if( !empty( $Rules ) ): ?>
|
||||
<?php foreach( $Rules as $Rule => $Value ): ?>
|
||||
<tr>
|
||||
<td><?php echo htmlspecialchars( $Rule ); ?></td>
|
||||
@@ -193,7 +192,6 @@
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
# PHP Source Query
|
||||
|
||||
[](https://travis-ci.com/xPaw/PHP-Source-Query)
|
||||
[](https://coveralls.io/github/xPaw/PHP-Source-Query)
|
||||
[](https://packagist.org/packages/xpaw/php-source-query-class)
|
||||
[](https://packagist.org/packages/xpaw/php-source-query-class)
|
||||
|
||||
@@ -11,12 +9,12 @@ The class also allows you to query servers using RCON although this only works f
|
||||
|
||||
[Minecraft](http://www.minecraft.net) also uses Source RCON protocol, and this means you can use this class to send commands to your minecraft server while having engine set to Source engine.
|
||||
|
||||
**:warning: Please do not create issues when you are unable to retrieve information from a server, unless you can prove that there is a bug within the library.**
|
||||
**:warning: Do not send me emails if this does not work for you, I will not help you.**
|
||||
|
||||
## Requirements
|
||||
* [Modern PHP version](https://php.net/supported-versions.php) (5.5 or newer)
|
||||
* [Modern PHP version](https://php.net/supported-versions.php) (7.4 or newer)
|
||||
* 64-bit PHP or [gmp module](https://secure.php.net/manual/en/book.gmp.php)
|
||||
* Web server must allow UDP connections
|
||||
* Your server must allow UDP connections
|
||||
|
||||
## Protocol Specifications
|
||||
* https://developer.valvesoftware.com/wiki/Server_queries
|
||||
@@ -39,6 +37,7 @@ AppID | Game | Query | RCON | Notes
|
||||
115300 | [Call of Duty: Modern Warfare 3](http://store.steampowered.com/app/115300/) | :white_check_mark: | :white_check_mark: |
|
||||
211820 | [Starbound](http://store.steampowered.com/app/211820/) | :white_check_mark: | :white_check_mark: | Call `SetUseOldGetChallengeMethod` method after connecting
|
||||
244850 | [Space Engineers](http://store.steampowered.com/app/244850/) | :white_check_mark: | :x: | Add +1 to the server port
|
||||
304930 | [Unturned](https://store.steampowered.com/app/304930/) | :white_check_mark: | :x: | Add +1 to the server port
|
||||
251570 | [7 Days to Die](http://store.steampowered.com/app/251570) | :white_check_mark: | :x: |
|
||||
252490 | [Rust](http://store.steampowered.com/app/252490/) | :white_check_mark: | :x: |
|
||||
282440 | [Quake Live](http://store.steampowered.com/app/282440) | :white_check_mark: | :x: | Quake Live uses the ZMQ messaging queue protocol for rcon control.
|
||||
|
||||
+22
-14
@@ -13,6 +13,7 @@
|
||||
namespace xPaw\SourceQuery;
|
||||
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
use xPaw\SourceQuery\Exception\SocketException;
|
||||
|
||||
/**
|
||||
* Base socket interface
|
||||
@@ -20,27 +21,29 @@
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
abstract class BaseSocket
|
||||
{
|
||||
/** @var ?resource */
|
||||
public $Socket;
|
||||
public $Engine;
|
||||
public int $Engine;
|
||||
|
||||
public $Address;
|
||||
public $Port;
|
||||
public $Timeout;
|
||||
public string $Address;
|
||||
public int $Port;
|
||||
public int $Timeout;
|
||||
|
||||
public function __destruct( )
|
||||
{
|
||||
$this->Close( );
|
||||
}
|
||||
|
||||
abstract public function Close( );
|
||||
abstract public function Open( $Address, $Port, $Timeout, $Engine );
|
||||
abstract public function Write( $Header, $String = '' );
|
||||
abstract public function Read( $Length = 1400 );
|
||||
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, $Length, $SherlockFunction )
|
||||
protected function ReadInternal( Buffer $Buffer, int $Length, callable $SherlockFunction ) : Buffer
|
||||
{
|
||||
if( $Buffer->Remaining( ) === 0 )
|
||||
{
|
||||
@@ -58,6 +61,7 @@
|
||||
$Packets = [];
|
||||
$IsCompressed = false;
|
||||
$ReadMore = false;
|
||||
$PacketChecksum = null;
|
||||
|
||||
do
|
||||
{
|
||||
@@ -92,6 +96,10 @@
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE );
|
||||
}
|
||||
}
|
||||
|
||||
$Packets[ $PacketNumber ] = $Buffer->Get( );
|
||||
@@ -100,30 +108,30 @@
|
||||
}
|
||||
while( $ReadMore && $SherlockFunction( $Buffer, $Length ) );
|
||||
|
||||
$Data = Implode( $Packets );
|
||||
$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' ) )
|
||||
if( !function_exists( 'bzdecompress' ) )
|
||||
{
|
||||
throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' );
|
||||
}
|
||||
|
||||
$Data = bzdecompress( $Data );
|
||||
|
||||
if( CRC32( $Data ) !== $PacketChecksum )
|
||||
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 ) );
|
||||
$Buffer->Set( substr( $Data, 4 ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . dechex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
}
|
||||
|
||||
return $Buffer;
|
||||
|
||||
+24
-46
@@ -25,34 +25,26 @@
|
||||
{
|
||||
/**
|
||||
* Buffer
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $Buffer;
|
||||
private string $Buffer = '';
|
||||
|
||||
/**
|
||||
* Buffer length
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $Length;
|
||||
private int $Length = 0;
|
||||
|
||||
/**
|
||||
* Current position in buffer
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $Position;
|
||||
private int $Position = 0;
|
||||
|
||||
/**
|
||||
* Sets buffer
|
||||
*
|
||||
* @param string $Buffer Buffer
|
||||
*/
|
||||
public function Set( $Buffer )
|
||||
public function Set( string $Buffer ) : void
|
||||
{
|
||||
$this->Buffer = $Buffer;
|
||||
$this->Length = StrLen( $Buffer );
|
||||
$this->Length = strlen( $Buffer );
|
||||
$this->Position = 0;
|
||||
}
|
||||
|
||||
@@ -61,7 +53,7 @@
|
||||
*
|
||||
* @return int Remaining bytes in buffer
|
||||
*/
|
||||
public function Remaining( )
|
||||
public function Remaining( ) : int
|
||||
{
|
||||
return $this->Length - $this->Position;
|
||||
}
|
||||
@@ -70,10 +62,8 @@
|
||||
* Gets data from buffer
|
||||
*
|
||||
* @param int $Length Bytes to read
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function Get( $Length = -1 )
|
||||
public function Get( int $Length = -1 ) : string
|
||||
{
|
||||
if( $Length === 0 )
|
||||
{
|
||||
@@ -91,7 +81,7 @@
|
||||
return '';
|
||||
}
|
||||
|
||||
$Data = SubStr( $this->Buffer, $this->Position, $Length );
|
||||
$Data = substr( $this->Buffer, $this->Position, $Length );
|
||||
|
||||
$this->Position += $Length;
|
||||
|
||||
@@ -100,90 +90,78 @@
|
||||
|
||||
/**
|
||||
* Get byte from buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function GetByte( )
|
||||
public function GetByte( ) : int
|
||||
{
|
||||
return Ord( $this->Get( 1 ) );
|
||||
return ord( $this->Get( 1 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get short from buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function GetShort( )
|
||||
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 ) );
|
||||
$Data = unpack( 'v', $this->Get( 2 ) );
|
||||
|
||||
return $Data[ 1 ];
|
||||
return (int)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get long from buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function GetLong( )
|
||||
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 ) );
|
||||
$Data = unpack( 'l', $this->Get( 4 ) );
|
||||
|
||||
return $Data[ 1 ];
|
||||
return (int)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get float from buffer
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function GetFloat( )
|
||||
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 ) );
|
||||
$Data = unpack( 'f', $this->Get( 4 ) );
|
||||
|
||||
return $Data[ 1 ];
|
||||
return (float)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unsigned long from buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function GetUnsignedLong( )
|
||||
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 ) );
|
||||
$Data = unpack( 'V', $this->Get( 4 ) );
|
||||
|
||||
return $Data[ 1 ];
|
||||
return (int)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Read one string from buffer ending with null byte
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function GetString( )
|
||||
public function GetString( ) : string
|
||||
{
|
||||
$ZeroBytePosition = StrPos( $this->Buffer, "\0", $this->Position );
|
||||
$ZeroBytePosition = strpos( $this->Buffer, "\0", $this->Position );
|
||||
|
||||
if( $ZeroBytePosition === false )
|
||||
{
|
||||
|
||||
@@ -17,4 +17,5 @@
|
||||
const COULD_NOT_CREATE_SOCKET = 1;
|
||||
const NOT_CONNECTED = 2;
|
||||
const CONNECTION_FAILED = 3;
|
||||
const INVALID_ENGINE = 3;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
namespace xPaw\SourceQuery;
|
||||
|
||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
|
||||
/**
|
||||
* Class GoldSourceRcon
|
||||
@@ -20,51 +21,50 @@
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
*/
|
||||
class GoldSourceRcon
|
||||
{
|
||||
/**
|
||||
* Points to socket class
|
||||
*
|
||||
* @var Socket
|
||||
* @var BaseSocket
|
||||
*/
|
||||
private $Socket;
|
||||
|
||||
private $RconPassword;
|
||||
private $RconRequestId;
|
||||
private $RconChallenge;
|
||||
private string $RconPassword = '';
|
||||
private string $RconChallenge = '';
|
||||
|
||||
public function __construct( $Socket )
|
||||
public function __construct( BaseSocket $Socket )
|
||||
{
|
||||
$this->Socket = $Socket;
|
||||
}
|
||||
|
||||
public function Close( )
|
||||
public function Close( ) : void
|
||||
{
|
||||
$this->RconChallenge = 0;
|
||||
$this->RconRequestId = 0;
|
||||
$this->RconPassword = 0;
|
||||
$this->RconChallenge = '';
|
||||
$this->RconPassword = '';
|
||||
}
|
||||
|
||||
public function Open( )
|
||||
public function Open( ) : void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function Write( $Header, $String = '' )
|
||||
public function Write( int $Header, string $String = '' ) : bool
|
||||
{
|
||||
$Command = Pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
|
||||
$Length = StrLen( $Command );
|
||||
$Command = pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
|
||||
$Length = strlen( $Command );
|
||||
|
||||
return $Length === FWrite( $this->Socket->Socket, $Command, $Length );
|
||||
return $Length === fwrite( $this->Socket->Socket, $Command, $Length );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $Length
|
||||
* @throws AuthenticationException
|
||||
* @return bool
|
||||
* @return Buffer
|
||||
*/
|
||||
public function Read( $Length = 1400 )
|
||||
public function Read( int $Length = 1400 ) : Buffer
|
||||
{
|
||||
// GoldSource RCON has same structure as Query
|
||||
$Buffer = $this->Socket->Read( );
|
||||
@@ -89,7 +89,7 @@
|
||||
//$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?
|
||||
$ReadMore = strlen( $Packet ) > 1000; // use 1300?
|
||||
|
||||
if( $ReadMore )
|
||||
{
|
||||
@@ -115,7 +115,7 @@
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Command( $Command )
|
||||
public function Command( string $Command ) : string
|
||||
{
|
||||
if( !$this->RconChallenge )
|
||||
{
|
||||
@@ -128,7 +128,7 @@
|
||||
return $Buffer->Get( );
|
||||
}
|
||||
|
||||
public function Authorize( $Password )
|
||||
public function Authorize( string $Password ) : void
|
||||
{
|
||||
$this->RconPassword = $Password;
|
||||
|
||||
@@ -140,6 +140,6 @@
|
||||
throw new AuthenticationException( 'Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD );
|
||||
}
|
||||
|
||||
$this->RconChallenge = Trim( $Buffer->Get( ) );
|
||||
$this->RconChallenge = trim( $Buffer->Get( ) );
|
||||
}
|
||||
}
|
||||
|
||||
+18
-17
@@ -25,40 +25,41 @@
|
||||
*/
|
||||
class Socket extends BaseSocket
|
||||
{
|
||||
public function Close( )
|
||||
public function Close( ) : void
|
||||
{
|
||||
if( $this->Socket )
|
||||
if( $this->Socket !== null )
|
||||
{
|
||||
FClose( $this->Socket );
|
||||
fclose( $this->Socket );
|
||||
|
||||
$this->Socket = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function Open( $Address, $Port, $Timeout, $Engine )
|
||||
public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void
|
||||
{
|
||||
$this->Timeout = $Timeout;
|
||||
$this->Engine = $Engine;
|
||||
$this->Port = $Port;
|
||||
$this->Address = $Address;
|
||||
|
||||
$this->Socket = @FSockOpen( 'udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout );
|
||||
$Socket = @fsockopen( 'udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout );
|
||||
|
||||
if( $ErrNo || $this->Socket === false )
|
||||
if( $ErrNo || $Socket === false )
|
||||
{
|
||||
throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET );
|
||||
}
|
||||
|
||||
Stream_Set_Timeout( $this->Socket, $Timeout );
|
||||
Stream_Set_Blocking( $this->Socket, true );
|
||||
$this->Socket = $Socket;
|
||||
stream_set_timeout( $this->Socket, $Timeout );
|
||||
stream_set_blocking( $this->Socket, true );
|
||||
}
|
||||
|
||||
public function Write( $Header, $String = '' )
|
||||
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 );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,21 +69,21 @@
|
||||
*
|
||||
* @return Buffer Buffer
|
||||
*/
|
||||
public function Read( $Length = 1400 )
|
||||
public function Read( int $Length = 1400 ) : Buffer
|
||||
{
|
||||
$Buffer = new Buffer( );
|
||||
$Buffer->Set( FRead( $this->Socket, $Length ) );
|
||||
$Buffer->Set( fread( $this->Socket, $Length ) );
|
||||
|
||||
$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
|
||||
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Sherlock( $Buffer, $Length )
|
||||
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;
|
||||
}
|
||||
|
||||
+67
-56
@@ -12,6 +12,7 @@
|
||||
|
||||
namespace xPaw\SourceQuery;
|
||||
|
||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||
use xPaw\SourceQuery\Exception\InvalidArgumentException;
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
use xPaw\SourceQuery\Exception\SocketException;
|
||||
@@ -21,6 +22,7 @@
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
||||
* @uses xPaw\SourceQuery\Exception\InvalidArgumentException
|
||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
||||
@@ -36,7 +38,7 @@
|
||||
/**
|
||||
* Packets sent
|
||||
*/
|
||||
const A2S_PING = 0x69;
|
||||
const A2A_PING = 0x69;
|
||||
const A2S_INFO = 0x54;
|
||||
const A2S_PLAYER = 0x55;
|
||||
const A2S_RULES = 0x56;
|
||||
@@ -45,10 +47,10 @@
|
||||
/**
|
||||
* Packets received
|
||||
*/
|
||||
const S2A_PING = 0x6A;
|
||||
const S2A_CHALLENGE = 0x41;
|
||||
const S2A_INFO = 0x49;
|
||||
const S2A_INFO_OLD = 0x6D; // Old GoldSource, HLTV uses it
|
||||
const A2A_ACK = 0x6A;
|
||||
const S2C_CHALLENGE = 0x41;
|
||||
const S2A_INFO_SRC = 0x49;
|
||||
const S2A_INFO_OLD = 0x6D; // Old GoldSource, HLTV uses it (actually called S2A_INFO_DETAILED)
|
||||
const S2A_PLAYER = 0x44;
|
||||
const S2A_RULES = 0x45;
|
||||
const S2A_RCON = 0x6C;
|
||||
@@ -56,6 +58,7 @@
|
||||
/**
|
||||
* Source rcon sent
|
||||
*/
|
||||
const SERVERDATA_REQUESTVALUE = 0;
|
||||
const SERVERDATA_EXECCOMMAND = 2;
|
||||
const SERVERDATA_AUTH = 3;
|
||||
|
||||
@@ -68,37 +71,29 @@
|
||||
/**
|
||||
* Points to rcon class
|
||||
*
|
||||
* @var SourceRcon
|
||||
* @var SourceRcon|GoldSourceRcon|null
|
||||
*/
|
||||
private $Rcon;
|
||||
|
||||
/**
|
||||
* Points to socket class
|
||||
*
|
||||
* @var Socket
|
||||
*/
|
||||
private $Socket;
|
||||
private BaseSocket $Socket;
|
||||
|
||||
/**
|
||||
* True if connection is open, false if not
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $Connected;
|
||||
private bool $Connected = false;
|
||||
|
||||
/**
|
||||
* Contains challenge
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $Challenge;
|
||||
private string $Challenge = '';
|
||||
|
||||
/**
|
||||
* Use old method for getting challenge number
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $UseOldGetChallengeMethod;
|
||||
private bool $UseOldGetChallengeMethod = false;
|
||||
|
||||
public function __construct( BaseSocket $Socket = null )
|
||||
{
|
||||
@@ -121,16 +116,16 @@
|
||||
* @throws InvalidArgumentException
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function Connect( $Address, $Port, $Timeout = 3, $Engine = self::SOURCE )
|
||||
public function Connect( string $Address, int $Port, int $Timeout = 3, int $Engine = self::SOURCE ) : void
|
||||
{
|
||||
$this->Disconnect( );
|
||||
|
||||
if( !is_int( $Timeout ) || $Timeout < 0 )
|
||||
if( $Timeout < 0 )
|
||||
{
|
||||
throw new InvalidArgumentException( 'Timeout must be an integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER );
|
||||
throw new InvalidArgumentException( 'Timeout must be a positive integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER );
|
||||
}
|
||||
|
||||
$this->Socket->Open( $Address, (int)$Port, $Timeout, (int)$Engine );
|
||||
$this->Socket->Open( $Address, $Port, $Timeout, $Engine );
|
||||
|
||||
$this->Connected = true;
|
||||
}
|
||||
@@ -142,7 +137,7 @@
|
||||
*
|
||||
* @returns bool Previous value
|
||||
*/
|
||||
public function SetUseOldGetChallengeMethod( $Value )
|
||||
public function SetUseOldGetChallengeMethod( bool $Value ) : bool
|
||||
{
|
||||
$Previous = $this->UseOldGetChallengeMethod;
|
||||
|
||||
@@ -154,10 +149,10 @@
|
||||
/**
|
||||
* Closes all open connections
|
||||
*/
|
||||
public function Disconnect( )
|
||||
public function Disconnect( ) : void
|
||||
{
|
||||
$this->Connected = false;
|
||||
$this->Challenge = 0;
|
||||
$this->Challenge = '';
|
||||
|
||||
$this->Socket->Close( );
|
||||
|
||||
@@ -178,17 +173,17 @@
|
||||
*
|
||||
* @return bool True on success, false on failure
|
||||
*/
|
||||
public function Ping( )
|
||||
public function Ping( ) : bool
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED );
|
||||
}
|
||||
|
||||
$this->Socket->Write( self::A2S_PING );
|
||||
$this->Socket->Write( self::A2A_PING );
|
||||
$Buffer = $this->Socket->Read( );
|
||||
|
||||
return $Buffer->GetByte( ) === self::S2A_PING;
|
||||
return $Buffer->GetByte( ) === self::A2A_ACK;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,17 +194,34 @@
|
||||
*
|
||||
* @return array Returns an array with information on success
|
||||
*/
|
||||
public function GetInfo( )
|
||||
public function GetInfo( ) : array
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED );
|
||||
}
|
||||
|
||||
$this->Socket->Write( self::A2S_INFO, "Source Engine Query\0" );
|
||||
$Buffer = $this->Socket->Read( );
|
||||
if( $this->Challenge )
|
||||
{
|
||||
$this->Socket->Write( self::A2S_INFO, "Source Engine Query\0" . $this->Challenge );
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->Socket->Write( self::A2S_INFO, "Source Engine Query\0" );
|
||||
}
|
||||
|
||||
$Buffer = $this->Socket->Read( );
|
||||
$Type = $Buffer->GetByte( );
|
||||
$Server = [];
|
||||
|
||||
if( $Type === self::S2C_CHALLENGE )
|
||||
{
|
||||
$this->Challenge = $Buffer->Get( 4 );
|
||||
|
||||
$this->Socket->Write( self::A2S_INFO, "Source Engine Query\0" . $this->Challenge );
|
||||
$Buffer = $this->Socket->Read( );
|
||||
$Type = $Buffer->GetByte( );
|
||||
}
|
||||
|
||||
// Old GoldSource protocol, HLTV still uses it
|
||||
if( $Type === self::S2A_INFO_OLD && $this->Socket->Engine === self::GOLDSOURCE )
|
||||
@@ -228,13 +240,14 @@
|
||||
$Server[ 'Players' ] = $Buffer->GetByte( );
|
||||
$Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
|
||||
$Server[ 'Protocol' ] = $Buffer->GetByte( );
|
||||
$Server[ 'Dedicated' ] = Chr( $Buffer->GetByte( ) );
|
||||
$Server[ 'Os' ] = Chr( $Buffer->GetByte( ) );
|
||||
$Server[ 'Dedicated' ] = chr( $Buffer->GetByte( ) );
|
||||
$Server[ 'Os' ] = chr( $Buffer->GetByte( ) );
|
||||
$Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
|
||||
$Server[ 'IsMod' ] = $Buffer->GetByte( ) === 1;
|
||||
|
||||
if( $Server[ 'IsMod' ] )
|
||||
{
|
||||
$Mod = [];
|
||||
$Mod[ 'Url' ] = $Buffer->GetString( );
|
||||
$Mod[ 'Download' ] = $Buffer->GetString( );
|
||||
$Buffer->Get( 1 ); // NULL byte
|
||||
@@ -242,22 +255,18 @@
|
||||
$Mod[ 'Size' ] = $Buffer->GetLong( );
|
||||
$Mod[ 'ServerSide' ] = $Buffer->GetByte( ) === 1;
|
||||
$Mod[ 'CustomDLL' ] = $Buffer->GetByte( ) === 1;
|
||||
$Server[ 'Mod' ] = $Mod;
|
||||
}
|
||||
|
||||
$Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
|
||||
$Server[ 'Bots' ] = $Buffer->GetByte( );
|
||||
|
||||
if( isset( $Mod ) )
|
||||
{
|
||||
$Server[ 'Mod' ] = $Mod;
|
||||
}
|
||||
|
||||
return $Server;
|
||||
}
|
||||
|
||||
if( $Type !== self::S2A_INFO )
|
||||
if( $Type !== self::S2A_INFO_SRC )
|
||||
{
|
||||
throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
}
|
||||
|
||||
$Server[ 'Protocol' ] = $Buffer->GetByte( );
|
||||
@@ -269,8 +278,8 @@
|
||||
$Server[ 'Players' ] = $Buffer->GetByte( );
|
||||
$Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
|
||||
$Server[ 'Bots' ] = $Buffer->GetByte( );
|
||||
$Server[ 'Dedicated' ] = Chr( $Buffer->GetByte( ) );
|
||||
$Server[ 'Os' ] = Chr( $Buffer->GetByte( ) );
|
||||
$Server[ 'Dedicated' ] = chr( $Buffer->GetByte( ) );
|
||||
$Server[ 'Os' ] = chr( $Buffer->GetByte( ) );
|
||||
$Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
|
||||
$Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
|
||||
|
||||
@@ -364,7 +373,7 @@
|
||||
*
|
||||
* @return array Returns an array with players on success
|
||||
*/
|
||||
public function GetPlayers( )
|
||||
public function GetPlayers( ) : array
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
@@ -381,7 +390,7 @@
|
||||
|
||||
if( $Type !== self::S2A_PLAYER )
|
||||
{
|
||||
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
}
|
||||
|
||||
$Players = [];
|
||||
@@ -389,11 +398,12 @@
|
||||
|
||||
while( $Count-- > 0 && $Buffer->Remaining( ) > 0 )
|
||||
{
|
||||
$Player = [];
|
||||
$Player[ 'Id' ] = $Buffer->GetByte( ); // PlayerID, is it just always 0?
|
||||
$Player[ 'Name' ] = $Buffer->GetString( );
|
||||
$Player[ 'Frags' ] = $Buffer->GetLong( );
|
||||
$Player[ 'Time' ] = (int)$Buffer->GetFloat( );
|
||||
$Player[ 'TimeF' ] = GMDate( ( $Player[ 'Time' ] > 3600 ? "H:i:s" : "i:s" ), $Player[ 'Time' ] );
|
||||
$Player[ 'TimeF' ] = gmdate( ( $Player[ 'Time' ] > 3600 ? 'H:i:s' : 'i:s' ), $Player[ 'Time' ] );
|
||||
|
||||
$Players[ ] = $Player;
|
||||
}
|
||||
@@ -409,7 +419,7 @@
|
||||
*
|
||||
* @return array Returns an array with rules on success
|
||||
*/
|
||||
public function GetRules( )
|
||||
public function GetRules( ) : array
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
@@ -425,7 +435,7 @@
|
||||
|
||||
if( $Type !== self::S2A_RULES )
|
||||
{
|
||||
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
}
|
||||
|
||||
$Rules = [];
|
||||
@@ -436,7 +446,7 @@
|
||||
$Rule = $Buffer->GetString( );
|
||||
$Value = $Buffer->GetString( );
|
||||
|
||||
if( !Empty( $Rule ) )
|
||||
if( !empty( $Rule ) )
|
||||
{
|
||||
$Rules[ $Rule ] = $Value;
|
||||
}
|
||||
@@ -448,12 +458,9 @@
|
||||
/**
|
||||
* Get challenge (used for players/rules packets)
|
||||
*
|
||||
* @param $Header
|
||||
* @param $ExpectedResult
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
private function GetChallenge( $Header, $ExpectedResult )
|
||||
private function GetChallenge( int $Header, int $ExpectedResult ) : void
|
||||
{
|
||||
if( $this->Challenge )
|
||||
{
|
||||
@@ -472,7 +479,7 @@
|
||||
|
||||
switch( $Type )
|
||||
{
|
||||
case self::S2A_CHALLENGE:
|
||||
case self::S2C_CHALLENGE:
|
||||
{
|
||||
$this->Challenge = $Buffer->Get( 4 );
|
||||
|
||||
@@ -490,7 +497,7 @@
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -504,7 +511,7 @@
|
||||
* @throws InvalidPacketException
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function SetRconPassword( $Password )
|
||||
public function SetRconPassword( string $Password ) : void
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
@@ -525,6 +532,10 @@
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE );
|
||||
}
|
||||
}
|
||||
|
||||
$this->Rcon->Open( );
|
||||
@@ -542,7 +553,7 @@
|
||||
*
|
||||
* @return string Answer from server in string
|
||||
*/
|
||||
public function Rcon( $Command )
|
||||
public function Rcon( string $Command ) : string
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
|
||||
+27
-29
@@ -29,24 +29,23 @@
|
||||
{
|
||||
/**
|
||||
* Points to socket class
|
||||
*
|
||||
* @var Socket
|
||||
*/
|
||||
private $Socket;
|
||||
private BaseSocket $Socket;
|
||||
|
||||
/** @var ?resource */
|
||||
private $RconSocket;
|
||||
private $RconRequestId;
|
||||
private int $RconRequestId = 0;
|
||||
|
||||
public function __construct( $Socket )
|
||||
public function __construct( BaseSocket $Socket )
|
||||
{
|
||||
$this->Socket = $Socket;
|
||||
}
|
||||
|
||||
public function Close( )
|
||||
public function Close( ) : void
|
||||
{
|
||||
if( $this->RconSocket )
|
||||
{
|
||||
FClose( $this->RconSocket );
|
||||
fclose( $this->RconSocket );
|
||||
|
||||
$this->RconSocket = null;
|
||||
}
|
||||
@@ -54,38 +53,39 @@
|
||||
$this->RconRequestId = 0;
|
||||
}
|
||||
|
||||
public function Open( )
|
||||
public function Open( ) : void
|
||||
{
|
||||
if( !$this->RconSocket )
|
||||
{
|
||||
$this->RconSocket = @FSockOpen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
|
||||
$RconSocket = @fsockopen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
|
||||
|
||||
if( $ErrNo || !$this->RconSocket )
|
||||
if( $ErrNo || !$RconSocket )
|
||||
{
|
||||
throw new SocketException( 'Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED );
|
||||
}
|
||||
|
||||
Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout );
|
||||
Stream_Set_Blocking( $this->RconSocket, true );
|
||||
$this->RconSocket = $RconSocket;
|
||||
stream_set_timeout( $this->RconSocket, $this->Socket->Timeout );
|
||||
stream_set_blocking( $this->RconSocket, true );
|
||||
}
|
||||
}
|
||||
|
||||
public function Write( $Header, $String = '' )
|
||||
public function Write( int $Header, string $String = '' ) : bool
|
||||
{
|
||||
// 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
|
||||
$Command = Pack( 'V', StrLen( $Command ) ) . $Command;
|
||||
$Length = StrLen( $Command );
|
||||
$Command = pack( 'V', strlen( $Command ) ) . $Command;
|
||||
$Length = strlen( $Command );
|
||||
|
||||
return $Length === FWrite( $this->RconSocket, $Command, $Length );
|
||||
return $Length === fwrite( $this->RconSocket, $Command, $Length );
|
||||
}
|
||||
|
||||
public function Read( )
|
||||
public function Read( ) : Buffer
|
||||
{
|
||||
$Buffer = new Buffer( );
|
||||
$Buffer->Set( FRead( $this->RconSocket, 4 ) );
|
||||
$Buffer->Set( fread( $this->RconSocket, 4 ) );
|
||||
|
||||
if( $Buffer->Remaining( ) < 4 )
|
||||
{
|
||||
@@ -94,23 +94,21 @@
|
||||
|
||||
$PacketSize = $Buffer->GetLong( );
|
||||
|
||||
$Buffer->Set( FRead( $this->RconSocket, $PacketSize ) );
|
||||
$Buffer->Set( fread( $this->RconSocket, $PacketSize ) );
|
||||
|
||||
$Data = $Buffer->Get( );
|
||||
|
||||
$Remaining = $PacketSize - StrLen( $Data );
|
||||
$Remaining = $PacketSize - strlen( $Data );
|
||||
|
||||
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 );
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$Data .= $Data2;
|
||||
@@ -122,7 +120,7 @@
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Command( $Command )
|
||||
public function Command( string $Command ) : string
|
||||
{
|
||||
$this->Write( SourceQuery::SERVERDATA_EXECCOMMAND, $Command );
|
||||
$Buffer = $this->Read( );
|
||||
@@ -144,9 +142,9 @@
|
||||
|
||||
// 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 )
|
||||
if( strlen( $Data ) >= 4000 )
|
||||
{
|
||||
$this->Write( SourceQuery::SERVERDATA_RESPONSE_VALUE );
|
||||
$this->Write( SourceQuery::SERVERDATA_REQUESTVALUE );
|
||||
|
||||
do
|
||||
{
|
||||
@@ -174,7 +172,7 @@
|
||||
return rtrim( $Data, "\0" );
|
||||
}
|
||||
|
||||
public function Authorize( $Password )
|
||||
public function Authorize( string $Password ) : void
|
||||
{
|
||||
$this->Write( SourceQuery::SERVERDATA_AUTH, $Password );
|
||||
$Buffer = $this->Read( );
|
||||
|
||||
+52
-62
@@ -1,32 +1,33 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use xPaw\SourceQuery\BaseSocket;
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
use xPaw\SourceQuery\SourceQuery;
|
||||
use xPaw\SourceQuery\Buffer;
|
||||
|
||||
class TestableSocket extends BaseSocket
|
||||
{
|
||||
private $PacketQueue;
|
||||
/** @var \SplQueue<string> */
|
||||
private \SplQueue $PacketQueue;
|
||||
|
||||
public function __construct( )
|
||||
{
|
||||
/** @var \SplQueue<string> */
|
||||
$this->PacketQueue = new \SplQueue();
|
||||
$this->PacketQueue->setIteratorMode( \SplDoublyLinkedList::IT_MODE_DELETE );
|
||||
|
||||
}
|
||||
|
||||
public function Queue( $Data )
|
||||
public function Queue( string $Data ) : void
|
||||
{
|
||||
$this->PacketQueue->push( $Data );
|
||||
}
|
||||
|
||||
public function Close( )
|
||||
public function Close( ) : void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function Open( $Address, $Port, $Timeout, $Engine )
|
||||
public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void
|
||||
{
|
||||
$this->Timeout = $Timeout;
|
||||
$this->Engine = $Engine;
|
||||
@@ -34,12 +35,12 @@
|
||||
$this->Address = $Address;
|
||||
}
|
||||
|
||||
public function Write( $Header, $String = '' )
|
||||
public function Write( int $Header, string $String = '' ) : bool
|
||||
{
|
||||
//
|
||||
return true;
|
||||
}
|
||||
|
||||
public function Read( $Length = 1400 )
|
||||
public function Read( int $Length = 1400 ) : Buffer
|
||||
{
|
||||
$Buffer = new Buffer( );
|
||||
$Buffer->Set( $this->PacketQueue->shift() );
|
||||
@@ -49,7 +50,7 @@
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Sherlock( $Buffer, $Length )
|
||||
public function Sherlock( Buffer $Buffer, int $Length ) : bool
|
||||
{
|
||||
if( $this->PacketQueue->isEmpty() )
|
||||
{
|
||||
@@ -62,57 +63,49 @@
|
||||
}
|
||||
}
|
||||
|
||||
class SourceQueryTests extends TestCase
|
||||
class Tests extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
private $Socket;
|
||||
private $SourceQuery;
|
||||
private TestableSocket $Socket;
|
||||
private SourceQuery $SourceQuery;
|
||||
|
||||
public function setUp()
|
||||
public function setUp() : void
|
||||
{
|
||||
$this->Socket = new TestableSocket();
|
||||
$this->SourceQuery = new SourceQuery( $this->Socket );
|
||||
$this->SourceQuery->Connect( 1, 2 );
|
||||
$this->SourceQuery->Connect( '', 2 );
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
public function tearDown() : void
|
||||
{
|
||||
$this->SourceQuery->Disconnect();
|
||||
|
||||
unset( $this->Socket, $this->SourceQuery );
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidTimeout()
|
||||
public function testInvalidTimeout() : void
|
||||
{
|
||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidArgumentException::class );
|
||||
$SourceQuery = new SourceQuery( );
|
||||
$SourceQuery->Connect( 1, 2, -1 );
|
||||
$SourceQuery->Connect( '', 2, -1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
public function testNotConnectedGetInfo()
|
||||
public function testNotConnectedGetInfo() : void
|
||||
{
|
||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||
$this->SourceQuery->Disconnect();
|
||||
$this->SourceQuery->GetInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
public function testNotConnectedPing()
|
||||
public function testNotConnectedPing() : void
|
||||
{
|
||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||
$this->SourceQuery->Disconnect();
|
||||
$this->SourceQuery->Ping();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
public function testNotConnectedGetPlayers()
|
||||
public function testNotConnectedGetPlayers() : void
|
||||
{
|
||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||
$this->SourceQuery->Disconnect();
|
||||
$this->SourceQuery->GetPlayers();
|
||||
}
|
||||
@@ -120,42 +113,37 @@
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
public function testNotConnectedGetRules()
|
||||
public function testNotConnectedGetRules() : void
|
||||
{
|
||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||
$this->SourceQuery->Disconnect();
|
||||
$this->SourceQuery->GetRules();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
public function testNotConnectedSetRconPassword()
|
||||
public function testNotConnectedSetRconPassword() : void
|
||||
{
|
||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||
$this->SourceQuery->Disconnect();
|
||||
$this->SourceQuery->SetRconPassword('a');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
public function testNotConnectedRcon()
|
||||
public function testNotConnectedRcon() : void
|
||||
{
|
||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||
$this->SourceQuery->Disconnect();
|
||||
$this->SourceQuery->Rcon('a');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
||||
*/
|
||||
public function testRconWithoutPassword()
|
||||
public function testRconWithoutPassword() : void
|
||||
{
|
||||
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||
$this->SourceQuery->Rcon('a');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider InfoProvider
|
||||
*/
|
||||
public function testGetInfo( $RawInput, $ExpectedOutput )
|
||||
public function testGetInfo( string $RawInput, array $ExpectedOutput ) : void
|
||||
{
|
||||
if( isset( $ExpectedOutput[ 'IsMod' ] ) )
|
||||
{
|
||||
@@ -169,7 +157,7 @@
|
||||
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
||||
}
|
||||
|
||||
public function InfoProvider()
|
||||
public function InfoProvider() : array
|
||||
{
|
||||
$DataProvider = [];
|
||||
|
||||
@@ -188,33 +176,33 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
* @dataProvider BadPacketProvider
|
||||
*/
|
||||
public function testBadGetInfo( $Data )
|
||||
public function testBadGetInfo( string $Data ) : void
|
||||
{
|
||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
||||
$this->Socket->Queue( $Data );
|
||||
|
||||
$this->SourceQuery->GetInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
* @dataProvider BadPacketProvider
|
||||
*/
|
||||
public function testBadGetChallengeViaPlayers( $Data )
|
||||
public function testBadGetChallengeViaPlayers( string $Data ) : void
|
||||
{
|
||||
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
||||
$this->Socket->Queue( $Data );
|
||||
|
||||
$this->SourceQuery->GetPlayers();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
* @dataProvider BadPacketProvider
|
||||
*/
|
||||
public function testBadGetPlayersAfterCorrectChallenge( $Data )
|
||||
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->Socket->Queue( $Data );
|
||||
|
||||
@@ -222,18 +210,18 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException xPaw\SourceQuery\Exception\InvalidPacketException
|
||||
* @dataProvider BadPacketProvider
|
||||
*/
|
||||
public function testBadGetRulesAfterCorrectChallenge( $Data )
|
||||
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->Socket->Queue( $Data );
|
||||
|
||||
$this->SourceQuery->GetRules();
|
||||
}
|
||||
|
||||
public function BadPacketProvider( )
|
||||
public function BadPacketProvider( ) : array
|
||||
{
|
||||
return
|
||||
[
|
||||
@@ -247,7 +235,7 @@
|
||||
];
|
||||
}
|
||||
|
||||
public function testGetChallengeTwice( )
|
||||
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" );
|
||||
@@ -259,8 +247,9 @@
|
||||
|
||||
/**
|
||||
* @dataProvider RulesProvider
|
||||
* @param array<string> $RawInput
|
||||
*/
|
||||
public function testGetRules( $RawInput, $ExpectedOutput )
|
||||
public function testGetRules( array $RawInput, array $ExpectedOutput ) : void
|
||||
{
|
||||
$this->Socket->Queue( hex2bin( "ffffffff4104fce20e" ) ); // Challenge
|
||||
|
||||
@@ -274,7 +263,7 @@
|
||||
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
||||
}
|
||||
|
||||
public function RulesProvider()
|
||||
public function RulesProvider() : array
|
||||
{
|
||||
$DataProvider = [];
|
||||
|
||||
@@ -294,8 +283,9 @@
|
||||
|
||||
/**
|
||||
* @dataProvider PlayersProvider
|
||||
* @param array<string> $RawInput
|
||||
*/
|
||||
public function testGetPlayers( $RawInput, $ExpectedOutput )
|
||||
public function testGetPlayers( array $RawInput, array $ExpectedOutput ) : void
|
||||
{
|
||||
$this->Socket->Queue( hex2bin( "ffffffff4104fce20e" ) ); // Challenge
|
||||
|
||||
@@ -309,7 +299,7 @@
|
||||
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
||||
}
|
||||
|
||||
public function PlayersProvider()
|
||||
public function PlayersProvider() : array
|
||||
{
|
||||
$DataProvider = [];
|
||||
|
||||
@@ -327,7 +317,7 @@
|
||||
return $DataProvider;
|
||||
}
|
||||
|
||||
public function testPing()
|
||||
public function testPing() : void
|
||||
{
|
||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x6A\x00");
|
||||
$this->assertTrue( $this->SourceQuery->Ping() );
|
||||
|
||||
+4
-3
@@ -18,12 +18,13 @@
|
||||
],
|
||||
"require":
|
||||
{
|
||||
"php": ">=7.2"
|
||||
"php": ">=7.4"
|
||||
},
|
||||
"require-dev":
|
||||
{
|
||||
"phpunit/phpunit": "^6.0",
|
||||
"php-coveralls/php-coveralls": "^2.1"
|
||||
"phpunit/phpunit": "^9.5",
|
||||
"vimeo/psalm": "^4.7",
|
||||
"phpstan/phpstan": "^0.12.83"
|
||||
},
|
||||
"autoload":
|
||||
{
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
parameters:
|
||||
checkMissingIterableValueType: false
|
||||
checkFunctionNameCase: true
|
||||
level: 6
|
||||
paths:
|
||||
- .
|
||||
excludes_analyse:
|
||||
- vendor
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0"?>
|
||||
<psalm
|
||||
errorLevel="1"
|
||||
resolveFromConfigFile="true"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="https://getpsalm.org/schema/config"
|
||||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
|
||||
>
|
||||
<projectFiles>
|
||||
<directory name="SourceQuery" />
|
||||
<directory name="Tests" />
|
||||
<ignoreFiles>
|
||||
<directory name="vendor" />
|
||||
</ignoreFiles>
|
||||
</projectFiles>
|
||||
</psalm>
|
||||
Reference in New Issue
Block a user