-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix ha-proxy.ja to be compatible to latest http-proxy-middleware v3
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
Showing
2 changed files
with
57 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters