Skip to content

Commit 65f1867

Browse files
authored
Re-add names of DHCP-connected devices in dashboard (#133)
Fixes #132 (and #128).
1 parent f307c4e commit 65f1867

File tree

6 files changed

+54
-15
lines changed

6 files changed

+54
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](https://semver.org/) and follow principles of [keep a changelog](https://keepachangelog.com).
55

6+
## Version 2.17.5, unreleased
7+
8+
### Changed
9+
- Re-add the client name when displaying wireless client info in the dashboard (issue #132; related issue #128).
10+
611
## Version 2.17.4, 2024-01-02
712

813
### Changed

classes/local/utils.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,13 @@ public static function is_private_ipv4_address($address) {
340340
* Get IP addresses of wireless connected clients.
341341
*
342342
* @param string $interface the network interface to check.
343+
* @param string $leasesfile the file containing the dnsmasq leases.
343344
* @return associative array of MAC address, IP address or empty array
344345
* if no clients connected.
345346
*/
346-
public static function get_connected_ip_adresses($interface) {
347+
public static function get_connected_ip_adresses($interface, $leasesfile) {
347348
$iwoutput = shell_exec('iw dev ' . $interface . ' station dump') ?: '';
348-
$arpoutput = shell_exec('arp -ai ' . $interface) ?: '';
349+
$arpoutput = shell_exec('arp -ani ' . $interface) ?: '';
349350

350351
// Extract MAC and IP addresses.
351352
preg_match_all('/Station\s+([a-fA-F0-9:]+)/', $iwoutput, $iwmatches);
@@ -356,14 +357,38 @@ public static function get_connected_ip_adresses($interface) {
356357
sort($iwmacadresses);
357358
$arpmacippairs = array_combine($arpmatches[2], $arpmatches[1]);
358359

359-
// Compare the sorted MAC addresses and populate array of pairs.
360-
$connectedmacippairs = [];
360+
// Get leases from `dnsmasq` lease file.
361+
if ( file_exists($leasesfile) ) {
362+
if ( filesize($leasesfile) > 0 ) {
363+
$leases = explode("\n", trim(file_get_contents($leasesfile)));
364+
} else {
365+
$leases = [];
366+
}
367+
} else {
368+
$leases = [];
369+
}
370+
371+
// Compare the sorted MAC addresses and populate array of connection data.
372+
$connecteddata = [];
361373
foreach ($iwmacadresses as $macaddress) {
362374
if (isset($arpmacippairs[$macaddress])) {
363-
$connectedmacippairs[$macaddress] = $arpmacippairs[$macaddress];
375+
// Find MAC and IP addresses in lease file, and get matching device name.
376+
if ($m = preg_grep('/^.*' . $macaddress . '\s' . $arpmacippairs[$macaddress] . '.*$/i', $leases)) {
377+
$name = explode(' ', reset($m))[3];
378+
if ( $name == '*') {
379+
$name = get_string('hiddendhcpname', 'tool_moodlebox');
380+
}
381+
} else {
382+
$name = get_string('unknowndhcpname', 'tool_moodlebox');
383+
}
384+
$connecteddata[$macaddress] = [
385+
'ip' => $arpmacippairs[$macaddress],
386+
'name' => $name,
387+
];
364388
}
365389
}
366-
return $connectedmacippairs;
390+
391+
return $connecteddata;
367392
}
368393

369394
/**

classes/privacy/provider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class provider implements \core_privacy\local\metadata\null_provider {
4040
*
4141
* @return String
4242
*/
43-
public static function get_reason() : string {
43+
public static function get_reason(): string {
4444
return 'privacy:metadata';
4545
}
4646

index.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,16 @@
135135
// Get CPU load.
136136
$cpuload = sys_getloadavg();
137137

138+
// Get DHCP leases file name.
139+
if ($networkmanager) {
140+
$leasesfile = '/tmp/dnsmasq.leases';
141+
} else {
142+
$leasesfile = '/var/lib/misc/dnsmasq.leases';
143+
}
144+
138145
// Get IP addresses of connected clients.
139146
$interface = 'uap0';
140-
$leases = \tool_moodlebox\local\utils::get_connected_ip_adresses($interface);
147+
$leases = \tool_moodlebox\local\utils::get_connected_ip_adresses($interface, $leasesfile);
141148
$dhcpclientnumber = count($leases);
142149

143150
// Get local static IP address.
@@ -257,9 +264,9 @@
257264
if ($dhcpclientnumber > 0) {
258265
$table->add_data([get_string('dhcpclients', 'tool_moodlebox') .
259266
' (' . get_string('dhcpclientnumber', 'tool_moodlebox') . ': ' . $dhcpclientnumber . ')', '', ]);
260-
foreach ($leases as $mac => $ip) {
267+
foreach ($leases as $mac => $data) {
261268
$table->add_data([get_string('dhcpclientinfo', 'tool_moodlebox'),
262-
$ip . ' (' . $mac . ')', ], 'subinfo');
269+
$data['ip'] . ' (' . $data['name'] . ', ' . $mac . ')', ], 'subinfo');
263270
}
264271
}
265272
// Ethernet info.

lang/en/tool_moodlebox.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
$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.';
4040
$string['datetimesetting'] = 'Date and time';
4141
$string['defaultgateway'] = 'Default gateway';
42-
$string['dhcpclientinfo'] = 'Client IP and MAC addresses';
42+
$string['dhcpclientinfo'] = 'Client IP address, name and MAC address';
4343
$string['dhcpclientnumber'] = 'number of clients';
4444
$string['dhcpclients'] = 'DHCP clients';
4545
$string['documentation'] = 'MoodleBox documentation';
@@ -50,6 +50,7 @@
5050
$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>';
5151
$string['hardwareinfo'] = 'Hardware information';
5252
$string['hidden'] = 'Hidden';
53+
$string['hiddendhcpname'] = '--hidden--';
5354
$string['infofileerror'] = 'Information not available';
5455
$string['infoheading'] = 'MoodleBox support information';
5556
$string['information'] = 'Information';
@@ -64,7 +65,7 @@
6465
6566
* Critical MoodleBox operation details, such as remaining disk space on the SD card and processor load, temperature and frequency
6667
* Current settings of Wi-Fi network supplied by the MoodleBox
67-
* Number, IP and MAC addresses of all devices connected to the MoodleBox
68+
* Number, IP address, name and MAC address of all devices connected to the MoodleBox
6869
* Raspberry Pi model and operating system
6970
* MoodleBox version and MoodleBox plugin version
7071
';
@@ -123,6 +124,7 @@
123124
$string['systeminfo'] = 'System information';
124125
$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>';
125126
$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>';
127+
$string['unknowndhcpname'] = '--unknown--';
126128
$string['unknownmodel'] = 'Unknown or unsupported Raspberry Pi model';
127129
$string['unsupportedhardware'] = 'Unsupported server hardware detected! This plugin does only work on Raspberry Pi';
128130
$string['uptime'] = 'System uptime';

version.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
$plugin = new stdClass();
2828

29-
$plugin->version = 2024010201;
30-
$plugin->release = '2.17.4';
29+
$plugin->version = 2024032800;
30+
$plugin->release = '2.17.5-beta';
3131
$plugin->requires = 2018120300;
3232
$plugin->supported = [36, 403];
33-
$plugin->maturity = MATURITY_STABLE;
33+
$plugin->maturity = MATURITY_BETA;
3434
$plugin->component = 'tool_moodlebox';

0 commit comments

Comments
 (0)