From 6021d6e1b0e12cd1c9fbf4801d4b072727310765 Mon Sep 17 00:00:00 2001 From: Tanner Van Teeffelen Date: Tue, 2 Jun 2026 22:01:29 -0400 Subject: [PATCH] Initial commit for updating A records. --- .idea/.gitignore | 10 +++ .idea/Hostinger-DDNS.iml | 10 +++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 7 +++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ Dockerfile | 13 ++++ main.py | 61 +++++++++++++++++++ requirements.txt | 2 + 9 files changed, 123 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Hostinger-DDNS.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Dockerfile create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..30cf57e --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Hostinger-DDNS.iml b/.idea/Hostinger-DDNS.iml new file mode 100644 index 0000000..625c060 --- /dev/null +++ b/.idea/Hostinger-DDNS.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cf0df80 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..71fad2d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b0c4cd5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.14-slim + +WORKDIR /app + +# Install dependencies +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Copy script +COPY main.py . + +# Run script +CMD ["python", "main.py"] \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..f2f6ffe --- /dev/null +++ b/main.py @@ -0,0 +1,61 @@ +import requests, os, time +from dotenv import load_dotenv + + +while True: + load_dotenv() + + #Enviroment variables. + DOMAIN = os.getenv("DOMAIN") + SUBDOMAIN = os.getenv("SUBDOMAIN") + DNS_RECORD_TYPE = os.getenv("DNS_RECORD_TYPE") + TOKEN = os.getenv("TOKEN") + + #Internal variables. + public_ip = requests.get("https://api.ipify.org/").text.strip() #The public IP of the container. + dns_ip = None #The current IP of the DNS record, unpopulated. + + #Set the proper headers. + headers = { + "Authorization": f"Bearer {TOKEN}", + "Content-Type": "application/json" + } + + #Get the zone using the token and domain. + zone = (requests.get( + f"https://developers.hostinger.com/api/dns/v1/zones/{DOMAIN}", + headers=headers).json()) + + #Get the current DNS record, and set it to the dns_ip variable. + for record in zone: + if record["name"] == SUBDOMAIN and record["type"] == DNS_RECORD_TYPE: + dns_ip = record["records"][0]["content"] + + if dns_ip == public_ip: + #No action needed. + print("Values are the same, no need to update DNS record.") + elif dns_ip != public_ip: + #Step 1: Notify the user that the value is different. + print("DNS record is different, modifying DNS record...") + print("Current DNS value is:", dns_ip) + + #Step 2: Set the value to the new public IP. + for record in zone: + if record["name"] == SUBDOMAIN and record["type"] == DNS_RECORD_TYPE: + if record.get("records"): + record["records"][0]["content"] = public_ip + + print("Updated DNS value will be:", public_ip) + + #Step 3: Push the value to Hostinger. + response = requests.put( + f"https://developers.hostinger.com/api/dns/v1/zones/{DOMAIN}", + headers=headers, + json={"zone": zone} + ) + + #Step 4: Notify the user of the status of the response. + print("Status code:", response.status_code) + print("Response:", response.text) + + time.sleep(300) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d44fe44 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +requests +python-dotenv \ No newline at end of file