From 5514eb1f8745c303cf5da0888d7375ff3d4bc927 Mon Sep 17 00:00:00 2001
From: Derek Kaser <11674153+dkaser@users.noreply.github.com>
Date: Sun, 27 Oct 2024 00:06:20 -0400
Subject: [PATCH] refactor: move formatting out of Info class (#14)
---
.../plugins/tailscale/Tailscale-4-Info.page | 32 +++-
.../emhttp/plugins/tailscale/Tailscale.page | 8 +-
.../tailscale/Tailscale_dashboard.page | 7 +-
.../include/Tailscale/ConnectionInfo.php | 14 ++
.../include/Tailscale/DashboardInfo.php | 13 ++
.../tailscale/include/Tailscale/Info.php | 156 ++++++------------
.../tailscale/include/Tailscale/LockInfo.php | 11 ++
.../include/Tailscale/PeerStatus.php | 23 +++
.../include/Tailscale/StatusInfo.php | 16 ++
.../tailscale/include/Tailscale/Utils.php | 19 +++
.../tailscale/include/Tailscale/Warning.php | 15 ++
11 files changed, 204 insertions(+), 110 deletions(-)
create mode 100644 src/usr/local/emhttp/plugins/tailscale/include/Tailscale/ConnectionInfo.php
create mode 100644 src/usr/local/emhttp/plugins/tailscale/include/Tailscale/DashboardInfo.php
create mode 100644 src/usr/local/emhttp/plugins/tailscale/include/Tailscale/LockInfo.php
create mode 100644 src/usr/local/emhttp/plugins/tailscale/include/Tailscale/PeerStatus.php
create mode 100644 src/usr/local/emhttp/plugins/tailscale/include/Tailscale/StatusInfo.php
create mode 100644 src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Warning.php
diff --git a/src/usr/local/emhttp/plugins/tailscale/Tailscale-4-Info.page b/src/usr/local/emhttp/plugins/tailscale/Tailscale-4-Info.page
index d080c3e..15f7cfd 100644
--- a/src/usr/local/emhttp/plugins/tailscale/Tailscale-4-Info.page
+++ b/src/usr/local/emhttp/plugins/tailscale/Tailscale-4-Info.page
@@ -14,6 +14,8 @@ if (!$tailscaleConfig->Enable) {
}
$tailscaleInfo = $tailscaleInfo ?? new Tailscale\Info($tr);
+$tailscaleStatusInfo = $tailscaleInfo->getStatusInfo();
+$tailscaleConInfo = $tailscaleInfo->getConnectionInfo();
?>
@@ -23,7 +25,25 @@ $tailscaleInfo = $tailscaleInfo ?? new Tailscale\Info($tr);
- = $tailscaleInfo->getStatusInfo(); ?>
+ 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);
+ }
+ ?>
@@ -38,6 +58,14 @@ $tailscaleInfo = $tailscaleInfo ?? new Tailscale\Info($tr);
- = $tailscaleInfo->getConnectionInfo(); ?>
+ 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);
+ ?>
diff --git a/src/usr/local/emhttp/plugins/tailscale/Tailscale.page b/src/usr/local/emhttp/plugins/tailscale/Tailscale.page
index c4b8516..e44b896 100755
--- a/src/usr/local/emhttp/plugins/tailscale/Tailscale.page
+++ b/src/usr/local/emhttp/plugins/tailscale/Tailscale.page
@@ -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()) ?>
+
diff --git a/src/usr/local/emhttp/plugins/tailscale/Tailscale_dashboard.page b/src/usr/local/emhttp/plugins/tailscale/Tailscale_dashboard.page
index 33ef77e..ca7ece7 100644
--- a/src/usr/local/emhttp/plugins/tailscale/Tailscale_dashboard.page
+++ b/src/usr/local/emhttp/plugins/tailscale/Tailscale_dashboard.page
@@ -13,7 +13,12 @@ $tailscale_dashboard = "" . $tr->tr("tailscale_disabled") . " |
"
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("
", $tailscaleDashInfo->TailscaleIPs));
}
$mytiles['tailscale']['column2'] =
diff --git a/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/ConnectionInfo.php b/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/ConnectionInfo.php
new file mode 100644
index 0000000..33fe3fb
--- /dev/null
+++ b/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/ConnectionInfo.php
@@ -0,0 +1,14 @@
+ $TailscaleIPs */
+ public array $TailscaleIPs = array();
+
+ public string $HostName = "";
+ public string $DNSName = "";
+ public string $Online = "";
+}
diff --git a/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Info.php b/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Info.php
index 3205c62..2de5a88 100644
--- a/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Info.php
+++ b/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Info.php
@@ -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 "{$title} | {$value} |
" . PHP_EOL;
- }
-
- public static function printDash(string $title, string $value): string
- {
- return "{$title}{$value} |
" . 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("
", $status->Self->Tags) : "";
- $loggedIn = isset($prefs->LoggedOut) ? ($prefs->LoggedOut ? $this->tr("no") : $this->tr("yes")) : $this->tr("unknown");
- $tsHealth = isset($status->Health) ? implode("
", $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("
", $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("
", $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("
", $status->TailscaleIPs) : $this->tr("unknown");
- $magicDNSSuffix = isset($status->MagicDNSSuffix) ? $status->MagicDNSSuffix : $this->tr("unknown");
- $advertisedRoutes = isset($prefs->AdvertiseRoutes) ? implode("
", $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("
", $status->TailscaleIPs) : $this->tr("unknown");
+ $info->MagicDNSSuffix = isset($status->MagicDNSSuffix) ? $status->MagicDNSSuffix : $this->tr("unknown");
+ $info->AdvertisedRoutes = isset($prefs->AdvertiseRoutes) ? implode("
", $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("
", $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;
@@ -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 "" . sprintf($this->tr("warnings.key_expiration"), $intervalPrint, $expiryPrint) . "";
+ return $warning;
}
- return "";
+ return null;
}
public function getTailscaleLockEnabled(): bool
@@ -239,20 +209,20 @@ public function getTailscaleLockPending(): array
return $pending;
}
- public function getTailscaleLockWarning(): string
+ public function getTailscaleLockWarning(): ?Warning
{
if ($this->getTailscaleLockEnabled() && ( ! $this->getTailscaleLockSigned())) {
- return "" . $this->tr("warnings.lock") . "";
+ 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 "" . $this->tr("warnings.netbios") . "";
+ return new Warning($this->tr("warnings.netbios"), "warn");
}
- return "";
+ return null;
}
/**
@@ -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;
-}
diff --git a/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/LockInfo.php b/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/LockInfo.php
new file mode 100644
index 0000000..46d29b9
--- /dev/null
+++ b/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/LockInfo.php
@@ -0,0 +1,11 @@
+{$title} | {$value} | " . PHP_EOL;
+ }
+
+ public static function printDash(string $title, string $value): string
+ {
+ return "{$title}{$value} |
" . PHP_EOL;
+ }
+
+ public static function formatWarning(?Warning $warning): string
+ {
+ if ($warning == null) {
+ return "";
+ }
+
+ return "" . $warning->Message . "";
+ }
+
public static function make_option(bool $selected, string $value, string $text, string $extra = ""): string
{
return "";
diff --git a/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Warning.php b/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Warning.php
new file mode 100644
index 0000000..4c0e186
--- /dev/null
+++ b/src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Warning.php
@@ -0,0 +1,15 @@
+Message = $message;
+ $this->Priority = $priority;
+ }
+}