From fdd33ffe31c22569c480c8608840962c37098772 Mon Sep 17 00:00:00 2001 From: kirillius Date: Thu, 26 Dec 2024 14:27:57 +0300 Subject: [PATCH] openvpn plugin refactor --- bin/ovpn-connect | 29 +++++++++++++++++++++++++++++ classes/RPC.php | 4 ++-- classes/StaticRPC.php | 15 +++++++++++++++ plugins/openvpn/Openvpn.php | 33 +++++++++++++++++++++++++++++++++ plugins/openvpn/metadata.json | 6 ++++++ plugins/openvpn/ovpn-connect | 35 ----------------------------------- 6 files changed, 85 insertions(+), 37 deletions(-) create mode 100755 bin/ovpn-connect create mode 100644 classes/StaticRPC.php create mode 100755 plugins/openvpn/Openvpn.php create mode 100644 plugins/openvpn/metadata.json delete mode 100755 plugins/openvpn/ovpn-connect diff --git a/bin/ovpn-connect b/bin/ovpn-connect new file mode 100755 index 0000000..cf9b845 --- /dev/null +++ b/bin/ovpn-connect @@ -0,0 +1,29 @@ +#!/usr/bin/php +getPlugins()["openvpn"] ?? null; + if ($instance === null) { + throw new RuntimeException("Plugin is not enabled"); + } + /** + * @var Openvpn $instance + */ + + $config = $instance->getRoutingConfig(); + $outfile = $argv[1]; + file_put_contents($outfile, implode("\n", $config)); +} catch (Exception $e) { + echo "\nError:" . $e->getMessage() . "\n"; + exit(1); +} + +exit(0); \ No newline at end of file diff --git a/classes/RPC.php b/classes/RPC.php index 0f67dbc..1a0be96 100644 --- a/classes/RPC.php +++ b/classes/RPC.php @@ -2,8 +2,8 @@ class RPC { - private Config $config; - private array $plugins = []; + protected Config $config; + protected array $plugins = []; public function __construct() { diff --git a/classes/StaticRPC.php b/classes/StaticRPC.php new file mode 100644 index 0000000..574c759 --- /dev/null +++ b/classes/StaticRPC.php @@ -0,0 +1,15 @@ +plugins; + } +} \ No newline at end of file diff --git a/plugins/openvpn/Openvpn.php b/plugins/openvpn/Openvpn.php new file mode 100755 index 0000000..94968d3 --- /dev/null +++ b/plugins/openvpn/Openvpn.php @@ -0,0 +1,33 @@ +#!/usr/bin/php +context = $context; + } + + public function getRoutingConfig(): array + { + $networks = (new NetworkConfigReader())->getConfigs(); + $data = []; + + //add new routes + foreach ($this->context->getConfig() ["networks"] as $key) { + if (isset($networks[$key])) { + foreach ($networks[$key]["networks"] as $route) { + $parts = explode("/", $route); + $mask = long2ip(-1 << (32 - (int)$parts[1])); + $dst = $parts[0]; + $data[] = "push \"route {$dst} {$mask}\""; + } + } + } + + return $data; + } +} + diff --git a/plugins/openvpn/metadata.json b/plugins/openvpn/metadata.json new file mode 100644 index 0000000..fe1b9d5 --- /dev/null +++ b/plugins/openvpn/metadata.json @@ -0,0 +1,6 @@ +{ + "class": "Openvpn", + "config": { + + } +} \ No newline at end of file diff --git a/plugins/openvpn/ovpn-connect b/plugins/openvpn/ovpn-connect deleted file mode 100755 index b26bcdc..0000000 --- a/plugins/openvpn/ovpn-connect +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/php -getConfigs(); - $config = new Config(); - $config->read(); - $data = []; - - //add new routes - foreach ($config["networks"] as $key) { - if (isset($networks[$key])) { - foreach ($networks[$key]["networks"] as $route) { - $parts = explode("/", $route); - $mask = long2ip(-1 << (32 - (int)$parts[1])); - $dst = $parts[0]; - $data[] = "push \"route {$dst} {$mask}\""; - } - } - } - - file_put_contents($TMPFILE, implode("\n", $data)); - - -} catch (Exception $e) { - echo "\nError:" . $e->getMessage() . "\n"; - exit(1); -} - -exit(0); \ No newline at end of file