From 71b2ddfdc573dcbeb062ce99cebfb6c8927a0545 Mon Sep 17 00:00:00 2001 From: jung-geun Date: Thu, 19 Sep 2024 09:26:47 +0000 Subject: [PATCH] Refactor file paths in Makefile, run_script.sh, and start.sh Update default_env.json to include CLOUDFLARE_CNAME configuration Update update_dns.py to handle CNAME records for multiple A records Add .gitignore for env.json in init directory --- init/.gitignore | 1 + init/default_env.json | 4 +++- src/update_dns.py | 23 ++++++++++++++++++----- 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 init/.gitignore diff --git a/init/.gitignore b/init/.gitignore new file mode 100644 index 0000000..3e9f3c7 --- /dev/null +++ b/init/.gitignore @@ -0,0 +1 @@ +env.json \ No newline at end of file diff --git a/init/default_env.json b/init/default_env.json index 8049ad9..db0e5c7 100644 --- a/init/default_env.json +++ b/init/default_env.json @@ -6,7 +6,9 @@ "@": true }, "CLOUDFLARE_CNAME": { - "*": true + "*": { + "@": true + } }, "CLOUDFLARE_MX": {} } \ No newline at end of file diff --git a/src/update_dns.py b/src/update_dns.py index b4a34a4..9540e09 100644 --- a/src/update_dns.py +++ b/src/update_dns.py @@ -80,6 +80,8 @@ class DDNS: required_keys, ) + self.domain = config["CLOUDFLARE_DOMAIN"] + return config def get_config(self): @@ -258,9 +260,16 @@ class DDNS: def update_cname_list(self, cname_list, domain): try: records_list = self.read_record(type="CNAME", content=domain) + tmp_cname_list = [] + for cname in cname_list.keys(): + a_record = list(cname_list[cname].keys())[0] + a_record = a_record if a_record != "@" else self.domain.split(".")[0] + proxy = list(cname_list[cname].values())[0] + if a_record == domain.split(".")[0]: + tmp_cname_list.append([cname, proxy]) if not records_list: - for cname, proxy in cname_list.items(): + for [cname, proxy] in tmp_cname_list: self.create_record( type="CNAME", name=cname, content=domain, proxy=proxy ) @@ -270,7 +279,7 @@ class DDNS: for r in records_list: pre_list[r["name"].split(".")[0]] = [r["proxied"], r["id"]] - for cname, proxy in cname_list.items(): + for [cname, proxy] in tmp_cname_list: if cname in pre_list.keys(): if proxy != pre_list[cname][0]: self.update_record( @@ -316,9 +325,13 @@ if __name__ == "__main__": API.update_ip(API.current_ip) # Update CNAME records - result = API.update_cname_list( - config["CLOUDFLARE_CNAME"], config["CLOUDFLARE_DOMAIN"] - ) + for a in config["CLOUDFLARE_A"]: + domain = ( + a + "." + config["CLOUDFLARE_DOMAIN"] + if a != "@" + else config["CLOUDFLARE_DOMAIN"] + ) + result = API.update_cname_list(config["CLOUDFLARE_CNAME"], domain) if not result: logger.error("Failed to update CNAME records") sys.exit(1)