mirror of
https://github.com/Akarys42/cloudflare-ddns-docker.git
synced 2025-05-10 08:45:16 -06:00
Add domain update
This commit is contained in:
@ -1,12 +1,11 @@
|
||||
import logging
|
||||
import threading
|
||||
from dataclasses import dataclass
|
||||
from typing import Tuple, List, Dict, NoReturn
|
||||
from typing import Dict, List, Tuple
|
||||
|
||||
import requests
|
||||
|
||||
from cloudflare_ddns.constants import VERIFY_TOKEN, LIST_ZONES, LIST_DNS, ACCEPTED_RECORDS
|
||||
from cloudflare_ddns.utils import parse_duration, BearerAuth
|
||||
from cloudflare_ddns.constants import ACCEPTED_RECORDS, LIST_DNS, LIST_ZONES, VERIFY_TOKEN, PATCH_DNS
|
||||
from cloudflare_ddns.utils import BearerAuth, parse_duration, get_ip
|
||||
|
||||
log = logging.getLogger("ddns")
|
||||
|
||||
@ -20,17 +19,16 @@ class Domain:
|
||||
|
||||
|
||||
class ApplicationJob(threading.Thread):
|
||||
def __init__(self, raw_delay: str, token: str, raw_domains: Tuple[str], default_ipv6: bool):
|
||||
def __init__(self, raw_delay: str, token: str, raw_domains: Tuple[str]):
|
||||
super().__init__()
|
||||
|
||||
self.stop_signal = threading.Event()
|
||||
|
||||
self.delay = None
|
||||
self.domains: List[Domain] = []
|
||||
self.found_domains: Dict[str, Domain] = {}
|
||||
self.current_ip = None
|
||||
|
||||
self.auth = BearerAuth(token)
|
||||
self.default_ipv6 = default_ipv6
|
||||
|
||||
self.raw_domains = raw_domains
|
||||
self.raw_delay = raw_delay
|
||||
@ -45,17 +43,33 @@ class ApplicationJob(threading.Thread):
|
||||
self.parse_domains()
|
||||
log.debug(f"Using domains: {', '.join(f'{domain.record_type}:{domain.domain}' for domain in self.domains)}")
|
||||
|
||||
log.info("Starting app.")
|
||||
self.update_records()
|
||||
while not self.stop_signal.wait(self.delay):
|
||||
log.info(f"Starting app. Records will be updated every {self.delay} seconds.")
|
||||
try:
|
||||
self.update_records()
|
||||
except Exception:
|
||||
log.exception("Error while updating records for the first time, aborting.")
|
||||
log.info("Exiting with code 70.")
|
||||
exit(70)
|
||||
|
||||
def update_records(self):
|
||||
...
|
||||
while not self.stop_signal.wait(self.delay):
|
||||
try:
|
||||
self.update_records()
|
||||
except Exception:
|
||||
log.exception(f"Error while updating records. Retrying in {self.delay} seconds.")
|
||||
|
||||
def parse_domains(self):
|
||||
type_1 = "A" if not self.default_ipv6 else "AAAA"
|
||||
type_2 = "A" if self.default_ipv6 else "AAAA"
|
||||
def update_records(self) -> None:
|
||||
log.info("Starting record update.")
|
||||
for record in self.domains:
|
||||
log.debug(f"Updating record for {record.domain}.")
|
||||
requests.patch(
|
||||
PATCH_DNS.format(zone_identifier=record.zone, identifier=record.id),
|
||||
json={"content": get_ip(record.record_type == 'AAAA')},
|
||||
auth=self.auth
|
||||
).raise_for_status()
|
||||
log.info("Successfully updated records.")
|
||||
|
||||
def parse_domains(self) -> None:
|
||||
found_domains = {}
|
||||
|
||||
for zone_json in requests.get(LIST_ZONES, auth=self.auth).json()["result"]:
|
||||
for record_json in requests.get(
|
||||
@ -69,11 +83,11 @@ class ApplicationJob(threading.Thread):
|
||||
record_json["zone_id"],
|
||||
record_json["id"]
|
||||
)
|
||||
self.found_domains[f'{record_json["name"]}-{record_json["type"]}'] = domain
|
||||
found_domains[f'{record_json["name"]}-{record_json["type"]}'] = domain
|
||||
|
||||
log.debug(
|
||||
f"Found domains: "
|
||||
f"{', '.join(f'{domain.record_type}:{domain.domain}' for domain in self.found_domains.values())}"
|
||||
f"{', '.join(f'{domain.record_type}:{domain.domain}' for domain in found_domains.values())}"
|
||||
)
|
||||
for domain in self.raw_domains:
|
||||
if ':' in domain:
|
||||
@ -84,19 +98,28 @@ class ApplicationJob(threading.Thread):
|
||||
log.info(f"Exiting with code 65.")
|
||||
exit(65)
|
||||
|
||||
if f"{domain}-{type_}" not in self.found_domains:
|
||||
if f"{domain}-{type_}" not in found_domains:
|
||||
log.error(
|
||||
f"Cannot find an {type_} record for the domain {domain} in your Cloudflare settings. "
|
||||
f"Have you defined this record yet?"
|
||||
)
|
||||
log.info(f"Exiting with code 65.")
|
||||
exit(65)
|
||||
|
||||
self.domains.append(found_domains[f"{domain}-{type_}"])
|
||||
|
||||
else:
|
||||
if f"{domain}-{type_1}" in self.found_domains:
|
||||
type_ = type_1
|
||||
elif f"{domain}-{type_2}" in self.found_domains:
|
||||
type_ = type_2
|
||||
else:
|
||||
found = False
|
||||
|
||||
if f"{domain}-A" in found_domains:
|
||||
self.domains.append(found_domains[f"{domain}-A"])
|
||||
found = True
|
||||
|
||||
if f"{domain}-AAAA" in found_domains:
|
||||
self.domains.append(found_domains[f"{domain}-AAAA"])
|
||||
found = True
|
||||
|
||||
if not found:
|
||||
log.error(
|
||||
f"Cannot find the domain {domain} in your Cloudflare settings. "
|
||||
f"Have you defined this record yet?"
|
||||
@ -104,11 +127,6 @@ class ApplicationJob(threading.Thread):
|
||||
log.info(f"Exiting with code 65.")
|
||||
exit(65)
|
||||
|
||||
self.domains.append(self.found_domains[f"{domain}-{type_}"])
|
||||
|
||||
|
||||
|
||||
|
||||
def validate_arguments(self):
|
||||
failed = False
|
||||
|
||||
|
Reference in New Issue
Block a user