Commit d8874625 authored by Markus's avatar Markus
Browse files

2nd commit

- requirements  added
- wsgi script using bobo to update dyndns
parent 8860e9b9
import bobo
from tempfile import NamedTemporaryFile
from subprocess import call
from base64 import b64decode
ZONE = "hack-hro.de"
BASEDOMAIN = "people"
@bobo.subroute('/update', scan=True)
class Dynschiss:
def __init__(self, request):
self.request = request
authstring = self.request.headers['Authorization'].split()[1]
self.user = b64decode(authstring).decode('utf-8').split(sep=':')[0]
if "ip" and "domain" in self.request.params:
self.ip = self.request.params['ip']
self.domain = self.request.params['domain']
self.status = "sucess"
else:
self.status = "fail"
@bobo.post('/')
def base(self):
updater = DynDnsUpdater(ZONE, self.user + "." + BASEDOMAIN)
updater.update(self.domain, self.ip)
return self.status
class DynDnsUpdater:
template = """server %s %d
zone %s.
update delete %s A
update add %s %d A %s
send"""
def __init__(self, zone, baseDomain, dnsIP="127.0.0.1", dnsPort=53, ttl=60):
self.zone = zone
self.baseDomain = baseDomain
self.dnsIP = dnsIP
self.dnsPort = dnsPort
self.ttl = ttl
def update(self, domain, ip):
self._validate(domain)
template = self._renderUpdateTemplate(domain, ip)
self._writeUpdate(template)
return self
def _renderUpdateTemplate(self, domain, ip):
return self.template % (self.dnsIP, self.dnsPort, self.zone,
domain, domain, self.ttl, ip)
def _validate(self, domain):
namespace = "%s.%s" % ("." + self.baseDomain if
self.baseDomain else "", self.zone)
if(not domain.endswith(namespace)):
raise ValueError("domain is not in predefined namespace. your\
domain must end with %s" % (namespace))
def _writeUpdate(self, content):
tmp = NamedTemporaryFile()
tmp.file.write(bytes(content, 'UTF-8'))
tmp.file.close()
call(["nsupdate", tmp.name])
if __name__ == '__main__':
app = Dynschiss()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment