Solved: Nginx, cgi, embed page using luci template

Hi;

I am attempting to embed home-assistant in the openwrt menu system. I can get it working with an iframe, but ran into cors / cross site / http in https issues. To avoid browser security issues and future incompatibilities, I am attempting to load pages from home-assistant (which renders http) , then echo them (as https).

If nginx had a php plugin, would be as simple as:

<?php $page = file_get_contents('http://<%=callerIP%>:8123'); echo $page; ?>

but, it does not, so, I am attempting to use lua in my template (to embed yahoo.com as a simple testcase):

/usr/lib/lua/luci/view/home-assistant_tab.htm:

<%+header%>

<% local http = require("socket.http") %> <% local body, code = http.request("http://yahoo.com") %> <% if not body then error("code") end %> <% print('body',body) %>
<%+footer%>

Result: XML Parsing Error: not well-formed

Questions:
Am I on the right track here?
What is wrong with above template?
Is there a better way to accomplish this?

Thanks;
Bill

Run nginx as a reverse proxy -- typically it's the proxy_pass directive.

If you want your upstream servers' logs to log the client, not your proxy, you probably also want something along the lines of

proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header        X-Forwarded-Proto $scheme;

Thanks, but I don't want to replace luci openwrt site with forwarded url, I want to embed the url page in luci menu system. Like this: http://www.wellho.net/resources/ex.php?item=u116/autobrowser

this is slowly driving me nuts.

Works, from lua command line:

  http = require("socket.http")
  ltn12 = require("ltn12")
  body, code = http.request("http://yahoo.com")
  print(body)

Not Work (malformed xml) in view template:

<%+header%>
<div style="align: left; width: 85%; height: 100%;">
<%
  http = require("socket.http")
  ltn12 = require("ltn12")
  body, code = http.request("http://yahoo.com")
  print(body)
%>
</div>
<%+footer%>

anyone?

page source, with and without 'ltn12 = require("ltn12")'

A runtime error occured: /usr/lib/lua/socket/http.lua:337: attempt to index upvalue 'ltn12' (a boolean value)

turns out I was way on the wrong track (attempting to proxy http to https, running into cors issues)

The goal was to embed home-assistant into openwrt menu system. Do this:

  • configure home-assistant for https, valid certs....
  • embed it (/usr/lib/lua/luci/view/home-assistant_tab.htm):
<%+header%>
<script type="text/javascript">
  function iframeLoaded() {
      var iFrameID = document.getElementById('idHomeFrame');
      if(iFrameID) {
            // here you can make the height, I delete it first, then I make it again
            iFrameID.height = "";
            iFrameID.height = iFrameID.contentWindow.document.body.scrollHeight + "px";
      }
  }
</script>
<div style="align: left; width: 85%; height: 100%;">
  <%local caller = luci.http.getenv("HTTP_REFERER")%>
  <%local callerIP = string.match( caller, '%d+.%d+.%d+.%d+')%>
  <iframe id="idHomeFrame" onload="iframeLoaded()" style="position: absolute; width: 85%; height: 100%; border: none;" src="https://<%=callerIP%>:8123"></iframe>
</div>
<%+footer%>

Regards;
Bill

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.