Skip to content

Commit 2aa553f

Browse files
authored
feat: add notification for serve port conflicts (#38)
1 parent 6dc5e7b commit 2aa553f

File tree

1 file changed

+23
-6
lines changed
  • src/usr/local/emhttp/plugins/tailscale/include/Tailscale

1 file changed

+23
-6
lines changed

src/usr/local/emhttp/plugins/tailscale/include/Tailscale/System.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace Tailscale;
44

5+
enum NotificationType: string
6+
{
7+
case NORMAL = 'normal';
8+
case WARNING = 'warning';
9+
case ALERT = 'alert';
10+
}
11+
512
class System
613
{
714
public const RESTART_COMMAND = "/usr/local/emhttp/webGui/scripts/reload_services";
@@ -87,6 +94,12 @@ public static function checkServeConfig(): void
8794

8895
if ($configPort == $httpPort || $configPort == $httpsPort) {
8996
Utils::logmsg("Serve TCP Port {$configPort} conflicts with WebGUI, removing");
97+
self::sendNotification(
98+
"Tailscale Serve Port Conflict",
99+
"Tailscale Serve Port Conflict",
100+
"Port {$configPort} conflicts with WebGUI port. The Tailscale serve config has been reset to remove the conflict.",
101+
NotificationType::ALERT
102+
);
90103
$localAPI->resetServeConfig();
91104
Utils::run_command(self::RESTART_COMMAND);
92105

@@ -184,25 +197,29 @@ public static function notifyOnKeyExpiration(): void
184197

185198
switch (true) {
186199
case $interval->days <= 7:
187-
$priority = 'alert';
200+
$priority = NotificationType::ALERT;
188201
break;
189202
case $interval->days <= 30:
190-
$priority = 'warning';
203+
$priority = NotificationType::WARNING;
191204
break;
192205
default:
193206
return;
194207
}
195208

196-
$event = "Tailscale Key Expiration - {$priority} - {$expiryTime->format('Ymd')}";
209+
$event = "Tailscale Key Expiration - {$priority->value} - {$expiryTime->format('Ymd')}";
197210
Utils::logmsg("Sending notification for key expiration: {$event}");
198-
199-
$command = self::NOTIFY_COMMAND . " -l '/Settings/Tailscale' -e " . escapeshellarg($event) . " -s " . escapeshellarg("Tailscale key is expiring") . " -d " . escapeshellarg("{$message}") . " -i \"{$priority}\" -x 2>/dev/null";
200-
exec($command);
211+
self::sendNotification($event, "Tailscale key is expiring", $message, $priority);
201212
} else {
202213
Utils::logmsg("Tailscale key expiration is not set.");
203214
}
204215
}
205216

217+
public static function sendNotification(string $event, string $subject, string $message, NotificationType $priority): void
218+
{
219+
$command = self::NOTIFY_COMMAND . " -l '/Settings/Tailscale' -e " . escapeshellarg($event) . " -s " . escapeshellarg($subject) . " -d " . escapeshellarg("{$message}") . " -i \"{$priority->value}\" -x 2>/dev/null";
220+
exec($command);
221+
}
222+
206223
public static function refreshWebGuiCert(bool $restartIfChanged = true): void
207224
{
208225
$localAPI = new LocalAPI();

0 commit comments

Comments
 (0)