bastienwirtz / homer

A very simple static homepage for your server.
https://homer-demo.netlify.app/
Apache License 2.0
9.25k stars 782 forks source link

Error reading PiHole stats #317

Closed AlexFullmoon closed 2 years ago

AlexFullmoon commented 3 years ago

Pihole at https://dns.mydomain.tld/api.php returns valid-looking json:

{"domains_being_blocked":999385,"dns_queries_today":3187,"ads_blocked_today":66,"ads_percentage_today":2.070913,"unique_domains":779,"queries_forwarded":2418,"queries_cached":683,"clients_ever_seen":11,"unique_clients":11,"dns_queries_all_types":3187,"reply_NODATA":50,"reply_NXDOMAIN":13,"reply_CNAME":27,"reply_IP":42,"privacy_level":0,"status":"enabled","gravity_last_updated":{"file_exists":true,"absolute":1634201879,"relative":{"days":0,"hours":0,"minutes":39}}}

Yet homer/PiHole.vue fails with SyntaxError: Unexpected token F in JSON at position 0 PiHole.vue:71

sargonas commented 3 years ago

If it helps you any, I just looked at my own, and it seems to be working fine, and from looking through the API JSON results, the format and data points haven't changed in a long time so in theory should still be parsing fine by default.

AlexFullmoon commented 3 years ago

Ugh. As I suspect, its again something something CORS something misconfiguration headers.

Any advice on how to debug that?

sargonas commented 3 years ago

CORS issues are my bane as well debugging and I am afraid my knowledge on working around them is limited. I wonder though, are you using a reverse proxy internally for your hostnames? if so it may perhaps work better if you call the PiHole by it's IP address directly.

AlexFullmoon commented 3 years ago

I wonder though, are you using a reverse proxy internally for your hostnames?

Sure I do, nginx (with wather limited configuration options). Calling through http://IP would cause mixed content error.

sargonas commented 3 years ago

I could easily have my web tools mixed up, but im pretty sure you can call http://IP:port i think, if that helps.

mcclurec commented 2 years ago

Likely related to this PR cc @bastienwirtz we should finalize on a fetch mixin pattern and get it merged

ellisdickinson46 commented 2 years ago

For anyone needing to diagnose this CORS issue, this thread may solve part of the problem temporarily: https://discourse.pi-hole.net/t/add-domain-to-authorized-hostnames-permanently/32007/2.

To summarise, you can modify, the $AUTHORISED_HOSTNAMES declaration in the auth.php file of pihole found at /path-to-pihole/admin/scripts/pi-hole/php/auth.php to include the domain or IP of your homer instance. Do note this will likely need updating every time you update Pi-hole. Additionally because of the use of credentials when fetching data, you will also need to set the Access-Control-Allow-Credentials to true, this header should be set on your proxy.

As for the SyntaxError you will see, after some poking, it is the beginning of an error from Pi-hole that reads: "Failed CORS: [browser_origin] vs [authorised_hostnames]", this is likely caused by CORS headers being set on your proxy instead of on your Pi-hole instance itself. Changing it to be on Pi-hole fixed this problem for me.

Edit: Fixed Typos

bastienwirtz commented 2 years ago

Hi there !

CORS related issues are now documented here (feel free to add information if needed) and I finished to add the support for the useCredential option. It should help!

I'm closing this issue as every info are available to solve the PiHole CORS issue. Feel free to reopen if needed!