Skip to content

Commit

Permalink
Re-add names of DHCP-connected devices in dashboard (#133)
Browse files Browse the repository at this point in the history
Fixes #132 (and #128).
  • Loading branch information
martignoni authored Mar 28, 2024
1 parent f307c4e commit 65f1867
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 31 additions & 6 deletions classes/local/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
13 changes: 10 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions lang/en/tool_moodlebox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -50,6 +50,7 @@
$string['forum_desc'] = '<p>If you can\'t find an answer to your question in the <a href="https://moodlebox.net/en/help/" title="MoodleBox documentation" target="_blank">MoodleBox documentation</a>, search the <a href="https://discuss.moodlebox.net/" title="MoodleBox forum" target="_blank">MoodleBox support forum</a> to see if your question has already been answered. Otherwise, feel free to open a new discussion.</p>';
$string['hardwareinfo'] = 'Hardware information';
$string['hidden'] = 'Hidden';
$string['hiddendhcpname'] = '--hidden--';
$string['infofileerror'] = 'Information not available';
$string['infoheading'] = 'MoodleBox support information';
$string['information'] = 'Information';
Expand All @@ -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
';
Expand Down Expand Up @@ -123,6 +124,7 @@
$string['systeminfo'] = 'System information';
$string['undervoltagedetected'] = '<p><b>Warning: under-voltage detected!</b> 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.</p><p>It is strongly recommended to <b>change your power supply</b>, giving preference to the official <a href="https://www.raspberrypi.com/products/micro-usb-power-supply/" target="_blank">Raspberry Pi 12.5W Micro USB Power Supply</a> for Raspberry Pi 3A+, 3B, 3B+ and Zero 2 W, or <a href="https://www.raspberrypi.com/products/type-c-power-supply/" target="_blank">Raspberry Pi 15W USB-C Power Supply</a> for Raspberry Pi 4B, or <a href="https://www.raspberrypi.com/products/27w-power-supply/" target="_blank">Raspberry Pi 27W USB-C Power Supply</a> for Raspberry Pi 5.</p>';
$string['undervoltageoccurred'] = '<p>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.</p><p>It is strongly recommended to <b>change your power supply</b>, giving preference to the official <a href="https://www.raspberrypi.com/products/micro-usb-power-supply/" target="_blank">Raspberry Pi 12.5W Micro USB Power Supply</a> for Raspberry Pi 3A+, 3B, 3B+ and Zero 2 W, or <a href="https://www.raspberrypi.com/products/type-c-power-supply/" target="_blank">Raspberry Pi 15W USB-C Power Supply</a> for Raspberry Pi 4B, or <a href="https://www.raspberrypi.com/products/27w-power-supply/" target="_blank">Raspberry Pi 27W USB-C Power Supply</a> for Raspberry Pi 5.</p>';
$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';
Expand Down
6 changes: 3 additions & 3 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit 65f1867

Please sign in to comment.