use API token instead of password hash in pihole module

* with newer versions of pi-hole, it is not possible
  anymore to use the password hash for the API authentication.
  Instead, one needs to use the dedicated API token for that.
  In order to stay backwards compatible, and not break existing
  bumblebee_status setups, the 'pwhash' parameter is still supported
  (in case someone runs an outdated pi-hole version).

* the new pi-hole API endpoints do not allow to access the summary
  endpoint without an API token. So, therefore '&auth=<api token>' was
  added.
This commit is contained in:
Bernhard B 2023-05-28 17:03:58 +02:00
parent d446a44f06
commit b3007dd042

View file

@ -4,13 +4,20 @@
Parameters: Parameters:
* pihole.address : pi-hole address (e.q: http://192.168.1.3) * pihole.address : pi-hole address (e.q: http://192.168.1.3)
* pihole.pwhash : pi-hole webinterface password hash (can be obtained from the /etc/pihole/SetupVars.conf file)
* pihole.apitoken : pi-hole API token (can be obtained in the pi-hole webinterface (Settings -> API)
OR (deprecated!)
* pihole.pwhash : pi-hole webinterface password hash (can be obtained from the /etc/pihole/SetupVars.conf file)
contributed by `bbernhard <https://github.com/bbernhard>`_ - many thanks! contributed by `bbernhard <https://github.com/bbernhard>`_ - many thanks!
""" """
import requests import requests
import logging
import core.module import core.module
import core.widget import core.widget
import core.input import core.input
@ -22,7 +29,18 @@ class Module(core.module.Module):
super().__init__(config, theme, core.widget.Widget(self.pihole_status)) super().__init__(config, theme, core.widget.Widget(self.pihole_status))
self._pihole_address = self.parameter("address", "") self._pihole_address = self.parameter("address", "")
self._pihole_pw_hash = self.parameter("pwhash", "") pihole_pw_hash = self.parameter("pwhash", "")
pihole_api_token = self.parameter("apitoken", "")
self._pihole_secret = (
pihole_api_token if pihole_api_token != "" else pihole_pw_hash
)
if pihole_pw_hash != "":
logging.warn(
"pihole: The 'pwhash' parameter is deprecated - consider using the 'apitoken' parameter instead!"
)
self._pihole_status = None self._pihole_status = None
self._ads_blocked_today = "-" self._ads_blocked_today = "-"
self.update_pihole_status() self.update_pihole_status()
@ -42,7 +60,11 @@ class Module(core.module.Module):
def update_pihole_status(self): def update_pihole_status(self):
try: try:
data = requests.get(self._pihole_address + "/admin/api.php?summary").json() data = requests.get(
self._pihole_address
+ "/admin/api.php?summary&auth="
+ self._pihole_secret
).json()
self._pihole_status = True if data["status"] == "enabled" else False self._pihole_status = True if data["status"] == "enabled" else False
self._ads_blocked_today = data["ads_blocked_today"] self._ads_blocked_today = data["ads_blocked_today"]
except Exception as e: except Exception as e:
@ -56,13 +78,13 @@ class Module(core.module.Module):
req = requests.get( req = requests.get(
self._pihole_address self._pihole_address
+ "/admin/api.php?disable&auth=" + "/admin/api.php?disable&auth="
+ self._pihole_pw_hash + self._pihole_secret
) )
else: else:
req = requests.get( req = requests.get(
self._pihole_address self._pihole_address
+ "/admin/api.php?enable&auth=" + "/admin/api.php?enable&auth="
+ self._pihole_pw_hash + self._pihole_secret
) )
if req is not None: if req is not None:
if req.status_code == 200: if req.status_code == 200: