From 36748e5c3da9d2f97bee777ed1cb3ebaa69ce489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Preu=C3=9F?= Date: Fri, 9 Aug 2024 01:42:35 -0700 Subject: [PATCH] Start implementing network pages --- classes/Hardware/NetworkInterface.class.php | 99 ++++++++++++ classes/Hardware/NetworkInterfaces.class.php | 36 ++++- classes/System/CoreAdmin.class.php | 5 +- default/server/network.php | 151 ++++++++++++++----- handler/server/network.php | 60 +++++++- handler/server/reboot.php | 1 + 6 files changed, 313 insertions(+), 39 deletions(-) create mode 100644 classes/Hardware/NetworkInterface.class.php diff --git a/classes/Hardware/NetworkInterface.class.php b/classes/Hardware/NetworkInterface.class.php new file mode 100644 index 0000000..07fccbd --- /dev/null +++ b/classes/Hardware/NetworkInterface.class.php @@ -0,0 +1,99 @@ +value){ + return $status; + } + } + + throw new \ValueError("$name is not a valid backing value for enum " . self::class ); + } + + public static function tryFromName(string $name) : NetworkState | null { + try { + return self::fromName($name); + } catch (\ValueError $error) { + return null; + } + } + } + + class NetworkInterface { + private ?string $id = null; + private bool $virtual = false; + private bool $tunnel = false; + private ?string $address = null; + private ?string $broadcast = null; + private ?string $type = null; + private ?NetworkState $state = null; + + public function __construct(string $id) { + $this->id = $id; + + switch($this->id) { + case 'sit0': // Tunnel-Device for IPv6 in IPv4 + $this->tunnel = true; + $this->virtual = true; + break; + case 'teql0': + $this->virtual = true; + break; + } + } + + public function getID() { + return $this->id; + } + + public function isVirtual() : bool { + return $this->virtual; + } + + public function isTunnel() : bool { + return $this->tunnel; + } + + public function getState() : NetworkState { + return $this->state; + } + public function setState(NetworkState $state) { + $this->state = $state; + } + + public function getType() { + return $this->type; + } + public function setType($type) { + $this->type = $type; + } + public function setAddress($address) { + $this->address = $address; + } + public function setBroadcast($broadcast) { + $this->broadcast = $broadcast; + } + + public function enable() { + shell_exec(sprintf('ifup %s', $this->id)); + } + + public function disable() { + shell_exec(sprintf('ifdown %s', $this->id)); + } + } +?> diff --git a/classes/Hardware/NetworkInterfaces.class.php b/classes/Hardware/NetworkInterfaces.class.php index 712c352..fa5d36a 100644 --- a/classes/Hardware/NetworkInterfaces.class.php +++ b/classes/Hardware/NetworkInterfaces.class.php @@ -22,7 +22,33 @@ } protected function __construct() { - + $result = shell_exec('ls /sys/class/net | jq -R -s -c \'split("\n")[:-1]\''); + + foreach(json_decode($result) AS $entry) { + $this->devices[$entry] = new NetworkInterface($entry); + } + + $result = shell_exec('ip --json address show'); + + foreach(json_decode($result) AS $entry) { + $device = $this->devices[$entry->ifname]; + $device->debug = $entry; + + if(isset($entry->link_type)) { + $device->setType($entry->link_type); + } + if(isset($entry->operstate)) { + $device->setState(NetworkState::tryFromName($entry->operstate)); + } + + if(isset($entry->address)) { + $device->setAddress($entry->address); + } + + if(isset($entry->broadcast)) { + $device->setBroadcast($entry->broadcast); + } + } } public function getDevices() : array { @@ -40,6 +66,10 @@ public function getIPAddress() : string { return trim($_SERVER['SERVER_ADDR']); } + + public function getDevice($id) : NetworkInterface | null { + return $this->devices[$id]; + } } class NetworkInterfaces { @@ -62,5 +92,9 @@ public static function getIPAddress() : string { return NetworkInterfacesFactory::getInstance()->getDevices(); } + + public static function getDevice($id) : NetworkInterface | null { + return NetworkInterfacesFactory::getInstance()->getDevice($id); + } } ?> \ No newline at end of file diff --git a/classes/System/CoreAdmin.class.php b/classes/System/CoreAdmin.class.php index b9c0bd2..45fc70b 100644 --- a/classes/System/CoreAdmin.class.php +++ b/classes/System/CoreAdmin.class.php @@ -52,13 +52,14 @@ } }); - $this->getRouter()->addRoute('^/server(?:/([a-zA-Z0-9\-_]+))(?:/([a-zA-Z0-9\-_]+))?$', function(?string $destination = null, ?string $tab = null) { + $this->getRouter()->addRoute('^/server(?:/([a-zA-Z0-9\-_]+))(?:/([a-zA-Z0-9\-_]+))?(?:/([a-zA-Z0-9\-_]+))?$', function(?string $destination = null, ?string $tab = null, ?string $action = null) { if(!Auth::isLoggedIn()) { Response::redirect('/'); } if($this->getTemplate()->display('server' . (!empty($destination) ? sprintf('/%s', $destination) : ''), [ - 'tab' => $tab + 'tab' => $tab, + 'action' => $action ]) == false) { $this->getTemplate()->display('error/404'); } diff --git a/default/server/network.php b/default/server/network.php index 0725b21..59e084f 100644 --- a/default/server/network.php +++ b/default/server/network.php @@ -1,7 +1,8 @@ header(); @@ -15,42 +16,122 @@ $template->footer(); exit(); } + ?> + +
+
+ getDevices() AS $entry) { + if(!$entry->isTunnel() && !$entry->isVirtual() && $entry->getType() == 'ether') { + ?> +
+
+ getState()) { + case NetworkState::UP: + $status = 'text-success'; + break; + case NetworkState::DOWN: + $status = 'text-danger'; + break; + case NetworkState::UNKNOWN: + $status = 'text-warning'; + break; + } + + Icon::show('network', [ + 'classes' => [ 'align-self-start', 'flex-shrink-1', 'bg-dark', 'rounded', $status, 'p-2', 'm-2' ], + 'attributes' => [ 'style' => 'font-size: 30px' ], + 'data-current' => 'theme-auto' + ]); + ?> + + +

getID(); ?>

+
+
+ +
+ +
+
+ + + + + +
+
+
+        debug);
+            }
+        ?>
+
+ class != 'network') { - continue; - } - - ?> -
-
- description; ?> -
-
-
logicalname; ?>
- configuration->driver == 'bonding') { - - } else { - ?> -

capacity; ?> units; ?>

- - -
-
- "; - print_r(`lshw -C network -json`); - print_r(json_decode(`ip --json address show`)); - print_r(json_decode(`ip --json link show`)); - print_r(json_decode(`ip --json route show`)); + #print_r(`lshw -C network -json`); + #print_r(json_decode(`ip --json address show`)); + #print_r(json_decode(`ip --json link show`)); + #print_r(json_decode(`ip --json route show`)); $this->footer(); ?> \ No newline at end of file diff --git a/handler/server/network.php b/handler/server/network.php index f66274f..1bc8f14 100644 --- a/handler/server/network.php +++ b/handler/server/network.php @@ -1,4 +1,62 @@ assign('network', NetworkInterfaces::get()); + + switch($action) { + case 'start': + if(!Auth::hasPermission('SERVER::MANAGE')) { + $this->assign('error', I18N::get('You have no permissions for this action!')); + return; + } + + if(defined('DEMO') && DEMO) { + $this->assign('error', I18N::get('DEMO-VERSION: This action can\'t be used!')); + return; + } + + $device = NetworkInterfaces::getDevice($tab); + + if(empty($device)) { + $this->assign('error', sprintf(I18N::get('Unknown Network-Interface: %s'), $tab)); + return; + } + + $this->assign('success', sprintf(I18N::get('The Network-Interface "%s" will be started!'), $tab)); + $device->enable(); + break; + case 'stop': + if(!Auth::hasPermission('SERVER::MANAGE')) { + $this->assign('error', I18N::get('You have no permissions for this action!')); + return; + } + + if(defined('DEMO') && DEMO) { + $this->assign('error', I18N::get('DEMO-VERSION: This action can\'t be used!')); + return; + } + + $device = NetworkInterfaces::getDevice($tab); + + if(empty($device)) { + $this->assign('error', sprintf(I18N::get('Unknown Network-Interface: %s'), $tab)); + return; + } + + $this->assign('success', sprintf(I18N::get('The Network-Interface "%s" will be stopped!'), $tab)); + $device->disable(); + break; + case 'info': + $device = NetworkInterfaces::getDevice($tab); + + if(empty($device)) { + $this->assign('error', sprintf(I18N::get('Unknown Network-Interface: %s'), $tab)); + return; + } + + $this->assign('device', $device); + break; + } ?> \ No newline at end of file diff --git a/handler/server/reboot.php b/handler/server/reboot.php index c27ffa3..28a8fad 100644 --- a/handler/server/reboot.php +++ b/handler/server/reboot.php @@ -23,6 +23,7 @@ } $rebooting = $template->getCore()->getSettings('REBOOT', null); + if(!empty($rebooting)) { $this->assign('success', I18N::get('The System will be rebooted now!') . sprintf('

%s

', $rebooting)); }