From bd4c7b25c4bc22730ce0e562f9ff5dc90aae9a55 Mon Sep 17 00:00:00 2001 From: kirillius Date: Thu, 26 Dec 2024 13:35:04 +0300 Subject: [PATCH] quagga plugin refactor --- README.md | 12 ++-- assets/App.js | 16 ------ classes/IPluggable.php | 7 +-- classes/PluginContext.php | 35 ++++++++++++ classes/RPC.php | 8 +-- plugins/quagga/QuaggaPlugin.php | 99 ++++++++++++++++++++++++++------- plugins/quagga/metadata.json | 6 +- plugins/quagga/plugin.js | 20 ++++++- plugins/updates/Updates.php | 20 ++----- zebracfg.php | 72 ------------------------ 10 files changed, 152 insertions(+), 143 deletions(-) create mode 100644 classes/PluginContext.php delete mode 100644 zebracfg.php diff --git a/README.md b/README.md index 922380d..2ba54e9 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,13 @@ ### Установка ```shell -apk add php php-session jq -``` - -```shell +apk add php php-session jq git +cd /opt +git clone https://git.kirillius.ru/kirillius/protected-resources-list.git +cd /opt/protected-resources-list/ chmod -R +x ./bin +ln -s /opt/protected-resources-list/bin/webui /etc/init.d/webui +cp config.json.example config.json +rc-update add webui +/etc/init.d/webui start ``` \ No newline at end of file diff --git a/assets/App.js b/assets/App.js index 8a785b4..4ffa2b1 100644 --- a/assets/App.js +++ b/assets/App.js @@ -67,22 +67,6 @@ App.render = async function () { self.prop("disabled", false); })(); }); - $("#restart-quagga").click(function () { - if (confirm("Are you sure?")) { - const self = $(this); - self.prop("disabled", true); - (async function () { - try { - alert(await JSONRPC.__invoke("restart_quagga")); - } finally { - setTimeout(() => { - self.prop("disabled", false); - }, 5000); - } - - })(); - } - }); } diff --git a/classes/IPluggable.php b/classes/IPluggable.php index 330d27e..149322f 100644 --- a/classes/IPluggable.php +++ b/classes/IPluggable.php @@ -4,10 +4,5 @@ interface IPluggable { const PROTECTED_NAMES = ["enable", "disable", "render", "init"]; - public function enable(); - - public function disable(); - - - public function init(); + public function init(PluginContext $context); } \ No newline at end of file diff --git a/classes/PluginContext.php b/classes/PluginContext.php new file mode 100644 index 0000000..e863662 --- /dev/null +++ b/classes/PluginContext.php @@ -0,0 +1,35 @@ +RPC = $RPC; + $this->config = $config; + $this->metadata = $metadata; + } + + public function getRPC(): RPC + { + return $this->RPC; + } + + public function getConfig(): Config + { + return $this->config; + } + + public function getMetadata(): array + { + return $this->metadata; + } +} \ No newline at end of file diff --git a/classes/RPC.php b/classes/RPC.php index 4d95872..0f67dbc 100644 --- a/classes/RPC.php +++ b/classes/RPC.php @@ -13,7 +13,8 @@ class RPC foreach ($this->config["plugins"] as $plugin) { try { $meta = $this->getPluginMetadata($plugin); - $this->loadPlugin($plugin, $meta["class"]); + $inst = $this->loadPlugin($plugin, $meta["class"]); + $inst->init(new PluginContext($this, $this->config, $meta)); } catch (Error $e) { continue; } @@ -39,7 +40,7 @@ class RPC return $meta; } - private function loadPlugin($name, $classname): void + private function loadPlugin($name, $classname): IPluggable { $file = dirname(__DIR__) . "/plugins/" . $name . "/" . $classname . ".php"; if (!file_exists($file)) { @@ -52,7 +53,7 @@ class RPC throw new RuntimeException("Class $classname have to implement IPluggable"); } - $this->plugins[$name] = $instance; + return $this->plugins[$name] = $instance; } public function getConfig(): array @@ -80,7 +81,6 @@ class RPC { $this->checkAuth(); return (new NetworkConfigReader())->getConfigs(); - } diff --git a/plugins/quagga/QuaggaPlugin.php b/plugins/quagga/QuaggaPlugin.php index 3b4d1f0..f76a613 100644 --- a/plugins/quagga/QuaggaPlugin.php +++ b/plugins/quagga/QuaggaPlugin.php @@ -1,32 +1,89 @@ context->getConfig()["quagga"]["file"]; + + if (!file_exists($configfile)) { + throw new RuntimeException("Quagga config file not found"); + } + + $networks = (new NetworkConfigReader())->getConfigs(); + $routeParser = new RoutingTableReader(); + $routes = $routeParser->getRoutes(); + $defGatewayInterface = ""; + $defGateway = ""; + + foreach ($routes as $route) { + if ($route["dst"] === "0.0.0.0/0") { + $defGatewayInterface = $route["dev"]; + $defGateway = $route["gateway"]; + break; + } + } + + if (!$defGatewayInterface) { + throw new RuntimeException("Failed to detect default gateway interface"); + } + + $contents = file_get_contents($configfile); + $lines = explode("\n", $contents); + + //remove existing routes + foreach ($lines as $key => $line) { + if (str_starts_with($line, self::REM_PREFIX) or str_starts_with($line, "ip route ") and str_contains($line . " ", $defGateway . " ")) { + unset($lines[$key]); + } + } + + + //add new routes + foreach ($this->context->getConfig()["networks"] as $key) { + $lines[] = self::REM_PREFIX . $key; + if (isset($networks[$key])) { + foreach ($networks[$key]["networks"] as $route) { + $lines[] = "ip route " . $route . " " . $defGateway; + } + } + } + + foreach ($lines as $key => $line) { + if (trim($line) === "") { + unset($lines[$key]); + } + } + + + $backupFile = $configfile . ".sav"; + + unlink($backupFile); + rename($configfile, $backupFile); + file_put_contents($configfile, implode("\n", $lines)); + + //restart zebra + return shell_exec($this->context->getConfig()["quagga"]["restart_cmd"]); + - public function init() - { - // TODO: Implement init() method. } + public function init(PluginContext $context): void + { + $this->context = $context; + $this->checkConfig(); + } + + private function checkConfig(): void + { + $config = $this->context->getConfig(); + if (!isset($config["quagga"])) { + $config["quagga"] = $this->context->getMetadata()["config"]; + } + } } \ No newline at end of file diff --git a/plugins/quagga/metadata.json b/plugins/quagga/metadata.json index 3b8cea7..2e19a60 100644 --- a/plugins/quagga/metadata.json +++ b/plugins/quagga/metadata.json @@ -1,9 +1,7 @@ { "class": "QuaggaPlugin", "config": { - "quagga": { - "restart_cmd": "/etc/init.d/zebra restart", - "file": "/etc/quagga/zebra.conf" - } + "restart_cmd": "/etc/init.d/zebra restart", + "file": "/etc/quagga/zebra.conf" } } \ No newline at end of file diff --git a/plugins/quagga/plugin.js b/plugins/quagga/plugin.js index a8f4a98..3678bd7 100644 --- a/plugins/quagga/plugin.js +++ b/plugins/quagga/plugin.js @@ -1 +1,19 @@ -// \ No newline at end of file +(async function () { + $("#buttons").append(``); + $("#restart-quagga").click(function () { + if (confirm("Are you sure?")) { + const self = $(this); + self.prop("disabled", true); + (async function () { + try { + alert(await JSONRPC.__invoke("restart_quagga")); + } finally { + setTimeout(() => { + self.prop("disabled", false); + }, 5000); + } + + })(); + } + }); +})(); \ No newline at end of file diff --git a/plugins/updates/Updates.php b/plugins/updates/Updates.php index 2acf88f..6edc71e 100644 --- a/plugins/updates/Updates.php +++ b/plugins/updates/Updates.php @@ -3,21 +3,6 @@ class Updates implements IPluggable { - public function enable() - { - - } - - public function disable() - { - - } - - - public function init() - { - - } public function check(): ?bool { @@ -34,4 +19,9 @@ class Updates implements IPluggable { return @shell_exec("git --no-pager pull --verbose 2>&1"); } + + public function init(PluginContext $context) + { + + } } \ No newline at end of file diff --git a/zebracfg.php b/zebracfg.php deleted file mode 100644 index 6b13307..0000000 --- a/zebracfg.php +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/php -getConfigs(); - $config = new Config(); - $config->read(); - - $routeParser = new RoutingTableReader(); - $routes = $routeParser->getRoutes(); - $defGatewayInterface = ""; - $defGateway = ""; - - foreach ($routes as $route) { - if ($route["dst"] === "0.0.0.0/0") { - $defGatewayInterface = $route["dev"]; - $defGateway = $route["gateway"]; - break; - } - } - - if (!$defGatewayInterface) { - throw new RuntimeException("Failed to detect default gateway interface"); - } - - $contents = file_get_contents(CFGFILE); - $lines = explode("\n", $contents); - - //remove existing routes - foreach ($lines as $key => $line) { - if (str_starts_with($line, REM_PREFIX) or str_starts_with($line, "ip route ") and str_contains($line . " ", $defGateway . " ")) { - unset($lines[$key]); - } - } - - //add new routes - foreach ($config["networks"] as $key) { - $lines[] = REM_PREFIX . $key; - if (isset($networks[$key])) { - foreach ($networks[$key]["networks"] as $route) { - $lines[] = "ip route " . $route . " " . $defGateway; - } - } - } - - foreach ($lines as $key => $line) { - if (trim($line) === "") { - unset($lines[$key]); - } - } - - $backupFile = CFGFILE . ".sav"; - - unlink($backupFile); - rename(CFGFILE, $backupFile); - file_put_contents(CFGFILE, implode("\n", $lines)); - - //restart zebra - echo shell_exec($config["zebra_restart_cmd"]); -} catch (Exception $e) { - echo "\nError:" . $e->getMessage() . "\n"; - exit(1); -} - -exit(0); \ No newline at end of file