Initial commit for updating A records.
This commit is contained in:
parent
350ee43a46
commit
6021d6e1b0
9 changed files with 123 additions and 0 deletions
10
.idea/.gitignore
generated
vendored
Normal file
10
.idea/.gitignore
generated
vendored
Normal file
|
|
@ -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
|
||||
10
.idea/Hostinger-DDNS.iml
generated
Normal file
10
.idea/Hostinger-DDNS.iml
generated
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Python 3.14 (Hostinger-DDNS)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
||||
7
.idea/misc.xml
generated
Normal file
7
.idea/misc.xml
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.14 (Hostinger-DDNS)" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.14 (Hostinger-DDNS)" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/Hostinger-DDNS.iml" filepath="$PROJECT_DIR$/.idea/Hostinger-DDNS.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
13
Dockerfile
Normal file
13
Dockerfile
Normal file
|
|
@ -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"]
|
||||
61
main.py
Normal file
61
main.py
Normal file
|
|
@ -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)
|
||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
requests
|
||||
python-dotenv
|
||||
Loading…
Add table
Reference in a new issue