1
0
mirror of https://github.com/xPaw/PHP-Source-Query.git synced 2026-05-18 13:43:33 +02:00

15 Commits

Author SHA1 Message Date
Pavel Djundik 48307b5556 Merge pull request #151 from ihasTaco/master 2021-10-29 11:53:54 +03:00
ihasTaco 76edb8b2d7 Update README.md 2021-10-07 00:25:04 -06:00
ihasTaco 8511444465 Update README.md 2021-10-05 04:34:03 -06:00
Pavel Djundik 3bfc073211 Strings 2021-04-16 10:29:21 +03:00
Pavel Djundik 890d98da92 Add phpstan 2021-04-16 10:26:03 +03:00
Pavel Djundik 7a7e6b0d67 Test on php 8 2021-04-16 10:11:18 +03:00
Pavel Djundik 904e547fe4 Assign socket only if it was created
Fixes #149
2021-04-16 10:07:10 +03:00
Pavel Djundik cd3624704e Use Valve's packet names (proto_oob.h) 2020-12-08 11:59:45 +02:00
Pavel Djundik e96807bb24 Correct const for request value (it is still same value) 2020-12-05 12:31:51 +02:00
Pavel Djundik 7f2e4484d5 Update README.md 2020-12-05 12:16:45 +02:00
Pavel Djundik 673e572233 Use challenge straight away if available 2020-12-04 10:20:42 +02:00
Pavel Djundik 7c8e5add77 Support challenges in A2S_INFO 2020-12-04 10:18:46 +02:00
Pavel Djundik fbabd440da Fix variable 2020-12-04 10:14:45 +02:00
Pavel Djundik bbb6c4c23e Revert "Add 1200 byte padding to all requests"
This reverts commit 7231921b0b.
2020-12-04 10:05:37 +02:00
Pavel Djundik 9da781a993 Remove old badges 2020-12-03 13:01:53 +02:00
14 changed files with 127 additions and 125 deletions
+3 -1
View File
@@ -7,12 +7,14 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy: strategy:
matrix: matrix:
php: [7.4] php: ['7.4', '8.0']
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 phpstan
run: php${{ matrix.php }} vendor/bin/phpstan
- name: Run psalm - name: Run psalm
run: php${{ matrix.php }} vendor/bin/psalm run: php${{ matrix.php }} vendor/bin/psalm
+2 -2
View File
@@ -4,8 +4,8 @@
use xPaw\SourceQuery\SourceQuery; use xPaw\SourceQuery\SourceQuery;
// For the sake of this example // For the sake of this example
Header( 'Content-Type: text/plain' ); header( 'Content-Type: text/plain' );
Header( 'X-Content-Type-Options: nosniff' ); header( 'X-Content-Type-Options: nosniff' );
// Edit this -> // Edit this ->
define( 'SQ_SERVER_ADDR', 'localhost' ); define( 'SQ_SERVER_ADDR', 'localhost' );
+2 -2
View File
@@ -4,8 +4,8 @@
use xPaw\SourceQuery\SourceQuery; use xPaw\SourceQuery\SourceQuery;
// For the sake of this example // For the sake of this example
Header( 'Content-Type: text/plain' ); header( 'Content-Type: text/plain' );
Header( 'X-Content-Type-Options: nosniff' ); header( 'X-Content-Type-Options: nosniff' );
// Edit this -> // Edit this ->
define( 'SQ_SERVER_ADDR', 'localhost' ); define( 'SQ_SERVER_ADDR', 'localhost' );
+10 -9
View File
@@ -10,13 +10,14 @@
define( 'SQ_ENGINE', SourceQuery::SOURCE ); define( 'SQ_ENGINE', SourceQuery::SOURCE );
// Edit this <- // Edit this <-
$Timer = MicroTime( true ); $Timer = microtime( true );
$Query = new SourceQuery( ); $Query = new SourceQuery( );
$Info = Array( ); $Info = [];
$Rules = Array( ); $Rules = [];
$Players = Array( ); $Players = [];
$Exception = null;
try try
{ {
@@ -36,7 +37,7 @@
$Query->Disconnect( ); $Query->Disconnect( );
} }
$Timer = Number_Format( MicroTime( true ) - $Timer, 4, '.', '' ); $Timer = number_format( microtime( true ) - $Timer, 4, '.', '' );
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
@@ -87,7 +88,7 @@
</div> </div>
<div class="container"> <div class="container">
<?php if( $Exception !== '' ): ?> <?php if( $Exception !== null ): ?>
<div class="panel panel-error"> <div class="panel panel-error">
<pre class="panel-body"><?php echo htmlspecialchars( $Exception->__toString( ) ); ?></pre> <pre class="panel-body"><?php echo htmlspecialchars( $Exception->__toString( ) ); ?></pre>
</div> </div>
@@ -102,12 +103,12 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php if( Is_Array( $Info ) ): ?> <?php if( !empty( $Info ) ): ?>
<?php foreach( $Info as $InfoKey => $InfoValue ): ?> <?php foreach( $Info as $InfoKey => $InfoValue ): ?>
<tr> <tr>
<td><?php echo htmlspecialchars( $InfoKey ); ?></td> <td><?php echo htmlspecialchars( $InfoKey ); ?></td>
<td><?php <td><?php
if( Is_Array( $InfoValue ) ) if( is_array( $InfoValue ) )
{ {
echo "<pre>"; echo "<pre>";
print_r( $InfoValue ); print_r( $InfoValue );
@@ -175,7 +176,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php if( Is_Array( $Rules ) ): ?> <?php if( !empty( $Rules ) ): ?>
<?php foreach( $Rules as $Rule => $Value ): ?> <?php foreach( $Rules as $Rule => $Value ): ?>
<tr> <tr>
<td><?php echo htmlspecialchars( $Rule ); ?></td> <td><?php echo htmlspecialchars( $Rule ); ?></td>
+3 -4
View File
@@ -1,7 +1,5 @@
# PHP Source Query # PHP Source Query
[![Build Status](https://img.shields.io/travis/com/xPaw/PHP-Source-Query.svg)](https://travis-ci.com/xPaw/PHP-Source-Query)
[![Test Coverage](https://img.shields.io/coveralls/github/xPaw/PHP-Source-Query/master.svg)](https://coveralls.io/github/xPaw/PHP-Source-Query)
[![Packagist Downloads](https://img.shields.io/packagist/dt/xpaw/php-source-query-class.svg)](https://packagist.org/packages/xpaw/php-source-query-class) [![Packagist Downloads](https://img.shields.io/packagist/dt/xpaw/php-source-query-class.svg)](https://packagist.org/packages/xpaw/php-source-query-class)
[![Packagist Version](https://img.shields.io/packagist/v/xpaw/php-source-query-class.svg)](https://packagist.org/packages/xpaw/php-source-query-class) [![Packagist Version](https://img.shields.io/packagist/v/xpaw/php-source-query-class.svg)](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. [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 ## Requirements
* [Modern PHP version](https://php.net/supported-versions.php) (7.4 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 * Your server must allow UDP connections
## Protocol Specifications ## Protocol Specifications
* https://developer.valvesoftware.com/wiki/Server_queries * 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: | 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 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 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: | 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: | 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. 282440 | [Quake Live](http://store.steampowered.com/app/282440) | :white_check_mark: | :x: | Quake Live uses the ZMQ messaging queue protocol for rcon control.
+6 -7
View File
@@ -25,7 +25,7 @@
*/ */
abstract class BaseSocket abstract class BaseSocket
{ {
/** @var resource */ /** @var ?resource */
public $Socket; public $Socket;
public int $Engine; public int $Engine;
@@ -41,7 +41,6 @@
abstract public function Close( ) : void; abstract public function Close( ) : void;
abstract public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void; abstract public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void;
abstract public function Write( int $Header, string $String = '' ) : bool; abstract public function Write( int $Header, string $String = '' ) : bool;
abstract public function WritePadded( int $Header, string $String = '' ) : bool;
abstract public function Read( int $Length = 1400 ) : Buffer; abstract public function Read( int $Length = 1400 ) : Buffer;
protected function ReadInternal( Buffer $Buffer, int $Length, callable $SherlockFunction ) : Buffer protected function ReadInternal( Buffer $Buffer, int $Length, callable $SherlockFunction ) : Buffer
@@ -109,30 +108,30 @@
} }
while( $ReadMore && $SherlockFunction( $Buffer, $Length ) ); while( $ReadMore && $SherlockFunction( $Buffer, $Length ) );
$Data = Implode( $Packets ); $Data = implode( $Packets );
// TODO: Test this // TODO: Test this
if( $IsCompressed ) if( $IsCompressed )
{ {
// Let's make sure this function exists, it's not included in PHP by default // Let's make sure this function exists, it's not included in PHP by default
if( !Function_Exists( 'bzdecompress' ) ) if( !function_exists( 'bzdecompress' ) )
{ {
throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' ); throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' );
} }
$Data = bzdecompress( $Data ); $Data = bzdecompress( $Data );
if( !is_string( $Data ) || CRC32( $Data ) !== $PacketChecksum ) if( !is_string( $Data ) || crc32( $Data ) !== $PacketChecksum )
{ {
throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH ); throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH );
} }
} }
$Buffer->Set( SubStr( $Data, 4 ) ); $Buffer->Set( substr( $Data, 4 ) );
} }
else else
{ {
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . dechex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
} }
return $Buffer; return $Buffer;
+8 -8
View File
@@ -44,7 +44,7 @@
public function Set( string $Buffer ) : void public function Set( string $Buffer ) : void
{ {
$this->Buffer = $Buffer; $this->Buffer = $Buffer;
$this->Length = StrLen( $Buffer ); $this->Length = strlen( $Buffer );
$this->Position = 0; $this->Position = 0;
} }
@@ -81,7 +81,7 @@
return ''; return '';
} }
$Data = SubStr( $this->Buffer, $this->Position, $Length ); $Data = substr( $this->Buffer, $this->Position, $Length );
$this->Position += $Length; $this->Position += $Length;
@@ -93,7 +93,7 @@
*/ */
public function GetByte( ) : int public function GetByte( ) : int
{ {
return Ord( $this->Get( 1 ) ); return ord( $this->Get( 1 ) );
} }
/** /**
@@ -106,7 +106,7 @@
throw new InvalidPacketException( 'Not enough data to unpack a short.', InvalidPacketException::BUFFER_EMPTY ); throw new InvalidPacketException( 'Not enough data to unpack a short.', InvalidPacketException::BUFFER_EMPTY );
} }
$Data = UnPack( 'v', $this->Get( 2 ) ); $Data = unpack( 'v', $this->Get( 2 ) );
return (int)$Data[ 1 ]; return (int)$Data[ 1 ];
} }
@@ -121,7 +121,7 @@
throw new InvalidPacketException( 'Not enough data to unpack a long.', InvalidPacketException::BUFFER_EMPTY ); throw new InvalidPacketException( 'Not enough data to unpack a long.', InvalidPacketException::BUFFER_EMPTY );
} }
$Data = UnPack( 'l', $this->Get( 4 ) ); $Data = unpack( 'l', $this->Get( 4 ) );
return (int)$Data[ 1 ]; return (int)$Data[ 1 ];
} }
@@ -136,7 +136,7 @@
throw new InvalidPacketException( 'Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY ); throw new InvalidPacketException( 'Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY );
} }
$Data = UnPack( 'f', $this->Get( 4 ) ); $Data = unpack( 'f', $this->Get( 4 ) );
return (float)$Data[ 1 ]; return (float)$Data[ 1 ];
} }
@@ -151,7 +151,7 @@
throw new InvalidPacketException( 'Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY ); throw new InvalidPacketException( 'Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY );
} }
$Data = UnPack( 'V', $this->Get( 4 ) ); $Data = unpack( 'V', $this->Get( 4 ) );
return (int)$Data[ 1 ]; return (int)$Data[ 1 ];
} }
@@ -161,7 +161,7 @@
*/ */
public function GetString( ) : string public function GetString( ) : string
{ {
$ZeroBytePosition = StrPos( $this->Buffer, "\0", $this->Position ); $ZeroBytePosition = strpos( $this->Buffer, "\0", $this->Position );
if( $ZeroBytePosition === false ) if( $ZeroBytePosition === false )
{ {
+5 -5
View File
@@ -53,10 +53,10 @@
public function Write( int $Header, string $String = '' ) : bool public function Write( int $Header, string $String = '' ) : bool
{ {
$Command = Pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String ); $Command = pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
$Length = StrLen( $Command ); $Length = strlen( $Command );
return $Length === FWrite( $this->Socket->Socket, $Command, $Length ); return $Length === fwrite( $this->Socket->Socket, $Command, $Length );
} }
/** /**
@@ -89,7 +89,7 @@
//$StringBuffer .= SubStr( $Packet, 0, -2 ); //$StringBuffer .= SubStr( $Packet, 0, -2 );
// Let's assume if this packet is not long enough, there are no more after this one // Let's assume if this packet is not long enough, there are no more after this one
$ReadMore = StrLen( $Packet ) > 1000; // use 1300? $ReadMore = strlen( $Packet ) > 1000; // use 1300?
if( $ReadMore ) if( $ReadMore )
{ {
@@ -140,6 +140,6 @@
throw new AuthenticationException( 'Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD ); throw new AuthenticationException( 'Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD );
} }
$this->RconChallenge = Trim( $Buffer->Get( ) ); $this->RconChallenge = trim( $Buffer->Get( ) );
} }
} }
+9 -28
View File
@@ -29,7 +29,7 @@
{ {
if( $this->Socket !== null ) if( $this->Socket !== null )
{ {
FClose( $this->Socket ); fclose( $this->Socket );
$this->Socket = null; $this->Socket = null;
} }
@@ -42,42 +42,23 @@
$this->Port = $Port; $this->Port = $Port;
$this->Address = $Address; $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 ); throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET );
} }
Stream_Set_Timeout( $this->Socket, $Timeout ); $this->Socket = $Socket;
Stream_Set_Blocking( $this->Socket, true ); stream_set_timeout( $this->Socket, $Timeout );
stream_set_blocking( $this->Socket, true );
} }
public function Write( int $Header, string $String = '' ) : bool public function Write( int $Header, string $String = '' ) : bool
{
$Command = Pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
$Length = StrLen( $Command );
return $Length === FWrite( $this->Socket, $Command, $Length );
}
/**
* Write a request packge to the socket. Pads it up to 1200 bytes to prevent reflective DoS.
*
* @see https://steamcommunity.com/discussions/forum/14/2989789048633291344/
* @return bool Whether fwrite succeeded.
*/
public function WritePadded( 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 );
if( $Length < 1200 )
{
$Command .= str_repeat( "\0", 1200 - $Length );
$Length = 1200;
}
return $Length === fwrite( $this->Socket, $Command, $Length ); return $Length === fwrite( $this->Socket, $Command, $Length );
} }
@@ -91,7 +72,7 @@
public function Read( int $Length = 1400 ) : Buffer public function Read( int $Length = 1400 ) : Buffer
{ {
$Buffer = new Buffer( ); $Buffer = new Buffer( );
$Buffer->Set( FRead( $this->Socket, $Length ) ); $Buffer->Set( fread( $this->Socket, $Length ) );
$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] ); $this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
@@ -100,9 +81,9 @@
public function Sherlock( Buffer $Buffer, int $Length ) : bool public function Sherlock( Buffer $Buffer, int $Length ) : bool
{ {
$Data = FRead( $this->Socket, $Length ); $Data = fread( $this->Socket, $Length );
if( StrLen( $Data ) < 4 ) if( strlen( $Data ) < 4 )
{ {
return false; return false;
} }
+42 -28
View File
@@ -38,7 +38,7 @@
/** /**
* Packets sent * Packets sent
*/ */
const A2S_PING = 0x69; const A2A_PING = 0x69;
const A2S_INFO = 0x54; const A2S_INFO = 0x54;
const A2S_PLAYER = 0x55; const A2S_PLAYER = 0x55;
const A2S_RULES = 0x56; const A2S_RULES = 0x56;
@@ -47,10 +47,10 @@
/** /**
* Packets received * Packets received
*/ */
const S2A_PING = 0x6A; const A2A_ACK = 0x6A;
const S2A_CHALLENGE = 0x41; const S2C_CHALLENGE = 0x41;
const S2A_INFO = 0x49; const S2A_INFO_SRC = 0x49;
const S2A_INFO_OLD = 0x6D; // Old GoldSource, HLTV uses it const S2A_INFO_OLD = 0x6D; // Old GoldSource, HLTV uses it (actually called S2A_INFO_DETAILED)
const S2A_PLAYER = 0x44; const S2A_PLAYER = 0x44;
const S2A_RULES = 0x45; const S2A_RULES = 0x45;
const S2A_RCON = 0x6C; const S2A_RCON = 0x6C;
@@ -58,6 +58,7 @@
/** /**
* Source rcon sent * Source rcon sent
*/ */
const SERVERDATA_REQUESTVALUE = 0;
const SERVERDATA_EXECCOMMAND = 2; const SERVERDATA_EXECCOMMAND = 2;
const SERVERDATA_AUTH = 3; const SERVERDATA_AUTH = 3;
@@ -179,10 +180,10 @@
throw new SocketException( 'Not connected.', SocketException::NOT_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( ); $Buffer = $this->Socket->Read( );
return $Buffer->GetByte( ) === self::S2A_PING; return $Buffer->GetByte( ) === self::A2A_ACK;
} }
/** /**
@@ -200,12 +201,28 @@
throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED ); throw new SocketException( 'Not connected.', SocketException::NOT_CONNECTED );
} }
$this->Socket->WritePadded( self::A2S_INFO, "Source Engine Query\0" ); if( $this->Challenge )
$Buffer = $this->Socket->Read( ); {
$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( ); $Type = $Buffer->GetByte( );
$Server = []; $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 // 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 )
{ {
@@ -223,8 +240,8 @@
$Server[ 'Players' ] = $Buffer->GetByte( ); $Server[ 'Players' ] = $Buffer->GetByte( );
$Server[ 'MaxPlayers' ] = $Buffer->GetByte( ); $Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
$Server[ 'Protocol' ] = $Buffer->GetByte( ); $Server[ 'Protocol' ] = $Buffer->GetByte( );
$Server[ 'Dedicated' ] = Chr( $Buffer->GetByte( ) ); $Server[ 'Dedicated' ] = chr( $Buffer->GetByte( ) );
$Server[ 'Os' ] = Chr( $Buffer->GetByte( ) ); $Server[ 'Os' ] = chr( $Buffer->GetByte( ) );
$Server[ 'Password' ] = $Buffer->GetByte( ) === 1; $Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
$Server[ 'IsMod' ] = $Buffer->GetByte( ) === 1; $Server[ 'IsMod' ] = $Buffer->GetByte( ) === 1;
@@ -247,9 +264,9 @@
return $Server; 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( ); $Server[ 'Protocol' ] = $Buffer->GetByte( );
@@ -261,8 +278,8 @@
$Server[ 'Players' ] = $Buffer->GetByte( ); $Server[ 'Players' ] = $Buffer->GetByte( );
$Server[ 'MaxPlayers' ] = $Buffer->GetByte( ); $Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
$Server[ 'Bots' ] = $Buffer->GetByte( ); $Server[ 'Bots' ] = $Buffer->GetByte( );
$Server[ 'Dedicated' ] = Chr( $Buffer->GetByte( ) ); $Server[ 'Dedicated' ] = chr( $Buffer->GetByte( ) );
$Server[ 'Os' ] = Chr( $Buffer->GetByte( ) ); $Server[ 'Os' ] = chr( $Buffer->GetByte( ) );
$Server[ 'Password' ] = $Buffer->GetByte( ) === 1; $Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
$Server[ 'Secure' ] = $Buffer->GetByte( ) === 1; $Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
@@ -365,7 +382,7 @@
$this->GetChallenge( self::A2S_PLAYER, self::S2A_PLAYER ); $this->GetChallenge( self::A2S_PLAYER, self::S2A_PLAYER );
$this->Socket->WritePadded( self::A2S_PLAYER, $this->Challenge ); $this->Socket->Write( self::A2S_PLAYER, $this->Challenge );
$Buffer = $this->Socket->Read( 14000 ); // Moronic Arma 3 developers do not split their packets, so we have to read more data $Buffer = $this->Socket->Read( 14000 ); // Moronic Arma 3 developers do not split their packets, so we have to read more data
// This violates the protocol spec, and they probably should fix it: https://developer.valvesoftware.com/wiki/Server_queries#Protocol // This violates the protocol spec, and they probably should fix it: https://developer.valvesoftware.com/wiki/Server_queries#Protocol
@@ -373,7 +390,7 @@
if( $Type !== self::S2A_PLAYER ) if( $Type !== self::S2A_PLAYER )
{ {
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
} }
$Players = []; $Players = [];
@@ -386,7 +403,7 @@
$Player[ 'Name' ] = $Buffer->GetString( ); $Player[ 'Name' ] = $Buffer->GetString( );
$Player[ 'Frags' ] = $Buffer->GetLong( ); $Player[ 'Frags' ] = $Buffer->GetLong( );
$Player[ 'Time' ] = (int)$Buffer->GetFloat( ); $Player[ 'Time' ] = (int)$Buffer->GetFloat( );
$Player[ 'TimeF' ] = GMDate( ( $Player[ 'Time' ] > 3600 ? "H:i:s" : "i:s" ), $Player[ 'Time' ] ); $Player[ 'TimeF' ] = gmdate( ( $Player[ 'Time' ] > 3600 ? 'H:i:s' : 'i:s' ), $Player[ 'Time' ] );
$Players[ ] = $Player; $Players[ ] = $Player;
} }
@@ -411,14 +428,14 @@
$this->GetChallenge( self::A2S_RULES, self::S2A_RULES ); $this->GetChallenge( self::A2S_RULES, self::S2A_RULES );
$this->Socket->WritePadded( self::A2S_RULES, $this->Challenge ); $this->Socket->Write( self::A2S_RULES, $this->Challenge );
$Buffer = $this->Socket->Read( ); $Buffer = $this->Socket->Read( );
$Type = $Buffer->GetByte( ); $Type = $Buffer->GetByte( );
if( $Type !== self::S2A_RULES ) if( $Type !== self::S2A_RULES )
{ {
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
} }
$Rules = []; $Rules = [];
@@ -429,7 +446,7 @@
$Rule = $Buffer->GetString( ); $Rule = $Buffer->GetString( );
$Value = $Buffer->GetString( ); $Value = $Buffer->GetString( );
if( !Empty( $Rule ) ) if( !empty( $Rule ) )
{ {
$Rules[ $Rule ] = $Value; $Rules[ $Rule ] = $Value;
} }
@@ -452,20 +469,17 @@
if( $this->UseOldGetChallengeMethod ) if( $this->UseOldGetChallengeMethod )
{ {
$this->Socket->Write( self::A2S_SERVERQUERY_GETCHALLENGE, "\xFF\xFF\xFF\xFF" ); $Header = self::A2S_SERVERQUERY_GETCHALLENGE;
}
else
{
$this->Socket->WritePadded( $Header, "\xFF\xFF\xFF\xFF" );
} }
$this->Socket->Write( $Header, "\xFF\xFF\xFF\xFF" );
$Buffer = $this->Socket->Read( ); $Buffer = $this->Socket->Read( );
$Type = $Buffer->GetByte( ); $Type = $Buffer->GetByte( );
switch( $Type ) switch( $Type )
{ {
case self::S2A_CHALLENGE: case self::S2C_CHALLENGE:
{ {
$this->Challenge = $Buffer->Get( 4 ); $this->Challenge = $Buffer->Get( 4 );
@@ -483,7 +497,7 @@
} }
default: default:
{ {
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . dechex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
} }
} }
} }
+18 -19
View File
@@ -32,7 +32,7 @@
*/ */
private BaseSocket $Socket; private BaseSocket $Socket;
/** @var resource */ /** @var ?resource */
private $RconSocket; private $RconSocket;
private int $RconRequestId = 0; private int $RconRequestId = 0;
@@ -45,7 +45,7 @@
{ {
if( $this->RconSocket ) if( $this->RconSocket )
{ {
FClose( $this->RconSocket ); fclose( $this->RconSocket );
$this->RconSocket = null; $this->RconSocket = null;
} }
@@ -57,34 +57,35 @@
{ {
if( !$this->RconSocket ) 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 ); throw new SocketException( 'Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED );
} }
Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout ); $this->RconSocket = $RconSocket;
Stream_Set_Blocking( $this->RconSocket, true ); stream_set_timeout( $this->RconSocket, $this->Socket->Timeout );
stream_set_blocking( $this->RconSocket, true );
} }
} }
public function Write( int $Header, string $String = '' ) : bool public function Write( int $Header, string $String = '' ) : bool
{ {
// Pack the packet together // Pack the packet together
$Command = Pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00"; $Command = pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
// Prepend packet length // Prepend packet length
$Command = Pack( 'V', StrLen( $Command ) ) . $Command; $Command = pack( 'V', strlen( $Command ) ) . $Command;
$Length = StrLen( $Command ); $Length = strlen( $Command );
return $Length === FWrite( $this->RconSocket, $Command, $Length ); return $Length === fwrite( $this->RconSocket, $Command, $Length );
} }
public function Read( ) : Buffer public function Read( ) : Buffer
{ {
$Buffer = new Buffer( ); $Buffer = new Buffer( );
$Buffer->Set( FRead( $this->RconSocket, 4 ) ); $Buffer->Set( fread( $this->RconSocket, 4 ) );
if( $Buffer->Remaining( ) < 4 ) if( $Buffer->Remaining( ) < 4 )
{ {
@@ -93,23 +94,21 @@
$PacketSize = $Buffer->GetLong( ); $PacketSize = $Buffer->GetLong( );
$Buffer->Set( FRead( $this->RconSocket, $PacketSize ) ); $Buffer->Set( fread( $this->RconSocket, $PacketSize ) );
$Data = $Buffer->Get( ); $Data = $Buffer->Get( );
$Remaining = $PacketSize - StrLen( $Data ); $Remaining = $PacketSize - strlen( $Data );
while( $Remaining > 0 ) while( $Remaining > 0 )
{ {
$Data2 = FRead( $this->RconSocket, $Remaining ); $Data2 = fread( $this->RconSocket, $Remaining );
$PacketSize = StrLen( $Data2 ); $PacketSize = strlen( $Data2 );
if( $PacketSize === 0 ) if( $PacketSize === 0 )
{ {
throw new InvalidPacketException( 'Read ' . strlen( $Data ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY ); throw new InvalidPacketException( 'Read ' . strlen( $Data ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
break;
} }
$Data .= $Data2; $Data .= $Data2;
@@ -143,9 +142,9 @@
// We do this stupid hack to handle split packets // We do this stupid hack to handle split packets
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses // See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
if( StrLen( $Data ) >= 4000 ) if( strlen( $Data ) >= 4000 )
{ {
$this->Write( SourceQuery::SERVERDATA_RESPONSE_VALUE ); $this->Write( SourceQuery::SERVERDATA_REQUESTVALUE );
do do
{ {
+6 -7
View File
@@ -6,10 +6,12 @@
class TestableSocket extends BaseSocket class TestableSocket extends BaseSocket
{ {
/** @var \SplQueue<string> */
private \SplQueue $PacketQueue; private \SplQueue $PacketQueue;
public function __construct( ) public function __construct( )
{ {
/** @var \SplQueue<string> */
$this->PacketQueue = new \SplQueue(); $this->PacketQueue = new \SplQueue();
$this->PacketQueue->setIteratorMode( \SplDoublyLinkedList::IT_MODE_DELETE ); $this->PacketQueue->setIteratorMode( \SplDoublyLinkedList::IT_MODE_DELETE );
@@ -38,15 +40,10 @@
return true; return true;
} }
public function WritePadded( int $Header, string $String = '' ) : bool
{
return true;
}
public function Read( int $Length = 1400 ) : Buffer public function Read( int $Length = 1400 ) : Buffer
{ {
$Buffer = new Buffer( ); $Buffer = new Buffer( );
$Buffer->Set( (string)$this->PacketQueue->shift() ); $Buffer->Set( $this->PacketQueue->shift() );
$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] ); $this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
@@ -60,7 +57,7 @@
return false; return false;
} }
$Buffer->Set( (string)$this->PacketQueue->shift() ); $Buffer->Set( $this->PacketQueue->shift() );
return $Buffer->GetLong( ) === -2; return $Buffer->GetLong( ) === -2;
} }
@@ -250,6 +247,7 @@
/** /**
* @dataProvider RulesProvider * @dataProvider RulesProvider
* @param array<string> $RawInput
*/ */
public function testGetRules( array $RawInput, array $ExpectedOutput ) : void public function testGetRules( array $RawInput, array $ExpectedOutput ) : void
{ {
@@ -285,6 +283,7 @@
/** /**
* @dataProvider PlayersProvider * @dataProvider PlayersProvider
* @param array<string> $RawInput
*/ */
public function testGetPlayers( array $RawInput, array $ExpectedOutput ) : void public function testGetPlayers( array $RawInput, array $ExpectedOutput ) : void
{ {
+3 -3
View File
@@ -22,9 +22,9 @@
}, },
"require-dev": "require-dev":
{ {
"phpunit/phpunit": "9.2", "phpunit/phpunit": "^9.5",
"vimeo/psalm": "^3.12", "vimeo/psalm": "^4.7",
"php-coveralls/php-coveralls": "^2.2" "phpstan/phpstan": "^0.12.83"
}, },
"autoload": "autoload":
{ {
+8
View File
@@ -0,0 +1,8 @@
parameters:
checkMissingIterableValueType: false
checkFunctionNameCase: true
level: 6
paths:
- .
excludes_analyse:
- vendor