Skip to content

Commit 26f800a

Browse files
karlitschekkesselb
authored andcommitted
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 <karlitschek@users.noreply.github.com>
1 parent 08254b3 commit 26f800a

7 files changed

Lines changed: 60 additions & 1 deletion

File tree

lib/OperatingSystems/FreeBSD.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function getNetworkInfo(): array {
101101
$result = [
102102
'gateway' => '',
103103
'hostname' => \gethostname(),
104+
'dns' => '',
104105
];
105106

106107
try {

lib/OperatingSystems/IOperatingSystem.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function getDiskInfo(): array;
3333
* [
3434
* 'gateway' => string,
3535
* 'hostname' => string,
36+
* 'dns' => string,
3637
* ]
3738
*/
3839
public function getNetworkInfo(): array;

lib/OperatingSystems/Linux.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,22 @@ public function getNetworkInfo(): array {
126126
$result = [
127127
'gateway' => '',
128128
'hostname' => \gethostname(),
129+
'dns' => '',
129130
];
130131

131132
if (function_exists('shell_exec')) {
132133
$result['gateway'] = shell_exec('ip route | awk \'/default/ { print $3 }\'');
133134
}
134135

136+
try {
137+
$resolvConf = $this->readContent('/etc/resolv.conf');
138+
if (preg_match_all('/^\s*nameserver\s+(\S+)/m', $resolvConf, $matches)) {
139+
$result['dns'] = implode(', ', array_unique($matches[1]));
140+
}
141+
} catch (RuntimeException) {
142+
// okay
143+
}
144+
135145
return $result;
136146
}
137147

templates/settings-admin.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ function FormatMegabytes(int $byte): string {
203203
<?php p($l->t('Gateway:')); ?>
204204
<span class="info"><?php p($_['networkinfo']['gateway']); ?></span>
205205
</div>
206+
<div class="col col-12">
207+
<?php p($l->t('DNS:')); ?>
208+
<span class="info"><?php p($_['networkinfo']['dns']); ?></span>
209+
</div>
206210
<div class="col col-12">
207211
<div class="row">
208212
<?php foreach ($interfaces as $interface): ?>
@@ -532,4 +536,3 @@ function FormatMegabytes(int $byte): string {
532536
</div>
533537

534538
</div>
535-

tests/lib/DummyTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,12 @@ public function testSupported(): void {
5656
public function testGetNetworkInterfaces(): void {
5757
$this->assertEquals([], $this->os->getNetworkInterfaces());
5858
}
59+
60+
public function testGetNetworkInfo(): void {
61+
$networkInfo = $this->os->getNetworkInfo();
62+
63+
$this->assertArrayHasKey('hostname', $networkInfo);
64+
$this->assertSame('', $networkInfo['gateway']);
65+
$this->assertSame('', $networkInfo['dns']);
66+
}
5967
}

tests/lib/FreeBSDTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ public function testGetNetworkInterfaces(): void {
134134
$this->assertEquals($expected, $actual);
135135
}
136136

137+
public function testGetNetworkInfo(): void {
138+
$this->os->method('executeCommand')
139+
->with('netstat -rn')
140+
->willReturn("Routing tables\n\ndefault 192.0.2.1 UGS\n");
141+
142+
$networkInfo = $this->os->getNetworkInfo();
143+
144+
$this->assertSame('192.0.2.1', $networkInfo['gateway']);
145+
$this->assertSame('', $networkInfo['dns']);
146+
$this->assertArrayHasKey('hostname', $networkInfo);
147+
}
148+
137149
public function testGetNetworkInterfacesError(): void {
138150
$this->os->method('getNetInterfaces')
139151
->willThrowException(new RuntimeException('Unable to get network interfaces'));

tests/lib/LinuxTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,30 @@ public function testSupported(): void {
256256
$this->assertTrue($this->os->supported());
257257
}
258258

259+
public function testGetNetworkInfo(): void {
260+
$this->os->method('readContent')
261+
->with('/etc/resolv.conf')
262+
->willReturn("# comment\nnameserver 127.0.0.53\nnameserver 1.1.1.1\n; ignored\nnameserver 127.0.0.53");
263+
264+
$networkInfo = $this->os->getNetworkInfo();
265+
266+
$this->assertSame('127.0.0.53, 1.1.1.1', $networkInfo['dns']);
267+
$this->assertArrayHasKey('hostname', $networkInfo);
268+
$this->assertArrayHasKey('gateway', $networkInfo);
269+
}
270+
271+
public function testGetNetworkInfoWithoutResolvConf(): void {
272+
$this->os->method('readContent')
273+
->with('/etc/resolv.conf')
274+
->willThrowException(new RuntimeException('Unable to read: "/etc/resolv.conf"'));
275+
276+
$networkInfo = $this->os->getNetworkInfo();
277+
278+
$this->assertSame('', $networkInfo['dns']);
279+
$this->assertArrayHasKey('hostname', $networkInfo);
280+
$this->assertArrayHasKey('gateway', $networkInfo);
281+
}
282+
259283
public function testGetNetworkInterfaces(): void {
260284
$this->os->method('getNetInterfaces')
261285
->willReturn(json_decode(file_get_contents(__DIR__ . '/../data/linux_net_get_interfaces.json'), true, 512, JSON_THROW_ON_ERROR));

0 commit comments

Comments
 (0)