From 65f1867133d46be6611356dcdcb1e4a6a8fac755 Mon Sep 17 00:00:00 2001 From: Nicolas Martignoni Date: Thu, 28 Mar 2024 13:50:29 +0100 Subject: [PATCH] Re-add names of DHCP-connected devices in dashboard (#133) Fixes #132 (and #128). --- CHANGELOG.md | 5 +++++ classes/local/utils.php | 37 ++++++++++++++++++++++++++++++------ classes/privacy/provider.php | 2 +- index.php | 13 ++++++++++--- lang/en/tool_moodlebox.php | 6 ++++-- version.php | 6 +++--- 6 files changed, 54 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 661f5d5..f4cf300 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/) and follow principles of [keep a changelog](https://keepachangelog.com). +## Version 2.17.5, unreleased + +### Changed +- Re-add the client name when displaying wireless client info in the dashboard (issue #132; related issue #128). + ## Version 2.17.4, 2024-01-02 ### Changed diff --git a/classes/local/utils.php b/classes/local/utils.php index 2cc4dc4..119664a 100644 --- a/classes/local/utils.php +++ b/classes/local/utils.php @@ -340,12 +340,13 @@ public static function is_private_ipv4_address($address) { * Get IP addresses of wireless connected clients. * * @param string $interface the network interface to check. + * @param string $leasesfile the file containing the dnsmasq leases. * @return associative array of MAC address, IP address or empty array * if no clients connected. */ - public static function get_connected_ip_adresses($interface) { + public static function get_connected_ip_adresses($interface, $leasesfile) { $iwoutput = shell_exec('iw dev ' . $interface . ' station dump') ?: ''; - $arpoutput = shell_exec('arp -ai ' . $interface) ?: ''; + $arpoutput = shell_exec('arp -ani ' . $interface) ?: ''; // Extract MAC and IP addresses. preg_match_all('/Station\s+([a-fA-F0-9:]+)/', $iwoutput, $iwmatches); @@ -356,14 +357,38 @@ public static function get_connected_ip_adresses($interface) { sort($iwmacadresses); $arpmacippairs = array_combine($arpmatches[2], $arpmatches[1]); - // Compare the sorted MAC addresses and populate array of pairs. - $connectedmacippairs = []; + // Get leases from `dnsmasq` lease file. + if ( file_exists($leasesfile) ) { + if ( filesize($leasesfile) > 0 ) { + $leases = explode("\n", trim(file_get_contents($leasesfile))); + } else { + $leases = []; + } + } else { + $leases = []; + } + + // Compare the sorted MAC addresses and populate array of connection data. + $connecteddata = []; foreach ($iwmacadresses as $macaddress) { if (isset($arpmacippairs[$macaddress])) { - $connectedmacippairs[$macaddress] = $arpmacippairs[$macaddress]; + // Find MAC and IP addresses in lease file, and get matching device name. + if ($m = preg_grep('/^.*' . $macaddress . '\s' . $arpmacippairs[$macaddress] . '.*$/i', $leases)) { + $name = explode(' ', reset($m))[3]; + if ( $name == '*') { + $name = get_string('hiddendhcpname', 'tool_moodlebox'); + } + } else { + $name = get_string('unknowndhcpname', 'tool_moodlebox'); + } + $connecteddata[$macaddress] = [ + 'ip' => $arpmacippairs[$macaddress], + 'name' => $name, + ]; } } - return $connectedmacippairs; + + return $connecteddata; } /** diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php index 3f3c62b..e13e394 100644 --- a/classes/privacy/provider.php +++ b/classes/privacy/provider.php @@ -40,7 +40,7 @@ class provider implements \core_privacy\local\metadata\null_provider { * * @return String */ - public static function get_reason() : string { + public static function get_reason(): string { return 'privacy:metadata'; } diff --git a/index.php b/index.php index eed03fb..1bd5997 100644 --- a/index.php +++ b/index.php @@ -135,9 +135,16 @@ // Get CPU load. $cpuload = sys_getloadavg(); + // Get DHCP leases file name. + if ($networkmanager) { + $leasesfile = '/tmp/dnsmasq.leases'; + } else { + $leasesfile = '/var/lib/misc/dnsmasq.leases'; + } + // Get IP addresses of connected clients. $interface = 'uap0'; - $leases = \tool_moodlebox\local\utils::get_connected_ip_adresses($interface); + $leases = \tool_moodlebox\local\utils::get_connected_ip_adresses($interface, $leasesfile); $dhcpclientnumber = count($leases); // Get local static IP address. @@ -257,9 +264,9 @@ if ($dhcpclientnumber > 0) { $table->add_data([get_string('dhcpclients', 'tool_moodlebox') . ' (' . get_string('dhcpclientnumber', 'tool_moodlebox') . ': ' . $dhcpclientnumber . ')', '', ]); - foreach ($leases as $mac => $ip) { + foreach ($leases as $mac => $data) { $table->add_data([get_string('dhcpclientinfo', 'tool_moodlebox'), - $ip . ' (' . $mac . ')', ], 'subinfo'); + $data['ip'] . ' (' . $data['name'] . ', ' . $mac . ')', ], 'subinfo'); } } // Ethernet info. diff --git a/lang/en/tool_moodlebox.php b/lang/en/tool_moodlebox.php index 63af313..3f8ae24 100644 --- a/lang/en/tool_moodlebox.php +++ b/lang/en/tool_moodlebox.php @@ -39,7 +39,7 @@ $string['datetimesetmessage'] = 'The clock of the MoodleBox isn\'t on time. It\'s highly recommended to set the date and time to the current time.'; $string['datetimesetting'] = 'Date and time'; $string['defaultgateway'] = 'Default gateway'; -$string['dhcpclientinfo'] = 'Client IP and MAC addresses'; +$string['dhcpclientinfo'] = 'Client IP address, name and MAC address'; $string['dhcpclientnumber'] = 'number of clients'; $string['dhcpclients'] = 'DHCP clients'; $string['documentation'] = 'MoodleBox documentation'; @@ -50,6 +50,7 @@ $string['forum_desc'] = '

If you can\'t find an answer to your question in the MoodleBox documentation, search the MoodleBox support forum to see if your question has already been answered. Otherwise, feel free to open a new discussion.

'; $string['hardwareinfo'] = 'Hardware information'; $string['hidden'] = 'Hidden'; +$string['hiddendhcpname'] = '--hidden--'; $string['infofileerror'] = 'Information not available'; $string['infoheading'] = 'MoodleBox support information'; $string['information'] = 'Information'; @@ -64,7 +65,7 @@ * Critical MoodleBox operation details, such as remaining disk space on the SD card and processor load, temperature and frequency * Current settings of Wi-Fi network supplied by the MoodleBox -* Number, IP and MAC addresses of all devices connected to the MoodleBox +* Number, IP address, name and MAC address of all devices connected to the MoodleBox * Raspberry Pi model and operating system * MoodleBox version and MoodleBox plugin version '; @@ -123,6 +124,7 @@ $string['systeminfo'] = 'System information'; $string['undervoltagedetected'] = '

Warning: under-voltage detected! The power supply of the MoodleBox is inadequate, which can cause various problems, for example a limitation of the number of Wi-Fi clients or even an unexpected shutdown of the device.

It is strongly recommended to change your power supply, giving preference to the official Raspberry Pi 12.5W Micro USB Power Supply for Raspberry Pi 3A+, 3B, 3B+ and Zero 2 W, or Raspberry Pi 15W USB-C Power Supply for Raspberry Pi 4B, or Raspberry Pi 27W USB-C Power Supply for Raspberry Pi 5.

'; $string['undervoltageoccurred'] = '

An under-voltage situation has occurred since last boot of the MoodleBox. This could indicate the power supply of the MoodleBox is inadequate, which can cause various problems, for example a limitation of the number of Wi-Fi clients or even an unexpected shutdown of the device.

It is strongly recommended to change your power supply, giving preference to the official Raspberry Pi 12.5W Micro USB Power Supply for Raspberry Pi 3A+, 3B, 3B+ and Zero 2 W, or Raspberry Pi 15W USB-C Power Supply for Raspberry Pi 4B, or Raspberry Pi 27W USB-C Power Supply for Raspberry Pi 5.

'; +$string['unknowndhcpname'] = '--unknown--'; $string['unknownmodel'] = 'Unknown or unsupported Raspberry Pi model'; $string['unsupportedhardware'] = 'Unsupported server hardware detected! This plugin does only work on Raspberry Pi'; $string['uptime'] = 'System uptime'; diff --git a/version.php b/version.php index 12a180b..25b9a70 100644 --- a/version.php +++ b/version.php @@ -26,9 +26,9 @@ $plugin = new stdClass(); -$plugin->version = 2024010201; -$plugin->release = '2.17.4'; +$plugin->version = 2024032800; +$plugin->release = '2.17.5-beta'; $plugin->requires = 2018120300; $plugin->supported = [36, 403]; -$plugin->maturity = MATURITY_STABLE; +$plugin->maturity = MATURITY_BETA; $plugin->component = 'tool_moodlebox';