1
0
mirror of https://github.com/xPaw/PHP-Source-Query.git synced 2026-06-11 00:23:15 +02:00

Split up Socket and Rcon classes to be engine specific extensions.

This commit is contained in:
Anthony Birkett
2021-05-31 11:12:21 +01:00
parent a2f7834ef5
commit b01c1f643f
20 changed files with 1520 additions and 1156 deletions
+28
View File
@@ -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;
}
+175
View File
@@ -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;
}
}
+44
View File
@@ -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;
}
+235
View File
@@ -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;
}
}