Skip to content

Commit

Permalink
Rework RCON
Browse files Browse the repository at this point in the history
  • Loading branch information
Insax committed Feb 6, 2024
1 parent 4f4b23d commit 75d75ab
Show file tree
Hide file tree
Showing 39 changed files with 1,013 additions and 593 deletions.
12 changes: 11 additions & 1 deletion app/Console/Commands/SyncPlayersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use App\Models\Server;
use App\Models\ServerWhitelist;
use Illuminate\Console\Command;
use RCON;
use Rcon;

class SyncPlayersCommand extends Command
{
private $servers;

/**
* The name and signature of the console command.
*
Expand All @@ -30,6 +32,13 @@ class SyncPlayersCommand extends Command
*/
public function handle()
{
$this->servers = Server::whereActive(true);
foreach ($this->servers as $server)
{
$result = Rcon::showPlayers($server);
var_dump($result);
}
/*
foreach (Server::whereActive(true)->get() as $server)
{
$onlinePlayers = array();
Expand Down Expand Up @@ -83,5 +92,6 @@ public function handle()
}
}
}
*/
}
}
6 changes: 3 additions & 3 deletions app/PalWorld/RCON/Facades/Facade.php → app/Facades/Rcon.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace App\PalWorld\RCON\Facades;
namespace App\Facades;

use Illuminate\Support\Facades\Facade as BaseFacade;

final class Facade extends BaseFacade
class Rcon extends BaseFacade
{
/**
* Return Laravel Framework facade accessor name.
Expand All @@ -13,6 +13,6 @@ final class Facade extends BaseFacade
*/
protected static function getFacadeAccessor()
{
return 'RCON';
return 'Rcon';
}
}
59 changes: 59 additions & 0 deletions app/Gameserver/Communication/Rcon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Gameserver\Communication;

use App\Gameserver\Communication\Responses\BroadcastResponse;
use App\Gameserver\Communication\Responses\InfoResponse;
use App\Gameserver\Communication\Responses\KickPlayerResponse;
use App\Gameserver\Communication\Responses\BanPlayerResponse;
use App\Gameserver\Communication\Responses\Response;
use App\Gameserver\Communication\Responses\SaveResponse;
use App\Gameserver\Communication\Responses\ShowPlayersResponse;
use App\Models\Server;
use App\Support\RCON\PalworldRcon;

class Rcon
{
public function info(Server $server) : Response
{
$rcon = new PalworldRcon($server->rconData->host, $server->rconData->port, \Crypt::decrypt($server->rconData->password), $server->rconData->timeout);
$response = $rcon->command('info');
return new InfoResponse($response);
}

public function showPlayers(Server $server) : Response
{
$rcon = new PalworldRcon($server->rconData->host, $server->rconData->port, \Crypt::decrypt($server->rconData->password), $server->rconData->timeout);
$response = $rcon->command('showPlayers');
return new ShowPlayersResponse($response);
}

public function kickPlayer(Server $server, string|int $playerId) : Response
{
$rcon = new PalworldRcon($server->rconData->host, $server->rconData->port, \Crypt::decrypt($server->rconData->password), $server->rconData->timeout);
$response = $rcon->command("kick $playerId");
return new KickPlayerResponse($response);
}

public function banPlayer(Server $server, string|int $playerId) : Response
{
$rcon = new PalworldRcon($server->rconData->host, $server->rconData->port, \Crypt::decrypt($server->rconData->password), $server->rconData->timeout);
$response = $rcon->command("ban $playerId");
return new BanPlayerResponse($response);
}

public function broadcast(Server $server, string $message) : Response
{
$message = str_replace($message, ' ', '\x1f');
$rcon = new PalworldRcon($server->rconData->host, $server->rconData->port, \Crypt::decrypt($server->rconData->password), $server->rconData->timeout);
$response = $rcon->command("broadcast $message");
return new BroadcastResponse($response);
}

public function save(Server $server)
{
$rcon = new PalworldRcon($server->rconData->host, $server->rconData->port, \Crypt::decrypt($server->rconData->password), $server->rconData->timeout);
$response = $rcon->command("save");
return new SaveResponse($response);
}
}
32 changes: 32 additions & 0 deletions app/Gameserver/Communication/Responses/BanPlayerResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Short description for file
*
* Long description for file (if any)...
*
* PHP version 8.1
*
* @author insa
* @license LICENSE.md
* @category CategoryName
* @copyright 2024 insa
* @package PackageName
*/

namespace App\Gameserver\Communication\Responses;

use App\Gameserver\Communication\Responses\Response;

