This commit is contained in:
Collin 2024-02-01 23:11:11 +00:00
parent 6d1891a583
commit 5833b2a660
4 changed files with 105 additions and 9 deletions

5
.gitignore vendored
View File

@ -1,3 +1,6 @@
build/run-phar.sh
build/debug/*
build/release/*
src/vendor/
src/vendor/
Client.php
test.php

View File

@ -1 +1 @@
cd build/debug ; php LonaDB-2.0.phar
cd build/debug ; php LonaDB-4.0.0-beta.phar

View File

@ -62,12 +62,14 @@ class LonaDB {
$this->Logger->LoadLogger();
$this->Logger->DropCache();
$this->Logger->Info("Loading Server class.");
$this->Server = new Server($this);
$this->Logger->Info("Loading TableManager class.");
$this->TableManager = new TableManager($this);
$this->Logger->Info("Loading UserManager class.");
$this->UserManager = new UserManager($this);
//The server has to be loaded as the last class!
$this->Logger->Info("Loading Server class.");
$this->Server = new Server($this);
}
catch (\Exception $e){
$this->Logger->Error($e);
@ -78,7 +80,7 @@ class LonaDB {
$this->Logger->InfoCache("Invalid or missing config. Starting setup.");
echo "Database port:\n";
$portHandle = fopen ("php://stdin","r");
$port = fgets($portHandle);
$port = intval(str_replace("\n", "", fgets($portHandle)));
fclose($portHandle);
echo "Database address:\n";
@ -107,7 +109,7 @@ class LonaDB {
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(AES_256_CBC));
$save = array(
"port" => str_replace("\n","",$port),
"port" => $port,
"address" => str_replace("\n","",$addr),
"logging" => $log,
"encryptionKey" => str_replace("\n","",$key),

View File

@ -9,10 +9,101 @@ class Server {
private array $config;
private LonaDB $LonaDB;
private string $address;
private int $port;
private array $actions = [];
public function __construct(LonaDB $lonaDB) {
$this->LonaDB = $lonaDB;
$this->config = $lonaDB->config;
}
}
?>
$this->address = $this->config["address"];
$this->port = $this->config["port"];
$this->loadActions();
$this->startSocket();
}
private function loadActions() {
$actionFiles = scandir(__DIR__ . "/Actions/");
foreach ($actionFiles as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$actionName = pathinfo($file, PATHINFO_FILENAME);
$this->actions[$actionName] = require(__DIR__ . "/Actions/" . $file);
$this->LonaDB->Logger->Info("Loaded Networking action from file '".$actionName."'" . PHP_EOL);
}
}
}
public function startSocket() {
$socket = stream_socket_server("tcp://".$this->address.":".$this->port, $errno, $errstr);
if (!$socket) {
$this->LonaDB->Logger->Error($errno." - ".$errstr . PHP_EOL);
exit();
}
$this->LonaDB->Logger->Info("Server listening on port " . $this->port. PHP_EOL);
stream_set_blocking($socket, 0);
while ($client = stream_socket_accept($socket, -1)) {
$pid = pcntl_fork();
if ($pid == -1) {
} elseif ($pid) {
fclose($client);
} else {
$this->handleClient($client);
exit();
}
}
}
private function handleClient($client) {
$dataString = stream_get_contents($client, 1048576);
$this->handleData($dataString, $client);
fclose($client);
}
private function handleData($dataString, $client) {
try {
$data = json_decode($dataString, true);
$password = $data['login']['password'];
$login = $this->LonaDB->UserManager->CheckPassword($data['login']['name'], $password);
if (!$login) {
$response = json_encode(["success" => false, "err" => "login_error", "process" => $data['process']]);
fwrite($client, $response);
return;
}
if (!$data['process']) {
$response = json_encode(["success" => false, "err" => "bad_process_id", "process" => $data['process']]);
fwrite($client, $response);
return;
}
if (!$this->actions[$data['action']]) {
$response = json_encode(["success" => false, "err" => "action_not_found"]);
fwrite($client, $response);
return;
}
try {
$this->actions[$data['action']]->Run($this->LonaDB, $data, $client);
} catch (Exception $e) {
$this->LonaDB->Loggin->Error($e->getMessage() . PHP_EOL);
}
ob_end_flush();
ob_flush();
flush();
} catch (Exception $e) {
$this->LonaDB->Loggin->Error($e->getMessage() . PHP_EOL);
}
}
}