Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

feat: watch for serve config that conflicts with WebGUI - #37

Merged
Derek Kaser (dkaser) merged 4 commits into
mainfrom
check-serve
May 6, 2025
Merged

feat: watch for serve config that conflicts with WebGUI#37
Derek Kaser (dkaser) merged 4 commits into
mainfrom
check-serve

Conversation

@dkaser

@dkaser Derek Kaser (dkaser) commented May 6, 2025

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features
    • Introduced automatic detection and resolution of TCP port conflicts between the WebGUI and other services, helping to prevent service interruptions.
  • Improvements
    • Enhanced logging options, including configurable rate-limited logging for improved clarity and reduced log noise.
  • Chores
    • Internal checks and validations are now performed automatically to maintain system stability.

coderabbitai[bot]

This comment was marked as outdated.

…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>
coderabbitai[bot]

This comment was marked as outdated.

…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>
coderabbitai[bot]

This comment was marked as outdated.

@unraid Unraid (unraid) deleted a comment from coderabbitai Bot May 6, 2025
@dkaser

Copy link
Copy Markdown
Collaborator Author

CodeRabbit (@coderabbitai) full review

@coderabbitai

coderabbitai Bot commented May 6, 2025

Copy link
Copy Markdown
✅ Actions performed

Full review triggered.

@coderabbitai

coderabbitai Bot commented May 6, 2025

Copy link
Copy Markdown

Walkthrough

New 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

File(s) Change Summary
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/LocalAPI.php Added getServeConfig() and resetServeConfig() public methods for interacting with the Tailscale local API serve configuration.
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/System.php Added checkServeConfig() static method to validate and reset serve config if it conflicts with WebGUI ports; updated logging usage.
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Utils.php Updated logmsg() to accept an optional rateLimit parameter, enabling 10-minute interval logging when specified.
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Watcher.php Inserted a call to System::checkServeConfig() in the main loop, integrating serve config conflict checks into the watcher process.

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()
Loading

Poem

In the warren of code, a new check appears,
Tails wag as serve configs face their fears.
Ports are compared, conflicts reset,
With logging that’s timely—no spam to regret!
A Watcher now guards with vigilant might,
Hopping through configs, keeping things right.
🐇✨


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (2)
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/System.php (2)

67-98: ⚠️ Potential issue

Add 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 suggestion

Improve 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 rateLimit parameter of Utils::logmsg could 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 checkWebgui method.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 105381b and 38c398d.

📒 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 tailscaleLocalAPI helper 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)

@dkaser
Derek Kaser (dkaser) merged commit 6dc5e7b into main May 6, 2025
@dkaser
Derek Kaser (dkaser) deleted the check-serve branch May 6, 2025 03:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant