parent
							
								
									a2f7834ef5
								
							
						
					
					
						commit
						b01c1f643f
					
				| @ -1,177 +0,0 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| /** | ||||
|  * @author Pavel Djundik | ||||
|  * | ||||
|  * @link https://xpaw.me | ||||
|  * @link https://github.com/xPaw/PHP-Source-Query | ||||
|  * | ||||
|  * @license GNU Lesser General Public License, version 2.1 | ||||
|  * | ||||
|  * @internal | ||||
|  */ | ||||
| 
 | ||||
| namespace xPaw\SourceQuery; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Exception\InvalidPacketException; | ||||
| use xPaw\SourceQuery\Exception\SocketException; | ||||
| 
 | ||||
| /** | ||||
|  * Base socket interface | ||||
|  * | ||||
|  * @package xPaw\SourceQuery | ||||
|  * | ||||
|  * @uses InvalidPacketException | ||||
|  * @uses SocketException | ||||
|  */ | ||||
| abstract class BaseSocket | ||||
| { | ||||
|     /** | ||||
|      * @var resource|null | ||||
|      */ | ||||
|     public $Socket; | ||||
| 
 | ||||
|     /** | ||||
|      * @var int $Engine | ||||
|      */ | ||||
|     public int $Engine = SourceQuery::SOURCE; | ||||
| 
 | ||||
|     /** | ||||
|      * @var string $Address | ||||
|      */ | ||||
|     public string $Address = ''; | ||||
| 
 | ||||
|     /** | ||||
|      * @var int $Port | ||||
|      */ | ||||
|     public int $Port = 0; | ||||
| 
 | ||||
|     /** | ||||
|      * @var int $Timeout | ||||
|      */ | ||||
|     public int $Timeout = 0; | ||||
| 
 | ||||
|     /** | ||||
|      * Destructor | ||||
|      */ | ||||
|     public function __destruct() | ||||
|     { | ||||
|         $this->Close(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Close | ||||
|      */ | ||||
|     abstract public function Close(): void; | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $Address | ||||
|      * @param int $Port | ||||
|      * @param int $Timeout | ||||
|      * @param int $Engine | ||||
|      */ | ||||
|     abstract public function Open(string $Address, int $Port, int $Timeout, int $Engine): void; | ||||
| 
 | ||||
|     /** | ||||
|      * @param int $Header | ||||
|      * @param string $String | ||||
|      * | ||||
|      * @return bool | ||||
|      */ | ||||
|     abstract public function Write(int $Header, string $String = ''): bool; | ||||
| 
 | ||||
|     /** | ||||
|      * @param int $Length | ||||
|      * | ||||
|      * @return Buffer | ||||
|      */ | ||||
|     abstract public function Read(int $Length = 1400): Buffer; | ||||
| 
 | ||||
|     /** | ||||
|      * @param Buffer $Buffer | ||||
|      * @param int $Length | ||||
|      * @param callable $SherlockFunction | ||||
|      * | ||||
|      * @return Buffer | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      * @throws SocketException | ||||
|      */ | ||||
|     protected function ReadInternal(Buffer $Buffer, int $Length, callable $SherlockFunction): Buffer | ||||
|     { | ||||
|         if ($Buffer->Remaining() === 0) { | ||||
|             throw new InvalidPacketException('Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY); | ||||
|         } | ||||
| 
 | ||||
|         $Header = $Buffer->GetLong(); | ||||
| 
 | ||||
|         // Single packet, do nothing. | ||||
|         if ($Header === -1) { | ||||
|             return $Buffer; | ||||
|         } | ||||
| 
 | ||||
|         if ($Header === -2) { // Split packet | ||||
|             $Packets      = []; | ||||
|             $IsCompressed = false; | ||||
|             $PacketChecksum = null; | ||||
| 
 | ||||
|             do { | ||||
|                 $RequestID = $Buffer->GetLong(); | ||||
| 
 | ||||
|                 switch ($this->Engine) { | ||||
|                     case SourceQuery::GOLDSOURCE: | ||||
|                     { | ||||
|                         $PacketCountAndNumber = $Buffer->GetByte(); | ||||
|                         $PacketCount          = $PacketCountAndNumber & 0xF; | ||||
|                         $PacketNumber         = $PacketCountAndNumber >> 4; | ||||
| 
 | ||||
|                         break; | ||||
|                     } | ||||
|                     case SourceQuery::SOURCE: | ||||
|                     { | ||||
|                         $IsCompressed         = ($RequestID & 0x80000000) !== 0; | ||||
|                         $PacketCount          = $Buffer->GetByte(); | ||||
|                         $PacketNumber         = $Buffer->GetByte() + 1; | ||||
| 
 | ||||
|                         if ($IsCompressed) { | ||||
|                             $Buffer->GetLong(); // Split size | ||||
| 
 | ||||
|                             $PacketChecksum = $Buffer->GetUnsignedLong(); | ||||
|                         } else { | ||||
|                             $Buffer->GetShort(); // Split size | ||||
|                         } | ||||
| 
 | ||||
|                         break; | ||||
|                     } | ||||
|                     default: | ||||
|                     { | ||||
|                         throw new SocketException('Unknown engine.', SocketException::INVALID_ENGINE); | ||||
|                     } | ||||
|                 } | ||||
| 
 | ||||
|                 $Packets[ $PacketNumber ] = $Buffer->Get(); | ||||
| 
 | ||||
|                 $ReadMore = $PacketCount > count($Packets); | ||||
|             } while ($ReadMore && $SherlockFunction($Buffer, $Length)); | ||||
| 
 | ||||
|             $Data = implode($Packets); | ||||
| 
 | ||||
|             // TODO: Test this | ||||
|             if ($IsCompressed) { | ||||
|                 $Data = bzdecompress($Data); | ||||
| 
 | ||||
|                 if (!is_string($Data) || crc32($Data) !== $PacketChecksum) { | ||||
|                     throw new InvalidPacketException('CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH); | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             $Buffer->Set(substr($Data, 4)); | ||||
|         } else { | ||||
|             throw new InvalidPacketException('Socket read: Raw packet header mismatch. (0x' . dechex($Header) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH); | ||||
|         } | ||||
| 
 | ||||
|         return $Buffer; | ||||
|     } | ||||
| } | ||||
| @ -1,171 +0,0 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| /** | ||||
|  * @author Pavel Djundik | ||||
|  * | ||||
|  * @link https://xpaw.me | ||||
|  * @link https://github.com/xPaw/PHP-Source-Query | ||||
|  * | ||||
|  * @license GNU Lesser General Public License, version 2.1 | ||||
|  * | ||||
|  * @internal | ||||
|  */ | ||||
| 
 | ||||
| namespace xPaw\SourceQuery; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Exception\AuthenticationException; | ||||
| use xPaw\SourceQuery\Exception\InvalidPacketException; | ||||
| 
 | ||||
| /** | ||||
|  * Class GoldSourceRcon | ||||
|  * | ||||
|  * @package xPaw\SourceQuery | ||||
|  * | ||||
|  * @uses AuthenticationException | ||||
|  * @uses InvalidPacketException | ||||
|  */ | ||||
| final class GoldSourceRcon | ||||
| { | ||||
|     /** | ||||
|      * Points to socket class | ||||
|      * | ||||
|      * @var BaseSocket | ||||
|      */ | ||||
|     private BaseSocket $Socket; | ||||
| 
 | ||||
|     /** | ||||
|      * @var string $RconPassword | ||||
|      */ | ||||
|     private string $RconPassword = ''; | ||||
| 
 | ||||
|     /** | ||||
|      * @var string $RconChallenge | ||||
|      */ | ||||
|     private string $RconChallenge = ''; | ||||
| 
 | ||||
|     /** | ||||
|      * @param BaseSocket $Socket | ||||
|      */ | ||||
|     public function __construct(BaseSocket $Socket) | ||||
|     { | ||||
|         $this->Socket = $Socket; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Close | ||||
|      */ | ||||
|     public function Close(): void | ||||
|     { | ||||
|         $this->RconChallenge = ''; | ||||
|         $this->RconPassword  = ''; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Open | ||||
|      */ | ||||
|     public function Open(): void | ||||
|     { | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $String | ||||
|      * | ||||
|      * @return bool | ||||
|      */ | ||||
|     public function Write(string $String = ''): bool | ||||
|     { | ||||
|         $Command = pack('cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String); | ||||
|         $Length  = strlen($Command); | ||||
| 
 | ||||
|         return $Length === fwrite($this->Socket->Socket, $Command, $Length); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @throws AuthenticationException | ||||
|      * @throws InvalidPacketException | ||||
|      * | ||||
|      * @return Buffer | ||||
|      */ | ||||
|     public function Read(): Buffer | ||||
|     { | ||||
|         // GoldSource RCON has same structure as Query | ||||
|         $Buffer = $this->Socket->Read(); | ||||
| 
 | ||||
|         $StringBuffer = ''; | ||||
| 
 | ||||
|         // There is no indentifier of the end, so we just need to continue reading | ||||
|         do { | ||||
|             $ReadMore = $Buffer->Remaining() > 0; | ||||
| 
 | ||||
|             if ($ReadMore) { | ||||
|                 if ($Buffer->GetByte() !== SourceQuery::S2A_RCON) { | ||||
|                     throw new InvalidPacketException('Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH); | ||||
|                 } | ||||
| 
 | ||||
|                 $Packet = $Buffer->Get(); | ||||
|                 $StringBuffer .= $Packet; | ||||
|                 //$StringBuffer .= SubStr( $Packet, 0, -2 ); | ||||
| 
 | ||||
|                 // Let's assume if this packet is not long enough, there are no more after this one | ||||
|                 $ReadMore = strlen($Packet) > 1000; // use 1300? | ||||
| 
 | ||||
|                 if ($ReadMore) { | ||||
|                     $Buffer = $this->Socket->Read(); | ||||
|                 } | ||||
|             } | ||||
|         } while ($ReadMore); | ||||
| 
 | ||||
|         $Trimmed = trim($StringBuffer); | ||||
| 
 | ||||
|         if ($Trimmed === 'Bad rcon_password.') { | ||||
|             throw new AuthenticationException($Trimmed, AuthenticationException::BAD_PASSWORD); | ||||
|         } elseif ($Trimmed === 'You have been banned from this server.') { | ||||
|             throw new AuthenticationException($Trimmed, AuthenticationException::BANNED); | ||||
|         } | ||||
| 
 | ||||
|         $Buffer->Set($Trimmed); | ||||
| 
 | ||||
|         return $Buffer; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $Command | ||||
|      * | ||||
|      * @return string | ||||
|      * | ||||
|      * @throws AuthenticationException | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function Command(string $Command): string | ||||
|     { | ||||
|         if (!$this->RconChallenge) { | ||||
|             throw new AuthenticationException('Tried to execute a RCON command before successful authorization.', AuthenticationException::BAD_PASSWORD); | ||||
|         } | ||||
| 
 | ||||
|         $this->Write('rcon ' . $this->RconChallenge . ' "' . $this->RconPassword . '" ' . $Command . "\0"); | ||||
|         $Buffer = $this->Read(); | ||||
| 
 | ||||
|         return $Buffer->Get(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $Password | ||||
|      * | ||||
|      * @throws AuthenticationException | ||||
|      */ | ||||
|     public function Authorize(string $Password): void | ||||
|     { | ||||
|         $this->RconPassword = $Password; | ||||
| 
 | ||||
|         $this->Write('challenge rcon'); | ||||
|         $Buffer = $this->Socket->Read(); | ||||
| 
 | ||||
|         if ($Buffer->Get(14) !== 'challenge rcon') { | ||||
|             throw new AuthenticationException('Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD); | ||||
|         } | ||||
| 
 | ||||
|         $this->RconChallenge = trim($Buffer->Get()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,28 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| namespace xPaw\SourceQuery\Rcon; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Buffer; | ||||
| use xPaw\SourceQuery\Exception\AuthenticationException; | ||||
| use xPaw\SourceQuery\Exception\InvalidPacketException; | ||||
| 
 | ||||
| abstract class AbstractRcon implements RconInterface | ||||
| { | ||||
|     /** | ||||
|      * @param int|null $header | ||||
|      * @param string $string | ||||
|      * | ||||
|      * @return bool | ||||
|      */ | ||||
|     abstract protected function write(?int $header, string $string = ''): bool; | ||||
| 
 | ||||
|     /** | ||||
|      * @throws AuthenticationException | ||||
|      * @throws InvalidPacketException | ||||
|      * | ||||
|      * @return Buffer | ||||
|      */ | ||||
|     abstract protected function read(): Buffer; | ||||
| } | ||||
| @ -0,0 +1,175 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| /** | ||||
|  * @author Pavel Djundik | ||||
|  * | ||||
|  * @link https://xpaw.me | ||||
|  * @link https://github.com/xPaw/PHP-Source-Query | ||||
|  * | ||||
|  * @license GNU Lesser General Public License, version 2.1 | ||||
|  * | ||||
|  * @internal | ||||
|  */ | ||||
| 
 | ||||
| namespace xPaw\SourceQuery\Rcon; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Buffer; | ||||
| use xPaw\SourceQuery\Exception\AuthenticationException; | ||||
| use xPaw\SourceQuery\Exception\InvalidPacketException; | ||||
| use xPaw\SourceQuery\Socket\SocketInterface; | ||||
| use xPaw\SourceQuery\SourceQuery; | ||||
| 
 | ||||
| /** | ||||
|  * Class GoldSourceRcon | ||||
|  * | ||||
|  * @package xPaw\SourceQuery | ||||
|  * | ||||
|  * @uses AuthenticationException | ||||
|  * @uses InvalidPacketException | ||||
|  */ | ||||
| final class GoldSourceRcon extends AbstractRcon | ||||
| { | ||||
|     /** | ||||
|      * Points to socket class | ||||
|      * | ||||
|      * @var SocketInterface | ||||
|      */ | ||||
|     private SocketInterface $socket; | ||||
| 
 | ||||
|     /** | ||||
|      * @var string | ||||
|      */ | ||||
|     private string $rconPassword = ''; | ||||
| 
 | ||||
|     /** | ||||
|      * @var string | ||||
|      */ | ||||
|     private string $rconChallenge = ''; | ||||
| 
 | ||||
|     /** | ||||
|      * @param SocketInterface $socket | ||||
|      */ | ||||
|     public function __construct(SocketInterface $socket) | ||||
|     { | ||||
|         $this->socket = $socket; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Close | ||||
|      */ | ||||
|     public function close(): void | ||||
|     { | ||||
|         $this->rconChallenge = ''; | ||||
|         $this->rconPassword  = ''; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Open | ||||
|      */ | ||||
|     public function open(): void | ||||
|     { | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $command | ||||
|      * | ||||
|      * @return string | ||||
|      * | ||||
|      * @throws AuthenticationException | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function command(string $command): string | ||||
|     { | ||||
|         if (!$this->rconChallenge) { | ||||
|             throw new AuthenticationException('Tried to execute a RCON command before successful authorization.', AuthenticationException::BAD_PASSWORD); | ||||
|         } | ||||
| 
 | ||||
|         $this->write(null, 'rcon ' . $this->rconChallenge . ' "' . $this->rconPassword . '" ' . $command . "\0"); | ||||
|         $buffer = $this->read(); | ||||
| 
 | ||||
|         return $buffer->get(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $password | ||||
|      * | ||||
|      * @throws AuthenticationException | ||||
|      */ | ||||
|     public function authorize(string $password): void | ||||
|     { | ||||
|         $this->rconPassword = $password; | ||||
| 
 | ||||
|         $this->write(null, 'challenge rcon'); | ||||
|         $buffer = $this->socket->read(); | ||||
| 
 | ||||
|         if ($buffer->get(14) !== 'challenge rcon') { | ||||
|             throw new AuthenticationException('Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD); | ||||
|         } | ||||
| 
 | ||||
|         $this->rconChallenge = trim($buffer->get()); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param int|null $header | ||||
|      * @param string $string | ||||
|      * | ||||
|      * @return bool | ||||
|      */ | ||||
|     protected function write(?int $header, string $string = ''): bool | ||||
|     { | ||||
|         $command = pack('cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $string); | ||||
|         $length = strlen($command); | ||||
| 
 | ||||
|         return $length === fwrite($this->socket->getSocket(), $command, $length); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @throws AuthenticationException | ||||
|      * @throws InvalidPacketException | ||||
|      * | ||||
|      * @return Buffer | ||||
|      */ | ||||
|     protected function read(): Buffer | ||||
|     { | ||||
|         // GoldSource RCON has same structure as Query | ||||
|         $buffer = $this->socket->read(); | ||||
| 
 | ||||
|         $stringBuffer = ''; | ||||
| 
 | ||||
|         // There is no indentifier of the end, so we just need to continue reading | ||||
|         do { | ||||
|             $readMore = $buffer->remaining() > 0; | ||||
| 
 | ||||
|             if ($readMore) { | ||||
|                 if ($buffer->getByte() !== SourceQuery::S2A_RCON) { | ||||
|                     throw new InvalidPacketException('Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH); | ||||
|                 } | ||||
| 
 | ||||
|                 $packet = $buffer->get(); | ||||
|                 $stringBuffer .= $packet; | ||||
|                 //$stringBuffer .= SubStr( $packet, 0, -2 ); | ||||
| 
 | ||||
|                 // Let's assume if this packet is not long enough, there are no more after this one | ||||
|                 $readMore = strlen($packet) > 1000; // use 1300? | ||||
| 
 | ||||
|                 if ($readMore) { | ||||
|                     $buffer = $this->socket->read(); | ||||
|                 } | ||||
|             } | ||||
|         } while ($readMore); | ||||
| 
 | ||||
|         $trimmed = trim($stringBuffer); | ||||
| 
 | ||||
|         if ($trimmed === 'Bad rcon_password.') { | ||||
|             throw new AuthenticationException($trimmed, AuthenticationException::BAD_PASSWORD); | ||||
|         } elseif ($trimmed === 'You have been banned from this server.') { | ||||
|             throw new AuthenticationException($trimmed, AuthenticationException::BANNED); | ||||
|         } | ||||
| 
 | ||||
|         $buffer->set($trimmed); | ||||
| 
 | ||||
|         return $buffer; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,44 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| namespace xPaw\SourceQuery\Rcon; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Exception\AuthenticationException; | ||||
| use xPaw\SourceQuery\Exception\InvalidPacketException; | ||||
| use xPaw\SourceQuery\Socket\SocketInterface; | ||||
| 
 | ||||
| interface RconInterface | ||||
| { | ||||
|     /** | ||||
|      * @param SocketInterface $socket | ||||
|      */ | ||||
|     public function __construct(SocketInterface $socket); | ||||
| 
 | ||||
|     /** | ||||
|      * Close | ||||
|      */ | ||||
|     public function close(): void; | ||||
| 
 | ||||
|     /** | ||||
|      * Open | ||||
|      */ | ||||
|     public function open(): void; | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $command | ||||
|      * | ||||
|      * @return string | ||||
|      * | ||||
|      * @throws AuthenticationException | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function command(string $command): string; | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $password | ||||
|      * | ||||
|      * @throws AuthenticationException | ||||
|      */ | ||||
|     public function authorize(string $password): void; | ||||
| } | ||||
| @ -0,0 +1,235 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| /** | ||||
|  * @author Pavel Djundik | ||||
|  * | ||||
|  * @link https://xpaw.me | ||||
|  * @link https://github.com/xPaw/PHP-Source-Query | ||||
|  * | ||||
|  * @license GNU Lesser General Public License, version 2.1 | ||||
|  * | ||||
|  * @internal | ||||
|  */ | ||||
| 
 | ||||
| namespace xPaw\SourceQuery\Rcon; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Buffer; | ||||
| use xPaw\SourceQuery\Exception\AuthenticationException; | ||||
| use xPaw\SourceQuery\Exception\InvalidPacketException; | ||||
| use xPaw\SourceQuery\Exception\SocketException; | ||||
| use xPaw\SourceQuery\Socket\SocketInterface; | ||||
| use xPaw\SourceQuery\SourceQuery; | ||||
| 
 | ||||
| /** | ||||
|  * Class SourceRcon | ||||
|  * | ||||
|  * @package xPaw\SourceQuery | ||||
|  * | ||||
|  * @uses AuthenticationException | ||||
|  * @uses InvalidPacketException | ||||
|  * @uses SocketException | ||||
|  */ | ||||
| final class SourceRcon extends AbstractRcon | ||||
| { | ||||
|     /** | ||||
|      * Points to socket class | ||||
|      */ | ||||
|     private SocketInterface $socket; | ||||
| 
 | ||||
|     /** | ||||
|      * @var ?resource | ||||
|      */ | ||||
|     private $rconSocket; | ||||
| 
 | ||||
|     /** | ||||
|      * @var int | ||||
|      */ | ||||
|     private int $rconRequestId = 0; | ||||
| 
 | ||||
|     /** | ||||
|      * @param SocketInterface $socket | ||||
|      */ | ||||
|     public function __construct(SocketInterface $socket) | ||||
|     { | ||||
|         $this->socket = $socket; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Close | ||||
|      */ | ||||
|     public function close(): void | ||||
|     { | ||||
|         if ($this->rconSocket) { | ||||
|             fclose($this->rconSocket); | ||||
| 
 | ||||
|             $this->rconSocket = null; | ||||
|         } | ||||
| 
 | ||||
|         $this->rconRequestId = 0; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @throws SocketException | ||||
|      */ | ||||
|     public function open(): void | ||||
|     { | ||||
|         if (!$this->rconSocket) { | ||||
|             $rconSocket = @fsockopen( | ||||
|                 $this->socket->getAddress(), | ||||
|                 $this->socket->getPort(), | ||||
|                 $errNo, | ||||
|                 $errStr, | ||||
|                 $this->socket->getTimeout() | ||||
|             ); | ||||
| 
 | ||||
|             if ($errNo || !$rconSocket) { | ||||
|                 throw new SocketException('Can\'t connect to RCON server: ' . $errStr, SocketException::CONNECTION_FAILED); | ||||
|             } | ||||
| 
 | ||||
|             $this->rconSocket = $rconSocket; | ||||
|             stream_set_timeout($this->rconSocket, $this->socket->getTimeout()); | ||||
|             stream_set_blocking($this->rconSocket, true); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $command | ||||
|      * | ||||
|      * @return string | ||||
|      * | ||||
|      * @throws AuthenticationException | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function command(string $command): string | ||||
|     { | ||||
|         $this->write(SourceQuery::SERVERDATA_EXECCOMMAND, $command); | ||||
|         $buffer = $this->read(); | ||||
| 
 | ||||
|         $buffer->getLong(); // RequestID | ||||
| 
 | ||||
|         $type = $buffer->getLong(); | ||||
| 
 | ||||
|         if ($type === SourceQuery::SERVERDATA_AUTH_RESPONSE) { | ||||
|             throw new AuthenticationException('Bad rcon_password.', AuthenticationException::BAD_PASSWORD); | ||||
|         } elseif ($type !== SourceQuery::SERVERDATA_RESPONSE_VALUE) { | ||||
|             throw new InvalidPacketException('Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH); | ||||
|         } | ||||
| 
 | ||||
|         $data = $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($data) >= 4000) { | ||||
|             $this->write(SourceQuery::SERVERDATA_REQUESTVALUE); | ||||
| 
 | ||||
|             do { | ||||
|                 $buffer = $this->read(); | ||||
| 
 | ||||
|                 $buffer->getLong(); // RequestID | ||||
| 
 | ||||
|                 if ($buffer->getLong() !== SourceQuery::SERVERDATA_RESPONSE_VALUE) { | ||||
|                     break; | ||||
|                 } | ||||
| 
 | ||||
|                 $data2 = $buffer->get(); | ||||
| 
 | ||||
|                 if ($data2 === "\x00\x01\x00\x00\x00\x00") { | ||||
|                     break; | ||||
|                 } | ||||
| 
 | ||||
|                 $data .= $data2; | ||||
|             } while (true); | ||||
|         } | ||||
| 
 | ||||
|         return rtrim($data, "\0"); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $password | ||||
|      * | ||||
|      * @throws AuthenticationException | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function authorize(string $password): void | ||||
|     { | ||||
|         $this->write(SourceQuery::SERVERDATA_AUTH, $password); | ||||
|         $buffer = $this->read(); | ||||
| 
 | ||||
|         $requestId = $buffer->getLong(); | ||||
|         $type = $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) { | ||||
|             $buffer = $this->read(); | ||||
| 
 | ||||
|             $requestId = $buffer->getLong(); | ||||
|             $type = $buffer->getLong(); | ||||
|         } | ||||
| 
 | ||||
|         if ($requestId === -1 || $type !== SourceQuery::SERVERDATA_AUTH_RESPONSE) { | ||||
|             throw new AuthenticationException('RCON authorization failed.', AuthenticationException::BAD_PASSWORD); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param int|null $header | ||||
|      * @param string $string | ||||
|      * | ||||
|      * @return bool | ||||
|      */ | ||||
|     protected function write(?int $header, string $string = ''): bool | ||||
|     { | ||||
|         // Pack the packet together | ||||
|         $command = pack('VV', ++$this->rconRequestId, $header) . $string . "\x00\x00"; | ||||
| 
 | ||||
|         // Prepend packet length | ||||
|         $command = pack('V', strlen($command)) . $command; | ||||
|         $length  = strlen($command); | ||||
| 
 | ||||
|         return $length === fwrite($this->rconSocket, $command, $length); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @return Buffer | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     protected function read(): Buffer | ||||
|     { | ||||
|         $buffer = new Buffer(); | ||||
|         $buffer->set(fread($this->rconSocket, 4)); | ||||
| 
 | ||||
|         if ($buffer->remaining() < 4) { | ||||
|             throw new InvalidPacketException('Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY); | ||||
|         } | ||||
| 
 | ||||
|         $packetSize = $buffer->getLong(); | ||||
| 
 | ||||
|         $buffer->set(fread($this->rconSocket, $packetSize)); | ||||
| 
 | ||||
|         $data = $buffer->get(); | ||||
| 
 | ||||
|         $remaining = $packetSize - strlen($data); | ||||
| 
 | ||||
|         while ($remaining > 0) { | ||||
|             $data2 = fread($this->rconSocket, $remaining); | ||||
| 
 | ||||
|             $packetSize = strlen($data2); | ||||
| 
 | ||||
|             if ($packetSize === 0) { | ||||
|                 throw new InvalidPacketException('Read ' . strlen($data) . ' bytes from socket, ' . $remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY); | ||||
|             } | ||||
| 
 | ||||
|             $data .= $data2; | ||||
|             $remaining -= $packetSize; | ||||
|         } | ||||
| 
 | ||||
|         $buffer->set($data); | ||||
| 
 | ||||
|         return $buffer; | ||||
|     } | ||||
| } | ||||
| @ -1,130 +0,0 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| /** | ||||
|  * @author Pavel Djundik | ||||
|  * | ||||
|  * @link https://xpaw.me | ||||
|  * @link https://github.com/xPaw/PHP-Source-Query | ||||
|  * | ||||
|  * @license GNU Lesser General Public License, version 2.1 | ||||
|  * | ||||
|  * @internal | ||||
|  */ | ||||
| 
 | ||||
| namespace xPaw\SourceQuery; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Exception\InvalidPacketException; | ||||
| use xPaw\SourceQuery\Exception\SocketException; | ||||
| 
 | ||||
| /** | ||||
|  * Class Socket | ||||
|  * | ||||
|  * @package xPaw\SourceQuery | ||||
|  * | ||||
|  * @uses InvalidPacketException | ||||
|  * @uses SocketException | ||||
|  */ | ||||
| final class Socket extends BaseSocket | ||||
| { | ||||
|     /** | ||||
|      * Close | ||||
|      */ | ||||
|     public function Close(): void | ||||
|     { | ||||
|         if ($this->Socket !== null && $this->Socket !== 0) { | ||||
|             fclose($this->Socket); | ||||
| 
 | ||||
|             $this->Socket = null; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $Address | ||||
|      * @param int $Port | ||||
|      * @param int $Timeout | ||||
|      * @param int $Engine | ||||
|      * | ||||
|      * @throws SocketException | ||||
|      */ | ||||
|     public function Open(string $Address, int $Port, int $Timeout, int $Engine): void | ||||
|     { | ||||
|         $this->Timeout = $Timeout; | ||||
|         $this->Engine  = $Engine; | ||||
|         $this->Port    = $Port; | ||||
|         $this->Address = $Address; | ||||
| 
 | ||||
|         $Socket = @fsockopen('udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout); | ||||
| 
 | ||||
|         if ($ErrNo || $Socket === false) { | ||||
|             throw new SocketException('Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET); | ||||
|         } | ||||
| 
 | ||||
|         $this->Socket = $Socket; | ||||
|         stream_set_timeout($this->Socket, $Timeout); | ||||
|         stream_set_blocking($this->Socket, true); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param int $Header | ||||
|      * @param string $String | ||||
|      * | ||||
|      * @return 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); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Reads from socket and returns Buffer. | ||||
|      * | ||||
|      * @param int $Length | ||||
|      * | ||||
|      * @return Buffer Buffer | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      * @throws SocketException | ||||
|      * | ||||
|      */ | ||||
|     public function Read(int $Length = 1400): Buffer | ||||
|     { | ||||
|         $Buffer = new Buffer(); | ||||
|         $data = fread($this->Socket, $Length); | ||||
| 
 | ||||
|         if (!$data) { | ||||
|             throw new SocketException('Failed to open socket.'); | ||||
|         } | ||||
| 
 | ||||
|         $Buffer->Set($data); | ||||
| 
 | ||||
|         $this->ReadInternal($Buffer, $Length, [ $this, 'Sherlock' ]); | ||||
| 
 | ||||
|         return $Buffer; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param Buffer $Buffer | ||||
|      * @param int $Length | ||||
|      * | ||||
|      * @return bool | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function Sherlock(Buffer $Buffer, int $Length): bool | ||||
|     { | ||||
|         $Data = fread($this->Socket, $Length); | ||||
| 
 | ||||
|         if (strlen($Data) < 4) { | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
|         $Buffer->Set($Data); | ||||
| 
 | ||||
|         return $Buffer->GetLong() === -2; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,285 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| /** | ||||
|  * @author Pavel Djundik | ||||
|  * | ||||
|  * @link https://xpaw.me | ||||
|  * @link https://github.com/xPaw/PHP-Source-Query | ||||
|  * | ||||
|  * @license GNU Lesser General Public License, version 2.1 | ||||
|  * | ||||
|  * @internal | ||||
|  */ | ||||
| 
 | ||||
| namespace xPaw\SourceQuery\Socket; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Buffer; | ||||
| use xPaw\SourceQuery\Exception\InvalidArgumentException; | ||||
| use xPaw\SourceQuery\Exception\InvalidPacketException; | ||||
| use xPaw\SourceQuery\Exception\SocketException; | ||||
| 
 | ||||
| /** | ||||
|  * Base socket | ||||
|  * | ||||
|  * @package xPaw\SourceQuery | ||||
|  * | ||||
|  * @uses InvalidPacketException | ||||
|  * @uses SocketException | ||||
|  */ | ||||
| abstract class AbstractSocket implements SocketInterface | ||||
| { | ||||
|     /** | ||||
|      * @var resource|null | ||||
|      */ | ||||
|     public $socket; | ||||
| 
 | ||||
|     /** | ||||
|      * @var int | ||||
|      */ | ||||
|     public int $engine = SocketType::SOURCE; | ||||
| 
 | ||||
|     /** | ||||
|      * @var string $address | ||||
|      */ | ||||
|     public string $address = ''; | ||||
| 
 | ||||
|     /** | ||||
|      * @var int $port | ||||
|      */ | ||||
|     public int $port = 0; | ||||
| 
 | ||||
|     /** | ||||
|      * @var int $timeout | ||||
|      */ | ||||
|     public int $timeout = 0; | ||||
| 
 | ||||
|     /** | ||||
|      * Destructor | ||||
|      */ | ||||
|     public function __destruct() | ||||
|     { | ||||
|         $this->close(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @return string | ||||
|      */ | ||||
|     public function getAddress(): string | ||||
|     { | ||||
|         return $this->address; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @return int | ||||
|      */ | ||||
|     public function getPort(): int | ||||
|     { | ||||
|         return $this->port; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @return int | ||||
|      */ | ||||
|     public function getTimeout(): int | ||||
|     { | ||||
|         return $this->timeout; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @return resource | ||||
|      * | ||||
|      * @throws InvalidArgumentException | ||||
|      */ | ||||
|     public function getSocket() | ||||
|     { | ||||
|         if (!$this->socket) { | ||||
|             throw new InvalidArgumentException('Socket not open.'); | ||||
|         } | ||||
| 
 | ||||
|         return $this->socket; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Close | ||||
|      */ | ||||
|     public function close(): void | ||||
|     { | ||||
|         if ($this->socket) { | ||||
|             fclose($this->socket); | ||||
| 
 | ||||
|             $this->socket = null; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $address | ||||
|      * @param int $port | ||||
|      * @param int $timeout | ||||
|      * @param int $engine | ||||
|      * | ||||
|      * @throws SocketException | ||||
|      */ | ||||
|     public function open(string $address, int $port, int $timeout, int $engine): void | ||||
|     { | ||||
|         $this->timeout = $timeout; | ||||
|         $this->engine  = $engine; | ||||
|         $this->port    = $port; | ||||
|         $this->address = $address; | ||||
| 
 | ||||
|         $socket = @fsockopen('udp://' . $address, $port, $errNo, $errStr, $timeout); | ||||
| 
 | ||||
|         if ($errNo || $socket === false) { | ||||
|             throw new SocketException('Could not create socket: ' . $errStr, SocketException::COULD_NOT_CREATE_SOCKET); | ||||
|         } | ||||
| 
 | ||||
|         $this->socket = $socket; | ||||
|         stream_set_timeout($this->socket, $timeout); | ||||
|         stream_set_blocking($this->socket, true); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param int $header | ||||
|      * @param string $string | ||||
|      * | ||||
|      * @return 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); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Reads from socket and returns Buffer. | ||||
|      * | ||||
|      * @param int $length | ||||
|      * | ||||
|      * @return Buffer Buffer | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      * @throws SocketException | ||||
|      * | ||||
|      */ | ||||
|     public function read(int $length = 1400): Buffer | ||||
|     { | ||||
|         $buffer = new Buffer(); | ||||
|         $data = fread($this->socket, $length); | ||||
| 
 | ||||
|         if (!$data) { | ||||
|             throw new SocketException('Failed to open socket.'); | ||||
|         } | ||||
| 
 | ||||
|         $buffer->set($data); | ||||
| 
 | ||||
|         $this->readInternal($buffer, $length, [ $this, 'sherlock' ]); | ||||
| 
 | ||||
|         return $buffer; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param Buffer $buffer | ||||
|      * @param int $length | ||||
|      * | ||||
|      * @return bool | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function sherlock(Buffer $buffer, int $length): bool | ||||
|     { | ||||
|         $data = fread($this->socket, $length); | ||||
| 
 | ||||
|         if (strlen($data) < 4) { | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
|         $buffer->set($data); | ||||
| 
 | ||||
|         return $buffer->getLong() === -2; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * | ||||
|      * Get packet data (count, number, checksum) from the buffer. Different for goldsrc/src. | ||||
|      * | ||||
|      * @param Buffer $buffer | ||||
|      * @param int $count | ||||
|      * @param int $number | ||||
|      * @param bool $isCompressed | ||||
|      * @param int|null $checksum | ||||
|      */ | ||||
|     abstract protected function readInternalPacketData( | ||||
|         Buffer $buffer, | ||||
|         int &$count, | ||||
|         int &$number, | ||||
|         bool &$isCompressed, | ||||
|         ?int &$checksum | ||||
|     ): void; | ||||
| 
 | ||||
|     /** | ||||
|      * @param Buffer $buffer | ||||
|      * @param int $length | ||||
|      * @param callable $sherlockFunction | ||||
|      * | ||||
|      * @return Buffer | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     protected function readInternal(Buffer $buffer, int $length, callable $sherlockFunction): Buffer | ||||
|     { | ||||
|         if ($buffer->remaining() === 0) { | ||||
|             throw new InvalidPacketException('Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY); | ||||
|         } | ||||
| 
 | ||||
|         $header = $buffer->getLong(); | ||||
| 
 | ||||
|         // Single packet, do nothing. | ||||
|         if ($header === -1) { | ||||
|             return $buffer; | ||||
|         } | ||||
| 
 | ||||
|         if ($header === -2) { // Split packet | ||||
|             $packets = []; | ||||
|             $packetCount = 0; | ||||
|             $packetNumber = 0; | ||||
|             $packetChecksum = null; | ||||
| 
 | ||||
|             do { | ||||
|                 $requestId = $buffer->getLong(); | ||||
|                 $isCompressed = ($requestId & 0x80000000) !== 0; | ||||
| 
 | ||||
|                 $this->readInternalPacketData( | ||||
|                     $buffer, | ||||
|                     $packetCount, | ||||
|                     $packetNumber, | ||||
|                     $isCompressed, | ||||
|                     $packetChecksum | ||||
|                 ); | ||||
| 
 | ||||
|                 $packets[$packetNumber] = $buffer->get(); | ||||
| 
 | ||||
|                 $readMore = $packetCount > count($packets); | ||||
|             } while ($readMore && $sherlockFunction($buffer, $length)); | ||||
| 
 | ||||
|             $data = implode($packets); | ||||
| 
 | ||||
|             // TODO: Test this | ||||
|             if ($isCompressed) { | ||||
|                 $data = bzdecompress($data); | ||||
| 
 | ||||
|                 if (!is_string($data) || crc32($data) !== $packetChecksum) { | ||||
|                     throw new InvalidPacketException('CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH); | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             $buffer->set(substr($data, 4)); | ||||
|         } else { | ||||
|             throw new InvalidPacketException('Socket read: Raw packet header mismatch. (0x' . dechex($header) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH); | ||||
|         } | ||||
| 
 | ||||
|         return $buffer; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,38 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| namespace xPaw\SourceQuery\Socket; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Buffer; | ||||
| 
 | ||||
| final class GoldSourceSocket extends AbstractSocket | ||||
| { | ||||
|     /** | ||||
|      * @return int | ||||
|      */ | ||||
|     public function getType(): int | ||||
|     { | ||||
|         return SocketType::GOLDSOURCE; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param Buffer $buffer | ||||
|      * @param int $count | ||||
|      * @param int $number | ||||
|      * @param bool $isCompressed | ||||
|      * @param int|null $checksum | ||||
|      */ | ||||
|     protected function readInternalPacketData( | ||||
|         Buffer $buffer, | ||||
|         int &$count, | ||||
|         int &$number, | ||||
|         bool &$isCompressed, | ||||
|         ?int &$checksum | ||||
|     ): void { | ||||
|         $packetCountAndNumber = $buffer->getByte(); | ||||
|         $count = $packetCountAndNumber & 0xF; | ||||
|         $number = $packetCountAndNumber >> 4; | ||||
|         $isCompressed = false; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,79 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| /** | ||||
|  * @author Pavel Djundik | ||||
|  * | ||||
|  * @link https://xpaw.me | ||||
|  * @link https://github.com/xPaw/PHP-Source-Query | ||||
|  * | ||||
|  * @license GNU Lesser General Public License, version 2.1 | ||||
|  * | ||||
|  * @internal | ||||
|  */ | ||||
| 
 | ||||
| namespace xPaw\SourceQuery\Socket; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Buffer; | ||||
| 
 | ||||
| /** | ||||
|  * Base socket interface | ||||
|  * | ||||
|  * @package xPaw\SourceQuery\Socket | ||||
|  */ | ||||
| interface SocketInterface | ||||
| { | ||||
|     /** | ||||
|      * @return string | ||||
|      */ | ||||
|     public function getAddress(): string; | ||||
| 
 | ||||
|     /** | ||||
|      * @return int | ||||
|      */ | ||||
|     public function getPort(): int; | ||||
| 
 | ||||
|     /** | ||||
|      * @return int | ||||
|      */ | ||||
|     public function getTimeout(): int; | ||||
| 
 | ||||
|     /** | ||||
|      * @return resource | ||||
|      */ | ||||
|     public function getSocket(); | ||||
| 
 | ||||
|     /** | ||||
|      * Get the socket type (goldsrc/src). | ||||
|      */ | ||||
|     public function getType(): int; | ||||
| 
 | ||||
|     /** | ||||
|      * Close | ||||
|      */ | ||||
|     public function close(): void; | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $address | ||||
|      * @param int $port | ||||
|      * @param int $timeout | ||||
|      * @param int $engine | ||||
|      */ | ||||
|     public function open(string $address, int $port, int $timeout, int $engine): void; | ||||
| 
 | ||||
|     /** | ||||
|      * @param int $header | ||||
|      * @param string $string | ||||
|      * | ||||
|      * @return bool | ||||
|      */ | ||||
|     public function write(int $header, string $string = ''): bool; | ||||
| 
 | ||||
|     /** | ||||
|      * @param int $length | ||||
|      * | ||||
|      * @return Buffer | ||||
|      */ | ||||
|     public function read(int $length = 1400): Buffer; | ||||
| } | ||||
| @ -0,0 +1,11 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| namespace xPaw\SourceQuery\Socket; | ||||
| 
 | ||||
| abstract class SocketType | ||||
| { | ||||
|     public const GOLDSOURCE = 0; | ||||
|     public const SOURCE = 1; | ||||
| } | ||||
| @ -0,0 +1,47 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| namespace xPaw\SourceQuery\Socket; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Buffer; | ||||
| use xPaw\SourceQuery\Exception\InvalidPacketException; | ||||
| 
 | ||||
| final class SourceSocket extends AbstractSocket | ||||
| { | ||||
|     /** | ||||
|      * @return int | ||||
|      */ | ||||
|     public function getType(): int | ||||
|     { | ||||
|         return SocketType::SOURCE; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param Buffer $buffer | ||||
|      * @param int $count | ||||
|      * @param int $number | ||||
|      * @param bool $isCompressed | ||||
|      * @param int|null $checksum | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     protected function readInternalPacketData( | ||||
|         Buffer $buffer, | ||||
|         int &$count, | ||||
|         int &$number, | ||||
|         bool &$isCompressed, | ||||
|         ?int &$checksum | ||||
|     ): void { | ||||
|         $count = $buffer->getByte(); | ||||
|         $number = $buffer->getByte() + 1; | ||||
| 
 | ||||
|         if ($isCompressed) { | ||||
|             $buffer->getLong(); // Split size | ||||
| 
 | ||||
|             $checksum = $buffer->getUnsignedLong(); | ||||
|         } else { | ||||
|             $buffer->getShort(); // Split size | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,211 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| namespace xPaw\SourceQuery\Socket; | ||||
| 
 | ||||
| use SplQueue; | ||||
| use SplDoublyLinkedList; | ||||
| use xPaw\SourceQuery\Buffer; | ||||
| use xPaw\SourceQuery\Exception\InvalidPacketException; | ||||
| 
 | ||||
| final class TestableSocket extends AbstractSocket | ||||
| { | ||||
|     /** | ||||
|      * @var SplQueue<string> | ||||
|      */ | ||||
|     private SplQueue $packetQueue; | ||||
| 
 | ||||
|     /** | ||||
|      * @var int | ||||
|      */ | ||||
|     private int $type; | ||||
| 
 | ||||
|     /** | ||||
|      * TestableSocket constructor. | ||||
|      * | ||||
|      * @param int $type | ||||
|      */ | ||||
|     public function __construct(int $type) | ||||
|     { | ||||
|         $this->packetQueue = new SplQueue(); | ||||
|         $this->packetQueue->setIteratorMode(SplDoublyLinkedList::IT_MODE_DELETE); | ||||
|         $this->type = $type; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @return int | ||||
|      */ | ||||
|     public function getType(): int | ||||
|     { | ||||
|         return $this->type; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $data | ||||
|      */ | ||||
|     public function queue(string $data): void | ||||
|     { | ||||
|         $this->packetQueue->push($data); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Close. | ||||
|      */ | ||||
|     public function close(): void | ||||
|     { | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $address | ||||
|      * @param int $port | ||||
|      * @param int $timeout | ||||
|      * @param int $engine | ||||
|      */ | ||||
|     public function open(string $address, int $port, int $timeout, int $engine): void | ||||
|     { | ||||
|         $this->timeout = $timeout; | ||||
|         $this->engine  = $engine; | ||||
|         $this->port    = $port; | ||||
|         $this->address = $address; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param int $header | ||||
|      * @param string $string | ||||
|      * | ||||
|      * @return bool | ||||
|      */ | ||||
|     public function write(int $header, string $string = ''): bool | ||||
|     { | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param int $length | ||||
|      * | ||||
|      * @return Buffer | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function read(int $length = 1400): Buffer | ||||
|     { | ||||
|         $buffer = new Buffer(); | ||||
|         $buffer->set($this->packetQueue->shift()); | ||||
| 
 | ||||
|         $this->readInternal($buffer, $length, [ $this, 'sherlock' ]); | ||||
| 
 | ||||
|         return $buffer; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param Buffer $buffer | ||||
|      * @param int $length | ||||
|      * | ||||
|      * @return bool | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function sherlock(Buffer $buffer, int $length): bool | ||||
|     { | ||||
|         if ($this->packetQueue->isEmpty()) { | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
|         $buffer->set($this->packetQueue->shift()); | ||||
| 
 | ||||
|         return $buffer->getLong() === -2; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param Buffer $buffer | ||||
|      * @param int $count | ||||
|      * @param int $number | ||||
|      * @param bool $isCompressed | ||||
|      * @param int|null $checksum | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     protected function readInternalPacketData( | ||||
|         Buffer $buffer, | ||||
|         int &$count, | ||||
|         int &$number, | ||||
|         bool &$isCompressed, | ||||
|         ?int &$checksum | ||||
|     ): void { | ||||
|         switch ($this->type) { | ||||
|             case SocketType::GOLDSOURCE: | ||||
|                 $this->readInternalPacketDataGoldSource( | ||||
|                     $buffer, | ||||
|                     $count, | ||||
|                     $number, | ||||
|                     $isCompressed, | ||||
|                     $checksum | ||||
|                 ); | ||||
| 
 | ||||
|                 break; | ||||
| 
 | ||||
|             case SocketType::SOURCE: | ||||
|             default: | ||||
|                 $this->readInternalPacketDataSource( | ||||
|                     $buffer, | ||||
|                     $count, | ||||
|                     $number, | ||||
|                     $isCompressed, | ||||
|                     $checksum | ||||
|                 ); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Same as GoldSourceSocket::readInternalPacketData. | ||||
|      * | ||||
|      * @param Buffer $buffer | ||||
|      * @param int $count | ||||
|      * @param int $number | ||||
|      * @param bool $isCompressed | ||||
|      * @param int|null $checksum | ||||
|      */ | ||||
|     protected function readInternalPacketDataGoldSource( | ||||
|         Buffer $buffer, | ||||
|         int &$count, | ||||
|         int &$number, | ||||
|         bool &$isCompressed, | ||||
|         ?int &$checksum | ||||
|     ): void { | ||||
|         $packetCountAndNumber = $buffer->getByte(); | ||||
|         $count = $packetCountAndNumber & 0xF; | ||||
|         $number = $packetCountAndNumber >> 4; | ||||
|         $isCompressed = false; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Same as SourceSocket::readInternalPacketData. | ||||
|      * | ||||
|      * @param Buffer $buffer | ||||
|      * @param int $count | ||||
|      * @param int $number | ||||
|      * @param bool $isCompressed | ||||
|      * @param int|null $checksum | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     protected function readInternalPacketDataSource( | ||||
|         Buffer $buffer, | ||||
|         int &$count, | ||||
|         int &$number, | ||||
|         bool &$isCompressed, | ||||
|         ?int &$checksum | ||||
|     ): void { | ||||
|         $count = $buffer->getByte(); | ||||
|         $number = $buffer->getByte() + 1; | ||||
| 
 | ||||
|         if ($isCompressed) { | ||||
|             $buffer->getLong(); // Split size | ||||
| 
 | ||||
|             $checksum = $buffer->getUnsignedLong(); | ||||
|         } else { | ||||
|             $buffer->getShort(); // Split size | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -1,226 +0,0 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| /** | ||||
|  * @author Pavel Djundik | ||||
|  * | ||||
|  * @link https://xpaw.me | ||||
|  * @link https://github.com/xPaw/PHP-Source-Query | ||||
|  * | ||||
|  * @license GNU Lesser General Public License, version 2.1 | ||||
|  * | ||||
|  * @internal | ||||
|  */ | ||||
| 
 | ||||
| namespace xPaw\SourceQuery; | ||||
| 
 | ||||
| use xPaw\SourceQuery\Exception\AuthenticationException; | ||||
| use xPaw\SourceQuery\Exception\InvalidPacketException; | ||||
| use xPaw\SourceQuery\Exception\SocketException; | ||||
| 
 | ||||
| /** | ||||
|  * Class SourceRcon | ||||
|  * | ||||
|  * @package xPaw\SourceQuery | ||||
|  * | ||||
|  * @uses AuthenticationException | ||||
|  * @uses InvalidPacketException | ||||
|  * @uses SocketException | ||||
|  */ | ||||
| final class SourceRcon | ||||
| { | ||||
|     /** | ||||
|      * Points to socket class | ||||
|      */ | ||||
|     private BaseSocket $Socket; | ||||
| 
 | ||||
|     /** | ||||
|      * @var ?resource | ||||
|      */ | ||||
|     private $RconSocket; | ||||
| 
 | ||||
|     /** | ||||
|      * @var int $RconRequestId | ||||
|      */ | ||||
|     private int $RconRequestId = 0; | ||||
| 
 | ||||
|     /** | ||||
|      * @param BaseSocket $Socket | ||||
|      */ | ||||
|     public function __construct(BaseSocket $Socket) | ||||
|     { | ||||
|         $this->Socket = $Socket; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Close | ||||
|      */ | ||||
|     public function Close(): void | ||||
|     { | ||||
|         if ($this->RconSocket) { | ||||
|             fclose($this->RconSocket); | ||||
| 
 | ||||
|             $this->RconSocket = null; | ||||
|         } | ||||
| 
 | ||||
|         $this->RconRequestId = 0; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @throws SocketException | ||||
|      */ | ||||
|     public function Open(): void | ||||
|     { | ||||
|         if (!$this->RconSocket) { | ||||
|             $RconSocket = @fsockopen($this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout); | ||||
| 
 | ||||
|             if ($ErrNo || !$RconSocket) { | ||||
|                 throw new SocketException('Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED); | ||||
|             } | ||||
| 
 | ||||
|             $this->RconSocket = $RconSocket; | ||||
|             stream_set_timeout($this->RconSocket, $this->Socket->Timeout); | ||||
|             stream_set_blocking($this->RconSocket, true); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param int $Header | ||||
|      * @param string $String | ||||
|      * | ||||
|      * @return bool | ||||
|      */ | ||||
|     public function Write(int $Header, string $String = ''): bool | ||||
|     { | ||||
|         // Pack the packet together | ||||
|         $Command = pack('VV', ++$this->RconRequestId, $Header) . $String . "\x00\x00"; | ||||
| 
 | ||||
|         // Prepend packet length | ||||
|         $Command = pack('V', strlen($Command)) . $Command; | ||||
|         $Length  = strlen($Command); | ||||
| 
 | ||||
|         return $Length === fwrite($this->RconSocket, $Command, $Length); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @return Buffer | ||||
|      * | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function Read(): Buffer | ||||
|     { | ||||
|         $Buffer = new Buffer(); | ||||
|         $Buffer->Set(fread($this->RconSocket, 4)); | ||||
| 
 | ||||
|         if ($Buffer->Remaining() < 4) { | ||||
|             throw new InvalidPacketException('Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY); | ||||
|         } | ||||
| 
 | ||||
|         $PacketSize = $Buffer->GetLong(); | ||||
| 
 | ||||
|         $Buffer->Set(fread($this->RconSocket, $PacketSize)); | ||||
| 
 | ||||
|         $Data = $Buffer->Get(); | ||||
| 
 | ||||
|         $Remaining = $PacketSize - strlen($Data); | ||||
| 
 | ||||
|         while ($Remaining > 0) { | ||||
|             $Data2 = fread($this->RconSocket, $Remaining); | ||||
| 
 | ||||
|             $PacketSize = strlen($Data2); | ||||
| 
 | ||||
|             if ($PacketSize === 0) { | ||||
|                 throw new InvalidPacketException('Read ' . strlen($Data) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY); | ||||
|             } | ||||
| 
 | ||||
|             $Data .= $Data2; | ||||
|             $Remaining -= $PacketSize; | ||||
|         } | ||||
| 
 | ||||
|         $Buffer->Set($Data); | ||||
| 
 | ||||
|         return $Buffer; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $Command | ||||
|      * | ||||
|      * @return string | ||||
|      * | ||||
|      * @throws AuthenticationException | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function Command(string $Command): string | ||||
|     { | ||||
|         $this->Write(SourceQuery::SERVERDATA_EXECCOMMAND, $Command); | ||||
|         $Buffer = $this->Read(); | ||||
| 
 | ||||
|         $Buffer->GetLong(); // RequestID | ||||
| 
 | ||||
|         $Type = $Buffer->GetLong(); | ||||
| 
 | ||||
|         if ($Type === SourceQuery::SERVERDATA_AUTH_RESPONSE) { | ||||
|             throw new AuthenticationException('Bad rcon_password.', AuthenticationException::BAD_PASSWORD); | ||||
|         } elseif ($Type !== SourceQuery::SERVERDATA_RESPONSE_VALUE) { | ||||
|             throw new InvalidPacketException('Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH); | ||||
|         } | ||||
| 
 | ||||
|         $Data = $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($Data) >= 4000) { | ||||
|             $this->Write(SourceQuery::SERVERDATA_REQUESTVALUE); | ||||
| 
 | ||||
|             do { | ||||
|                 $Buffer = $this->Read(); | ||||
| 
 | ||||
|                 $Buffer->GetLong(); // RequestID | ||||
| 
 | ||||
|                 if ($Buffer->GetLong() !== SourceQuery::SERVERDATA_RESPONSE_VALUE) { | ||||
|                     break; | ||||
|                 } | ||||
| 
 | ||||
|                 $Data2 = $Buffer->Get(); | ||||
| 
 | ||||
|                 if ($Data2 === "\x00\x01\x00\x00\x00\x00") { | ||||
|                     break; | ||||
|                 } | ||||
| 
 | ||||
|                 $Data .= $Data2; | ||||
|             } while (true); | ||||
|         } | ||||
| 
 | ||||
|         return rtrim($Data, "\0"); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $Password | ||||
|      * | ||||
|      * @throws AuthenticationException | ||||
|      * @throws InvalidPacketException | ||||
|      */ | ||||
|     public function Authorize(string $Password): void | ||||
|     { | ||||
|         $this->Write(SourceQuery::SERVERDATA_AUTH, $Password); | ||||
|         $Buffer = $this->Read(); | ||||
| 
 | ||||
|         $RequestID = $Buffer->GetLong(); | ||||
|         $Type      = $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) { | ||||
|             $Buffer = $this->Read(); | ||||
| 
 | ||||
|             $RequestID = $Buffer->GetLong(); | ||||
|             $Type      = $Buffer->GetLong(); | ||||
|         } | ||||
| 
 | ||||
|         if ($RequestID === -1 || $Type !== SourceQuery::SERVERDATA_AUTH_RESPONSE) { | ||||
|             throw new AuthenticationException('RCON authorization failed.', AuthenticationException::BAD_PASSWORD); | ||||
|         } | ||||
|     } | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue
	
	 Anthony Birkett
						Anthony Birkett