gencname / generate_cnames.py
raannakasturi's picture
Refactor get_cnames function to accept wildcard parameter
9d05dd2
import hashlib
def get_domains(i_domains):
domains = []
for domain in i_domains.split(","):
domain = domain.strip()
domains.append(domain)
return domains
def extract_subdomains(domains):
exchange = min(domains, key=len)
return exchange
def prefix(domain):
domain_bytes = domain.encode()
prefix = hashlib.blake2b(domain_bytes, digest_size=12).hexdigest()
return prefix
def gen_cname_recs(domains):
cname_recs = []
for domain in domains:
cname_rec = f"_acme-challenge.{domain}"
cname_recs.append(cname_rec)
return cname_recs
def gen_cname_values(domains, cf_domain, exchange):
temp_cname_values = []
cname_values = []
for domain in domains:
cname_value = prefix(domain)
cname_value = f"{cname_value}.{domain}"
temp_cname_values.append(cname_value)
for cname_value in temp_cname_values:
modified_cname_value = cname_value.replace(exchange, cf_domain)
cname_values.append(modified_cname_value)
return cname_values
def gen_cname(domains, cf_domain, exchange):
print(domains)
cname_recs = gen_cname_recs(domains)
cname_values = gen_cname_values(domains, cf_domain, exchange)
return cname_recs, cname_values
def generate_cnames(i_domains, cf_domain, wildcard=False):
domains = get_domains(i_domains)
exchange = extract_subdomains(domains=domains)
if wildcard:
domain = []
domain.append(f"{exchange}")
cname_recs, cname_values = gen_cname(domain, cf_domain, exchange)
else:
cname_recs, cname_values = gen_cname(domains, cf_domain, exchange)
return cname_recs, cname_values
# i_domains = "dash.raanna.com, raanna.com"
# cf_domain = "silerudaagartha.eu.org"
# wildcard = True
# cname_recs, cname_values = generate_cnames(i_domains, cf_domain, wildcard)
# for rec, value in zip(cname_recs, cname_values):
# print(f"{rec} -> {value}")