initial commit

This commit is contained in:
Peder Bergebakken Sundt 2022-07-06 22:46:46 +02:00
commit 8c3997cfee
3 changed files with 75 additions and 0 deletions

14
.envrc Normal file
View File

@ -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

59
main.py Normal file
View File

@ -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")

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
httpx
domeneshop