🗝
about summary refs log tree commit diff
path: root/zoner.py
blob: f4d4b1c34718f2ba34f635e20f046bc18e6bfacf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
from pathlib import Path
import tomllib
import os
import requests
import subprocess
import sys


cfg_dir = Path.home() / ".config/zoner"
cfg_path = cfg_dir / "zoner.toml"
if not cfg_path.exists():
    print(f"config file at {cfg_path} not found")
    exit(1)
cfg = tomllib.loads(cfg_path.read_text())


class Record:
    kind: str
    name: str
    value: str
    ttl: int
    meta: dict

    def __init__(self, kind: str, name: str, value: str, ttl: int, **kwargs):
        self.kind = kind
        self.name = name
        self.content = value
        self.ttl = ttl
        self.meta = kwargs

    def __eq__(self, other: object):
        if isinstance(other, Record):
            return (
                self.kind == other.kind
                and self.name == other.name
                and self.content == other.content
                and self.ttl == other.ttl
            )
        else:
            raise TypeError(f"cannot compare {type(self)} with {type(other)}")

    def __str__(self):
        return f"{self.kind}\t{self.name}\t{self.content}\t$ttl {self.ttl}"


def parse(filename: Path, domain: str):
    text = filename.read_text()
    while "\t\t" in text:
        text = text.replace("\t\t", "\t")
    rules = {"ttl": 3600}
    for line in text.splitlines():
        if line.startswith(";") or len(line) == 0:  # comments and empty lines
            continue
        if line.startswith("$"):  # rule line
            update_rules(rules, line)
            continue
        # record line
        line_rules = rules
        segments = line.split("\t")
        [name, kind, content, *opts] = segments
        if len(opts) > 0:  # line-scope rules
            line_rules = line_rules.copy()
            for statement in opts:
                update_rules(line_rules, statement)
        if name == "@":
            recname = domain
        else:
            recname = f"{name}.{domain}"
        yield Record(kind, recname, content, line_rules["ttl"], apiname=name)


def update_rules(rules: dict, statement: str):
    [key, value] = statement.split(" ")
    match key:
        case "$ttl":
            rules["ttl"] = int(value)
        case _:
            raise ValueError(f"invalid rule {key}")


def check_porkbun(resp: requests.Response):
    if resp.status_code >= 400:
        raise requests.HTTPError(
            f"got code {resp.status_code}: {resp.json()}", response=resp
        )


def retrieve(domain: str):
    resp = requests.post(
        f"https://porkbun.com/api/json/v3/dns/retrieve/{domain}",
        json={"apikey": cfg["api_key"], "secretapikey": cfg["secret_key"]},
    )
    check_porkbun(resp)
    for record in resp.json()["records"]:
        if record["type"] == "NS":
            continue
        if record["type"] == "MX":
            # add prioty to content
            record["content"] = f"{record['prio']} {record['content']}"
        if record["ttl"] == None:
            record["ttl"] = 600
        else:
            record["ttl"] = int(record["ttl"])
        yield Record(
            record["type"],
            record["name"],
            record["content"],
            record["ttl"],
            id=record["id"],
        )


def resolve(domain: str):
    existing = list(retrieve(domain))
    requested = list(parse(cfg_dir / f"{domain}.zone", domain))

    to_delete = []
    to_create = []
    for item in existing:
        for check in requested:
            if item == check:
                break
        else:
            to_delete.append(item)
    for item in requested:
        for check in existing:
            if item == check:
                break
        else:
            to_create.append(item)
    return (to_delete, to_create)


def balance(domain: str):
    to_delete, to_create = resolve(domain)

    if len(to_delete) != 0:
        print("to delete:")
        for record in to_delete:
            print(record)
        print()

    if len(to_create) != 0:
        print("to create:")
        for record in to_create:
            print(record)
        print()

    if len(to_delete) == 0 and len(to_create) == 0:
        print("nothing to do")
        exit()

    ch = input("continue? [y/N] ")
    if ch != "y":
        exit()

    for record in to_delete:
        print(f"delete {record}")
        resp = requests.post(
            f"https://porkbun.com/api/json/v3/dns/delete/{domain}/{record.meta['id']}",
            json={"apikey": cfg["api_key"], "secretapikey": cfg["secret_key"]},
        )
        check_porkbun(resp)

    for record in to_create:
        print(f"create {record}")
        content = record.content
        prio = None
        if record.kind == "MX":
            prio, content = content.split(" ")
        resp = requests.post(
            f"https://porkbun.com/api/json/v3/dns/create/{domain}",
            json={
                "apikey": cfg["api_key"],
                "secretapikey": cfg["secret_key"],
                "name": record.meta["apiname"],
                "type": record.kind,
                "content": content,
                "ttl": str(record.ttl),
                "prio": prio,
            },
        )
        check_porkbun(resp)

    changes = set()
    for entry in to_create:
        changes.add(f"create {entry.kind} {entry.name}")
    for entry in to_delete:
        changes.add(f"delete {entry.kind} {entry.name}")
    return list(changes)


def git_update(domain: str):
    subprocess.check_call(["git", "add", f"{domain}.zone"], cwd=cfg_dir)
    if len(subprocess.check_output(["git", "diff", "--cached"], cwd=cfg_dir)) > 0:
        subprocess.check_call(["git", "commit", "-m", f"update {domain}"], cwd=cfg_dir)
        subprocess.check_call(["git", "push"], cwd=cfg_dir)


def main():
    if (cfg_dir / ".git").exists():
        subprocess.check_call(["git", "pull"], cwd=cfg_dir, stdout=subprocess.PIPE)
    if len(sys.argv) == 1:  # selector
        names = map(lambda path: path.name[:-5], cfg_dir.glob("*.zone"))
        ret = subprocess.run(
            ["fzf"], input="\n".join(names).encode(), stdout=subprocess.PIPE
        )
        if ret.returncode != 0:
            return
        domain = ret.stdout.decode().strip()
    else:
        domain = sys.argv[1]
    subprocess.run([os.environ["EDITOR"], (cfg_dir / f"{domain}.zone").as_posix()])
    changes = balance(domain)
    if (cfg_dir / ".git").exists():
        git_update(domain)
    if (cfg_dir / "hook").exists():
        args = []
        subprocess.check_call(
            [
                (cfg_dir / "hook").as_posix(),
                *changes,
            ]
        )


if __name__ == "__main__":
    main()