Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/backend.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ ns=ns1.example.com
ns1.example.com=127.0.0.1
ns2.example.com=127.0.0.1

[additional_cnames]
_acme-challenge=xyz.auth.example.com.
www=business.example.com
28 changes: 26 additions & 2 deletions src/nip.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(self):
self.ip_address = ''
self.ttl = ''
self.name_servers = {}
self.additional_cnames = {}

def configure(self):
fname = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'backend.conf')
Expand All @@ -81,7 +82,19 @@ def configure(self):
self.ttl = config.get('main', 'ttl')

for entry in config.items('nameservers'):
self.name_servers[entry[0]] = entry[1]
if not entry[0].endswith(self.domain):
name = entry[0] + '.' + self.domain
else:
name = entry[0]
self.name_servers[name] = entry[1]

for entry in config.items('additional_cnames') or []:
if not entry[0].endswith(self.domain):
name = entry[0] + '.' + self.domain
else:
name = entry[0]
self.additional_cnames[name] = entry[1]
log('add static CNAME: %s = %s' % (entry[0], entry[1]))

log('Name servers: %s' % self.name_servers)
log('ID: %s' % self.id)
Expand Down Expand Up @@ -112,11 +125,13 @@ def run(self):
qname = cmd[1].lower()
qtype = cmd[3]

if (qtype == 'A' or qtype == 'ANY') and qname.endswith(self.domain):
if qtype in ('CNAME', 'A', 'ANY') and qname.endswith(self.domain):
if qname == self.domain:
self.handle_self(self.domain)
elif qname in self.name_servers:
self.handle_nameservers(qname)
elif qname in self.additional_cnames:
self.handle_additional_cnames(qname)
else:
self.handle_subdomains(qname)
elif qtype == 'SOA' and qname.endswith(self.domain):
Expand Down Expand Up @@ -159,7 +174,16 @@ def handle_subdomains(self, qname):
self.write_name_servers(qname)
write('END')

def handle_additional_cnames(self, qname):
if DEBUG:
log('Found CNAME: %s' % qname)
cname=self.additional_cnames[qname]
write('DATA', qname, 'IN', 'CNAME', self.ttl, self.id, cname)
write('END')

def handle_nameservers(self, qname):
if DEBUG:
log('Found name server: %s' % qname)
ip = self.name_servers[qname]
write('DATA', qname, 'IN', 'A', self.ttl, self.id, ip)
write('END')
Expand Down