Build proxy for openHAB #2138
-
Hi 👋🏻 I'm trying to build a reverse proxy using YARP so that I can access openHAB via a configurable URL path segment. For example:
This is my current YARP config: using Microsoft.Extensions.Primitives;
using Yarp.ReverseProxy.Configuration;
using Yarp.ReverseProxy.Transforms;
namespace OpenHabUrlSegment;
internal class OpenHabProxyConfigProvider : IProxyConfigProvider
{
private readonly IReadOnlyList<RouteConfig> _routes;
private readonly IReadOnlyList<ClusterConfig> _clusters;
public OpenHabProxyConfigProvider()
{
_routes = new[]
{
new RouteConfig { RouteId = "route1", ClusterId = "cluster1", Match = new RouteMatch { Path = "openhab/{**catch-all}" } }
.WithTransformPathRemovePrefix("/openhab")
};
_clusters = new[]
{
new ClusterConfig
{
ClusterId = "cluster1",
Destinations = new Dictionary<string, DestinationConfig> { { "destination1", new DestinationConfig { Address = "http://127.0.0.1:9000/" } } }
}
};
}
public IProxyConfig GetConfig() => new OpenHabProxyConfig(_routes, _clusters);
private class OpenHabProxyConfig : IProxyConfig
{
private readonly CancellationTokenSource _cts = new();
public OpenHabProxyConfig(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
{
Routes = routes;
Clusters = clusters;
ChangeToken = new CancellationChangeToken(_cts.Token);
}
public IReadOnlyList<RouteConfig> Routes { get; }
public IReadOnlyList<ClusterConfig> Clusters { get; }
public IChangeToken ChangeToken { get; }
}
} Now the following happens:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy"
content="default-src * 'self' 'unsafe-inline' 'unsafe-eval' data: gap: content: blob:; style-src 'self' 'unsafe-inline';">
<meta name="viewport"
content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,minimal-ui,viewport-fit=cover">
<meta name="theme-color" content="#e64a19">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<title>openHAB</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="apple-touch-icon" href="/res/icons/apple-touch-icon.png" type="image/png" sizes="180x180"
crossorigin="use-credentials">
<link rel="icon" href="/res/icons/favicon.svg" type="image/svg+xml" sizes="any" crossorigin="use-credentials">
<link rel="icon" href="/res/icons/128x128.png" type="image/png" sizes="128x128" crossorigin="use-credentials">
<link rel="manifest" href="/manifest.json" crossorigin="use-credentials">
<link href="/css/app.d702c20a4e09bf4012ea.css" rel="stylesheet">
</head>
<body>
<div id="app"></div>
<script src="/js/app.d702c20a4e09bf4012ea.js"></script>
</body>
</html> Now the browser tries to load http://localhost:8080/js/app.d702c20a4e09bf4012ea.js which obviously fails with So my question is: what do I have to configure in YARP so that the correct URL including the URL path segment is used/added for all subsequent requests? Thank you 👋🏻 PS: I've found this GitHub issue where an openHAB user was able to accomplish this with Apache - however, I'm struggling to "translate" this into YARP's config. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 13 replies
-
Doing a re-write based on the referrer is clever. This would have to happen in middleware before routing since routing is expecting that path prefix. Have you tried using the rewrite middleware for that? |
Beta Was this translation helpful? Give feedback.
It seems like OpenHab is not well setup to be proxied. At this point it may be more efficient to submit changes there so that it honors the x-forward headers when its generating URL links. The OpenHab community are pretty open and friendly.
I haven't looked at how the configuration UI site is written, is it in Java like the bindings and runtime infrastructure? My house runs on OpenHab - I have an hdmi-cec binding I wrote to enable control of home theatre devices that don't have their own bindings.