Skip to content

Commit

Permalink
refactor: move formatting out of Info class (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkaser authored Oct 27, 2024
1 parent f68354f commit 5514eb1
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 110 deletions.
32 changes: 30 additions & 2 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale-4-Info.page
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ if (!$tailscaleConfig->Enable) {
}

$tailscaleInfo = $tailscaleInfo ?? new Tailscale\Info($tr);
$tailscaleStatusInfo = $tailscaleInfo->getStatusInfo();
$tailscaleConInfo = $tailscaleInfo->getConnectionInfo();
?>
<table class="unraid t1">
<thead>
Expand All @@ -23,7 +25,25 @@ $tailscaleInfo = $tailscaleInfo ?? new Tailscale\Info($tr);
</tr>
</thead>
<tbody>
<?= $tailscaleInfo->getStatusInfo(); ?>
<?php
$lockTranslate = $tr->tr("tailscale_lock");

echo Tailscale\Utils::printRow($tr->tr("info.version"), $tailscaleStatusInfo->TsVersion);
echo Tailscale\Utils::printRow($tr->tr("info.health"), $tailscaleStatusInfo->TsHealth);
echo Tailscale\Utils::printRow($tr->tr("info.login"), $tailscaleStatusInfo->LoggedIn);
echo Tailscale\Utils::printRow($tr->tr("info.netmap"), $tailscaleStatusInfo->InNetMap);
echo Tailscale\Utils::printRow($tr->tr("info.online"), $tailscaleStatusInfo->Online);
echo Tailscale\Utils::printRow($tr->tr("info.key_expire"), $tailscaleStatusInfo->KeyExpiration);
echo Tailscale\Utils::printRow($tr->tr("info.tags"), $tailscaleStatusInfo->Tags);
echo Tailscale\Utils::printRow("{$lockTranslate}: " . $tr->tr("enabled"), $tailscaleStatusInfo->LockEnabled);

if($tailscaleStatusInfo->LockInfo != null) {
echo Tailscale\Utils::printRow("{$lockTranslate}: " . $tr->tr("info.lock.signed"), $tailscaleStatusInfo->LockInfo->LockSigned);
echo Tailscale\Utils::printRow("{$lockTranslate}: " . $tr->tr("info.lock.signing"), $tailscaleStatusInfo->LockInfo->LockSigning);
echo Tailscale\Utils::printRow("{$lockTranslate}: " . $tr->tr("info.lock.node_key"), $tailscaleStatusInfo->LockInfo->NodeKey);
echo Tailscale\Utils::printRow("{$lockTranslate}: " . $tr->tr("info.lock.public_key"), $tailscaleStatusInfo->LockInfo->PubKey);
}
?>
</tbody>
<tbody>
<tr>
Expand All @@ -38,6 +58,14 @@ $tailscaleInfo = $tailscaleInfo ?? new Tailscale\Info($tr);
</tr>
</thead>
<tbody>
<?= $tailscaleInfo->getConnectionInfo(); ?>
<?php
echo Tailscale\Utils::printRow($tr->tr("info.hostname"), $tailscaleConInfo->HostName);
echo Tailscale\Utils::printRow($tr->tr("info.dns"), $tailscaleConInfo->DNSName);
echo Tailscale\Utils::printRow($tr->tr("info.ip"), $tailscaleConInfo->TailscaleIPs);
echo Tailscale\Utils::printRow($tr->tr("info.magicdns"), $tailscaleConInfo->MagicDNSSuffix);
echo Tailscale\Utils::printRow($tr->tr("info.routes"), $tailscaleConInfo->AdvertisedRoutes);
echo Tailscale\Utils::printRow($tr->tr("info.accept_routes"), $tailscaleConInfo->AcceptRoutes);
echo Tailscale\Utils::printRow($tr->tr("info.accept_dns"), $tailscaleConInfo->AcceptDNS);
?>
</tbody>
</table>
8 changes: 4 additions & 4 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale.page
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ if (!$tailscaleConfig->Enable) {
}

$tailscaleInfo = $tailscaleInfo ?? new Tailscale\Info($tr);

echo($tailscaleInfo->getTailscaleLockWarning());
echo($tailscaleInfo->getKeyExpirationWarning());
echo($tailscaleInfo->getTailscaleNetbiosWarning());
?>
<?= Tailscale\Utils::formatWarning($tailscaleInfo->getTailscaleLockWarning()) ?>
<?= Tailscale\Utils::formatWarning($tailscaleInfo->getTailscaleNetbiosWarning()) ?>
<?= Tailscale\Utils::formatWarning($tailscaleInfo->getKeyExpirationWarning()) ?>

<iframe src="/plugins/tailscale/interface.php" style="width:100%; height:600px; border: none;"></iframe>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ $tailscale_dashboard = "<tr><td>" . $tr->tr("tailscale_disabled") . "</td></tr>"

if ($tailscaleConfig->Enable) {
$tailscaleInfo = $tailscaleInfo ?? new Tailscale\Info($tr);
$tailscale_dashboard = $tailscaleInfo->getDashboardInfo();
$tailscaleDashInfo = $tailscaleInfo->getDashboardInfo();

$tailscale_dashboard = Tailscale\Utils::printDash($tr->tr("info.online"), $tailscaleDashInfo->Online);
$tailscale_dashboard .= Tailscale\Utils::printDash($tr->tr("info.hostname"), $tailscaleDashInfo->HostName);
$tailscale_dashboard .= Tailscale\Utils::printDash($tr->tr("info.dns"), $tailscaleDashInfo->DNSName);
$tailscale_dashboard .= Tailscale\Utils::printDash($tr->tr("info.ip"), implode("<br /><span class='w26'>&nbsp;</span>", $tailscaleDashInfo->TailscaleIPs));
}

$mytiles['tailscale']['column2'] =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Tailscale;

class ConnectionInfo
{
public string $HostName = "";
public string $DNSName = "";
public string $TailscaleIPs = "";
public string $MagicDNSSuffix = "";
public string $AdvertisedRoutes = "";
public string $AcceptRoutes = "";
public string $AcceptDNS = "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tailscale;

class DashboardInfo
{
/** @var array<string> $TailscaleIPs */
public array $TailscaleIPs = array();

public string $HostName = "";
public string $DNSName = "";
public string $Online = "";
}
156 changes: 53 additions & 103 deletions src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,107 +42,75 @@ public static function getLock(): \stdClass
return (object) json_decode(implode($out_status));
}

public static function printRow(string $title, string $value): string
{
return "<tr><td>{$title}</td><td>{$value}</td></tr>" . PHP_EOL;
}

public static function printDash(string $title, string $value): string
{
return "<tr><td><span class='w26'>{$title}</span>{$value}</td></tr>" . PHP_EOL;
}

private function tr(string $message): string
{
return $this->tr->tr($message);
}

public function getStatusInfo(): string
public function getStatusInfo(): StatusInfo
{
$status = $this->status;
$prefs = $this->prefs;
$lock = $this->lock;

$tsVersion = isset($status->Version) ? $status->Version : $this->tr("unknown");
$keyExpiration = isset($status->Self->KeyExpiry) ? $status->Self->KeyExpiry : $this->tr("disabled");
$online = isset($status->Self->Online) ? ($status->Self->Online ? $this->tr("yes") : $this->tr("no")) : $this->tr("unknown");
$inNetMap = isset($status->Self->InNetworkMap) ? ($status->Self->InNetworkMap ? $this->tr("yes") : $this->tr("no")) : $this->tr("unknown");
$tags = isset($status->Self->Tags) ? implode("<br />", $status->Self->Tags) : "";
$loggedIn = isset($prefs->LoggedOut) ? ($prefs->LoggedOut ? $this->tr("no") : $this->tr("yes")) : $this->tr("unknown");
$tsHealth = isset($status->Health) ? implode("<br />", $status->Health) : "";
$lockEnabled = $this->getTailscaleLockEnabled() ? $this->tr("yes") : $this->tr("no");

$lockTranslate = $this->tr("tailscale_lock");

$output = "";
$output .= self::printRow($this->tr("info.version"), $tsVersion);
$output .= self::printRow($this->tr("info.health"), $tsHealth);
$output .= self::printRow($this->tr("info.login"), $loggedIn);
$output .= self::printRow($this->tr("info.netmap"), $inNetMap);
$output .= self::printRow($this->tr("info.online"), $online);
$output .= self::printRow($this->tr("info.key_expire"), $keyExpiration);
$output .= self::printRow($this->tr("info.tags"), $tags);
$output .= self::printRow("{$lockTranslate}: " . $this->tr("enabled"), $lockEnabled);
$statusInfo = new StatusInfo();

$statusInfo->TsVersion = isset($status->Version) ? $status->Version : $this->tr("unknown");
$statusInfo->KeyExpiration = isset($status->Self->KeyExpiry) ? $status->Self->KeyExpiry : $this->tr("disabled");
$statusInfo->Online = isset($status->Self->Online) ? ($status->Self->Online ? $this->tr("yes") : $this->tr("no")) : $this->tr("unknown");
$statusInfo->InNetMap = isset($status->Self->InNetworkMap) ? ($status->Self->InNetworkMap ? $this->tr("yes") : $this->tr("no")) : $this->tr("unknown");
$statusInfo->Tags = isset($status->Self->Tags) ? implode("<br />", $status->Self->Tags) : "";
$statusInfo->LoggedIn = isset($prefs->LoggedOut) ? ($prefs->LoggedOut ? $this->tr("no") : $this->tr("yes")) : $this->tr("unknown");
$statusInfo->TsHealth = isset($status->Health) ? implode("<br />", $status->Health) : "";
$statusInfo->LockEnabled = $this->getTailscaleLockEnabled() ? $this->tr("yes") : $this->tr("no");

if ($this->getTailscaleLockEnabled()) {
$lockSigned = $this->getTailscaleLockSigned() ? $this->tr("yes") : $this->tr("no");
$lockSigning = $this->getTailscaleLockSigning() ? $this->tr("yes") : $this->tr("no");
$pubKey = $this->getTailscaleLockPubkey();
$nodeKey = $this->getTailscaleLockNodekey();

$output .= self::printRow("{$lockTranslate}: " . $this->tr("info.lock.signed"), $lockSigned);
$output .= self::printRow("{$lockTranslate}: " . $this->tr("info.lock.signing"), $lockSigning);
$output .= self::printRow("{$lockTranslate}: " . $this->tr("info.lock.node_key"), $nodeKey);
$output .= self::printRow("{$lockTranslate}: " . $this->tr("info.lock.public_key"), $pubKey);
$lockInfo = new LockInfo();

$lockInfo->LockSigned = $this->getTailscaleLockSigned() ? $this->tr("yes") : $this->tr("no");
$lockInfo->LockSigning = $this->getTailscaleLockSigning() ? $this->tr("yes") : $this->tr("no");
$lockInfo->PubKey = $this->getTailscaleLockPubkey();
$lockInfo->NodeKey = $this->getTailscaleLockNodekey();

$statusInfo->LockInfo = $lockInfo;
}

return $output;
return $statusInfo;
}

public function getConnectionInfo(): string
public function getConnectionInfo(): ConnectionInfo
{
$status = $this->status;
$prefs = $this->prefs;

$hostName = isset($status->Self->HostName) ? $status->Self->HostName : $this->tr("unknown");
$dnsName = isset($status->Self->DNSName) ? $status->Self->DNSName : $this->tr("unknown");
$tailscaleIPs = isset($status->TailscaleIPs) ? implode("<br />", $status->TailscaleIPs) : $this->tr("unknown");
$magicDNSSuffix = isset($status->MagicDNSSuffix) ? $status->MagicDNSSuffix : $this->tr("unknown");
$advertisedRoutes = isset($prefs->AdvertiseRoutes) ? implode("<br />", $prefs->AdvertiseRoutes) : $this->tr("none");
$acceptRoutes = isset($prefs->RouteAll) ? ($prefs->RouteAll ? $this->tr("yes") : $this->tr("no")) : $this->tr("unknown");
$acceptDNS = isset($prefs->CorpDNS) ? ($prefs->CorpDNS ? $this->tr("yes") : $this->tr("no")) : $this->tr("unknown");

$output = "";
$output .= self::printRow($this->tr("info.hostname"), $hostName);
$output .= self::printRow($this->tr("info.dns"), $dnsName);
$output .= self::printRow($this->tr("info.ip"), $tailscaleIPs);
$output .= self::printRow($this->tr("info.magicdns"), $magicDNSSuffix);
$output .= self::printRow($this->tr("info.routes"), $advertisedRoutes);
$output .= self::printRow($this->tr("info.accept_routes"), $acceptRoutes);
$output .= self::printRow($this->tr("info.accept_dns"), $acceptDNS);

return $output;
$info = new ConnectionInfo();

$info->HostName = isset($status->Self->HostName) ? $status->Self->HostName : $this->tr("unknown");
$info->DNSName = isset($status->Self->DNSName) ? $status->Self->DNSName : $this->tr("unknown");
$info->TailscaleIPs = isset($status->TailscaleIPs) ? implode("<br />", $status->TailscaleIPs) : $this->tr("unknown");
$info->MagicDNSSuffix = isset($status->MagicDNSSuffix) ? $status->MagicDNSSuffix : $this->tr("unknown");
$info->AdvertisedRoutes = isset($prefs->AdvertiseRoutes) ? implode("<br />", $prefs->AdvertiseRoutes) : $this->tr("none");
$info->AcceptRoutes = isset($prefs->RouteAll) ? ($prefs->RouteAll ? $this->tr("yes") : $this->tr("no")) : $this->tr("unknown");
$info->AcceptDNS = isset($prefs->CorpDNS) ? ($prefs->CorpDNS ? $this->tr("yes") : $this->tr("no")) : $this->tr("unknown");

return $info;
}

public function getDashboardInfo(): string
public function getDashboardInfo(): DashboardInfo
{
$status = $this->status;

$hostName = isset($status->Self->HostName) ? $status->Self->HostName : $this->tr("Unknown");
$dnsName = isset($status->Self->DNSName) ? $status->Self->DNSName : $this->tr("Unknown");
$tailscaleIPs = isset($status->TailscaleIPs) ? implode("<br /><span class='w26'>&nbsp;</span>", $status->TailscaleIPs) : $this->tr("unknown");
$online = isset($status->Self->Online) ? ($status->Self->Online ? $this->tr("yes") : $this->tr("no")) : $this->tr("unknown");
$info = new DashboardInfo();

$output = "";
$output .= self::printDash($this->tr("info.online"), $online);
$output .= self::printDash($this->tr("info.hostname"), $hostName);
$output .= self::printDash($this->tr("info.dns"), $dnsName);
$output .= self::printDash($this->tr("info.ip"), $tailscaleIPs);
$info->HostName = isset($status->Self->HostName) ? $status->Self->HostName : $this->tr("Unknown");
$info->DNSName = isset($status->Self->DNSName) ? $status->Self->DNSName : $this->tr("Unknown");
$info->TailscaleIPs = isset($status->TailscaleIPs) ? $status->TailscaleIPs : array();
$info->Online = isset($status->Self->Online) ? ($status->Self->Online ? $this->tr("yes") : $this->tr("no")) : $this->tr("unknown");

return $output;
return $info;
}

public function getKeyExpirationWarning(): string
public function getKeyExpirationWarning(): ?Warning
{
$status = $this->status;

Expand All @@ -154,21 +122,23 @@ public function getKeyExpirationWarning(): string
$expiryPrint = $expiryTime->format(\DateTimeInterface::RFC7231);
$intervalPrint = $interval->format('%a');

$warning = new Warning(sprintf($this->tr("warnings.key_expiration"), $intervalPrint, $expiryPrint));

switch (true) {
case $interval->days <= 7:
$priority = 'error';
$warning->Priority = 'error';
break;
case $interval->days <= 30:
$priority = 'warn';
$warning->Priority = 'warn';
break;
default:
$priority = 'system';
$warning->Priority = 'system';
break;
}

return "<span class='{$priority}' style='text-align: center; font-size: 1.4em; font-weight: bold;'>" . sprintf($this->tr("warnings.key_expiration"), $intervalPrint, $expiryPrint) . "</span>";
return $warning;
}
return "";
return null;
}

public function getTailscaleLockEnabled(): bool
Expand Down Expand Up @@ -239,20 +209,20 @@ public function getTailscaleLockPending(): array
return $pending;
}

public function getTailscaleLockWarning(): string
public function getTailscaleLockWarning(): ?Warning
{
if ($this->getTailscaleLockEnabled() && ( ! $this->getTailscaleLockSigned())) {
return "<span class='error' style='text-align: center; font-size: 1.4em; font-weight: bold;'>" . $this->tr("warnings.lock") . "</span>";
return new Warning($this->tr("warnings.lock"), "error");
}
return "";
return null;
}

public function getTailscaleNetbiosWarning(): string
public function getTailscaleNetbiosWarning(): ?Warning
{
if (($this->useNetbios == "yes") && ($this->smbEnabled != "no")) {
return "<span class='warn' style='text-align: center; font-size: 1.4em; font-weight: bold;'>" . $this->tr("warnings.netbios") . "</span>";
return new Warning($this->tr("warnings.netbios"), "warn");
}
return "";
return null;
}

/**
Expand Down Expand Up @@ -307,23 +277,3 @@ public function getPeerStatus(): array
return $result;
}
}

class PeerStatus
{
public string $Name = "";
public string $IP = "";
public string $LoginName = "";

public string $Address = "";

public bool $Online = false;
public bool $Active = false;
public bool $Relayed = false;

public bool $Traffic = false;
public int $TxBytes = 0;
public int $RxBytes = 0;

public bool $ExitNodeActive = false;
public bool $ExitNodeAvailable = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tailscale;

class LockInfo
{
public string $LockSigned = "";
public string $LockSigning = "";
public string $PubKey = "";
public string $NodeKey = "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tailscale;

class PeerStatus
{
public string $Name = "";
public string $IP = "";
public string $LoginName = "";

public string $Address = "";

public bool $Online = false;
public bool $Active = false;
public bool $Relayed = false;

public bool $Traffic = false;
public int $TxBytes = 0;
public int $RxBytes = 0;

public bool $ExitNodeActive = false;
public bool $ExitNodeAvailable = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tailscale;

class StatusInfo
{
public LockInfo $LockInfo;
public string $TsVersion = "";
public string $KeyExpiration = "";
public string $Online = "";
public string $InNetMap = "";
public string $Tags = "";
public string $LoggedIn = "";
public string $TsHealth = "";
public string $LockEnabled = "";
}
Loading

0 comments on commit 5514eb1

Please sign in to comment.