Add LAN clients to dashboard
This commit is contained in:
+85
-1
@@ -2604,6 +2604,20 @@ function iw_station_dump(string $iface): string
|
||||
return sh(['/usr/sbin/iw', 'dev', $iface, 'station', 'dump'], true, 4)['out'];
|
||||
}
|
||||
|
||||
function wifi_iface_by_driver(string $driver): ?string
|
||||
{
|
||||
foreach (glob('/sys/class/net/wlan*') ?: [] as $path) {
|
||||
$iface = basename($path);
|
||||
$driverPath = @readlink($path . '/device/driver');
|
||||
|
||||
if ($driverPath !== false && basename($driverPath) === $driver) {
|
||||
return $iface;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function parse_live_wifi_rows(string $iface, string $band, string $text, array $leases, array $aliases): array
|
||||
{
|
||||
$rows = [];
|
||||
@@ -2678,6 +2692,65 @@ function parse_live_wifi_rows(string $iface, string $band, string $text, array $
|
||||
return $rows;
|
||||
}
|
||||
|
||||
function lan_fdb_macs(string $iface = 'eth1'): array
|
||||
{
|
||||
$rows = sh(['/usr/sbin/bridge', 'fdb', 'show', 'br', 'br0'], false, 4)['out'];
|
||||
$macs = [];
|
||||
|
||||
foreach (explode("\n", $rows) as $line) {
|
||||
if (!preg_match('/^([0-9a-f]{2}(?::[0-9a-f]{2}){5})\s+dev\s+' . preg_quote($iface, '/') . '\b/i', trim($line), $m)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$mac = strtolower($m[1]);
|
||||
if (str_starts_with($mac, '01:') || str_starts_with($mac, '33:') || str_contains($line, 'permanent')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$macs[$mac] = true;
|
||||
}
|
||||
|
||||
return array_keys($macs);
|
||||
}
|
||||
|
||||
function lan_client_rows(array $leases, array $aliases, string $iface = 'eth1'): array
|
||||
{
|
||||
$rows = [];
|
||||
|
||||
foreach (lan_fdb_macs($iface) as $mac) {
|
||||
$lease = $leases[$mac] ?? [];
|
||||
$leaseHostname = (string)($lease['hostname'] ?? 'N/A');
|
||||
$aliasHostname = $aliases[$mac] ?? null;
|
||||
$hostname = $aliasHostname ?: $leaseHostname;
|
||||
$hostnameSource = $aliasHostname ? 'alias' : ($leaseHostname === 'N/A' ? 'generated' : 'lease');
|
||||
|
||||
$rows[] = [
|
||||
'band' => 'LAN',
|
||||
'iface' => $iface,
|
||||
'mac' => $mac,
|
||||
'ip' => $lease['ip'] ?? 'N/A',
|
||||
'hostname' => $hostname === 'N/A' ? '기기-' . strtoupper(substr(str_replace(':', '', $mac), -6)) : $hostname,
|
||||
'hostname_source' => $hostnameSource,
|
||||
'name' => $hostname === 'N/A' ? $mac : $hostname,
|
||||
'signal' => '-',
|
||||
'tx_bitrate' => '-',
|
||||
'rx_bitrate' => '-',
|
||||
'signal_source' => 'lan',
|
||||
'tx_bitrate_source' => 'lan',
|
||||
'rx_bitrate_source' => 'lan',
|
||||
'connected_time' => 'N/A',
|
||||
'inactive_time' => 'N/A',
|
||||
'rx_bytes' => 0,
|
||||
'tx_bytes' => 0,
|
||||
'rx_packets' => 0,
|
||||
'tx_packets' => 0,
|
||||
'tx_failed' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
function wifi_time_ms(string $value): int
|
||||
{
|
||||
if (preg_match('/(\d+)/', $value, $m)) {
|
||||
@@ -2762,6 +2835,10 @@ function apply_wifi_estimates(array $clients): array
|
||||
{
|
||||
foreach ($clients as &$client) {
|
||||
$band = (string)($client['band'] ?? '2.4G');
|
||||
if ($band === 'LAN') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$signalDbm = wifi_signal_dbm((string)($client['signal'] ?? ''));
|
||||
$txMbps = wifi_bitrate_mbps($client['tx_bitrate'] ?? null);
|
||||
$rxMbps = wifi_bitrate_mbps($client['rx_bitrate'] ?? null);
|
||||
@@ -2973,12 +3050,18 @@ function wifi_data(): array
|
||||
$aliases = wifi_client_aliases();
|
||||
$clients = [];
|
||||
|
||||
foreach (['wlan0' => '2.4G', 'wlan1' => '5G'] as $iface => $band) {
|
||||
$wifiIfaces = array_filter([
|
||||
'wlan0' => '2.4G',
|
||||
wifi_iface_by_driver('rtl88x2bu') ?: 'wlan1' => '5G',
|
||||
]);
|
||||
|
||||
foreach ($wifiIfaces as $iface => $band) {
|
||||
$clients = array_merge(
|
||||
$clients,
|
||||
parse_live_wifi_rows($iface, $band, iw_station_dump($iface), $leases, $aliases)
|
||||
);
|
||||
}
|
||||
$clients = array_merge($clients, lan_client_rows($leases, $aliases));
|
||||
|
||||
$clients = dedupe_wifi_clients($clients);
|
||||
$clients = apply_wifi_estimates($clients);
|
||||
@@ -2988,6 +3071,7 @@ function wifi_data(): array
|
||||
'clients' => $clients,
|
||||
'count24' => count(array_filter($clients, fn($c) => $c['band'] === '2.4G')),
|
||||
'count5' => count(array_filter($clients, fn($c) => $c['band'] === '5G')),
|
||||
'countLan' => count(array_filter($clients, fn($c) => $c['band'] === 'LAN')),
|
||||
'leases' => array_values($leases),
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user