commit 8c3997cfee02ef4cdc75cd1aa4167cdf87d0b14a Author: Peder Bergebakken Sundt Date: Wed Jul 6 22:46:46 2022 +0200 initial commit diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..c59871a --- /dev/null +++ b/.envrc @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +test -d .venv || { + python -m venv .venv + ( + source .venv/bin/activate + python -m pip install -r requirements.txt + ) +} + +source .venv/bin/activate + +test -f .envrc.secrets &&\ + source .envrc.secrets diff --git a/main.py b/main.py new file mode 100644 index 0000000..1c79db3 --- /dev/null +++ b/main.py @@ -0,0 +1,59 @@ +from domeneshop import Client +import os, httpx, pprint, toml + +def get_pub_ip() -> str: + for endpoint, getter in { + "http://myip.tf": lambda resp: resp.text, + "https://ipinfo.io/json": lambda resp: resp.json()["ip"], + "https://api.ipify.org": lambda resp: resp.text, + "http://ip.42.pl/raw": lambda resp: resp.text, + }.items(): + resp = httpx.get(endpoint) + if not resp.is_success: continue + try: + return resp.json()["ip"] + except: + pass + else: + raise Exception("Could not find external IP") + +# https://www.domeneshop.no/admin?view=api +with open("/var/lib/secrets/domeneshop.toml") as f: + globals().update(toml.load(f)["secrets"]) +#DOMENESHOP_TOKEN = os.environ["DOMENESHOP_TOKEN"] +#DOMENESHOP_SECRET = os.environ["DOMENESHOP_SECRET"] +IP_ADDRESS = get_pub_ip() # TODO: both ipv4 and ipv6 +DOMAINS = { + "pbsds.net": { + #"olavtr": ["A"], + "spismeg": ["A"], + }, +} + +client = Client(DOMENESHOP_TOKEN, DOMENESHOP_SECRET) +for domain in client.get_domains(): + if domain["domain"] not in DOMAINS: + continue + RECORDS = DOMAINS[domain["domain"]] + for record in client.get_records(domain["id"]): + if record["host"] in RECORDS \ + and record["type"] in RECORDS[record["host"]]: + print("Found: ", end="") + rich.pretty.pprint(record) + if record["data"] != IP_ADDRESS: + record["data"] = IP_ADDRESS + print("Push: ", end="") + rich.pretty.pprint(record) + client.modify_record(domain_id=domain["id"], record_id=record.pop("id"), record=record) + else: + print("Nothing done") + RECORDS[record["host"]].remove(record["type"]) + for k, v in list(RECORDS.items()): + if not v: RECORDS.pop(k) + if not RECORDS: DOMAINS.pop(domain["domain"]) +if DOMAINS: + print("ERROR: The following records were not found:") + pprint.pprint(DOMAINS) + exit(1) +else: + print("Success") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f20685e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +httpx +domeneshop