feat: watch for serve config that conflicts with WebGUI - #37
Conversation
…m.php Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Derek Kaser <11674153+dkaser@users.noreply.github.com>
…m.php Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Derek Kaser <11674153+dkaser@users.noreply.github.com>
|
CodeRabbit (@coderabbitai) full review |
✅ Actions performedFull review triggered. |
WalkthroughNew methods for managing and validating Tailscale serve configuration were added to the LocalAPI and System classes. Logging functionality was enhanced with optional rate limiting. The Watcher class now invokes a check for serve configuration conflicts during its main loop, integrating new conflict detection and resolution logic. Changes
Sequence Diagram(s)sequenceDiagram
participant Watcher
participant System
participant LocalAPI
participant Utils
Watcher->>System: checkWebgui()
Watcher->>System: checkServeConfig()
System->>LocalAPI: getServeConfig()
LocalAPI-->>System: Serve config data
System->>Utils: logmsg(conflict/debug)
alt Conflict detected
System->>LocalAPI: resetServeConfig()
System->>Utils: logmsg(reset/restart)
System->>System: restart command
end
Watcher->>System: fixLocalSubnetRoutes()
Poem
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (2)
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/System.php (2)
67-98:⚠️ Potential issueAdd error handling for serve config API response.
The checkServeConfig method lacks proper error handling for potential API failures and type checking for the configuration values.
Improve the error handling when retrieving and processing the serve configuration:
$localAPI = new LocalAPI(); $serveConfig = $localAPI->getServeConfig(); -$tcpConfig = $serveConfig->TCP ?? array(); +// Check if we got a valid response +if (!isset($serveConfig) || !is_object($serveConfig)) { + Utils::logmsg("Failed to get valid serve config"); + return; +} + +$tcpConfig = $serveConfig->TCP ?? array(); + +// Ensure tcpConfig is iterable +if (!is_array($tcpConfig) && !is_object($tcpConfig)) { + Utils::logmsg("TCP config is not iterable"); + return; +}🧰 Tools
🪛 PHPStan (2.0.3)
85-85: Argument of an invalid type mixed supplied for foreach, only iterables are supported.
(foreach.nonIterable)
86-86: Parameter #1 $value of function intval expects array|bool|float|int|resource|string|null, mixed given.
(argument.type)
85-97: 🛠️ Refactor suggestionImprove type checking and add user notification.
The code currently doesn't verify the port key types before processing and lacks user notification when conflicts are resolved.
foreach ($tcpConfig as $key => $val) { - $configPort = intval($key); + // Ensure the key is a valid port number + if (!is_numeric($key)) { + Utils::logmsg("Invalid TCP port key: {$key}"); + continue; + } + + $configPort = intval($key); if ($configPort == $httpPort || $configPort == $httpsPort) { Utils::logmsg("Serve TCP Port {$configPort} conflicts with WebGUI, removing"); $localAPI->resetServeConfig(); + // Notify the user before restarting + $event = "Tailscale Serve Config Reset"; + $command = self::NOTIFY_COMMAND . " -e " . escapeshellarg($event) . + " -s " . escapeshellarg("WebGUI port conflict resolved") . + " -d " . escapeshellarg("Removed Tailscale serve config using port {$configPort}") . + " -i \"warning\" -x 2>/dev/null"; + exec($command); Utils::run_command(self::RESTART_COMMAND); return;🧰 Tools
🪛 PHPStan (2.0.3)
85-85: Argument of an invalid type mixed supplied for foreach, only iterables are supported.
(foreach.nonIterable)
86-86: Parameter #1 $value of function intval expects array|bool|float|int|resource|string|null, mixed given.
(argument.type)
🧹 Nitpick comments (2)
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/System.php (2)
71-73: Improve config file error handling.Consider adding more robust error handling for parsing the config file and handling potential missing values.
The current implementation correctly checks for scalar values before using
intval(), but could be more defensive by checking if the config file was read successfully at all:$ident_config = parse_ini_file("/boot/config/ident.cfg") ?: array(); +// Log if we couldn't read the config file +if (empty($ident_config)) { + Utils::logmsg("Could not read ident.cfg, using default WebGUI ports"); +} + $httpPort = isset($ident_config['PORT']) && is_scalar($ident_config['PORT']) ? intval($ident_config['PORT']) : 80;Also applies to: 76-77
95-95: Consider using the rate limit parameter in Utils::logmsg.The logging check is being done in the code, but the
rateLimitparameter ofUtils::logmsgcould be used instead for consistency.-Utils::logmsg("Checked for WebGUI conflict with serve TCP Port {$configPort}", false, true); +Utils::logmsg("Checked for WebGUI conflict with serve TCP Port {$configPort}", false, true);While this doesn't change the functionality, it clarifies that this log message is intentionally rate-limited, matching the pattern used in the
checkWebguimethod.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)
📒 Files selected for processing (4)
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/LocalAPI.php(1 hunks)src/usr/local/emhttp/plugins/tailscale/include/Tailscale/System.php(2 hunks)src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Utils.php(1 hunks)src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Watcher.php(1 hunks)
🧰 Additional context used
🪛 PHPStan (2.0.3)
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/System.php
57-57: Part $ident_config['PORT'] (mixed) of encapsed string cannot be cast to string.
(encapsedStringPart.nonString)
85-85: Argument of an invalid type mixed supplied for foreach, only iterables are supported.
(foreach.nonIterable)
86-86: Parameter #1 $value of function intval expects array|bool|float|int|resource|string|null, mixed given.
(argument.type)
🔇 Additional comments (4)
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Utils.php (1)
197-203: Good implementation of rate-limited logging.The new rate limiting parameter allows for controlling log frequency, which is useful for recurring checks. The implementation appropriately checks if the current minute modulo 10 equals zero, ensuring logs are only written every 10 minutes while maintaining visibility into system state.
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Watcher.php (1)
54-56: Good integration of serve config monitoring.The added line properly integrates the new serve configuration validation into the watcher's main execution loop. This ensures that potential conflicts between Tailscale serve ports and the WebGUI are detected regularly alongside other system checks.
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/LocalAPI.php (1)
63-71: Well-implemented LocalAPI methods for serve config management.The new methods properly leverage the existing
tailscaleLocalAPIhelper for interacting with the Tailscale local API endpoints. This maintains consistency with the rest of the class implementation.src/usr/local/emhttp/plugins/tailscale/include/Tailscale/System.php (1)
57-57: Updated logging with rate limiting.The log message when WebGUI is confirmed to be listening now uses rate limiting, which helps reduce log noise for this recurring check.
🧰 Tools
🪛 PHPStan (2.0.3)
57-57: Part $ident_config['PORT'] (mixed) of encapsed string cannot be cast to string.
(encapsedStringPart.nonString)
Summary by CodeRabbit