Skip to content

fix(integrations): support UniFi controller default ports - #6210

Open
ajnart wants to merge 4 commits into
devfrom
unifi-ssl-error
Open

fix(integrations): support UniFi controller default ports#6210
ajnart wants to merge 4 commits into
devfrom
unifi-ssl-error

Conversation

@ajnart

@ajnart ajnart commented Jul 6, 2026

Copy link
Copy Markdown
Member

Summary

Fixes the misleading TLS error produced when a UniFi integration URL omits its port.

node-unifi always connects over HTTPS. UniFi OS consoles such as UDR and UDM normally use port 443, while self-hosted UniFi Network controllers normally use 8443. Homarr now:

  • preserves an explicitly supplied port
  • tries port 443 first when no port is supplied
  • falls back to 8443 only after a connection or controller-detection failure
  • does not hide authentication failures by retrying another port

The UniFi integration documentation now explains the expected URLs and local-account requirements.

Closes #6207

Test plan

  • vitest run packages/integrations/src/unifi-controller/test/unifi-controller-integration.spec.ts --coverage.enabled=false
  • tsc --noEmit -p packages/integrations/tsconfig.json
  • oxfmt --check on the three changed files
  • oxlint on the changed TypeScript files

Six unit tests cover UniFi OS port 443, fallback to 8443, explicit standard/custom ports, and preventing fallback for authentication errors.

Summary by CodeRabbit

  • Bug Fixes
    • Improved UniFi Controller connection port selection: explicit ports in the URL are honored, and secure default ports are tried when none is provided.
    • Added connection retry logic for retryable failures, with fallback to the alternate secure port when appropriate.
    • Ensured authentication failures do not trigger additional port retries.
  • Tests
    • Added integration test coverage for URL port resolution and retry behavior during network summary retrieval.
  • Documentation
    • Refreshed UniFi Controller integration docs formatting.
    • Updated documentation route listings to include new integration/widget pages.

The node-unifi library hardcodes https:// in its base URL and the
Unifi controller API only speaks HTTPS. When a user adds the
integration with a bare http:// or https:// URL, getPortFromUrl
returned 80 or 443, and the library attempted a TLS handshake on
the wrong port. The server's plain HTTP response caused
'EPROTO ... wrong version number'.

