-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Insax
committed
Feb 6, 2024
1 parent
4f4b23d
commit 75d75ab
Showing
39 changed files
with
1,013 additions
and
593 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
app/Gameserver/Communication/Responses/BanPlayerResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
app/Gameserver/Communication/Responses/BroadcastResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
app/Gameserver/Communication/Responses/KickPlayerResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
Oops, something went wrong.