mirror of
https://github.com/xPaw/PHP-Source-Query.git
synced 2026-07-14 10:04:49 +02:00
Compare commits
16 Commits
a92e4e4a34
...
2.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 673e572233 | |||
| 7c8e5add77 | |||
| fbabd440da | |||
| bbb6c4c23e | |||
| 9da781a993 | |||
| b4601cdf86 | |||
| 7231921b0b | |||
| 4f88fb0c76 | |||
| 7414334804 | |||
| 8a2c16123c | |||
| 528d0ef4fc | |||
| 914ec4b79a | |||
| b87c9faab9 | |||
| 1078e47b7e | |||
| e00235c29a | |||
| 75d3465690 |
@@ -7,10 +7,12 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
php: [7.2, 7.3]
|
php: [7.4]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: composer install --no-interaction --no-progress
|
run: composer install --no-interaction --no-progress
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: php${{ matrix.php }} vendor/bin/phpunit --configuration Tests/phpunit.xml --verbose --fail-on-warning
|
run: php${{ matrix.php }} vendor/bin/phpunit --configuration Tests/phpunit.xml --verbose --fail-on-warning
|
||||||
|
- name: Run psalm
|
||||||
|
run: php${{ matrix.php }} vendor/bin/psalm
|
||||||
|
|||||||
+5
-7
@@ -17,6 +17,7 @@
|
|||||||
$Info = Array( );
|
$Info = Array( );
|
||||||
$Rules = Array( );
|
$Rules = Array( );
|
||||||
$Players = Array( );
|
$Players = Array( );
|
||||||
|
$Exception = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -87,13 +88,11 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<?php if( isset( $Exception ) ): ?>
|
<?php if( $Exception !== null ): ?>
|
||||||
<div class="panel panel-primary">
|
<div class="panel panel-error">
|
||||||
<div class="panel-heading"><?php echo Get_Class( $Exception ); ?> at line <?php echo $Exception->getLine( ); ?></div>
|
<pre class="panel-body"><?php echo htmlspecialchars( $Exception->__toString( ) ); ?></pre>
|
||||||
<p><b><?php echo htmlspecialchars( $Exception->getMessage( ) ); ?></b></p>
|
|
||||||
<p><?php echo nl2br( $e->getTraceAsString(), false ); ?></p>
|
|
||||||
</div>
|
</div>
|
||||||
<?php else: ?>
|
<?php endif; ?>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-sm-6">
|
<div class="col-sm-6">
|
||||||
<table class="table table-bordered table-striped">
|
<table class="table table-bordered table-striped">
|
||||||
@@ -193,7 +192,6 @@
|
|||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
# PHP Source Query
|
# 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)
|
||||||
[](https://packagist.org/packages/xpaw/php-source-query-class)
|
[](https://packagist.org/packages/xpaw/php-source-query-class)
|
||||||
|
|
||||||
@@ -14,7 +12,7 @@ The class also allows you to query servers using RCON although this only works f
|
|||||||
**: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: 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.**
|
||||||
|
|
||||||
## Requirements
|
## 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)
|
* 64-bit PHP or [gmp module](https://secure.php.net/manual/en/book.gmp.php)
|
||||||
* Web server must allow UDP connections
|
* Web server must allow UDP connections
|
||||||
|
|
||||||
|
|||||||
+20
-12
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
@@ -13,34 +13,37 @@
|
|||||||
namespace xPaw\SourceQuery;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
|
use xPaw\SourceQuery\Exception\SocketException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base socket interface
|
* Base socket interface
|
||||||
*
|
*
|
||||||
* @package xPaw\SourceQuery
|
* @package xPaw\SourceQuery
|
||||||
*
|
*
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||||
|
* @uses xPaw\SourceQuery\Exception\SocketException
|
||||||
*/
|
*/
|
||||||
abstract class BaseSocket
|
abstract class BaseSocket
|
||||||
{
|
{
|
||||||
|
/** @var resource */
|
||||||
public $Socket;
|
public $Socket;
|
||||||
public $Engine;
|
public int $Engine;
|
||||||
|
|
||||||
public $Address;
|
public string $Address;
|
||||||
public $Port;
|
public int $Port;
|
||||||
public $Timeout;
|
public int $Timeout;
|
||||||
|
|
||||||
public function __destruct( )
|
public function __destruct( )
|
||||||
{
|
{
|
||||||
$this->Close( );
|
$this->Close( );
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public function Close( );
|
abstract public function Close( ) : void;
|
||||||
abstract public function Open( $Address, $Port, $Timeout, $Engine );
|
abstract public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void;
|
||||||
abstract public function Write( $Header, $String = '' );
|
abstract public function Write( int $Header, string $String = '' ) : bool;
|
||||||
abstract public function Read( $Length = 1400 );
|
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 )
|
if( $Buffer->Remaining( ) === 0 )
|
||||||
{
|
{
|
||||||
@@ -58,6 +61,7 @@
|
|||||||
$Packets = [];
|
$Packets = [];
|
||||||
$IsCompressed = false;
|
$IsCompressed = false;
|
||||||
$ReadMore = false;
|
$ReadMore = false;
|
||||||
|
$PacketChecksum = null;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@@ -92,6 +96,10 @@
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$Packets[ $PacketNumber ] = $Buffer->Get( );
|
$Packets[ $PacketNumber ] = $Buffer->Get( );
|
||||||
@@ -113,7 +121,7 @@
|
|||||||
|
|
||||||
$Data = bzdecompress( $Data );
|
$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 );
|
throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH );
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-39
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
@@ -25,31 +25,23 @@
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Buffer
|
* Buffer
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
*/
|
||||||
private $Buffer;
|
private string $Buffer = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Buffer length
|
* Buffer length
|
||||||
*
|
|
||||||
* @var int
|
|
||||||
*/
|
*/
|
||||||
private $Length;
|
private int $Length = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Current position in buffer
|
* Current position in buffer
|
||||||
*
|
|
||||||
* @var int
|
|
||||||
*/
|
*/
|
||||||
private $Position;
|
private int $Position = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets buffer
|
* Sets buffer
|
||||||
*
|
|
||||||
* @param string $Buffer Buffer
|
|
||||||
*/
|
*/
|
||||||
public function Set( $Buffer )
|
public function Set( string $Buffer ) : void
|
||||||
{
|
{
|
||||||
$this->Buffer = $Buffer;
|
$this->Buffer = $Buffer;
|
||||||
$this->Length = StrLen( $Buffer );
|
$this->Length = StrLen( $Buffer );
|
||||||
@@ -61,7 +53,7 @@
|
|||||||
*
|
*
|
||||||
* @return int Remaining bytes in buffer
|
* @return int Remaining bytes in buffer
|
||||||
*/
|
*/
|
||||||
public function Remaining( )
|
public function Remaining( ) : int
|
||||||
{
|
{
|
||||||
return $this->Length - $this->Position;
|
return $this->Length - $this->Position;
|
||||||
}
|
}
|
||||||
@@ -70,10 +62,8 @@
|
|||||||
* Gets data from buffer
|
* Gets data from buffer
|
||||||
*
|
*
|
||||||
* @param int $Length Bytes to read
|
* @param int $Length Bytes to read
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function Get( $Length = -1 )
|
public function Get( int $Length = -1 ) : string
|
||||||
{
|
{
|
||||||
if( $Length === 0 )
|
if( $Length === 0 )
|
||||||
{
|
{
|
||||||
@@ -100,20 +90,16 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get byte from buffer
|
* 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
|
* Get short from buffer
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*/
|
*/
|
||||||
public function GetShort( )
|
public function GetShort( ) : int
|
||||||
{
|
{
|
||||||
if( $this->Remaining( ) < 2 )
|
if( $this->Remaining( ) < 2 )
|
||||||
{
|
{
|
||||||
@@ -122,15 +108,13 @@
|
|||||||
|
|
||||||
$Data = UnPack( 'v', $this->Get( 2 ) );
|
$Data = UnPack( 'v', $this->Get( 2 ) );
|
||||||
|
|
||||||
return $Data[ 1 ];
|
return (int)$Data[ 1 ];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get long from buffer
|
* Get long from buffer
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*/
|
*/
|
||||||
public function GetLong( )
|
public function GetLong( ) : int
|
||||||
{
|
{
|
||||||
if( $this->Remaining( ) < 4 )
|
if( $this->Remaining( ) < 4 )
|
||||||
{
|
{
|
||||||
@@ -139,15 +123,13 @@
|
|||||||
|
|
||||||
$Data = UnPack( 'l', $this->Get( 4 ) );
|
$Data = UnPack( 'l', $this->Get( 4 ) );
|
||||||
|
|
||||||
return $Data[ 1 ];
|
return (int)$Data[ 1 ];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get float from buffer
|
* Get float from buffer
|
||||||
*
|
|
||||||
* @return float
|
|
||||||
*/
|
*/
|
||||||
public function GetFloat( )
|
public function GetFloat( ) : float
|
||||||
{
|
{
|
||||||
if( $this->Remaining( ) < 4 )
|
if( $this->Remaining( ) < 4 )
|
||||||
{
|
{
|
||||||
@@ -156,15 +138,13 @@
|
|||||||
|
|
||||||
$Data = UnPack( 'f', $this->Get( 4 ) );
|
$Data = UnPack( 'f', $this->Get( 4 ) );
|
||||||
|
|
||||||
return $Data[ 1 ];
|
return (float)$Data[ 1 ];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get unsigned long from buffer
|
* Get unsigned long from buffer
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*/
|
*/
|
||||||
public function GetUnsignedLong( )
|
public function GetUnsignedLong( ) : int
|
||||||
{
|
{
|
||||||
if( $this->Remaining( ) < 4 )
|
if( $this->Remaining( ) < 4 )
|
||||||
{
|
{
|
||||||
@@ -173,15 +153,13 @@
|
|||||||
|
|
||||||
$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
|
* 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 );
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
@@ -17,4 +17,5 @@
|
|||||||
const COULD_NOT_CREATE_SOCKET = 1;
|
const COULD_NOT_CREATE_SOCKET = 1;
|
||||||
const NOT_CONNECTED = 2;
|
const NOT_CONNECTED = 2;
|
||||||
const CONNECTION_FAILED = 3;
|
const CONNECTION_FAILED = 3;
|
||||||
|
const INVALID_ENGINE = 3;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
@@ -13,6 +13,7 @@
|
|||||||
namespace xPaw\SourceQuery;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||||
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class GoldSourceRcon
|
* Class GoldSourceRcon
|
||||||
@@ -20,38 +21,37 @@
|
|||||||
* @package xPaw\SourceQuery
|
* @package xPaw\SourceQuery
|
||||||
*
|
*
|
||||||
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
||||||
|
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||||
*/
|
*/
|
||||||
class GoldSourceRcon
|
class GoldSourceRcon
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Points to socket class
|
* Points to socket class
|
||||||
*
|
*
|
||||||
* @var Socket
|
* @var BaseSocket
|
||||||
*/
|
*/
|
||||||
private $Socket;
|
private $Socket;
|
||||||
|
|
||||||
private $RconPassword;
|
private string $RconPassword = '';
|
||||||
private $RconRequestId;
|
private string $RconChallenge = '';
|
||||||
private $RconChallenge;
|
|
||||||
|
|
||||||
public function __construct( $Socket )
|
public function __construct( BaseSocket $Socket )
|
||||||
{
|
{
|
||||||
$this->Socket = $Socket;
|
$this->Socket = $Socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Close( )
|
public function Close( ) : void
|
||||||
{
|
{
|
||||||
$this->RconChallenge = 0;
|
$this->RconChallenge = '';
|
||||||
$this->RconRequestId = 0;
|
$this->RconPassword = '';
|
||||||
$this->RconPassword = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 );
|
$Command = Pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
|
||||||
$Length = StrLen( $Command );
|
$Length = StrLen( $Command );
|
||||||
@@ -62,9 +62,9 @@
|
|||||||
/**
|
/**
|
||||||
* @param int $Length
|
* @param int $Length
|
||||||
* @throws AuthenticationException
|
* @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
|
// GoldSource RCON has same structure as Query
|
||||||
$Buffer = $this->Socket->Read( );
|
$Buffer = $this->Socket->Read( );
|
||||||
@@ -115,7 +115,7 @@
|
|||||||
return $Buffer;
|
return $Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Command( $Command )
|
public function Command( string $Command ) : string
|
||||||
{
|
{
|
||||||
if( !$this->RconChallenge )
|
if( !$this->RconChallenge )
|
||||||
{
|
{
|
||||||
@@ -128,7 +128,7 @@
|
|||||||
return $Buffer->Get( );
|
return $Buffer->Get( );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Authorize( $Password )
|
public function Authorize( string $Password ) : void
|
||||||
{
|
{
|
||||||
$this->RconPassword = $Password;
|
$this->RconPassword = $Password;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
@@ -25,9 +25,9 @@
|
|||||||
*/
|
*/
|
||||||
class Socket extends BaseSocket
|
class Socket extends BaseSocket
|
||||||
{
|
{
|
||||||
public function Close( )
|
public function Close( ) : void
|
||||||
{
|
{
|
||||||
if( $this->Socket )
|
if( $this->Socket !== null )
|
||||||
{
|
{
|
||||||
FClose( $this->Socket );
|
FClose( $this->Socket );
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Open( $Address, $Port, $Timeout, $Engine )
|
public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void
|
||||||
{
|
{
|
||||||
$this->Timeout = $Timeout;
|
$this->Timeout = $Timeout;
|
||||||
$this->Engine = $Engine;
|
$this->Engine = $Engine;
|
||||||
@@ -53,7 +53,7 @@
|
|||||||
Stream_Set_Blocking( $this->Socket, true );
|
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 );
|
$Command = Pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
|
||||||
$Length = StrLen( $Command );
|
$Length = StrLen( $Command );
|
||||||
@@ -68,7 +68,7 @@
|
|||||||
*
|
*
|
||||||
* @return Buffer Buffer
|
* @return Buffer Buffer
|
||||||
*/
|
*/
|
||||||
public function Read( $Length = 1400 )
|
public function Read( int $Length = 1400 ) : Buffer
|
||||||
{
|
{
|
||||||
$Buffer = new Buffer( );
|
$Buffer = new Buffer( );
|
||||||
$Buffer->Set( FRead( $this->Socket, $Length ) );
|
$Buffer->Set( FRead( $this->Socket, $Length ) );
|
||||||
@@ -78,7 +78,7 @@
|
|||||||
return $Buffer;
|
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 );
|
||||||
|
|
||||||
|
|||||||
+53
-43
@@ -2,7 +2,7 @@
|
|||||||
/**
|
/**
|
||||||
* This class provides the public interface to the PHP-Source-Query library.
|
* This class provides the public interface to the PHP-Source-Query library.
|
||||||
*
|
*
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
namespace xPaw\SourceQuery;
|
namespace xPaw\SourceQuery;
|
||||||
|
|
||||||
|
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||||
use xPaw\SourceQuery\Exception\InvalidArgumentException;
|
use xPaw\SourceQuery\Exception\InvalidArgumentException;
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||||
use xPaw\SourceQuery\Exception\SocketException;
|
use xPaw\SourceQuery\Exception\SocketException;
|
||||||
@@ -21,6 +22,7 @@
|
|||||||
*
|
*
|
||||||
* @package xPaw\SourceQuery
|
* @package xPaw\SourceQuery
|
||||||
*
|
*
|
||||||
|
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidArgumentException
|
* @uses xPaw\SourceQuery\Exception\InvalidArgumentException
|
||||||
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||||||
* @uses xPaw\SourceQuery\Exception\SocketException
|
* @uses xPaw\SourceQuery\Exception\SocketException
|
||||||
@@ -68,37 +70,29 @@
|
|||||||
/**
|
/**
|
||||||
* Points to rcon class
|
* Points to rcon class
|
||||||
*
|
*
|
||||||
* @var SourceRcon
|
* @var SourceRcon|GoldSourceRcon|null
|
||||||
*/
|
*/
|
||||||
private $Rcon;
|
private $Rcon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Points to socket class
|
* Points to socket class
|
||||||
*
|
|
||||||
* @var Socket
|
|
||||||
*/
|
*/
|
||||||
private $Socket;
|
private BaseSocket $Socket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* True if connection is open, false if not
|
* True if connection is open, false if not
|
||||||
*
|
|
||||||
* @var bool
|
|
||||||
*/
|
*/
|
||||||
private $Connected;
|
private bool $Connected = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains challenge
|
* Contains challenge
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
*/
|
||||||
private $Challenge;
|
private string $Challenge = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use old method for getting challenge number
|
* Use old method for getting challenge number
|
||||||
*
|
|
||||||
* @var bool
|
|
||||||
*/
|
*/
|
||||||
private $UseOldGetChallengeMethod;
|
private bool $UseOldGetChallengeMethod = false;
|
||||||
|
|
||||||
public function __construct( BaseSocket $Socket = null )
|
public function __construct( BaseSocket $Socket = null )
|
||||||
{
|
{
|
||||||
@@ -121,16 +115,16 @@
|
|||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
* @throws SocketException
|
* @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( );
|
$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;
|
$this->Connected = true;
|
||||||
}
|
}
|
||||||
@@ -142,7 +136,7 @@
|
|||||||
*
|
*
|
||||||
* @returns bool Previous value
|
* @returns bool Previous value
|
||||||
*/
|
*/
|
||||||
public function SetUseOldGetChallengeMethod( $Value )
|
public function SetUseOldGetChallengeMethod( bool $Value ) : bool
|
||||||
{
|
{
|
||||||
$Previous = $this->UseOldGetChallengeMethod;
|
$Previous = $this->UseOldGetChallengeMethod;
|
||||||
|
|
||||||
@@ -154,10 +148,10 @@
|
|||||||
/**
|
/**
|
||||||
* Closes all open connections
|
* Closes all open connections
|
||||||
*/
|
*/
|
||||||
public function Disconnect( )
|
public function Disconnect( ) : void
|
||||||
{
|
{
|
||||||
$this->Connected = false;
|
$this->Connected = false;
|
||||||
$this->Challenge = 0;
|
$this->Challenge = '';
|
||||||
|
|
||||||
$this->Socket->Close( );
|
$this->Socket->Close( );
|
||||||
|
|
||||||
@@ -178,7 +172,7 @@
|
|||||||
*
|
*
|
||||||
* @return bool True on success, false on failure
|
* @return bool True on success, false on failure
|
||||||
*/
|
*/
|
||||||
public function Ping( )
|
public function Ping( ) : bool
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if( !$this->Connected )
|
||||||
{
|
{
|
||||||
@@ -199,18 +193,35 @@
|
|||||||
*
|
*
|
||||||
* @return array Returns an array with information on success
|
* @return array Returns an array with information on success
|
||||||
*/
|
*/
|
||||||
public function GetInfo( )
|
public function GetInfo( ) : array
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if( !$this->Connected )
|
||||||
{
|
{
|
||||||
throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED );
|
throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED );
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Socket->Write( self::A2S_INFO, "Source Engine Query\0" );
|
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( );
|
$Buffer = $this->Socket->Read( );
|
||||||
|
|
||||||
$Type = $Buffer->GetByte( );
|
$Type = $Buffer->GetByte( );
|
||||||
|
$Server = [];
|
||||||
|
|
||||||
|
if( $Type === self::S2A_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
|
// Old GoldSource protocol, HLTV still uses it
|
||||||
if( $Type === self::S2A_INFO_OLD && $this->Socket->Engine === self::GOLDSOURCE )
|
if( $Type === self::S2A_INFO_OLD && $this->Socket->Engine === self::GOLDSOURCE )
|
||||||
{
|
{
|
||||||
@@ -235,6 +246,7 @@
|
|||||||
|
|
||||||
if( $Server[ 'IsMod' ] )
|
if( $Server[ 'IsMod' ] )
|
||||||
{
|
{
|
||||||
|
$Mod = [];
|
||||||
$Mod[ 'Url' ] = $Buffer->GetString( );
|
$Mod[ 'Url' ] = $Buffer->GetString( );
|
||||||
$Mod[ 'Download' ] = $Buffer->GetString( );
|
$Mod[ 'Download' ] = $Buffer->GetString( );
|
||||||
$Buffer->Get( 1 ); // NULL byte
|
$Buffer->Get( 1 ); // NULL byte
|
||||||
@@ -242,16 +254,12 @@
|
|||||||
$Mod[ 'Size' ] = $Buffer->GetLong( );
|
$Mod[ 'Size' ] = $Buffer->GetLong( );
|
||||||
$Mod[ 'ServerSide' ] = $Buffer->GetByte( ) === 1;
|
$Mod[ 'ServerSide' ] = $Buffer->GetByte( ) === 1;
|
||||||
$Mod[ 'CustomDLL' ] = $Buffer->GetByte( ) === 1;
|
$Mod[ 'CustomDLL' ] = $Buffer->GetByte( ) === 1;
|
||||||
|
$Server[ 'Mod' ] = $Mod;
|
||||||
}
|
}
|
||||||
|
|
||||||
$Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
|
$Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
|
||||||
$Server[ 'Bots' ] = $Buffer->GetByte( );
|
$Server[ 'Bots' ] = $Buffer->GetByte( );
|
||||||
|
|
||||||
if( isset( $Mod ) )
|
|
||||||
{
|
|
||||||
$Server[ 'Mod' ] = $Mod;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $Server;
|
return $Server;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,13 +297,13 @@
|
|||||||
{
|
{
|
||||||
$Server[ 'ExtraDataFlags' ] = $Flags = $Buffer->GetByte( );
|
$Server[ 'ExtraDataFlags' ] = $Flags = $Buffer->GetByte( );
|
||||||
|
|
||||||
// The server's game port
|
// S2A_EXTRA_DATA_HAS_GAME_PORT - Next 2 bytes include the game port.
|
||||||
if( $Flags & 0x80 )
|
if( $Flags & 0x80 )
|
||||||
{
|
{
|
||||||
$Server[ 'GamePort' ] = $Buffer->GetShort( );
|
$Server[ 'GamePort' ] = $Buffer->GetShort( );
|
||||||
}
|
}
|
||||||
|
|
||||||
// The server's steamid
|
// S2A_EXTRA_DATA_HAS_STEAMID - Next 8 bytes are the steamID
|
||||||
// Want to play around with this?
|
// Want to play around with this?
|
||||||
// You can use https://github.com/xPaw/SteamID.php
|
// You can use https://github.com/xPaw/SteamID.php
|
||||||
if( $Flags & 0x10 )
|
if( $Flags & 0x10 )
|
||||||
@@ -327,20 +335,20 @@
|
|||||||
unset( $SteamIDLower, $SteamIDInstance, $SteamID );
|
unset( $SteamIDLower, $SteamIDInstance, $SteamID );
|
||||||
}
|
}
|
||||||
|
|
||||||
// The spectator port and then the spectator server name
|
// S2A_EXTRA_DATA_HAS_SPECTATOR_DATA - Next 2 bytes include the spectator port, then the spectator server name.
|
||||||
if( $Flags & 0x40 )
|
if( $Flags & 0x40 )
|
||||||
{
|
{
|
||||||
$Server[ 'SpecPort' ] = $Buffer->GetShort( );
|
$Server[ 'SpecPort' ] = $Buffer->GetShort( );
|
||||||
$Server[ 'SpecName' ] = $Buffer->GetString( );
|
$Server[ 'SpecName' ] = $Buffer->GetString( );
|
||||||
}
|
}
|
||||||
|
|
||||||
// The game tag data string for the server
|
// S2A_EXTRA_DATA_HAS_GAMETAG_DATA - Next bytes are the game tag string
|
||||||
if( $Flags & 0x20 )
|
if( $Flags & 0x20 )
|
||||||
{
|
{
|
||||||
$Server[ 'GameTags' ] = $Buffer->GetString( );
|
$Server[ 'GameTags' ] = $Buffer->GetString( );
|
||||||
}
|
}
|
||||||
|
|
||||||
// GameID -- alternative to AppID?
|
// S2A_EXTRA_DATA_GAMEID - Next 8 bytes are the gameID of the server
|
||||||
if( $Flags & 0x01 )
|
if( $Flags & 0x01 )
|
||||||
{
|
{
|
||||||
$Server[ 'GameID' ] = $Buffer->GetUnsignedLong( ) | ( $Buffer->GetUnsignedLong( ) << 32 );
|
$Server[ 'GameID' ] = $Buffer->GetUnsignedLong( ) | ( $Buffer->GetUnsignedLong( ) << 32 );
|
||||||
@@ -364,7 +372,7 @@
|
|||||||
*
|
*
|
||||||
* @return array Returns an array with players on success
|
* @return array Returns an array with players on success
|
||||||
*/
|
*/
|
||||||
public function GetPlayers( )
|
public function GetPlayers( ) : array
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if( !$this->Connected )
|
||||||
{
|
{
|
||||||
@@ -389,6 +397,7 @@
|
|||||||
|
|
||||||
while( $Count-- > 0 && $Buffer->Remaining( ) > 0 )
|
while( $Count-- > 0 && $Buffer->Remaining( ) > 0 )
|
||||||
{
|
{
|
||||||
|
$Player = [];
|
||||||
$Player[ 'Id' ] = $Buffer->GetByte( ); // PlayerID, is it just always 0?
|
$Player[ 'Id' ] = $Buffer->GetByte( ); // PlayerID, is it just always 0?
|
||||||
$Player[ 'Name' ] = $Buffer->GetString( );
|
$Player[ 'Name' ] = $Buffer->GetString( );
|
||||||
$Player[ 'Frags' ] = $Buffer->GetLong( );
|
$Player[ 'Frags' ] = $Buffer->GetLong( );
|
||||||
@@ -409,7 +418,7 @@
|
|||||||
*
|
*
|
||||||
* @return array Returns an array with rules on success
|
* @return array Returns an array with rules on success
|
||||||
*/
|
*/
|
||||||
public function GetRules( )
|
public function GetRules( ) : array
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if( !$this->Connected )
|
||||||
{
|
{
|
||||||
@@ -448,12 +457,9 @@
|
|||||||
/**
|
/**
|
||||||
* Get challenge (used for players/rules packets)
|
* Get challenge (used for players/rules packets)
|
||||||
*
|
*
|
||||||
* @param $Header
|
|
||||||
* @param $ExpectedResult
|
|
||||||
*
|
|
||||||
* @throws InvalidPacketException
|
* @throws InvalidPacketException
|
||||||
*/
|
*/
|
||||||
private function GetChallenge( $Header, $ExpectedResult )
|
private function GetChallenge( int $Header, int $ExpectedResult ) : void
|
||||||
{
|
{
|
||||||
if( $this->Challenge )
|
if( $this->Challenge )
|
||||||
{
|
{
|
||||||
@@ -504,7 +510,7 @@
|
|||||||
* @throws InvalidPacketException
|
* @throws InvalidPacketException
|
||||||
* @throws SocketException
|
* @throws SocketException
|
||||||
*/
|
*/
|
||||||
public function SetRconPassword( $Password )
|
public function SetRconPassword( string $Password ) : void
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if( !$this->Connected )
|
||||||
{
|
{
|
||||||
@@ -525,6 +531,10 @@
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Rcon->Open( );
|
$this->Rcon->Open( );
|
||||||
@@ -542,7 +552,7 @@
|
|||||||
*
|
*
|
||||||
* @return string Answer from server in string
|
* @return string Answer from server in string
|
||||||
*/
|
*/
|
||||||
public function Rcon( $Command )
|
public function Rcon( string $Command ) : string
|
||||||
{
|
{
|
||||||
if( !$this->Connected )
|
if( !$this->Connected )
|
||||||
{
|
{
|
||||||
|
|||||||
+11
-12
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
@@ -29,20 +29,19 @@
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Points to socket class
|
* Points to socket class
|
||||||
*
|
|
||||||
* @var Socket
|
|
||||||
*/
|
*/
|
||||||
private $Socket;
|
private BaseSocket $Socket;
|
||||||
|
|
||||||
|
/** @var resource */
|
||||||
private $RconSocket;
|
private $RconSocket;
|
||||||
private $RconRequestId;
|
private int $RconRequestId = 0;
|
||||||
|
|
||||||
public function __construct( $Socket )
|
public function __construct( BaseSocket $Socket )
|
||||||
{
|
{
|
||||||
$this->Socket = $Socket;
|
$this->Socket = $Socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Close( )
|
public function Close( ) : void
|
||||||
{
|
{
|
||||||
if( $this->RconSocket )
|
if( $this->RconSocket )
|
||||||
{
|
{
|
||||||
@@ -54,7 +53,7 @@
|
|||||||
$this->RconRequestId = 0;
|
$this->RconRequestId = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Open( )
|
public function Open( ) : void
|
||||||
{
|
{
|
||||||
if( !$this->RconSocket )
|
if( !$this->RconSocket )
|
||||||
{
|
{
|
||||||
@@ -70,7 +69,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Write( $Header, $String = '' )
|
public function Write( int $Header, string $String = '' ) : bool
|
||||||
{
|
{
|
||||||
// Pack the packet together
|
// Pack the packet together
|
||||||
$Command = Pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
|
$Command = Pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
|
||||||
@@ -82,7 +81,7 @@
|
|||||||
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 = new Buffer( );
|
||||||
$Buffer->Set( FRead( $this->RconSocket, 4 ) );
|
$Buffer->Set( FRead( $this->RconSocket, 4 ) );
|
||||||
@@ -122,7 +121,7 @@
|
|||||||
return $Buffer;
|
return $Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Command( $Command )
|
public function Command( string $Command ) : string
|
||||||
{
|
{
|
||||||
$this->Write( SourceQuery::SERVERDATA_EXECCOMMAND, $Command );
|
$this->Write( SourceQuery::SERVERDATA_EXECCOMMAND, $Command );
|
||||||
$Buffer = $this->Read( );
|
$Buffer = $this->Read( );
|
||||||
@@ -174,7 +173,7 @@
|
|||||||
return rtrim( $Data, "\0" );
|
return rtrim( $Data, "\0" );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Authorize( $Password )
|
public function Authorize( string $Password ) : void
|
||||||
{
|
{
|
||||||
$this->Write( SourceQuery::SERVERDATA_AUTH, $Password );
|
$this->Write( SourceQuery::SERVERDATA_AUTH, $Password );
|
||||||
$Buffer = $this->Read( );
|
$Buffer = $this->Read( );
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
* Special thanks to koraktor for his awesome Steam Condenser class,
|
* Special thanks to koraktor for his awesome Steam Condenser class,
|
||||||
* I used it as a reference at some points.
|
* I used it as a reference at some points.
|
||||||
*
|
*
|
||||||
* @author Pavel Djundik <sourcequery@xpaw.me>
|
* @author Pavel Djundik
|
||||||
*
|
*
|
||||||
* @link https://xpaw.me
|
* @link https://xpaw.me
|
||||||
* @link https://github.com/xPaw/PHP-Source-Query
|
* @link https://github.com/xPaw/PHP-Source-Query
|
||||||
|
|||||||
+50
-64
@@ -1,13 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use xPaw\SourceQuery\BaseSocket;
|
use xPaw\SourceQuery\BaseSocket;
|
||||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
|
||||||
use xPaw\SourceQuery\SourceQuery;
|
use xPaw\SourceQuery\SourceQuery;
|
||||||
use xPaw\SourceQuery\Buffer;
|
use xPaw\SourceQuery\Buffer;
|
||||||
|
|
||||||
class TestableSocket extends BaseSocket
|
class TestableSocket extends BaseSocket
|
||||||
{
|
{
|
||||||
private $PacketQueue;
|
private \SplQueue $PacketQueue;
|
||||||
|
|
||||||
public function __construct( )
|
public function __construct( )
|
||||||
{
|
{
|
||||||
@@ -16,17 +15,17 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Queue( $Data )
|
public function Queue( string $Data ) : void
|
||||||
{
|
{
|
||||||
$this->PacketQueue->push( $Data );
|
$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->Timeout = $Timeout;
|
||||||
$this->Engine = $Engine;
|
$this->Engine = $Engine;
|
||||||
@@ -34,85 +33,77 @@
|
|||||||
$this->Address = $Address;
|
$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 = new Buffer( );
|
||||||
$Buffer->Set( $this->PacketQueue->shift() );
|
$Buffer->Set( (string)$this->PacketQueue->shift() );
|
||||||
|
|
||||||
$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
|
$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
|
||||||
|
|
||||||
return $Buffer;
|
return $Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Sherlock( $Buffer, $Length )
|
public function Sherlock( Buffer $Buffer, int $Length ) : bool
|
||||||
{
|
{
|
||||||
if( $this->PacketQueue->isEmpty() )
|
if( $this->PacketQueue->isEmpty() )
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$Buffer->Set( $this->PacketQueue->shift() );
|
$Buffer->Set( (string)$this->PacketQueue->shift() );
|
||||||
|
|
||||||
return $Buffer->GetLong( ) === -2;
|
return $Buffer->GetLong( ) === -2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SourceQueryTests extends TestCase
|
class Tests extends \PHPUnit\Framework\TestCase
|
||||||
{
|
{
|
||||||
private $Socket;
|
private TestableSocket $Socket;
|
||||||
private $SourceQuery;
|
private SourceQuery $SourceQuery;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp() : void
|
||||||
{
|
{
|
||||||
$this->Socket = new TestableSocket();
|
$this->Socket = new TestableSocket();
|
||||||
$this->SourceQuery = new SourceQuery( $this->Socket );
|
$this->SourceQuery = new SourceQuery( $this->Socket );
|
||||||
$this->SourceQuery->Connect( 1, 2 );
|
$this->SourceQuery->Connect( '', 2 );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tearDown()
|
public function tearDown() : void
|
||||||
{
|
{
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
|
|
||||||
unset( $this->Socket, $this->SourceQuery );
|
unset( $this->Socket, $this->SourceQuery );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testInvalidTimeout() : void
|
||||||
* @expectedException xPaw\SourceQuery\Exception\InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function testInvalidTimeout()
|
|
||||||
{
|
{
|
||||||
|
$this->expectException( xPaw\SourceQuery\Exception\InvalidArgumentException::class );
|
||||||
$SourceQuery = new SourceQuery( );
|
$SourceQuery = new SourceQuery( );
|
||||||
$SourceQuery->Connect( 1, 2, -1 );
|
$SourceQuery->Connect( '', 2, -1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testNotConnectedGetInfo() : void
|
||||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
|
||||||
*/
|
|
||||||
public function testNotConnectedGetInfo()
|
|
||||||
{
|
{
|
||||||
|
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->GetInfo();
|
$this->SourceQuery->GetInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testNotConnectedPing() : void
|
||||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
|
||||||
*/
|
|
||||||
public function testNotConnectedPing()
|
|
||||||
{
|
{
|
||||||
|
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->Ping();
|
$this->SourceQuery->Ping();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testNotConnectedGetPlayers() : void
|
||||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
|
||||||
*/
|
|
||||||
public function testNotConnectedGetPlayers()
|
|
||||||
{
|
{
|
||||||
|
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->GetPlayers();
|
$this->SourceQuery->GetPlayers();
|
||||||
}
|
}
|
||||||
@@ -120,42 +111,37 @@
|
|||||||
/**
|
/**
|
||||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
||||||
*/
|
*/
|
||||||
public function testNotConnectedGetRules()
|
public function testNotConnectedGetRules() : void
|
||||||
{
|
{
|
||||||
|
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->GetRules();
|
$this->SourceQuery->GetRules();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testNotConnectedSetRconPassword() : void
|
||||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
|
||||||
*/
|
|
||||||
public function testNotConnectedSetRconPassword()
|
|
||||||
{
|
{
|
||||||
|
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->SetRconPassword('a');
|
$this->SourceQuery->SetRconPassword('a');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testNotConnectedRcon() : void
|
||||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
|
||||||
*/
|
|
||||||
public function testNotConnectedRcon()
|
|
||||||
{
|
{
|
||||||
|
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||||
$this->SourceQuery->Disconnect();
|
$this->SourceQuery->Disconnect();
|
||||||
$this->SourceQuery->Rcon('a');
|
$this->SourceQuery->Rcon('a');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testRconWithoutPassword() : void
|
||||||
* @expectedException xPaw\SourceQuery\Exception\SocketException
|
|
||||||
*/
|
|
||||||
public function testRconWithoutPassword()
|
|
||||||
{
|
{
|
||||||
|
$this->expectException( xPaw\SourceQuery\Exception\SocketException::class );
|
||||||
$this->SourceQuery->Rcon('a');
|
$this->SourceQuery->Rcon('a');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider InfoProvider
|
* @dataProvider InfoProvider
|
||||||
*/
|
*/
|
||||||
public function testGetInfo( $RawInput, $ExpectedOutput )
|
public function testGetInfo( string $RawInput, array $ExpectedOutput ) : void
|
||||||
{
|
{
|
||||||
if( isset( $ExpectedOutput[ 'IsMod' ] ) )
|
if( isset( $ExpectedOutput[ 'IsMod' ] ) )
|
||||||
{
|
{
|
||||||
@@ -169,7 +155,7 @@
|
|||||||
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function InfoProvider()
|
public function InfoProvider() : array
|
||||||
{
|
{
|
||||||
$DataProvider = [];
|
$DataProvider = [];
|
||||||
|
|
||||||
@@ -188,33 +174,33 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException xPaw\SourceQuery\Exception\InvalidPacketException
|
|
||||||
* @dataProvider BadPacketProvider
|
* @dataProvider BadPacketProvider
|
||||||
*/
|
*/
|
||||||
public function testBadGetInfo( $Data )
|
public function testBadGetInfo( string $Data ) : void
|
||||||
{
|
{
|
||||||
|
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
||||||
$this->Socket->Queue( $Data );
|
$this->Socket->Queue( $Data );
|
||||||
|
|
||||||
$this->SourceQuery->GetInfo();
|
$this->SourceQuery->GetInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException xPaw\SourceQuery\Exception\InvalidPacketException
|
|
||||||
* @dataProvider BadPacketProvider
|
* @dataProvider BadPacketProvider
|
||||||
*/
|
*/
|
||||||
public function testBadGetChallengeViaPlayers( $Data )
|
public function testBadGetChallengeViaPlayers( string $Data ) : void
|
||||||
{
|
{
|
||||||
|
$this->expectException( xPaw\SourceQuery\Exception\InvalidPacketException::class );
|
||||||
$this->Socket->Queue( $Data );
|
$this->Socket->Queue( $Data );
|
||||||
|
|
||||||
$this->SourceQuery->GetPlayers();
|
$this->SourceQuery->GetPlayers();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException xPaw\SourceQuery\Exception\InvalidPacketException
|
|
||||||
* @dataProvider BadPacketProvider
|
* @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( "\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11" );
|
||||||
$this->Socket->Queue( $Data );
|
$this->Socket->Queue( $Data );
|
||||||
|
|
||||||
@@ -222,18 +208,18 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException xPaw\SourceQuery\Exception\InvalidPacketException
|
|
||||||
* @dataProvider BadPacketProvider
|
* @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( "\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11" );
|
||||||
$this->Socket->Queue( $Data );
|
$this->Socket->Queue( $Data );
|
||||||
|
|
||||||
$this->SourceQuery->GetRules();
|
$this->SourceQuery->GetRules();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function BadPacketProvider( )
|
public function BadPacketProvider( ) : array
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
@@ -247,7 +233,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\x41\x11\x11\x11\x11" );
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x45\x01\x00ayy\x00lmao\x00" );
|
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x45\x01\x00ayy\x00lmao\x00" );
|
||||||
@@ -260,7 +246,7 @@
|
|||||||
/**
|
/**
|
||||||
* @dataProvider RulesProvider
|
* @dataProvider RulesProvider
|
||||||
*/
|
*/
|
||||||
public function testGetRules( $RawInput, $ExpectedOutput )
|
public function testGetRules( array $RawInput, array $ExpectedOutput ) : void
|
||||||
{
|
{
|
||||||
$this->Socket->Queue( hex2bin( "ffffffff4104fce20e" ) ); // Challenge
|
$this->Socket->Queue( hex2bin( "ffffffff4104fce20e" ) ); // Challenge
|
||||||
|
|
||||||
@@ -274,7 +260,7 @@
|
|||||||
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function RulesProvider()
|
public function RulesProvider() : array
|
||||||
{
|
{
|
||||||
$DataProvider = [];
|
$DataProvider = [];
|
||||||
|
|
||||||
@@ -295,7 +281,7 @@
|
|||||||
/**
|
/**
|
||||||
* @dataProvider PlayersProvider
|
* @dataProvider PlayersProvider
|
||||||
*/
|
*/
|
||||||
public function testGetPlayers( $RawInput, $ExpectedOutput )
|
public function testGetPlayers( array $RawInput, array $ExpectedOutput ) : void
|
||||||
{
|
{
|
||||||
$this->Socket->Queue( hex2bin( "ffffffff4104fce20e" ) ); // Challenge
|
$this->Socket->Queue( hex2bin( "ffffffff4104fce20e" ) ); // Challenge
|
||||||
|
|
||||||
@@ -309,7 +295,7 @@
|
|||||||
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
$this->assertEquals( $ExpectedOutput, $RealOutput );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function PlayersProvider()
|
public function PlayersProvider() : array
|
||||||
{
|
{
|
||||||
$DataProvider = [];
|
$DataProvider = [];
|
||||||
|
|
||||||
@@ -327,7 +313,7 @@
|
|||||||
return $DataProvider;
|
return $DataProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPing()
|
public function testPing() : void
|
||||||
{
|
{
|
||||||
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x6A\x00");
|
$this->Socket->Queue( "\xFF\xFF\xFF\xFF\x6A\x00");
|
||||||
$this->assertTrue( $this->SourceQuery->Ping() );
|
$this->assertTrue( $this->SourceQuery->Ping() );
|
||||||
|
|||||||
+3
-10
@@ -4,13 +4,6 @@
|
|||||||
"homepage": "https://github.com/xPaw/PHP-Source-Query",
|
"homepage": "https://github.com/xPaw/PHP-Source-Query",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"license": "LGPL-2.1",
|
"license": "LGPL-2.1",
|
||||||
"authors":
|
|
||||||
[
|
|
||||||
{
|
|
||||||
"name": "Pavel Djundik",
|
|
||||||
"email": "sourcequery@xpaw.me"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"keywords":
|
"keywords":
|
||||||
[
|
[
|
||||||
"rcon",
|
"rcon",
|
||||||
@@ -25,12 +18,12 @@
|
|||||||
],
|
],
|
||||||
"require":
|
"require":
|
||||||
{
|
{
|
||||||
"php": ">=7.2"
|
"php": ">=7.4"
|
||||||
},
|
},
|
||||||
"require-dev":
|
"require-dev":
|
||||||
{
|
{
|
||||||
"phpunit/phpunit": "^6.0",
|
"phpunit/phpunit": "9.2",
|
||||||
"php-coveralls/php-coveralls": "^2.1"
|
"vimeo/psalm": "^3.12"
|
||||||
},
|
},
|
||||||
"autoload":
|
"autoload":
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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