Skip to content

Commit

Permalink
move obfuscateUrl to a util function
Browse files Browse the repository at this point in the history
  • Loading branch information
MindFreeze committed Oct 28, 2024
1 parent 07f9b3c commit af7b00e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/panels/config/cloud/account/cloud-remote-pref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import type { HomeAssistant } from "../../../../types";
import { showToast } from "../../../../util/toast";
import { showCloudCertificateDialog } from "../dialog-cloud-certificate/show-dialog-cloud-certificate";
import { obfuscateUrl } from "../../../../util/url";

@customElement("cloud-remote-pref")
export class CloudRemotePref extends LitElement {
Expand Down Expand Up @@ -142,7 +143,7 @@ export class CloudRemotePref extends LitElement {
<ha-textfield
.value=${this._unmaskedUrl
? `https://${remote_domain}`
: "https://•••••••••••••••••.ui.nabu.casa"}
: obfuscateUrl(`https://${remote_domain}`)}
readonly
.suffix=${
// reserve some space for the icon.
Expand Down
12 changes: 3 additions & 9 deletions src/panels/config/network/ha-config-url-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type { ValueChangedEvent, HomeAssistant } from "../../../types";
import { copyToClipboard } from "../../../common/util/copy-clipboard";
import { showToast } from "../../../util/toast";
import type { HaSwitch } from "../../../components/ha-switch";
import { obfuscateUrl } from "../../../util/url";

@customElement("ha-config-url-form")
class ConfigUrlForm extends LitElement {
Expand Down Expand Up @@ -141,7 +142,7 @@ class ConfigUrlForm extends LitElement {
.value=${this._unmaskedExternalUrl ||
(this._showCustomExternalUrl && canEdit)
? externalUrl
: this._obfuscateUrl(externalUrl)}
: obfuscateUrl(externalUrl)}
@change=${this._handleChange}
.disabled=${disabled || !this._showCustomExternalUrl}
.suffix=${
Expand Down Expand Up @@ -240,7 +241,7 @@ class ConfigUrlForm extends LitElement {
.value=${this._unmaskedInternalUrl ||
(this._showCustomInternalUrl && canEdit)
? internalUrl
: this._obfuscateUrl(internalUrl)}
: obfuscateUrl(internalUrl)}
@change=${this._handleChange}
.disabled=${disabled || !this._showCustomInternalUrl}
.suffix=${
Expand Down Expand Up @@ -336,13 +337,6 @@ class ConfigUrlForm extends LitElement {
this._unmaskedExternalUrl = !this._unmaskedExternalUrl;
}

private _obfuscateUrl(url: string) {
// hide any words that look like they might be a hostname or IP address
return url.replace(/(?<=:\/\/)[\w-]+|(?<=\.)[\w-]+/g, (match) =>
"•".repeat(match.length)
);
}

private async _copyURL(ev) {
const url = ev.currentTarget.url;
await copyToClipboard(url);
Expand Down
9 changes: 9 additions & 0 deletions src/util/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function obfuscateUrl(url: string) {
if (url.includes(".ui.nabu.casa")) {
return "https://•••••••••••••••••.ui.nabu.casa";
}
// hide any words that look like they might be a hostname or IP address
return url.replace(/(?<=:\/\/)[\w-]+|(?<=\.)[\w-]+/g, (match) =>
"•".repeat(match.length)
);
}

0 comments on commit af7b00e

Please sign in to comment.