Skip to content

Commit

Permalink
fix ha-proxy.ja to be compatible to latest http-proxy-middleware v3
Browse files Browse the repository at this point in the history
version which comes with a slightly changed API and thus broke ha-proxy
startup in latest versions. In addition, the ha-proxy init script should
now correctly report successful startup of ha-proxy. This fixes #2720.
  • Loading branch information
jens-maus committed Apr 20, 2024
1 parent b07850d commit 8386591
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 45 deletions.
93 changes: 49 additions & 44 deletions buildroot-external/overlay/base-raspmatic_oci/bin/ha-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,72 @@
// Home-Assistent UI so that the Ingress-based HA UI is able to embed
// the WebUI.
//
// Copyright (c) 2021-2023 Jens Maus <[email protected]>
//
// Copyright (c) 2021-2024 Jens Maus <[email protected]>
// Apache 2.0 License applies
//
// v1.0: initial version
// v1.1: adapted to http-proxy-middleware v3
//

const express = require('express');
const { createProxyMiddleware, responseInterceptor } = require('http-proxy-middleware');
const ipaddr = require('ipaddr.js');

const apiProxy = createProxyMiddleware('/', {
const apiProxy = createProxyMiddleware({
target: 'http://127.0.0.1:80',
changeOrigin: true, // for vhosted sites,
//logLevel: 'debug',
pathFilter: '/',
changeOrigin: true, // for vhosted sites
//logger: console,
selfHandleResponse: true,
timeout: 1200000, // max 20 min
onProxyRes: responseInterceptor(async (responseBody, proxyRes, req, res) => {
// modify Location: response header if present
if(typeof(proxyRes.headers.location) !== 'undefined') {
// replace any absolute http/https path with a relative one
var redirect = proxyRes.headers.location.replace(/(http|https):\/\/(.*?)\//, '/');
redirect = req.headers['x-ingress-path'] + redirect;
res.setHeader('location', redirect);
}
on: {
proxyRes: responseInterceptor(async (responseBody, proxyRes, req, res) => {
// modify Location: response header if present
if(typeof(proxyRes.headers.location) !== 'undefined') {
// replace any absolute http/https path with a relative one
var redirect = proxyRes.headers.location.replace(/(http|https):\/\/(.*?)\//, '/');
redirect = req.headers['x-ingress-path'] + redirect;
res.setHeader('location', redirect);
}

// modifying textual response bodies
if(proxyRes.headers['content-type'] &&
(
proxyRes.headers['content-type'].includes('text/') ||
proxyRes.headers['content-type'].includes('application/javascript') ||
proxyRes.headers['content-type'].includes('application/json')
)
) {
// modifying textual response bodies
if(proxyRes.headers['content-type'] &&
(
proxyRes.headers['content-type'].includes('text/') ||
proxyRes.headers['content-type'].includes('application/javascript') ||
proxyRes.headers['content-type'].includes('application/json')
)
) {

var body;
var body;

// if this a textual response body we make sure to prepend the ingress path
if(proxyRes.headers['content-type'].toLowerCase().includes('utf-8') || proxyRes.req.path.includes('/jpages/')) {
body = responseBody.toString('utf8');
} else {
body = responseBody.toString('latin1');
}
// if this a textual response body we make sure to prepend the ingress path
if(proxyRes.headers['content-type'].toLowerCase().includes('utf-8') || proxyRes.req.path.includes('/jpages/')) {
body = responseBody.toString('utf8');
} else {
body = responseBody.toString('latin1');
}

body = body.replace(/(?<=["'= \(\\]|\\u0027)\/(api|webui|ise|pda|config|pages|jpages|esp|upnp|tools|addons|tailscale)(\\?\/)(?!hassio_ingress)/g,
req.headers['x-ingress-path']+'/$1$2');
body = body.replace(/(?<=["'])\/(index|login|logout)\.htm/g,
req.headers['x-ingress-path']+'/$1.htm');
body = body.replace(/window\.location\.href='\/'/g,
'window.location.href=\'' + req.headers['x-ingress-path'] + '/\'');
body = body.replace(/window\.location\.href='\/index\.htm'/g,
'window.location.href=\'' + req.headers['x-ingress-path'] + '/index.htm\'');
body = body.replace(/(?<=["'= \(\\]|\\u0027)\/(api|webui|ise|pda|config|pages|jpages|esp|upnp|tools|addons|tailscale)(\\?\/)(?!hassio_ingress)/g,
req.headers['x-ingress-path']+'/$1$2');
body = body.replace(/(?<=["'])\/(index|login|logout)\.htm/g,
req.headers['x-ingress-path']+'/$1.htm');
body = body.replace(/window\.location\.href='\/'/g,
'window.location.href=\'' + req.headers['x-ingress-path'] + '/\'');
body = body.replace(/window\.location\.href='\/index\.htm'/g,
'window.location.href=\'' + req.headers['x-ingress-path'] + '/index.htm\'');

// convert back to a Buffer in the right character encoding
if(typeof(req.headers['content-type']) === 'undefined' && req.path.includes('/jpages/') === false) {
return new Buffer.from(body, 'latin1');
// convert back to a Buffer in the right character encoding
if(typeof(req.headers['content-type']) === 'undefined' && req.path.includes('/jpages/') === false) {
return new Buffer.from(body, 'latin1');
} else {
return new Buffer.from(body, 'utf8');
}
} else {
return new Buffer.from(body, 'utf8');
return responseBody;
}
} else {
return responseBody;
}
}),
}),
},
});

const app = express();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ start() {
export HM_HAPROXY_SRC
export NODE_PATH=/usr/lib/node_modules
if start-stop-daemon -S -q -m -b -p /var/run/ha-proxy.pid --exec /bin/ha-proxy.js; then
echo "OK"
sleep 1
# check if ha-proxy could be started
if start-stop-daemon -K -q -t -p /var/run/ha-proxy.pid 2>/dev/null; then
echo "OK"
else
echo "FAIL"
fi
else
echo "FAIL"
fi
Expand All @@ -30,6 +36,7 @@ stop() {
else
echo "FAIL"
fi
rm -f /var/run/ha-proxy.pid
}

restart() {
Expand Down

0 comments on commit 8386591

Please sign in to comment.