fix(integrations): support UniFi controller default ports (#6787)
* fix(integrations): use Unifi default port 8443 when no port is given 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. * style: auto-fix lint and format * fix(integrations): support UniFi controller default ports * docs(unifi): resolve review leftovers --------- Co-authored-by: ajnart <ajnart@users.noreply.github.com>
This commit is contained in:
1 parent
134ef48848
commit
ca7be8f3ad
3 files changed
+196
-21
No files matched your search
@@ -2,7 +2,6 @@ import type tls from "node:tls";
|
||||
import axios from "axios";
|
||||
import { HttpCookieAgent, HttpsCookieAgent } from "http-cookie-agent/http";
|
||||
|
||||
import { getPortFromUrl } from "@homarr/common";
|
||||
import {
|
||||
getAllTrustedCertificatesAsync,
|
||||
getTrustedCertificateHostnamesAsync,
|
||||
@@ -67,26 +66,53 @@ export class UnifiControllerIntegration extends Integration implements NetworkCo
|
||||
checkServerIdentity: createCustomCheckServerIdentity(await getTrustedCertificateHostnamesAsync()),
|
||||
};
|
||||
|
||||
const client = new Unifi.Controller({
|
||||
host: url.hostname,
|
||||
port: getPortFromUrl(url),
|
||||
username: this.getSecretValue("username"),
|
||||
password: this.getSecretValue("password"),
|
||||
createAxiosInstance({ cookies }) {
|
||||
return axios.create({
|
||||
adapter: "http",
|
||||
httpAgent: new HttpCookieAgent({ cookies }),
|
||||
httpsAgent: new HttpsCookieAgent({
|
||||
cookies,
|
||||
requestCert: true,
|
||||
...certificateOptions,
|
||||
}),
|
||||
});
|
||||
},
|
||||
});
|
||||
// node-unifi always connects over HTTPS, regardless of the protocol in the
|
||||
// integration URL. UniFi OS consoles use port 443 while self-hosted controllers normally
|
||||
// use 8443. If no port was provided, try both without hiding authentication errors.
|
||||
const ports = url.port ? [Number(url.port)] : [443, 8443];
|
||||
const createClientForPortAsync = async (port: number) => {
|
||||
const client = new Unifi.Controller({
|
||||
host: url.hostname,
|
||||
port,
|
||||
username: this.getSecretValue("username"),
|
||||
password: this.getSecretValue("password"),
|
||||
createAxiosInstance({ cookies }) {
|
||||
return axios.create({
|
||||
adapter: "http",
|
||||
httpAgent: new HttpCookieAgent({ cookies }),
|
||||
httpsAgent: new HttpsCookieAgent({
|
||||
cookies,
|
||||
requestCert: true,
|
||||
...certificateOptions,
|
||||
}),
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
await client.login(this.getSecretValue("username"), this.getSecretValue("password"), null);
|
||||
return client;
|
||||
await client.login(this.getSecretValue("username"), this.getSecretValue("password"), null);
|
||||
return client;
|
||||
};
|
||||
|
||||
for (const [index, port] of ports.entries()) {
|
||||
try {
|
||||
return await createClientForPortAsync(port);
|
||||
} catch (error) {
|
||||
const isLastPort = index === ports.length - 1;
|
||||
if (isLastPort || !this.isPortFallbackError(error)) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error("Unable to connect to the UniFi controller");
|
||||
}
|
||||
|
||||
private isPortFallbackError(error: unknown) {
|
||||
if (typeof error === "object" && error !== null && "isAxiosError" in error && error.isAxiosError === true) {
|
||||
return !("response" in error) || error.response === undefined;
|
||||
}
|
||||
|
||||
return error instanceof Error && error.message === "failed to detect UniFiOS status";
|
||||
}
|
||||
|
||||
private getStatusValueOverAllSites<S extends HealthSubsystem>(
|
||||
|
||||
Reference in new issue
Block a user