This commit is contained in:
Striker72rus 2026-03-25 15:57:53 +03:00
parent eb1ae161ff
commit f664f325af
7 changed files with 177 additions and 91 deletions

View File

@ -7,7 +7,7 @@ from homeassistant.core import HomeAssistant
from .const import CONF_CONFIG, DOMAIN
from .frontend import async_setup_frontend
from .helpers import current_entry_config
from .views import WallPanelConfigView, WallPanelPanelView
from .views import WallPanelConfigView, WallPanelPanelView, WallPanelProxyView
async def async_setup_entry(hass: HomeAssistant, entry) -> bool:
@ -30,6 +30,10 @@ async def async_setup_entry(hass: HomeAssistant, entry) -> bool:
hass.http.register_view(WallPanelPanelView)
state["_panel_view_registered"] = True
if not state.get("_proxy_view_registered"):
hass.http.register_view(WallPanelProxyView)
state["_proxy_view_registered"] = True
entry.async_on_unload(entry.add_update_listener(_async_options_updated))
return True

View File

@ -74,41 +74,20 @@ class WallPanelPanel extends HTMLElement {
return `/api/wall_panel/config/${encodeURIComponent(entryId)}`;
}
async _fetchPanelUrl() {
_proxyUrl() {
const payload = this._panelConfig();
const syncToken = String(payload.sync_token || '').trim();
if (!syncToken) {
const entryId = String(payload.entry_id || '').trim();
if (!entryId) {
return '';
}
const configUrls = [this._configUrl(), this._legacyConfigUrl()].filter(Boolean);
for (const configUrl of configUrls) {
try {
const response = await fetch(configUrl, {
method: 'GET',
headers: {
'X-Wall-Panel-Token': syncToken,
Accept: 'application/json',
},
credentials: 'same-origin',
cache: 'no-store',
});
if (!response.ok) {
continue;
return `/api/wall_panel/proxy/${encodeURIComponent(entryId)}/`;
}
const data = await response.json();
const panelUrl = String(data?.panel?.panel_url || data?.panel_url || '').trim();
if (panelUrl) {
return panelUrl;
}
} catch (error) {
continue;
}
}
return '';
_hasPanelUrl() {
const payload = this._panelConfig();
const panelUrl = String(payload.panel_url || payload.ingress_url || '').trim();
return panelUrl !== '';
}
_renderMessage(title, body, extra = '') {
@ -197,33 +176,23 @@ class WallPanelPanel extends HTMLElement {
async _tryAttachPanel() {
const payload = this._panelConfig();
const initialUrl = this._resolveUrl(
payload.panel_url || payload.ingress_url || ''
);
if (initialUrl && initialUrl === this._activePanelUrl) {
const proxyUrl = this._proxyUrl();
if (proxyUrl && proxyUrl === this._activePanelUrl) {
return;
}
if (initialUrl && !(window.location.protocol === 'https:' && initialUrl.startsWith('http://'))) {
this._renderIframe(initialUrl);
return;
}
const panelUrl = this._resolveUrl(await this._fetchPanelUrl());
if (panelUrl && panelUrl === this._activePanelUrl) {
return;
}
if (panelUrl && !(window.location.protocol === 'https:' && panelUrl.startsWith('http://'))) {
this._renderIframe(panelUrl);
if (proxyUrl && this._hasPanelUrl()) {
this._renderIframe(proxyUrl);
return;
}
const panelUrl = this._resolveUrl(payload.panel_url || payload.ingress_url || '');
const configUrl = this._configUrl();
this._renderMessage(
'Waiting for Wall Panel',
'The add-on will publish a secure HTTPS ingress URL here.',
panelUrl
? 'Open this panel through Home Assistant after the add-on URL is configured.'
: 'Set the PHP panel URL in the integration options.',
configUrl ? `<code>${configUrl}</code>` : ''
);
}

View File

@ -3,11 +3,14 @@
from __future__ import annotations
import secrets
from urllib.parse import urljoin
from typing import Any
from aiohttp import web
from homeassistant.components.http import HomeAssistantView
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from yarl import URL
from .const import CONF_CONFIG, CONF_PANEL_URL, CONF_SYNC_TOKEN, DOMAIN
from .helpers import (
@ -88,6 +91,34 @@ def _save_entry_panel_url(hass: HomeAssistant, entry, panel_url: str) -> None:
hass.data.setdefault(DOMAIN, {}).setdefault(entry.entry_id, {})["panel_url"] = panel_url
def _proxy_root(entry_id: str) -> str:
return f"/api/wall_panel/proxy/{entry_id}/"
def _forward_headers(request: web.Request) -> dict[str, str]:
headers: dict[str, str] = {}
for key in ("Accept", "Content-Type", "Range", "If-Modified-Since", "If-None-Match", "Origin", "Referer", "User-Agent"):
value = request.headers.get(key)
if value:
headers[key] = value
return headers
def _rewrite_location(location: str, base_url: str, proxy_root: str) -> str:
location = location.strip()
if not location:
return location
if location.startswith(base_url):
suffix = location[len(base_url):].lstrip("/")
return proxy_root + suffix
if location.startswith("/"):
return proxy_root + location.lstrip("/")
return location
class WallPanelConfigView(HomeAssistantView):
"""Serve and update the canonical Wall Panel config."""
@ -248,3 +279,81 @@ class WallPanelPanelView(HomeAssistantView):
"ok": True,
"panel": current_entry_panel(entry),
})
class WallPanelProxyView(HomeAssistantView):
"""Reverse proxy the PHP panel through Home Assistant."""
url = "/api/wall_panel/proxy/{entry_id}/{path:.*}"
name = "api:wall_panel:proxy"
requires_auth = True
async def _proxy(self, request: web.Request, entry_id: str, path: str, method: str) -> web.StreamResponse:
hass = request.app["hass"]
entry = _entry_from_hass(hass, entry_id)
if entry is None:
return _response({"ok": False, "error": "Unknown entry"}, 404)
base_url = str(entry.options.get(CONF_PANEL_URL, "") or "").strip()
if not base_url:
return _response({"ok": False, "error": "PHP panel URL is not configured"}, 400)
if not base_url.endswith("/"):
base_url += "/"
upstream_url = urljoin(base_url, path or "")
if request.query_string:
upstream_url = upstream_url + "?" + request.query_string
body = None
if method not in {"GET", "HEAD"}:
body = await request.read()
session = async_get_clientsession(hass)
async with session.request(
method,
upstream_url,
headers=_forward_headers(request),
data=body,
allow_redirects=False,
) as upstream:
response_headers: dict[str, str] = {}
for key, value in upstream.headers.items():
lower = key.lower()
if lower in {
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailers",
"transfer-encoding",
"upgrade",
"content-encoding",
}:
continue
if lower == "location":
response_headers[key] = _rewrite_location(value, base_url, _proxy_root(entry_id))
continue
response_headers[key] = value
response_body = await upstream.read()
return web.Response(
body=response_body,
status=upstream.status,
headers=response_headers,
)
async def async_get(self, request: web.Request, entry_id: str, path: str = "") -> web.StreamResponse:
return await self._proxy(request, entry_id, path, "GET")
async def async_post(self, request: web.Request, entry_id: str, path: str = "") -> web.StreamResponse:
return await self._proxy(request, entry_id, path, "POST")
async def async_put(self, request: web.Request, entry_id: str, path: str = "") -> web.StreamResponse:
return await self._proxy(request, entry_id, path, "PUT")
async def async_patch(self, request: web.Request, entry_id: str, path: str = "") -> web.StreamResponse:
return await self._proxy(request, entry_id, path, "PATCH")
async def async_delete(self, request: web.Request, entry_id: str, path: str = "") -> web.StreamResponse:
return await self._proxy(request, entry_id, path, "DELETE")

2
run.sh
View File

@ -26,6 +26,7 @@ log "config path: ${CONFIG_PATH}"
log "storage dir: ${STORAGE_DIR}"
if [ "$RUNTIME_MODE" = "addon" ]; then
if [ "${WALL_PANEL_ENABLE_INGRESS_REGISTRATION:-false}" = "true" ]; then
(
i=0
while [ "$i" -lt 120 ]; do
@ -47,5 +48,6 @@ if [ "$RUNTIME_MODE" = "addon" ]; then
log "ingress registration failed after retries"
) || true &
fi
fi
exec php -S "0.0.0.0:${PORT}" -t "$DOCROOT"

View File

@ -1,5 +1,5 @@
{
"active": true,
"active": false,
"sensor_entity_id": "binary_sensor.doorbell_all_occupancy",
"opened_at": 1774443242,
"expires_at": null

View File

@ -1,6 +1,6 @@
name: Wall Panel
description: Wall Panel PHP interface as a Home Assistant add-on
version: "1.0.20"
version: "1.0.22"
slug: wall_panel
url: https://git.striker72rus.ru/PHP/wallpanell.git
init: false

View File

@ -24,6 +24,7 @@ log "starting add-on on port ${PORT}"
log "config path: ${CONFIG_PATH}"
log "storage dir: ${STORAGE_DIR}"
if [ "${WALL_PANEL_ENABLE_INGRESS_REGISTRATION:-false}" = "true" ]; then
(
i=0
while [ "$i" -lt 120 ]; do
@ -44,5 +45,6 @@ log "storage dir: ${STORAGE_DIR}"
done
log "ingress registration failed after retries"
) || true &
fi
exec php -S "0.0.0.0:${PORT}" -t "$DOCROOT"