You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
210 lines
4.9 KiB
210 lines
4.9 KiB
11 years ago
|
<?php
|
||
10 years ago
|
/**
|
||
9 years ago
|
* @author Pavel Djundik <sourcequery@xpaw.me>
|
||
11 years ago
|
*
|
||
9 years ago
|
* @link https://xpaw.me
|
||
|
* @link https://github.com/xPaw/PHP-Source-Query-Class
|
||
|
*
|
||
|
* @license GNU Lesser General Public License, version 2.1
|
||
|
*
|
||
|
* @internal
|
||
11 years ago
|
*/
|
||
9 years ago
|
|
||
|
namespace xPaw\SourceQuery;
|
||
11 years ago
|
|
||
10 years ago
|
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||
|
use xPaw\SourceQuery\Exception\TimeoutException;
|
||
|
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||
9 years ago
|
|
||
|
/**
|
||
|
* Class SourceRcon
|
||
|
*
|
||
|
* @package xPaw\SourceQuery
|
||
|
*
|
||
|
* @uses xPaw\SourceQuery\Exception\AuthenticationException
|
||
|
* @uses xPaw\SourceQuery\Exception\TimeoutException
|
||
|
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
|
||
|
*/
|
||
|
class SourceRcon
|
||
11 years ago
|
{
|
||
|
/**
|
||
|
* Points to buffer class
|
||
|
*
|
||
9 years ago
|
* @var Buffer
|
||
11 years ago
|
*/
|
||
|
private $Buffer;
|
||
|
|
||
|
/**
|
||
|
* Points to socket class
|
||
|
*
|
||
9 years ago
|
* @var Socket
|
||
11 years ago
|
*/
|
||
|
private $Socket;
|
||
|
|
||
|
private $RconSocket;
|
||
|
private $RconRequestId;
|
||
|
|
||
|
public function __construct( $Buffer, $Socket )
|
||
|
{
|
||
|
$this->Buffer = $Buffer;
|
||
|
$this->Socket = $Socket;
|
||
|
}
|
||
|
|
||
|
public function Close( )
|
||
|
{
|
||
|
if( $this->RconSocket )
|
||
|
{
|
||
|
FClose( $this->RconSocket );
|
||
|
|
||
|
$this->RconSocket = null;
|
||
|
}
|
||
|
|
||
|
$this->RconRequestId = 0;
|
||
|
}
|
||
|
|
||
|
public function Open( )
|
||
|
{
|
||
|
if( !$this->RconSocket )
|
||
|
{
|
||
|
$this->RconSocket = @FSockOpen( $this->Socket->Ip, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
|
||
|
|
||
|
if( $ErrNo || !$this->RconSocket )
|
||
|
{
|
||
10 years ago
|
throw new TimeoutException( 'Can\'t connect to RCON server: ' . $ErrStr, TimeoutException::TIMEOUT_CONNECT );
|
||
11 years ago
|
}
|
||
|
|
||
|
Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout );
|
||
|
Stream_Set_Blocking( $this->RconSocket, true );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function Write( $Header, $String = '' )
|
||
|
{
|
||
|
// Pack the packet together
|
||
10 years ago
|
$Command = Pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
|
||
11 years ago
|
|
||
|
// Prepend packet length
|
||
|
$Command = Pack( 'V', StrLen( $Command ) ) . $Command;
|
||
|
$Length = StrLen( $Command );
|
||
|
|
||
|
return $Length === FWrite( $this->RconSocket, $Command, $Length );
|
||
|
}
|
||
|
|
||
10 years ago
|
public function Read( )
|
||
11 years ago
|
{
|
||
10 years ago
|
$this->Buffer->Set( FRead( $this->RconSocket, 4 ) );
|
||
11 years ago
|
|
||
10 years ago
|
if( $this->Buffer->Remaining( ) < 4 )
|
||
|
{
|
||
|
throw new InvalidPacketException( 'Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY );
|
||
|
}
|
||
|
|
||
11 years ago
|
$PacketSize = $this->Buffer->GetLong( );
|
||
|
|
||
10 years ago
|
$this->Buffer->Set( FRead( $this->RconSocket, $PacketSize ) );
|
||
|
|
||
11 years ago
|
$Buffer = $this->Buffer->Get( );
|
||
11 years ago
|
|
||
11 years ago
|
$Remaining = $PacketSize - StrLen( $Buffer );
|
||
|
|
||
|
while( $Remaining > 0 )
|
||
|
{
|
||
10 years ago
|
$Buffer2 = FRead( $this->RconSocket, $Remaining );
|
||
|
|
||
|
$PacketSize = StrLen( $Buffer2 );
|
||
|
|
||
|
if( $PacketSize === 0 )
|
||
|
{
|
||
|
throw new InvalidPacketException( 'Read ' . strlen( $Buffer ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
|
||
|
|
||
|
break;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
$Buffer .= $Buffer2;
|
||
|
$Remaining -= $PacketSize;
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
$this->Buffer->Set( $Buffer );
|
||
|
}
|
||
|
|
||
|
public function Command( $Command )
|
||
|
{
|
||
|
$this->Write( SourceQuery :: SERVERDATA_EXECCOMMAND, $Command );
|
||
11 years ago
|
|
||
11 years ago
|
$this->Read( );
|
||
|
|
||
11 years ago
|
$this->Buffer->GetLong( ); // RequestID
|
||
10 years ago
|
|
||
|
$Type = $this->Buffer->GetLong( );
|
||
11 years ago
|
|
||
|
if( $Type === SourceQuery :: SERVERDATA_AUTH_RESPONSE )
|
||
|
{
|
||
10 years ago
|
throw new AuthenticationException( 'Bad rcon_password.', AuthenticationException::BAD_PASSWORD );
|
||
11 years ago
|
}
|
||
|
else if( $Type !== SourceQuery :: SERVERDATA_RESPONSE_VALUE )
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
11 years ago
|
$Buffer = $this->Buffer->Get( );
|
||
|
|
||
|
// We do this stupid hack to handle split packets
|
||
|
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
|
||
|
if( StrLen( $Buffer ) >= 4000 )
|
||
|
{
|
||
|
do
|
||
|
{
|
||
|
$this->Write( SourceQuery :: SERVERDATA_RESPONSE_VALUE );
|
||
|
|
||
|
$this->Read( );
|
||
|
|
||
|
$this->Buffer->GetLong( ); // RequestID
|
||
|
|
||
|
if( $this->Buffer->GetLong( ) !== SourceQuery :: SERVERDATA_RESPONSE_VALUE )
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
|
||
10 years ago
|
$Buffer2 = $this->Buffer->Get( );
|
||
|
|
||
|
if( $Buffer2 === "\x00\x01\x00\x00\x00\x00" )
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
$Buffer .= $Buffer2;
|
||
11 years ago
|
}
|
||
10 years ago
|
while( true );
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
return rtrim( $Buffer, "\0" );
|
||
11 years ago
|
}
|
||
|
|
||
|
public function Authorize( $Password )
|
||
|
{
|
||
|
$this->Write( SourceQuery :: SERVERDATA_AUTH, $Password );
|
||
|
$this->Read( );
|
||
|
|
||
|
$RequestID = $this->Buffer->GetLong( );
|
||
|
$Type = $this->Buffer->GetLong( );
|
||
|
|
||
|
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
|
||
|
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
|
||
|
|
||
|
if( $Type === SourceQuery :: SERVERDATA_RESPONSE_VALUE )
|
||
|
{
|
||
|
$this->Read( );
|
||
|
|
||
|
$RequestID = $this->Buffer->GetLong( );
|
||
|
$Type = $this->Buffer->GetLong( );
|
||
|
}
|
||
|
|
||
|
if( $RequestID === -1 || $Type !== SourceQuery :: SERVERDATA_AUTH_RESPONSE )
|
||
|
{
|
||
10 years ago
|
throw new AuthenticationException( 'RCON authorization failed.', AuthenticationException::BAD_PASSWORD );
|
||
11 years ago
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
}
|