Filtering!

This commit is contained in:
2024-09-23 21:42:44 -05:00
parent 05aa90a1cb
commit da65989477
2 changed files with 45 additions and 8 deletions

View File

@ -6,5 +6,8 @@
"removedead": "True/False", "removedead": "True/False",
"adddisabled": "True/False", "adddisabled": "True/False",
"piholeurl": "<Put your pi-hole url here>", "piholeurl": "<Put your pi-hole url here>",
"piholepass": "<Put your pi-hole password here>" "piholepass": "<Put your pi-hole password here>",
"filterenabled": "True.False",
"filteredpiholeurl": "<Put another pi-hole url here>",
"filterlist": "<put domains you don't want on other servers here, separated by commas. example: domain,domain2,domain3>"
} }

48
main.py
View File

@ -129,14 +129,31 @@ def getNPMHosts(apiurl,type):
log.error("Please make sure your Nginx Proxy Manager API key or URL in conf.json is correct and accurate.") log.error("Please make sure your Nginx Proxy Manager API key or URL in conf.json is correct and accurate.")
return None return None
def addPiHoleHosts(apiurl, phpassword, targetsvr, list): def addPiHoleHosts(apiurl, phpassword, targetsvr, list, filter):
url = apiurl + "/scripts/pi-hole/php/customcname.php" url = apiurl + "/scripts/pi-hole/php/customcname.php"
hostname = apiurl.split("/")[2]
piauth = loginToPihole(apiurl,phpassword) piauth = loginToPihole(apiurl,phpassword)
# Adding filter functionality
if filter is not None:
log.info("A domain filter was applied for " + hostname + ". Filtering out domains in this run.")
filterlist = filter.replace(" ","").split(",")
for domain in filterlist:
log.debug("Filtering domain " + domain)
for host in list[:]:
log.debug("Host " + host + " Filter " + domain)
if domain in host:
index = list.index(host)
log.info("Removing host " + host + " from the hostlist on " + hostname + " due to the filtered domain " + domain + " .")
list.remove(host)
else:
log.debug("The domain " + host + "does not contain the domain " + domain)
log.debug(domain + " domain was filtered, moving on to next one")
log.info("Domains were filtered. Adding the list to the server...")
for i in list: for i in list:
payload = {"action": "add", "domain": i, "target": targetsvr, "token": piauth["csrftoken"]} payload = {"action": "add", "domain": i, "target": targetsvr, "token": piauth["csrftoken"]}
apireq = requests.post(url,data=payload,cookies={"PHPSESSID": piauth["phpsessid"]}) apireq = requests.post(url,data=payload,cookies={"PHPSESSID": piauth["phpsessid"]})
if apireq.status_code == 200: # Check if the API returned a 200 and accepted our token/key. if apireq.status_code == 200: # Check if the API returned a 200 and accepted our token/key.
log.debug("Adding host "+ i + " to Pi-hole's CNAME list.") log.debug("Adding host " + i + " to " + hostname + " Pi-hole CNAME list.")
response = apireq.json() response = apireq.json()
log.debug(response) log.debug(response)
try: try:
@ -147,7 +164,7 @@ def addPiHoleHosts(apiurl, phpassword, targetsvr, list):
log.debug("Pi-Hole API returned false!") log.debug("Pi-Hole API returned false!")
log.warning("The Pi-Hole API gave the following message:"+ response['message']) log.warning("The Pi-Hole API gave the following message:"+ response['message'])
elif response['success'] == True: elif response['success'] == True:
log.info("Added " + i + " to CNAME list.") log.info("Added " + i + " to " + hostname + " CNAME list.")
log.debug("PiHole API returned true. Message returned: "+ response['message']) log.debug("PiHole API returned true. Message returned: "+ response['message'])
except: except:
log.error("Pi-hole returned this message and was not JSON: "+ response) log.error("Pi-hole returned this message and was not JSON: "+ response)
@ -157,8 +174,8 @@ def removePiHoleHosts(apiurl, phpassword, targetsvr, list):
url = apiurl + "/scripts/pi-hole/php/customcname.php" url = apiurl + "/scripts/pi-hole/php/customcname.php"
piauth = loginToPihole(apiurl,phpassword) piauth = loginToPihole(apiurl,phpassword)
for i in list: for i in list:
log.debug("Removing host "+ i + " from Pi-hole's CNAME list.") log.info("Removing host "+ i + " from Pi-hole's CNAME list.")
payload = {"action": "add", "domain": i, "target": targetsvr, "token": piauth["csrftoken"]} payload = {"action": "delete", "domain": i, "target": targetsvr, "token": piauth["csrftoken"]}
apireq = requests.post(url,data=payload,cookies={"PHPSESSID": piauth["phpsessid"]}) apireq = requests.post(url,data=payload,cookies={"PHPSESSID": piauth["phpsessid"]})
if apireq.status_code == 200: # Check if the API returned a 200 and accepted our token/key. if apireq.status_code == 200: # Check if the API returned a 200 and accepted our token/key.
response = apireq.json() response = apireq.json()
@ -190,6 +207,23 @@ else:
else: else:
allhosts = proxyhosts + redirhosts allhosts = proxyhosts + redirhosts
log.info("Adding all hosts in the list to specified Pi-Hole server at "+ cfg["piholeurl"] + "...") log.info("Adding all hosts in the list to specified Pi-Hole server at "+ cfg["piholeurl"] + "...")
addPiHoleHosts(cfg["piholeurl"],cfg["piholepass"],cfg["npmdnshostname"],allhosts) addPiHoleHosts(cfg["piholeurl"],cfg["piholepass"],cfg["npmdnshostname"],allhosts,None)
log.info("Success!") log.info("Succeeded in adding all hosts in the list to specified Pi-Hole server at "+ cfg["piholeurl"])
if cfg['filterenabled'] == "True":
log.debug("The filter server option was enabled.")
try:
if cfg["filterlist"] is not str(''):
log.info("Adding all hosts in the list to filtered Pi-Hole server at "+ cfg["filteredpiholeurl"] + "...")
if cfg["filteredpiholepass"] is not str(''):
addPiHoleHosts(cfg["filteredpiholeurl"],cfg["filteredpiholepass"],cfg["npmdnshostname"],allhosts,cfg["filterlist"])
else:
addPiHoleHosts(cfg["filteredpiholeurl"],cfg["piholepass"],cfg["npmdnshostname"],allhosts,cfg["filterlist"])
log.info("Succeeded in adding all hosts in the list to filtered Pi-Hole server at "+ cfg["filteredpiholeurl"])
else:
log.error("There is nothing in the filter!")
log.error("Make sure you put at least 1 (one) domain in the filter list.")
except KeyError:
log.error("There is nothing in the filter!")
log.error("Make sure you put at least 1 (one) domain in the filter list.")
log.debug("Script ending") log.debug("Script ending")