mirror of
https://github.com/michivonah/adguard-ddns-client.git
synced 2025-12-22 22:26:28 +01:00
cleanup the script
This commit is contained in:
parent
03c64acd65
commit
68ed453b03
1 changed files with 13 additions and 33 deletions
46
main.py
46
main.py
|
|
@ -1,3 +1,8 @@
|
|||
# AdGuard Home - Add DDNS IP as allowed client
|
||||
# Michi von Ah - January 2024
|
||||
# AdGuard API Documentation (inofficial): https://fossies.org/linux/AdGuardHome/AGHTechDoc.md
|
||||
|
||||
# Import modules
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
import requests
|
||||
|
|
@ -8,43 +13,20 @@ import dns.resolver
|
|||
# Load envirommental variables
|
||||
load_dotenv()
|
||||
|
||||
# AdGuard Home API Informationen
|
||||
# Global defintions
|
||||
API_BASE_URL = os.getenv('API_BASE_URL')
|
||||
API_USERNAME = os.getenv('API_USERNAME')
|
||||
API_PASSWORD = os.getenv('API_PASSWORD')
|
||||
DOMAIN_NAME = os.getenv('DOMAIN_NAME')
|
||||
|
||||
# IP-Adresse, die zur Whitelist hinzugefügt werden soll
|
||||
IP_TO_WHITELIST = "1.2.3.4"
|
||||
|
||||
def add_ip_to_whitelist(api_base_url, api_username, api_password, ip_to_whitelist):
|
||||
# API-Endpunkt für die Whitelist
|
||||
api_endpoint = f"{api_base_url}/control/access/set"
|
||||
|
||||
# Daten für die POST-Anfrage
|
||||
data = {
|
||||
"allowed_clients": ip_to_whitelist,
|
||||
}
|
||||
|
||||
try:
|
||||
# API-Anfrage durchführen
|
||||
response = requests.post(api_endpoint, auth=HTTPBasicAuth(api_username, api_password), json=data)
|
||||
|
||||
# Überprüfen, ob die Anfrage erfolgreich war
|
||||
if response.status_code == 200:
|
||||
print(f"Erfolgreich: {ip_to_whitelist} wurde zur Whitelist hinzugefügt.")
|
||||
else:
|
||||
print(f"Fehler: {response.status_code} - {response.text}")
|
||||
except Exception as e:
|
||||
print(f"Fehler bei der Anfrage: {e}")
|
||||
|
||||
# Get the current access list of the AdGuard Home Server
|
||||
def getClients(api_base_url, api_username, api_password):
|
||||
endpoint = f"{api_base_url}/control/access/list"
|
||||
response = requests.get(endpoint, auth=HTTPBasicAuth(api_username, api_password))
|
||||
result = response.json()
|
||||
#return result["allowed_clients"]
|
||||
return result
|
||||
|
||||
# Adds the ip address from a domain name to the allowed clients list of AdGuard Home
|
||||
def addClient(api_base_url, api_username, api_password, domainname):
|
||||
endpoint = f"{api_base_url}/control/access/set"
|
||||
|
||||
|
|
@ -61,19 +43,17 @@ def addClient(api_base_url, api_username, api_password, domainname):
|
|||
"disallowed_clients":currentDisallowList,
|
||||
"blocked_hosts":currentBlockedHosts
|
||||
}
|
||||
#return data
|
||||
response = requests.post(endpoint, auth=HTTPBasicAuth(api_username, api_password), json=data)
|
||||
return response
|
||||
|
||||
# Resolves the ip address of a domainname
|
||||
def getIPAdress(domainname):
|
||||
lookup = dns.resolver.resolve(domainname, 'A')
|
||||
for ipval in lookup:
|
||||
ip = ipval.to_text()
|
||||
return ip
|
||||
|
||||
|
||||
# Funktion aufrufen
|
||||
#add_ip_to_whitelist(API_BASE_URL, API_USERNAME, API_PASSWORD, IP_TO_WHITELIST)
|
||||
#print(getClients(API_BASE_URL, API_USERNAME, API_PASSWORD))
|
||||
|
||||
print(addClient(API_BASE_URL, API_USERNAME, API_PASSWORD, DOMAIN_NAME))
|
||||
# Main programm
|
||||
if __name__ == '__main__':
|
||||
#print(getClients(API_BASE_URL, API_USERNAME, API_PASSWORD))
|
||||
print(addClient(API_BASE_URL, API_USERNAME, API_PASSWORD, DOMAIN_NAME))
|
||||
Loading…
Add table
Add a link
Reference in a new issue