class BanPlayerResponse extends Response
{

protected function extractBody(array $lines): array
{
return ['banned' => substr($this->getHeader(), 8)];
}

protected function isExpectedHeaderText(string $header): bool
{
return !strncmp('Banned: ', $header, 8);
}
}
32 changes: 32 additions & 0 deletions app/Gameserver/Communication/Responses/BroadcastResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Short description for file
*
* Long description for file (if any)...
*
* PHP version 8.1
*
* @author insa
* @license LICENSE.md
* @category CategoryName
* @copyright 2024 insa
* @package PackageName
*/

namespace App\Gameserver\Communication\Responses;

use App\Gameserver\Communication\Responses\Response;

class BroadcastResponse extends Response
{

protected function extractBody(array $lines): array
{
return ['message' => substr($this->getHeader(),14, null)];
}

protected function isExpectedHeaderText(string $header): bool
{
return !strncmp('Broadcasted: ', $header, 13);
}
}
34 changes: 34 additions & 0 deletions app/Gameserver/Communication/Responses/InfoResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Short description for file
*
* Long description for file (if any)...
*
* PHP version 8.1
*
* @author insa
* @license LICENSE.md
* @category CategoryName
* @copyright 2024 insa
* @package PackageName
*/

namespace App\Gameserver\Communication\Responses;

use App\Gameserver\Communication\Responses\Response;

class InfoResponse extends Response
{
protected function extractBody(array $lines): array
{
$regex = '/Server\[(v[\d.]+)]\s(.*)/';

preg_match($regex, $this->getHeader(), $matches);
return ['version' => $matches[1], 'serverName' => $matches[2]];
}

protected function isExpectedHeaderText(string $header) : bool
{
return !strncmp('Welcome to Pal Server', $header, 21);
}
}
32 changes: 32 additions & 0 deletions app/Gameserver/Communication/Responses/KickPlayerResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Short description for file
*
* Long description for file (if any)...
*
* PHP version 8.1
*
* @author insa
* @license LICENSE.md
* @category CategoryName
* @copyright 2024 insa
* @package PackageName
*/

namespace App\Gameserver\Communication\Responses;

use App\Gameserver\Communication\Responses\Response;

class KickPlayerResponse extends Response
{

protected function extractBody(array $lines): array
{
return ['kicked' => substr($this->getHeader(), 8)];
}

protected function isExpectedHeaderText(string $header): bool
{
return !strncmp('Kicked: ', $header, 8);
}
}
87 changes: 87 additions & 0 deletions app/Gameserver/Communication/Responses/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Short description for file
*
* Long description for file (if any)...
*
* PHP version 8.1
*
* @author insa
* @license LICENSE.md
* @category CategoryName
* @copyright 2024 insa
* @package PackageName
*/

namespace App\Gameserver\Communication\Responses;

abstract class Response
{
protected string $header;
protected array $body;
protected bool $isErrorResponse = false;
private array $lines;
private array $result;
private int $error = 0;

const ERROR_CONNECT = 1;
const ERROR_AUTH = 2;
const ERROR_CMD = 3;
public function __construct(private readonly string $responseText)
{
$this->extractHeader();
if($this->isErrorResponse)
return;
$this->result = $this->extractBody($this->lines);
}

public function getError()
{
return $this->error;
}

public function getResult() : array
{
return $this->result;
}

public function getHeader() : string
{
return $this->header;
}

protected abstract function extractBody(array $lines) : array;

private function extractHeader()
{
$this->lines = explode("\n", $this->responseText);
$this->header = array_shift($this->lines);
$this->checkHeader();
}

protected abstract function isExpectedHeaderText(string $header) : bool;

private function checkHeader()
{
if($this->isExpectedHeaderText($this->header))
return;

$this->isErrorResponse = true;

if($this->header == 'Authorization rejected')
{
$this->error = self::ERROR_AUTH;
\Log::alert($this->header);
}
elseif (strncmp('Could not connect to RCON', $this->header, 25))
{
$this->error = self::ERROR_CONNECT;
\Log::critical($this->header);
}
else
{
$this->error = self::ERROR_CMD;
\Log::info('Command Failed');
}
}
}
32 changes: 32 additions & 0 deletions app/Gameserver/Communication/Responses/SaveResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Short description for file
*
* Long description for file (if any)...
*
* PHP version 8.1
*
* @author insa
* @license LICENSE.md
* @category CategoryName
* @copyright 2024 insa
* @package PackageName
*/

namespace App\Gameserver\Communication\Responses;

use App\Gameserver\Communication\Responses\Response;

class SaveResponse extends Response
{

protected function extractBody(array $lines): array
{
return [];
}

protected function isExpectedHeaderText(string $header): bool
{
return $header == 'Complete Save';
}
}
Loading

0 comments on commit 75d75ab

Please sign in to comment.