Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function getNetworkInfo(): array {
$result = [
'gateway' => '',
'hostname' => \gethostname(),
'dns' => '',
];

try {
Expand Down
1 change: 1 addition & 0 deletions lib/OperatingSystems/IOperatingSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function getDiskInfo(): array;
* [
* 'gateway' => string,
* 'hostname' => string,
* 'dns' => string,
* ]
*/
public function getNetworkInfo(): array;
Expand Down
10 changes: 10 additions & 0 deletions lib/OperatingSystems/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion templates/settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ function FormatMegabytes(int $byte): string {
<?php p($l->t('Gateway:')); ?>
<span class="info"><?php p($_['networkinfo']['gateway']); ?></span>
</div>
<div class="col col-12">
<?php p($l->t('DNS:')); ?>
<span class="info"><?php p($_['networkinfo']['dns']); ?></span>
</div>
<div class="col col-12">
<div class="row">
<?php foreach ($interfaces as $interface): ?>
Expand Down Expand Up @@ -532,4 +536,3 @@ function FormatMegabytes(int $byte): string {
</div>

</div>

8 changes: 8 additions & 0 deletions tests/lib/DummyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
12 changes: 12 additions & 0 deletions tests/lib/FreeBSDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/LinuxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading