From 5833b2a6605a86cd48f272565527c247b7bd5c1b Mon Sep 17 00:00:00 2001 From: Collin Date: Thu, 1 Feb 2024 23:11:11 +0000 Subject: [PATCH] --- .gitignore | 5 ++- build/run-phar.sh | 2 +- src/LonaDB/LonaDB.php | 10 +++-- src/LonaDB/Server.php | 97 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 105 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 3c4cb68..049205d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +build/run-phar.sh build/debug/* build/release/* -src/vendor/ \ No newline at end of file +src/vendor/ +Client.php +test.php \ No newline at end of file diff --git a/build/run-phar.sh b/build/run-phar.sh index 60eec8d..f81e258 100755 --- a/build/run-phar.sh +++ b/build/run-phar.sh @@ -1 +1 @@ -cd build/debug ; php LonaDB-2.0.phar \ No newline at end of file +cd build/debug ; php LonaDB-4.0.0-beta.phar \ No newline at end of file diff --git a/src/LonaDB/LonaDB.php b/src/LonaDB/LonaDB.php index a2cd227..a2e90e5 100644 --- a/src/LonaDB/LonaDB.php +++ b/src/LonaDB/LonaDB.php @@ -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), diff --git a/src/LonaDB/Server.php b/src/LonaDB/Server.php index 3314f5a..b9ea41d 100644 --- a/src/LonaDB/Server.php +++ b/src/LonaDB/Server.php @@ -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); + } + } +} \ No newline at end of file