Derive the port from the user's explicit port, or fall back to 8443
(the integration's defaultPort). Closes #6207.
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 63be1131-e50f-49fd-9654-9baac26fd6c5

📥 Commits

Reviewing files that changed from the base of the PR and between e3cf680 and 6c3b584.

📒 Files selected for processing (1)
  • packages/definitions/src/docs/homarr-docs-sitemap.ts

📝 Walkthrough

Walkthrough

Changes

The UniFi integration now honors explicit URL ports, retries unspecified-port connections on 443 and 8443 for connection failures, and avoids retrying authentication failures. Tests cover these paths, while UniFi documentation formatting and sitemap route metadata are updated.

UniFi controller connection handling

Layer / File(s) Summary
Controller port selection and retry
packages/integrations/src/unifi-controller/unifi-controller-integration.ts
Uses explicit URL ports, otherwise tries 443 followed by 8443, retrying only connection-style failures.
Port and retry integration coverage
packages/integrations/src/unifi-controller/test/unifi-controller-integration.spec.ts
Adds mocked Vitest coverage for default ports, explicit ports, fallback retries, and authentication failures.
UniFi integration documentation formatting
apps/docs/docs/integrations/unifi-controller/index.mdx
Normalizes MDX formatting without changing documented values or credential fields.
Documentation route metadata
packages/definitions/src/docs/homarr-docs-sitemap.ts
Removes category routes and adds integration and widget route literals to HomarrDocumentationPath.

Sequence Diagram(s)

sequenceDiagram
  participant Integration as UnifiControllerIntegration
  participant Controller as UniFi Controller
  participant Classifier as Fallback error classifier
  Integration->>Controller: Connect on port 443
  Controller-->>Integration: Connection failure
  Integration->>Classifier: Classify error
  Classifier-->>Integration: Retryable
  Integration->>Controller: Connect on port 8443
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The sitemap type adds and removes several unrelated docs/widget routes that are not part of the UniFi fix. Remove unrelated sitemap route changes and keep the PR focused on the UniFi integration fix and its docs.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: UniFi controller support for default ports.
Linked Issues check ✅ Passed The changes address #6207 by trying the correct HTTPS ports, preserving explicit ports, and stopping retries on auth failures.
Yagni / Over-Engineering ✅ Passed PASS: The change is minimal—one retry loop, one small helper, existing URL parsing, and focused tests/docs; no extra abstraction or dependency.
Docs Are Up To Date ✅ Passed apps/docs/unifi-controller docs now describe 443-first, 8443 fallback, and explicit-port handling matching the integration change.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch unifi-ssl-error

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@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.

🧹 Nitpick comments (1)
packages/integrations/src/unifi-controller/unifi-controller-integration.ts (1)

64-67: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Odd comment wording — "ponytail:" looks like a stray artifact.

This prefix doesn't correspond to any known convention and reads as leftover/typo text. Consider cleaning up the comment.

✏️ Suggested cleanup
-    // ponytail: node-unifi hardcodes https:// in its base URL and never serves HTTP.
+    // node-unifi hardcodes https:// in its base URL and never serves HTTP.
     // Respect the user's explicit port; fall back to 8443 (the integration's defaultPort)
     // so a bare "http://192.168.1.1" maps to https://192.168.1.1:8443 instead of 80.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/integrations/src/unifi-controller/unifi-controller-integration.ts`
around lines 64 - 67, The comment near the port selection in
unifi-controller-integration is carrying a stray “ponytail:” prefix that looks
like leftover text. Clean up the inline comment around the port fallback logic
in the integration’s URL handling so it reads naturally and only explains the
node-unifi HTTPS/defaultPort behavior, without the artifact.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/integrations/src/unifi-controller/unifi-controller-integration.ts`:
- Around line 64-67: The comment near the port selection in
unifi-controller-integration is carrying a stray “ponytail:” prefix that looks
like leftover text. Clean up the inline comment around the port fallback logic
in the integration’s URL handling so it reads naturally and only explains the
node-unifi HTTPS/defaultPort behavior, without the artifact.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ad8a51ee-dda2-499d-a4ac-b1cd34910efd

📥 Commits

Reviewing files that changed from the base of the PR and between 5b91687 and 24989dd.

📒 Files selected for processing (2)
  • packages/integrations/src/unifi-controller/test/unifi-controller-integration.spec.ts
  • packages/integrations/src/unifi-controller/unifi-controller-integration.ts

@ajnart ajnart changed the title fix(integrations): use Unifi default port 8443 when no port is given fix(integrations): support UniFi controller default ports Jul 21, 2026
@github-actions
github-actions Bot requested a review from a team as a code owner July 21, 2026 08:08

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/docs/docs/integrations/unifi-controller/index.mdx`:
- Around line 18-19: Correct the typo in the UniFi integration documentation
paragraph by changing “Therfore” to “Therefore”; leave the surrounding content
unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: fe419332-082e-4fb5-98b2-303cd06a324c

📥 Commits

Reviewing files that changed from the base of the PR and between 24989dd and e3cf680.

📒 Files selected for processing (3)
  • apps/docs/docs/integrations/unifi-controller/index.mdx
  • packages/integrations/src/unifi-controller/test/unifi-controller-integration.spec.ts
  • packages/integrations/src/unifi-controller/unifi-controller-integration.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/integrations/src/unifi-controller/unifi-controller-integration.ts

Comment on lines 18 to 19
This integration is using the [node-unifi](https://www.npmjs.com/package/node-unifi) library under the hood to connect to the Unifi Controller.
Therfore it should support CloudKey Gen1, CloudKey Gen2, UnifiOS-based UDM-Pro controllers as well as self-hosted UniFi controllers.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Fix typographical error.

There is a minor typo in the documentation: "Therfore" should be "Therefore".

📝 Proposed fix
 This integration is using the [node-unifi](https://www.npmjs.com/package/node-unifi) library under the hood to connect to the Unifi Controller.
-Therfore it should support CloudKey Gen1, CloudKey Gen2, UnifiOS-based UDM-Pro controllers as well as self-hosted UniFi controllers.
+Therefore it should support CloudKey Gen1, CloudKey Gen2, UnifiOS-based UDM-Pro controllers as well as self-hosted UniFi controllers.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
This integration is using the [node-unifi](https://www.npmjs.com/package/node-unifi) library under the hood to connect to the Unifi Controller.
Therfore it should support CloudKey Gen1, CloudKey Gen2, UnifiOS-based UDM-Pro controllers as well as self-hosted UniFi controllers.
This integration is using the [node-unifi](https://www.npmjs.com/package/node-unifi) library under the hood to connect to the Unifi Controller.
Therefore it should support CloudKey Gen1, CloudKey Gen2, UnifiOS-based UDM-Pro controllers as well as self-hosted UniFi controllers.
🧰 Tools
🪛 LanguageTool

[grammar] ~18-~18: Ensure spelling is correct
Context: ...ood to connect to the Unifi Controller. Therfore it should support CloudKey Gen1, CloudK...

(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/docs/docs/integrations/unifi-controller/index.mdx` around lines 18 - 19,
Correct the typo in the UniFi integration documentation paragraph by changing
“Therfore” to “Therefore”; leave the surrounding content unchanged.

Source: Linters/SAST tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Unifi integration not working

1 participant