refactor & build: updated desec-ip-sync, might cause breaking change
Some checks failed
/ desec-script (push) Has been cancelled
forgejo/scripts/pipeline/head This commit looks good

This commit is contained in:
devaine 2026-11-10 22:56:54 -06:00
commit 5964402f81
Signed by: devaine
GPG key ID: 954B1DCAC6FF84EE
17 changed files with 576 additions and 136 deletions

16
server/desec-ip-sync/Dockerfile Executable file
View file

@ -0,0 +1,16 @@
FROM node:latest
COPY . /desec
WORKDIR /desec/server/desec-ip-sync/
SHELL ["bash", "-c"]
RUN apt update && apt install python3.11-venv -y
RUN bash run.bash
ENV PATH="/desec/server/desec-ip-sync/venv/bin:$PATH"
CMD ["python3", "main.py"]

7
server/desec-ip-sync/README.md Executable file
View file

@ -0,0 +1,7 @@
# Desec-IP-Sync
## Reason
[desec.io](https://desec.io/) is a free DNS hosting service that has securty in mind.
I personally use this service, and I noticed that there are times in which my IP always resets to another value. Which does hinder my ability to work.
This solves this issue by containerizing the service in Docker so it can be used by any `amd64` or `arm64` server. To which the program checks to see if the server's IP has changed, comapres it to an "old" IP, and changes old information from desec.io using it's API automatically without inteference.

View file

@ -0,0 +1,4 @@
mail._domainkey.mail
mail
_dmarc.mail
skyguy

View file

@ -0,0 +1,8 @@
services:
desec:
container_name: desec
image: git.devdoes.work/devaine/scripts:desec
platform: "linux/arm64"
environment:
- DESEC_TOKEN=${DESEC_TOKEN}
- CURRENT_DOMAIN=${CURRENT_DOMAIN}

162
server/desec-ip-sync/main.py Executable file
View file

@ -0,0 +1,162 @@
#!/usr/bin/env python
# TODO: Figure out a way to update the text file using a bash script that the program can read whilst running
# for the IGNORED_SUBDOMAINS_PREFIXES variable
import os
import sys
import argparse
from time import sleep
from dotenv import load_dotenv
import requests
load_dotenv()
# Constants
DOMAIN = os.getenv("CURRENT_DOMAIN")
DESEC_TOKEN = os.getenv("DESEC_TOKEN")
#IGNORED_SUBDOMAINS_PREFIXES = ["mail._domainkey.mail", "mail", "_dmarc.mail", "skyguy"]
TIMEOUT = 1800 # In seconds, 600 = 10min, 900 = 15m, 1800 = 30min
DESEC_TIMEOUT = 5 # Timeout for the API Queries
# Credits:
# https://desec.readthedocs.io/en/latest/dns/rrsets.html#modifying-an-rrset
# for documentation
def getSubDomains():
subDomainsLink = "https://desec.io/api/v1/domains/" + DOMAIN + "/rrsets/"
subDomainsHeader = {"Authorization": "Token " + DESEC_TOKEN}
subDomainsRequest = requests.get(subDomainsLink, headers=subDomainsHeader)
subDomainsJSON = subDomainsRequest.json()
return subDomainsJSON
# TODO: Make a file out of the subdomains you'd want ignored
def filterRecords():
ignoredSubdomains = []
filteredSubdomains = []
if not os.path.exists("data/ignoredSubdomains"):
with open("data/ignoredSubdomains", "w") as file:
file.close()
with open("data/ignoredSubdomains", "r") as ignoredSubdomainsFile:
for subdomain in ignoredSubdomainsFile:
ignoredSubdomains.append(subdomain)
for rrset in getSubDomains():
IS_NOT_IGNORED = (rrset["name"] not in ignoredSubdomains)
CONTAINS_A_RECORD = (rrset["type"] == "A")
NOT_CURRENT_IP = (rrset["records"][0] != requestIP)
if IS_NOT_IGNORED and CONTAINS_A_RECORD and NOT_CURRENT_IP:
filteredSubdomains.append(rrset["subname"])
if len(filteredSubdomains) > 0:
return filteredSubdomains
else:
return None
def changeRecords():
subdomains = filterRecords()
# If there are no outdated subdomains...
if subdomains is None:
print("No available subdomains to change")
return
for subdomain in subdomains:
subDomainsLink = (
"https://desec.io/api/v1/domains/" + DOMAIN + "/rrsets/" + subdomain + "/A/"
)
subDomainsHeader = {
"Authorization": "Token " + DESEC_TOKEN,
"Content-Type": "application/json",
}
subDomainsData = {
"subname": subdomain,
"type": "A",
"records": [requestIP],
"ttl": 3600,
}
changeSubDomainRequest = requests.put(
subDomainsLink, json=subDomainsData, headers=subDomainsHeader
)
print("DEBUG: For subdomain: " + subdomains)
print(changeSubDomainRequest.text)
sleep(DESEC_TIMEOUT)
def checkIP():
if not os.path.exists("data"):
os.mkdir("data")
with open("data/currentIP", "w") as file:
file.close()
# Request IP from website
global requestIP
getRequestIP = requests.get("https://ifconfig.me")
requestIP = getRequestIP.text
# Grab file data and close for data safety.
currentIPFile = open("data/currentIP", "r")
currentIP = currentIPFile.readline().replace("\n", "")
currentIPFile.close()
# If both IP's are differnet (New external IP!)
if requestIP != currentIP:
changeRecords()
with open("data/currentIP", "w") as currentIPFile:
currentIPFile.write(requestIP)
currentIPFile.close()
print("All IP's Changed!")
else:
print("DEBUG: No new IP found!")
print(f"DEBUG: New IP={requestIP}")
print(f"DEBUG: Current IP={currentIP}")
# NOTE: This function will always run even without internet available.
def checkInternet():
print("DEBUG: Checking Connection...")
try:
testRequest = requests.get("https://ifconfig.me", timeout=15)
except (requests.Timeout, requests.ConnectionError) as exception:
if (exception):
print("DEBUG: Connection failed!")
checkInternet()
if testRequest.text is not None:
return True
def appendSubdomain(args: list):
with open("data/ignoredSubdomains", "a") as ignoredSubdomainsFile:
for subdomain in args:
ignoredSubdomainsFile.write(subdomain + "\n")
print("Added subdomain: " + subdomain)
def main():
parser = argparse.ArgumentParser(prog="main.py")
parser.add_argument("-a", "--append", nargs="+")
if parser.parse_args().append is not None:
appendSubdomain(parser.parse_args().append)
sys.exit(1)
print("Starting Script")
while checkInternet():
checkIP()
sleep(TIMEOUT)
if __name__ == "__main__":
main()

View file

@ -0,0 +1,3 @@
python-dotenv
requests
jedi

16
server/desec-ip-sync/run.bash Executable file
View file

@ -0,0 +1,16 @@
#!/bin/bash
if [ ! -f .env ]; then
env | grep DESEC_TOKEN >>.env
env | grep CURRENT_DOMAIN >>.env
fi
# If virtual environment does exist..
if [ ! -d .venv ]; then
python3 -m venv .venv
source .venv/bin/activate
pip install -U -r requirements.txt
pip install -U pip # Update pip to the latest version
else
source .venv/bin/activate
fi