From 26f800ae74031258376e0cee0c92707e624ea835 Mon Sep 17 00:00:00 2001 From: Frank Karlitschek Date: Tue, 28 Apr 2026 13:55:28 +0200 Subject: [PATCH] feat(network): include DNS servers in network info Reads nameserver entries from /etc/resolv.conf (no shell, no sudo) and exposes them as a comma-separated string in getNetworkInfo()['dns']. Comments and empty lines are ignored; duplicate servers are deduped. Signed-off-by: Frank Karlitschek --- lib/OperatingSystems/FreeBSD.php | 1 + lib/OperatingSystems/IOperatingSystem.php | 1 + lib/OperatingSystems/Linux.php | 10 ++++++++++ templates/settings-admin.php | 5 ++++- tests/lib/DummyTest.php | 8 ++++++++ tests/lib/FreeBSDTest.php | 12 ++++++++++++ tests/lib/LinuxTest.php | 24 +++++++++++++++++++++++ 7 files changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/OperatingSystems/FreeBSD.php b/lib/OperatingSystems/FreeBSD.php index 653589b8..15c82719 100644 --- a/lib/OperatingSystems/FreeBSD.php +++ b/lib/OperatingSystems/FreeBSD.php @@ -101,6 +101,7 @@ public function getNetworkInfo(): array { $result = [ 'gateway' => '', 'hostname' => \gethostname(), + 'dns' => '', ]; try { diff --git a/lib/OperatingSystems/IOperatingSystem.php b/lib/OperatingSystems/IOperatingSystem.php index e6141a2f..6dc54316 100644 --- a/lib/OperatingSystems/IOperatingSystem.php +++ b/lib/OperatingSystems/IOperatingSystem.php @@ -33,6 +33,7 @@ public function getDiskInfo(): array; * [ * 'gateway' => string, * 'hostname' => string, + * 'dns' => string, * ] */ public function getNetworkInfo(): array; diff --git a/lib/OperatingSystems/Linux.php b/lib/OperatingSystems/Linux.php index 8f81884a..c197af75 100644 --- a/lib/OperatingSystems/Linux.php +++ b/lib/OperatingSystems/Linux.php @@ -126,12 +126,22 @@ public function getNetworkInfo(): array { $result = [ 'gateway' => '', 'hostname' => \gethostname(), + 'dns' => '', ]; if (function_exists('shell_exec')) { $result['gateway'] = shell_exec('ip route | awk \'/default/ { print $3 }\''); } + try { + $resolvConf = $this->readContent('/etc/resolv.conf'); + if (preg_match_all('/^\s*nameserver\s+(\S+)/m', $resolvConf, $matches)) { + $result['dns'] = implode(', ', array_unique($matches[1])); + } + } catch (RuntimeException) { + // okay + } + return $result; } diff --git a/templates/settings-admin.php b/templates/settings-admin.php index f537eb3a..87469161 100644 --- a/templates/settings-admin.php +++ b/templates/settings-admin.php @@ -203,6 +203,10 @@ function FormatMegabytes(int $byte): string { t('Gateway:')); ?> +
+ t('DNS:')); ?> + +
@@ -532,4 +536,3 @@ function FormatMegabytes(int $byte): string {
- diff --git a/tests/lib/DummyTest.php b/tests/lib/DummyTest.php index 4194e6af..5e81cae8 100644 --- a/tests/lib/DummyTest.php +++ b/tests/lib/DummyTest.php @@ -56,4 +56,12 @@ public function testSupported(): void { public function testGetNetworkInterfaces(): void { $this->assertEquals([], $this->os->getNetworkInterfaces()); } + + public function testGetNetworkInfo(): void { + $networkInfo = $this->os->getNetworkInfo(); + + $this->assertArrayHasKey('hostname', $networkInfo); + $this->assertSame('', $networkInfo['gateway']); + $this->assertSame('', $networkInfo['dns']); + } } diff --git a/tests/lib/FreeBSDTest.php b/tests/lib/FreeBSDTest.php index 7e565515..431c799e 100644 --- a/tests/lib/FreeBSDTest.php +++ b/tests/lib/FreeBSDTest.php @@ -134,6 +134,18 @@ public function testGetNetworkInterfaces(): void { $this->assertEquals($expected, $actual); } + public function testGetNetworkInfo(): void { + $this->os->method('executeCommand') + ->with('netstat -rn') + ->willReturn("Routing tables\n\ndefault 192.0.2.1 UGS\n"); + + $networkInfo = $this->os->getNetworkInfo(); + + $this->assertSame('192.0.2.1', $networkInfo['gateway']); + $this->assertSame('', $networkInfo['dns']); + $this->assertArrayHasKey('hostname', $networkInfo); + } + public function testGetNetworkInterfacesError(): void { $this->os->method('getNetInterfaces') ->willThrowException(new RuntimeException('Unable to get network interfaces')); diff --git a/tests/lib/LinuxTest.php b/tests/lib/LinuxTest.php index 421c7f9c..1df11262 100644 --- a/tests/lib/LinuxTest.php +++ b/tests/lib/LinuxTest.php @@ -256,6 +256,30 @@ public function testSupported(): void { $this->assertTrue($this->os->supported()); } + public function testGetNetworkInfo(): void { + $this->os->method('readContent') + ->with('/etc/resolv.conf') + ->willReturn("# comment\nnameserver 127.0.0.53\nnameserver 1.1.1.1\n; ignored\nnameserver 127.0.0.53"); + + $networkInfo = $this->os->getNetworkInfo(); + + $this->assertSame('127.0.0.53, 1.1.1.1', $networkInfo['dns']); + $this->assertArrayHasKey('hostname', $networkInfo); + $this->assertArrayHasKey('gateway', $networkInfo); + } + + public function testGetNetworkInfoWithoutResolvConf(): void { + $this->os->method('readContent') + ->with('/etc/resolv.conf') + ->willThrowException(new RuntimeException('Unable to read: "/etc/resolv.conf"')); + + $networkInfo = $this->os->getNetworkInfo(); + + $this->assertSame('', $networkInfo['dns']); + $this->assertArrayHasKey('hostname', $networkInfo); + $this->assertArrayHasKey('gateway', $networkInfo); + } + public function testGetNetworkInterfaces(): void { $this->os->method('getNetInterfaces') ->willReturn(json_decode(file_get_contents(__DIR__ . '/../data/linux_net_get_interfaces.json'), true, 512, JSON_THROW_ON_ERROR));