diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/Example.php b/Example.php index e08b069..4f51c10 100644 --- a/Example.php +++ b/Example.php @@ -1,30 +1,30 @@ - - define( 'SQ_SERVER_ADDR', 'localhost' ); - define( 'SQ_SERVER_PORT', 27015 ); - define( 'SQ_TIMEOUT', 1 ); - define( 'SQ_ENGINE', SourceQuery :: SOURCE ); - // Edit this <- - - $Query = new SourceQuery( ); - - try - { - $Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE ); - - print_r( $Query->GetInfo( ) ); - print_r( $Query->GetPlayers( ) ); - print_r( $Query->GetRules( ) ); - } - catch( Exception $e ) - { - echo $e->getMessage( ); - } - - $Query->Disconnect( ); + + define( 'SQ_SERVER_ADDR', 'localhost' ); + define( 'SQ_SERVER_PORT', 27015 ); + define( 'SQ_TIMEOUT', 1 ); + define( 'SQ_ENGINE', SourceQuery :: SOURCE ); + // Edit this <- + + $Query = new SourceQuery( ); + + try + { + $Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE ); + + print_r( $Query->GetInfo( ) ); + print_r( $Query->GetPlayers( ) ); + print_r( $Query->GetRules( ) ); + } + catch( Exception $e ) + { + echo $e->getMessage( ); + } + + $Query->Disconnect( ); diff --git a/SourceQuery/Buffer.class.php b/SourceQuery/Buffer.class.php index eee7e43..ac1c3b1 100644 --- a/SourceQuery/Buffer.class.php +++ b/SourceQuery/Buffer.class.php @@ -1,176 +1,176 @@ -Buffer = $Buffer; - $this->Length = StrLen( $Buffer ); - $this->Position = 0; - } - - /** - * Resets buffer - */ - public function Reset( ) - { - $this->Buffer = ""; - $this->Length = 0; - $this->Position = 0; - } - - /** - * Get remaining bytes - * - * @return int Remaining bytes in buffer - */ - public function Remaining( ) - { - return $this->Length - $this->Position; - } - - /** - * Gets data from buffer - * - * @param int $Length Bytes to read - * - * @return string - */ - public function Get( $Length = -1 ) - { - if( $Length === 0 ) - { - return ''; - } - - $Remaining = $this->Remaining( ); - - if( $Length === -1 ) - { - $Length = $Remaining; - } - else if( $Length > $Remaining ) - { - return ''; - } - - $Data = SubStr( $this->Buffer, $this->Position, $Length ); - - $this->Position += $Length; - - return $Data; - } - - /** - * Get byte from buffer - * - * @return int - */ - public function GetByte( ) - { - return Ord( $this->Get( 1 ) ); - } - - /** - * Get short from buffer - * - * @return int - */ - public function GetShort( ) - { - $Data = UnPack( 'v', $this->Get( 2 ) ); - - return $Data[ 1 ]; - } - - /** - * Get long from buffer - * - * @return int - */ - public function GetLong( ) - { - $Data = UnPack( 'l', $this->Get( 4 ) ); - - return $Data[ 1 ]; - } - - /** - * Get float from buffer - * - * @return float - */ - public function GetFloat( ) - { - $Data = UnPack( 'f', $this->Get( 4 ) ); - - return $Data[ 1 ]; - } - - /** - * Get unsigned long from buffer - * - * @return int - */ - public function GetUnsignedLong( ) - { - $Data = UnPack( 'V', $this->Get( 4 ) ); - - return $Data[ 1 ]; - } - - /** - * Read one string from buffer ending with null byte - * - * @return string - */ - public function GetString( ) - { - $ZeroBytePosition = StrPos( $this->Buffer, "\0", $this->Position ); - - if( $ZeroBytePosition === false ) - { - $String = ""; - } - else - { - $String = $this->Get( $ZeroBytePosition - $this->Position ); - - $this->Position++; - } - - return $String; - } - } +Buffer = $Buffer; + $this->Length = StrLen( $Buffer ); + $this->Position = 0; + } + + /** + * Resets buffer + */ + public function Reset( ) + { + $this->Buffer = ""; + $this->Length = 0; + $this->Position = 0; + } + + /** + * Get remaining bytes + * + * @return int Remaining bytes in buffer + */ + public function Remaining( ) + { + return $this->Length - $this->Position; + } + + /** + * Gets data from buffer + * + * @param int $Length Bytes to read + * + * @return string + */ + public function Get( $Length = -1 ) + { + if( $Length === 0 ) + { + return ''; + } + + $Remaining = $this->Remaining( ); + + if( $Length === -1 ) + { + $Length = $Remaining; + } + else if( $Length > $Remaining ) + { + return ''; + } + + $Data = SubStr( $this->Buffer, $this->Position, $Length ); + + $this->Position += $Length; + + return $Data; + } + + /** + * Get byte from buffer + * + * @return int + */ + public function GetByte( ) + { + return Ord( $this->Get( 1 ) ); + } + + /** + * Get short from buffer + * + * @return int + */ + public function GetShort( ) + { + $Data = UnPack( 'v', $this->Get( 2 ) ); + + return $Data[ 1 ]; + } + + /** + * Get long from buffer + * + * @return int + */ + public function GetLong( ) + { + $Data = UnPack( 'l', $this->Get( 4 ) ); + + return $Data[ 1 ]; + } + + /** + * Get float from buffer + * + * @return float + */ + public function GetFloat( ) + { + $Data = UnPack( 'f', $this->Get( 4 ) ); + + return $Data[ 1 ]; + } + + /** + * Get unsigned long from buffer + * + * @return int + */ + public function GetUnsignedLong( ) + { + $Data = UnPack( 'V', $this->Get( 4 ) ); + + return $Data[ 1 ]; + } + + /** + * Read one string from buffer ending with null byte + * + * @return string + */ + public function GetString( ) + { + $ZeroBytePosition = StrPos( $this->Buffer, "\0", $this->Position ); + + if( $ZeroBytePosition === false ) + { + $String = ""; + } + else + { + $String = $this->Get( $ZeroBytePosition - $this->Position ); + + $this->Position++; + } + + return $String; + } + } diff --git a/SourceQuery/Exceptions.class.php b/SourceQuery/Exceptions.class.php index f9b3c29..a9d4377 100644 --- a/SourceQuery/Exceptions.class.php +++ b/SourceQuery/Exceptions.class.php @@ -2,7 +2,7 @@ /** * Class written by xPaw * - * Website: http://xpaw.me + * Website: https://xpaw.me * GitHub: https://github.com/xPaw/PHP-Source-Query-Class */ diff --git a/SourceQuery/GoldSourceRcon.class.php b/SourceQuery/GoldSourceRcon.class.php index bc559d4..4fa0ed3 100644 --- a/SourceQuery/GoldSourceRcon.class.php +++ b/SourceQuery/GoldSourceRcon.class.php @@ -2,10 +2,10 @@ /** * Class written by xPaw * - * Website: http://xpaw.me + * Website: https://xpaw.me * GitHub: https://github.com/xPaw/PHP-Source-Query-Class */ - + use xPaw\SourceQuery\Exception\AuthenticationException; class SourceQueryGoldSourceRcon @@ -53,7 +53,7 @@ return $Length === FWrite( $this->Socket->Socket, $Command, $Length ); } - + /** * @param int $Length * @throws AuthenticationException diff --git a/SourceQuery/Socket.class.php b/SourceQuery/Socket.class.php index 3e3113d..7cb361a 100644 --- a/SourceQuery/Socket.class.php +++ b/SourceQuery/Socket.class.php @@ -1,174 +1,174 @@ -Buffer = $Buffer; - } - - public function Close( ) - { - if( $this->Socket ) - { - FClose( $this->Socket ); - - $this->Socket = null; - } - } - - public function Open( $Ip, $Port, $Timeout, $Engine ) - { - $this->Timeout = $Timeout; - $this->Engine = $Engine; - $this->Port = $Port; - $this->Ip = $Ip; - - $this->Socket = @FSockOpen( 'udp://' . $Ip, $Port, $ErrNo, $ErrStr, $Timeout ); - - if( $ErrNo || $this->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 ); - - return true; - } - - public function Write( $Header, $String = '' ) - { - $Command = Pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String ); - $Length = StrLen( $Command ); - - return $Length === FWrite( $this->Socket, $Command, $Length ); - } - - public function Read( $Length = 1400 ) - { - $this->Buffer->Set( FRead( $this->Socket, $Length ) ); - - if( $this->Buffer->Remaining( ) === 0 ) - { - // TODO: Should we throw an exception here? - return; - } - - $Header = $this->Buffer->GetLong( ); - - if( $Header === -1 ) // Single packet - { - // We don't have to do anything - } - else if( $Header === -2 ) // Split packet - { - $Packets = Array( ); - $IsCompressed = false; - $ReadMore = false; - - do - { - $RequestID = $this->Buffer->GetLong( ); - - switch( $this->Engine ) - { - case SourceQuery :: GOLDSOURCE: - { - $PacketCountAndNumber = $this->Buffer->GetByte( ); - $PacketCount = $PacketCountAndNumber & 0xF; - $PacketNumber = $PacketCountAndNumber >> 4; - - break; - } - case SourceQuery :: SOURCE: - { - $IsCompressed = ( $RequestID & 0x80000000 ) !== 0; - $PacketCount = $this->Buffer->GetByte( ); - $PacketNumber = $this->Buffer->GetByte( ) + 1; - - if( $IsCompressed ) - { - $this->Buffer->GetLong( ); // Split size - - $PacketChecksum = $this->Buffer->GetUnsignedLong( ); - } - else - { - $this->Buffer->GetShort( ); // Split size - } - - break; - } - } - - $Packets[ $PacketNumber ] = $this->Buffer->Get( ); - - $ReadMore = $PacketCount > sizeof( $Packets ); - } - while( $ReadMore && $this->Sherlock( $Length ) ); - - $Buffer = Implode( $Packets ); - - // TODO: Test this - if( $IsCompressed ) - { - // Let's make sure this function exists, it's not included in PHP by default - if( !Function_Exists( 'bzdecompress' ) ) - { - throw new RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' ); - } - - $Buffer = bzdecompress( $Buffer ); - - if( CRC32( $Buffer ) !== $PacketChecksum ) - { - throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH ); - } - } - - $this->Buffer->Set( SubStr( $Buffer, 4 ) ); - } - else - { - throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); - } - } - - private function Sherlock( $Length ) - { - $Data = FRead( $this->Socket, $Length ); - - if( StrLen( $Data ) < 4 ) - { - return false; - } - - $this->Buffer->Set( $Data ); - - return $this->Buffer->GetLong( ) === -2; - } - } +Buffer = $Buffer; + } + + public function Close( ) + { + if( $this->Socket ) + { + FClose( $this->Socket ); + + $this->Socket = null; + } + } + + public function Open( $Ip, $Port, $Timeout, $Engine ) + { + $this->Timeout = $Timeout; + $this->Engine = $Engine; + $this->Port = $Port; + $this->Ip = $Ip; + + $this->Socket = @FSockOpen( 'udp://' . $Ip, $Port, $ErrNo, $ErrStr, $Timeout ); + + if( $ErrNo || $this->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 ); + + return true; + } + + public function Write( $Header, $String = '' ) + { + $Command = Pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String ); + $Length = StrLen( $Command ); + + return $Length === FWrite( $this->Socket, $Command, $Length ); + } + + public function Read( $Length = 1400 ) + { + $this->Buffer->Set( FRead( $this->Socket, $Length ) ); + + if( $this->Buffer->Remaining( ) === 0 ) + { + // TODO: Should we throw an exception here? + return; + } + + $Header = $this->Buffer->GetLong( ); + + if( $Header === -1 ) // Single packet + { + // We don't have to do anything + } + else if( $Header === -2 ) // Split packet + { + $Packets = Array( ); + $IsCompressed = false; + $ReadMore = false; + + do + { + $RequestID = $this->Buffer->GetLong( ); + + switch( $this->Engine ) + { + case SourceQuery :: GOLDSOURCE: + { + $PacketCountAndNumber = $this->Buffer->GetByte( ); + $PacketCount = $PacketCountAndNumber & 0xF; + $PacketNumber = $PacketCountAndNumber >> 4; + + break; + } + case SourceQuery :: SOURCE: + { + $IsCompressed = ( $RequestID & 0x80000000 ) !== 0; + $PacketCount = $this->Buffer->GetByte( ); + $PacketNumber = $this->Buffer->GetByte( ) + 1; + + if( $IsCompressed ) + { + $this->Buffer->GetLong( ); // Split size + + $PacketChecksum = $this->Buffer->GetUnsignedLong( ); + } + else + { + $this->Buffer->GetShort( ); // Split size + } + + break; + } + } + + $Packets[ $PacketNumber ] = $this->Buffer->Get( ); + + $ReadMore = $PacketCount > sizeof( $Packets ); + } + while( $ReadMore && $this->Sherlock( $Length ) ); + + $Buffer = Implode( $Packets ); + + // TODO: Test this + if( $IsCompressed ) + { + // Let's make sure this function exists, it's not included in PHP by default + if( !Function_Exists( 'bzdecompress' ) ) + { + throw new RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' ); + } + + $Buffer = bzdecompress( $Buffer ); + + if( CRC32( $Buffer ) !== $PacketChecksum ) + { + throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH ); + } + } + + $this->Buffer->Set( SubStr( $Buffer, 4 ) ); + } + else + { + throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); + } + } + + private function Sherlock( $Length ) + { + $Data = FRead( $this->Socket, $Length ); + + if( StrLen( $Data ) < 4 ) + { + return false; + } + + $this->Buffer->Set( $Data ); + + return $this->Buffer->GetLong( ) === -2; + } + } diff --git a/SourceQuery/SourceQuery.class.php b/SourceQuery/SourceQuery.class.php index ff402e5..1fc2bab 100644 --- a/SourceQuery/SourceQuery.class.php +++ b/SourceQuery/SourceQuery.class.php @@ -1,566 +1,566 @@ -Buffer = new SourceQueryBuffer( ); - $this->Socket = new SourceQuerySocket( $this->Buffer ); - } - - public function __destruct( ) - { - $this->Disconnect( ); - } - - /** - * Opens connection to server - * - * @param string $Ip Server ip - * @param int $Port Server port - * @param int $Timeout Timeout period - * @param int $Engine Engine the server runs on (goldsource, source) - * - * @throws InvalidArgumentException - * @throws TimeoutException - */ - public function Connect( $Ip, $Port, $Timeout = 3, $Engine = self :: SOURCE ) - { - $this->Disconnect( ); - - if( !is_int( $Timeout ) || $Timeout < 0 ) - { - throw new InvalidArgumentException( 'Timeout must be an integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER ); - } - - if( !$this->Socket->Open( $Ip, (int)$Port, $Timeout, (int)$Engine ) ) - { - throw new TimeoutException( 'Could not connect to server.', TimeoutException::TIMEOUT_CONNECT ); - } - - $this->Connected = true; - } - - /** - * Forces GetChallenge to use old method for challenge retrieval because some games use outdated protocol (e.g Starbound) - * - * @param bool $Value Set to true to force old method - * - * @returns bool Previous value - */ - public function SetUseOldGetChallengeMethod( $Value ) - { - $Previous = $this->UseOldGetChallengeMethod; - - $this->UseOldGetChallengeMethod = $Value === true; - - return $Previous; - } - - /** - * Closes all open connections - */ - public function Disconnect( ) - { - $this->Connected = false; - $this->Challenge = 0; - - $this->Buffer->Reset( ); - - $this->Socket->Close( ); - - if( $this->Rcon ) - { - $this->Rcon->Close( ); - - $this->Rcon = null; - } - } - - /** - * Sends ping packet to the server - * NOTE: This may not work on some games (TF2 for example) - * - * @return bool True on success, false on failure - */ - public function Ping( ) - { - if( !$this->Connected ) - { - return false; - } - - $this->Socket->Write( self :: A2S_PING ); - $this->Socket->Read( ); - - return $this->Buffer->GetByte( ) === self :: S2A_PING; - } - - /** - * Get server information - * - * @throws InvalidPacketException - * - * @return bool|array Returns array with information on success, false on failure - */ - public function GetInfo( ) - { - if( !$this->Connected ) - { - return false; - } - - $this->Socket->Write( self :: A2S_INFO, "Source Engine Query\0" ); - $this->Socket->Read( ); - - $Type = $this->Buffer->GetByte( ); - - if( $Type === 0 ) - { - return false; - } - - // Old GoldSource protocol, HLTV still uses it - if( $Type === self :: S2A_INFO_OLD && $this->Socket->Engine === self :: GOLDSOURCE ) - { - /** - * If we try to read data again, and we get the result with type S2A_INFO (0x49) - * That means this server is running dproto, - * Because it sends answer for both protocols - */ - - $Server[ 'Address' ] = $this->Buffer->GetString( ); - $Server[ 'HostName' ] = $this->Buffer->GetString( ); - $Server[ 'Map' ] = $this->Buffer->GetString( ); - $Server[ 'ModDir' ] = $this->Buffer->GetString( ); - $Server[ 'ModDesc' ] = $this->Buffer->GetString( ); - $Server[ 'Players' ] = $this->Buffer->GetByte( ); - $Server[ 'MaxPlayers' ] = $this->Buffer->GetByte( ); - $Server[ 'Protocol' ] = $this->Buffer->GetByte( ); - $Server[ 'Dedicated' ] = Chr( $this->Buffer->GetByte( ) ); - $Server[ 'Os' ] = Chr( $this->Buffer->GetByte( ) ); - $Server[ 'Password' ] = $this->Buffer->GetByte( ) === 1; - $Server[ 'IsMod' ] = $this->Buffer->GetByte( ) === 1; - - if( $Server[ 'IsMod' ] ) - { - $Mod[ 'Url' ] = $this->Buffer->GetString( ); - $Mod[ 'Download' ] = $this->Buffer->GetString( ); - $this->Buffer->Get( 1 ); // NULL byte - $Mod[ 'Version' ] = $this->Buffer->GetLong( ); - $Mod[ 'Size' ] = $this->Buffer->GetLong( ); - $Mod[ 'ServerSide' ] = $this->Buffer->GetByte( ) === 1; - $Mod[ 'CustomDLL' ] = $this->Buffer->GetByte( ) === 1; - } - - $Server[ 'Secure' ] = $this->Buffer->GetByte( ) === 1; - $Server[ 'Bots' ] = $this->Buffer->GetByte( ); - - if( isset( $Mod ) ) - { - $Server[ 'Mod' ] = $Mod; - } - - return $Server; - } - - if( $Type !== self :: S2A_INFO ) - { - throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); - } - - $Server[ 'Protocol' ] = $this->Buffer->GetByte( ); - $Server[ 'HostName' ] = $this->Buffer->GetString( ); - $Server[ 'Map' ] = $this->Buffer->GetString( ); - $Server[ 'ModDir' ] = $this->Buffer->GetString( ); - $Server[ 'ModDesc' ] = $this->Buffer->GetString( ); - $Server[ 'AppID' ] = $this->Buffer->GetShort( ); - $Server[ 'Players' ] = $this->Buffer->GetByte( ); - $Server[ 'MaxPlayers' ] = $this->Buffer->GetByte( ); - $Server[ 'Bots' ] = $this->Buffer->GetByte( ); - $Server[ 'Dedicated' ] = Chr( $this->Buffer->GetByte( ) ); - $Server[ 'Os' ] = Chr( $this->Buffer->GetByte( ) ); - $Server[ 'Password' ] = $this->Buffer->GetByte( ) === 1; - $Server[ 'Secure' ] = $this->Buffer->GetByte( ) === 1; - - // The Ship (they violate query protocol spec by modifying the response) - if( $Server[ 'AppID' ] === 2400 ) - { - $Server[ 'GameMode' ] = $this->Buffer->GetByte( ); - $Server[ 'WitnessCount' ] = $this->Buffer->GetByte( ); - $Server[ 'WitnessTime' ] = $this->Buffer->GetByte( ); - } - - $Server[ 'Version' ] = $this->Buffer->GetString( ); - - // Extra Data Flags - if( $this->Buffer->Remaining( ) > 0 ) - { - $Server[ 'ExtraDataFlags' ] = $Flags = $this->Buffer->GetByte( ); - - // The server's game port - if( $Flags & 0x80 ) - { - $Server[ 'GamePort' ] = $this->Buffer->GetShort( ); - } - - // The server's SteamID - does this serve any purpose? - if( $Flags & 0x10 ) - { - $Server[ 'ServerID' ] = $this->Buffer->GetUnsignedLong( ) | ( $this->Buffer->GetUnsignedLong( ) << 32 ); // TODO: verify this - } - - // The spectator port and then the spectator server name - if( $Flags & 0x40 ) - { - $Server[ 'SpecPort' ] = $this->Buffer->GetShort( ); - $Server[ 'SpecName' ] = $this->Buffer->GetString( ); - } - - // The game tag data string for the server - if( $Flags & 0x20 ) - { - $Server[ 'GameTags' ] = $this->Buffer->GetString( ); - } - - // GameID -- alternative to AppID? - if( $Flags & 0x01 ) - { - $Server[ 'GameID' ] = $this->Buffer->GetUnsignedLong( ) | ( $this->Buffer->GetUnsignedLong( ) << 32 ); - } - - if( $this->Buffer->Remaining( ) > 0 ) - { - throw new InvalidPacketException( 'GetInfo: unread data? ' . $this->Buffer->Remaining( ) . ' bytes remaining in the buffer. Please report it to the library developer.', - InvalidPacketException::BUFFER_NOT_EMPTY ); - } - } - - return $Server; - } - - /** - * Get players on the server - * - * @throws InvalidPacketException - * - * @return bool|array Returns array with players on success, false on failure - */ - public function GetPlayers( ) - { - if( !$this->Connected ) - { - return false; - } - - switch( $this->GetChallenge( self :: A2S_PLAYER, self :: S2A_PLAYER ) ) - { - case self :: GETCHALLENGE_FAILED: - { - return false; - } - case self :: GETCHALLENGE_ALL_CLEAR: - { - $this->Socket->Write( self :: A2S_PLAYER, $this->Challenge ); - $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 - - $Type = $this->Buffer->GetByte( ); - - if( $Type === 0 ) - { - return false; - } - else if( $Type !== self :: S2A_PLAYER ) - { - throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); - } - - break; - } - } - - $Players = Array( ); - $Count = $this->Buffer->GetByte( ); - - while( $Count-- > 0 && $this->Buffer->Remaining( ) > 0 ) - { - $Player[ 'Id' ] = $this->Buffer->GetByte( ); // PlayerID, is it just always 0? - $Player[ 'Name' ] = $this->Buffer->GetString( ); - $Player[ 'Frags' ] = $this->Buffer->GetLong( ); - $Player[ 'Time' ] = (int)$this->Buffer->GetFloat( ); - $Player[ 'TimeF' ] = GMDate( ( $Player[ 'Time' ] > 3600 ? "H:i:s" : "i:s" ), $Player[ 'Time' ] ); - - $Players[ ] = $Player; - } - - return $Players; - } - - /** - * Get rules (cvars) from the server - * - * @throws InvalidPacketException - * - * @return bool|array Returns array with rules on success, false on failure - */ - public function GetRules( ) - { - if( !$this->Connected ) - { - return false; - } - - switch( $this->GetChallenge( self :: A2S_RULES, self :: S2A_RULES ) ) - { - case self :: GETCHALLENGE_FAILED: - { - return false; - } - case self :: GETCHALLENGE_ALL_CLEAR: - { - $this->Socket->Write( self :: A2S_RULES, $this->Challenge ); - $this->Socket->Read( ); - - $Type = $this->Buffer->GetByte( ); - - if( $Type === 0 ) - { - return false; - } - else if( $Type !== self :: S2A_RULES ) - { - throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); - } - - break; - } - } - - $Rules = Array( ); - $Count = $this->Buffer->GetShort( ); - - while( $Count-- > 0 && $this->Buffer->Remaining( ) > 0 ) - { - $Rule = $this->Buffer->GetString( ); - $Value = $this->Buffer->GetString( ); - - if( !Empty( $Rule ) ) - { - $Rules[ $Rule ] = $Value; - } - } - - return $Rules; - } - - /** - * Get challenge (used for players/rules packets) - * - * @param $Header - * @param $ExpectedResult - * @throws InvalidPacketException - * @return bool True if all went well, false if server uses old GoldSource protocol, and it already contains answer - */ - private function GetChallenge( $Header, $ExpectedResult ) - { - if( $this->Challenge ) - { - return self :: GETCHALLENGE_ALL_CLEAR; - } - - if( $this->UseOldGetChallengeMethod ) - { - $Header = self :: A2S_SERVERQUERY_GETCHALLENGE; - } - - $this->Socket->Write( $Header, 0xFFFFFFFF ); - $this->Socket->Read( ); - - $Type = $this->Buffer->GetByte( ); - - switch( $Type ) - { - case self :: S2A_CHALLENGE: - { - $this->Challenge = $this->Buffer->Get( 4 ); - - return self :: GETCHALLENGE_ALL_CLEAR; - } - case $ExpectedResult: - { - // Goldsource (HLTV) - - return self :: GETCHALLENGE_CONTAINS_ANSWER; - } - case 0: - { - return self :: GETCHALLENGE_FAILED; - } - default: - { - throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); - } - } - } - - /** - * Sets rcon password, for future use in Rcon() - * - * @param string $Password Rcon Password - * - * @return bool True on success, false on failure - */ - public function SetRconPassword( $Password ) - { - if( !$this->Connected ) - { - return false; - } - - switch( $this->Socket->Engine ) - { - case SourceQuery :: GOLDSOURCE: - { - $this->Rcon = new SourceQueryGoldSourceRcon( $this->Buffer, $this->Socket ); - - break; - } - case SourceQuery :: SOURCE: - { - $this->Rcon = new SourceQuerySourceRcon( $this->Buffer, $this->Socket ); - - break; - } - } - - $this->Rcon->Open( ); - - return $this->Rcon->Authorize( $Password ); - } - - /** - * Sends a command to the server for execution. - * - * @param string $Command Command to execute - * - * @return string|bool Answer from server in string, false on failure - */ - public function Rcon( $Command ) - { - if( !$this->Connected ) - { - return false; - } - - return $this->Rcon->Command( $Command ); - } - } +Buffer = new SourceQueryBuffer( ); + $this->Socket = new SourceQuerySocket( $this->Buffer ); + } + + public function __destruct( ) + { + $this->Disconnect( ); + } + + /** + * Opens connection to server + * + * @param string $Ip Server ip + * @param int $Port Server port + * @param int $Timeout Timeout period + * @param int $Engine Engine the server runs on (goldsource, source) + * + * @throws InvalidArgumentException + * @throws TimeoutException + */ + public function Connect( $Ip, $Port, $Timeout = 3, $Engine = self :: SOURCE ) + { + $this->Disconnect( ); + + if( !is_int( $Timeout ) || $Timeout < 0 ) + { + throw new InvalidArgumentException( 'Timeout must be an integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER ); + } + + if( !$this->Socket->Open( $Ip, (int)$Port, $Timeout, (int)$Engine ) ) + { + throw new TimeoutException( 'Could not connect to server.', TimeoutException::TIMEOUT_CONNECT ); + } + + $this->Connected = true; + } + + /** + * Forces GetChallenge to use old method for challenge retrieval because some games use outdated protocol (e.g Starbound) + * + * @param bool $Value Set to true to force old method + * + * @returns bool Previous value + */ + public function SetUseOldGetChallengeMethod( $Value ) + { + $Previous = $this->UseOldGetChallengeMethod; + + $this->UseOldGetChallengeMethod = $Value === true; + + return $Previous; + } + + /** + * Closes all open connections + */ + public function Disconnect( ) + { + $this->Connected = false; + $this->Challenge = 0; + + $this->Buffer->Reset( ); + + $this->Socket->Close( ); + + if( $this->Rcon ) + { + $this->Rcon->Close( ); + + $this->Rcon = null; + } + } + + /** + * Sends ping packet to the server + * NOTE: This may not work on some games (TF2 for example) + * + * @return bool True on success, false on failure + */ + public function Ping( ) + { + if( !$this->Connected ) + { + return false; + } + + $this->Socket->Write( self :: A2S_PING ); + $this->Socket->Read( ); + + return $this->Buffer->GetByte( ) === self :: S2A_PING; + } + + /** + * Get server information + * + * @throws InvalidPacketException + * + * @return bool|array Returns array with information on success, false on failure + */ + public function GetInfo( ) + { + if( !$this->Connected ) + { + return false; + } + + $this->Socket->Write( self :: A2S_INFO, "Source Engine Query\0" ); + $this->Socket->Read( ); + + $Type = $this->Buffer->GetByte( ); + + if( $Type === 0 ) + { + return false; + } + + // Old GoldSource protocol, HLTV still uses it + if( $Type === self :: S2A_INFO_OLD && $this->Socket->Engine === self :: GOLDSOURCE ) + { + /** + * If we try to read data again, and we get the result with type S2A_INFO (0x49) + * That means this server is running dproto, + * Because it sends answer for both protocols + */ + + $Server[ 'Address' ] = $this->Buffer->GetString( ); + $Server[ 'HostName' ] = $this->Buffer->GetString( ); + $Server[ 'Map' ] = $this->Buffer->GetString( ); + $Server[ 'ModDir' ] = $this->Buffer->GetString( ); + $Server[ 'ModDesc' ] = $this->Buffer->GetString( ); + $Server[ 'Players' ] = $this->Buffer->GetByte( ); + $Server[ 'MaxPlayers' ] = $this->Buffer->GetByte( ); + $Server[ 'Protocol' ] = $this->Buffer->GetByte( ); + $Server[ 'Dedicated' ] = Chr( $this->Buffer->GetByte( ) ); + $Server[ 'Os' ] = Chr( $this->Buffer->GetByte( ) ); + $Server[ 'Password' ] = $this->Buffer->GetByte( ) === 1; + $Server[ 'IsMod' ] = $this->Buffer->GetByte( ) === 1; + + if( $Server[ 'IsMod' ] ) + { + $Mod[ 'Url' ] = $this->Buffer->GetString( ); + $Mod[ 'Download' ] = $this->Buffer->GetString( ); + $this->Buffer->Get( 1 ); // NULL byte + $Mod[ 'Version' ] = $this->Buffer->GetLong( ); + $Mod[ 'Size' ] = $this->Buffer->GetLong( ); + $Mod[ 'ServerSide' ] = $this->Buffer->GetByte( ) === 1; + $Mod[ 'CustomDLL' ] = $this->Buffer->GetByte( ) === 1; + } + + $Server[ 'Secure' ] = $this->Buffer->GetByte( ) === 1; + $Server[ 'Bots' ] = $this->Buffer->GetByte( ); + + if( isset( $Mod ) ) + { + $Server[ 'Mod' ] = $Mod; + } + + return $Server; + } + + if( $Type !== self :: S2A_INFO ) + { + throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); + } + + $Server[ 'Protocol' ] = $this->Buffer->GetByte( ); + $Server[ 'HostName' ] = $this->Buffer->GetString( ); + $Server[ 'Map' ] = $this->Buffer->GetString( ); + $Server[ 'ModDir' ] = $this->Buffer->GetString( ); + $Server[ 'ModDesc' ] = $this->Buffer->GetString( ); + $Server[ 'AppID' ] = $this->Buffer->GetShort( ); + $Server[ 'Players' ] = $this->Buffer->GetByte( ); + $Server[ 'MaxPlayers' ] = $this->Buffer->GetByte( ); + $Server[ 'Bots' ] = $this->Buffer->GetByte( ); + $Server[ 'Dedicated' ] = Chr( $this->Buffer->GetByte( ) ); + $Server[ 'Os' ] = Chr( $this->Buffer->GetByte( ) ); + $Server[ 'Password' ] = $this->Buffer->GetByte( ) === 1; + $Server[ 'Secure' ] = $this->Buffer->GetByte( ) === 1; + + // The Ship (they violate query protocol spec by modifying the response) + if( $Server[ 'AppID' ] === 2400 ) + { + $Server[ 'GameMode' ] = $this->Buffer->GetByte( ); + $Server[ 'WitnessCount' ] = $this->Buffer->GetByte( ); + $Server[ 'WitnessTime' ] = $this->Buffer->GetByte( ); + } + + $Server[ 'Version' ] = $this->Buffer->GetString( ); + + // Extra Data Flags + if( $this->Buffer->Remaining( ) > 0 ) + { + $Server[ 'ExtraDataFlags' ] = $Flags = $this->Buffer->GetByte( ); + + // The server's game port + if( $Flags & 0x80 ) + { + $Server[ 'GamePort' ] = $this->Buffer->GetShort( ); + } + + // The server's SteamID - does this serve any purpose? + if( $Flags & 0x10 ) + { + $Server[ 'ServerID' ] = $this->Buffer->GetUnsignedLong( ) | ( $this->Buffer->GetUnsignedLong( ) << 32 ); // TODO: verify this + } + + // The spectator port and then the spectator server name + if( $Flags & 0x40 ) + { + $Server[ 'SpecPort' ] = $this->Buffer->GetShort( ); + $Server[ 'SpecName' ] = $this->Buffer->GetString( ); + } + + // The game tag data string for the server + if( $Flags & 0x20 ) + { + $Server[ 'GameTags' ] = $this->Buffer->GetString( ); + } + + // GameID -- alternative to AppID? + if( $Flags & 0x01 ) + { + $Server[ 'GameID' ] = $this->Buffer->GetUnsignedLong( ) | ( $this->Buffer->GetUnsignedLong( ) << 32 ); + } + + if( $this->Buffer->Remaining( ) > 0 ) + { + throw new InvalidPacketException( 'GetInfo: unread data? ' . $this->Buffer->Remaining( ) . ' bytes remaining in the buffer. Please report it to the library developer.', + InvalidPacketException::BUFFER_NOT_EMPTY ); + } + } + + return $Server; + } + + /** + * Get players on the server + * + * @throws InvalidPacketException + * + * @return bool|array Returns array with players on success, false on failure + */ + public function GetPlayers( ) + { + if( !$this->Connected ) + { + return false; + } + + switch( $this->GetChallenge( self :: A2S_PLAYER, self :: S2A_PLAYER ) ) + { + case self :: GETCHALLENGE_FAILED: + { + return false; + } + case self :: GETCHALLENGE_ALL_CLEAR: + { + $this->Socket->Write( self :: A2S_PLAYER, $this->Challenge ); + $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 + + $Type = $this->Buffer->GetByte( ); + + if( $Type === 0 ) + { + return false; + } + else if( $Type !== self :: S2A_PLAYER ) + { + throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); + } + + break; + } + } + + $Players = Array( ); + $Count = $this->Buffer->GetByte( ); + + while( $Count-- > 0 && $this->Buffer->Remaining( ) > 0 ) + { + $Player[ 'Id' ] = $this->Buffer->GetByte( ); // PlayerID, is it just always 0? + $Player[ 'Name' ] = $this->Buffer->GetString( ); + $Player[ 'Frags' ] = $this->Buffer->GetLong( ); + $Player[ 'Time' ] = (int)$this->Buffer->GetFloat( ); + $Player[ 'TimeF' ] = GMDate( ( $Player[ 'Time' ] > 3600 ? "H:i:s" : "i:s" ), $Player[ 'Time' ] ); + + $Players[ ] = $Player; + } + + return $Players; + } + + /** + * Get rules (cvars) from the server + * + * @throws InvalidPacketException + * + * @return bool|array Returns array with rules on success, false on failure + */ + public function GetRules( ) + { + if( !$this->Connected ) + { + return false; + } + + switch( $this->GetChallenge( self :: A2S_RULES, self :: S2A_RULES ) ) + { + case self :: GETCHALLENGE_FAILED: + { + return false; + } + case self :: GETCHALLENGE_ALL_CLEAR: + { + $this->Socket->Write( self :: A2S_RULES, $this->Challenge ); + $this->Socket->Read( ); + + $Type = $this->Buffer->GetByte( ); + + if( $Type === 0 ) + { + return false; + } + else if( $Type !== self :: S2A_RULES ) + { + throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); + } + + break; + } + } + + $Rules = Array( ); + $Count = $this->Buffer->GetShort( ); + + while( $Count-- > 0 && $this->Buffer->Remaining( ) > 0 ) + { + $Rule = $this->Buffer->GetString( ); + $Value = $this->Buffer->GetString( ); + + if( !Empty( $Rule ) ) + { + $Rules[ $Rule ] = $Value; + } + } + + return $Rules; + } + + /** + * Get challenge (used for players/rules packets) + * + * @param $Header + * @param $ExpectedResult + * @throws InvalidPacketException + * @return bool True if all went well, false if server uses old GoldSource protocol, and it already contains answer + */ + private function GetChallenge( $Header, $ExpectedResult ) + { + if( $this->Challenge ) + { + return self :: GETCHALLENGE_ALL_CLEAR; + } + + if( $this->UseOldGetChallengeMethod ) + { + $Header = self :: A2S_SERVERQUERY_GETCHALLENGE; + } + + $this->Socket->Write( $Header, 0xFFFFFFFF ); + $this->Socket->Read( ); + + $Type = $this->Buffer->GetByte( ); + + switch( $Type ) + { + case self :: S2A_CHALLENGE: + { + $this->Challenge = $this->Buffer->Get( 4 ); + + return self :: GETCHALLENGE_ALL_CLEAR; + } + case $ExpectedResult: + { + // Goldsource (HLTV) + + return self :: GETCHALLENGE_CONTAINS_ANSWER; + } + case 0: + { + return self :: GETCHALLENGE_FAILED; + } + default: + { + throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); + } + } + } + + /** + * Sets rcon password, for future use in Rcon() + * + * @param string $Password Rcon Password + * + * @return bool True on success, false on failure + */ + public function SetRconPassword( $Password ) + { + if( !$this->Connected ) + { + return false; + } + + switch( $this->Socket->Engine ) + { + case SourceQuery :: GOLDSOURCE: + { + $this->Rcon = new SourceQueryGoldSourceRcon( $this->Buffer, $this->Socket ); + + break; + } + case SourceQuery :: SOURCE: + { + $this->Rcon = new SourceQuerySourceRcon( $this->Buffer, $this->Socket ); + + break; + } + } + + $this->Rcon->Open( ); + + return $this->Rcon->Authorize( $Password ); + } + + /** + * Sends a command to the server for execution. + * + * @param string $Command Command to execute + * + * @return string|bool Answer from server in string, false on failure + */ + public function Rcon( $Command ) + { + if( !$this->Connected ) + { + return false; + } + + return $this->Rcon->Command( $Command ); + } + } diff --git a/SourceQuery/SourceRcon.class.php b/SourceQuery/SourceRcon.class.php index 3669eec..4e07a4c 100644 --- a/SourceQuery/SourceRcon.class.php +++ b/SourceQuery/SourceRcon.class.php @@ -2,7 +2,7 @@ /** * Class written by xPaw * - * Website: http://xpaw.me + * Website: https://xpaw.me * GitHub: https://github.com/xPaw/PHP-Source-Query-Class */ diff --git a/View.php b/View.php index 625d1a5..9879201 100644 --- a/View.php +++ b/View.php @@ -1,180 +1,180 @@ - - define( 'SQ_SERVER_ADDR', 'localhost' ); - define( 'SQ_SERVER_PORT', 27015 ); - define( 'SQ_TIMEOUT', 3 ); - define( 'SQ_ENGINE', SourceQuery :: SOURCE ); - // Edit this <- - - $Timer = MicroTime( true ); - - $Query = new SourceQuery( ); - - $Info = Array( ); - $Rules = Array( ); - $Players = Array( ); - - try - { - $Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE ); - //$Query->SetUseOldGetChallengeMethod( true ); // Use this when players/rules retrieval fails on games like Starbound - - $Info = $Query->GetInfo( ); - $Players = $Query->GetPlayers( ); - $Rules = $Query->GetRules( ); - } - catch( Exception $e ) - { - $Exception = $e; - } - - $Query->Disconnect( ); - - $Timer = Number_Format( MicroTime( true ) - $Timer, 4, '.', '' ); -?> - - -
- -This class was created to query game server which use the Source (Steamworks) query protocol.
- - -getMessage( ) ); ?>
-getTraceAsString(), false ); ?>
-Server Info s | -|
---|---|
- | "; - print_r( $InfoValue ); - echo ""; - } - else - { - if( $InfoValue === true ) - { - echo 'true'; - } - else if( $InfoValue === false ) - { - echo 'false'; - } - else - { - echo htmlspecialchars( $InfoValue ); - } - } -?> | -
No information received | -
Player | -Frags | -Time | -
---|---|---|
- | - | - |
No players received | -
Rules | -|
---|---|
- | - |
No rules received | -
This class was created to query game server which use the Source (Steamworks) query protocol.
+ + +getMessage( ) ); ?>
+getTraceAsString(), false ); ?>
+Server Info s | +|
---|---|
+ | "; + print_r( $InfoValue ); + echo ""; + } + else + { + if( $InfoValue === true ) + { + echo 'true'; + } + else if( $InfoValue === false ) + { + echo 'false'; + } + else + { + echo htmlspecialchars( $InfoValue ); + } + } +?> | +
No information received | +
Player | +Frags | +Time | +
---|---|---|
+ | + | + |
No players received | +
Rules | +|
---|---|
+ | + |
No rules received | +