Git Repository Public Repository

nip.io

URLs

Copy to Clipboard
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
#!/usr/bin/python

import sys
import re
import os
import ConfigParser

DEBUG=0

def log(msg):
    sys.stderr.write('backend (%s): %s\n' % (os.getpid(), msg))


def write(*l):
    args=len(l)
    c = 0
    for a in l:
        c += 1
        if DEBUG: log('writing: %s' % a)
        sys.stdout.write(a)
        if c < args:
            if DEBUG: log('writetab')
            sys.stdout.write('\t')
    if DEBUG: log('writenewline')
    sys.stdout.write('\n')
    sys.stdout.flush()


def get_next():
    if DEBUG: log('reading now')
    l = sys.stdin.readline()
    if DEBUG: log('read line: %s' % l)
    return l.strip().split('\t')


class DynamicBackend:
    def __init__(self):
        self.id = ''
        self.soa = ''
        self.domain = ''
        self.ip_address = ''
        self.ttl = ''
        self.name_servers = {}

    def configure(self):
        fname = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'backend.conf')
        if not os.path.exists(fname):
            log('%s does not exist' % fname)
            sys.exit(1)
    
        fp = open(fname)
        config = ConfigParser.ConfigParser()
        config.readfp(fp)
        fp.close()

        self.id = config.get('soa', 'id')
        self.soa = '%s %s %s' % (config.get('soa', 'ns'), config.get('soa','hostmaster'), self.id)
        self.domain = config.get('main', 'domain')
        self.ip_address = config.get('main', 'ipaddress')
        self.ttl = config.get('main', 'ttl')
    
        for entry in config.items('nameservers'):
            self.name_servers[entry[0]] = entry[1]

        log('Name servers: %s' % self.name_servers)
        log('ID: %s' % self.id)
        log('TTL %s' % self.ttl)
        log('SOA: %s' % self.soa)
        log('IP Address: %s' % self.ip_address)
        log('DOMAIN: %s' % self.domain)

    def run(self):
        log('starting up')
        handshake = get_next()
        if handshake[1] != '1':
            log('Not version 1: %s' % handshake)
            sys.exit(1)
        write('OK', 'We are good')
        log('Done handshake')

        while True:
            cmd = get_next()
            if DEBUG: log(cmd)

            if len(cmd) < 6:
                log('did not understand: %s' % cmd)
                write('FAIL')
                continue

            qname = cmd[1].lower()
            qtype = cmd[3]

            if (qtype == 'A' or qtype == '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)
                else:
                    self.handle_subdomains(qname)
            elif qtype == 'SOA' and qname.endswith(self.domain):
                self.handle_soa(qname)
            else:
                self.handle_unknown(qtype, qname)

    def handle_self(self, name):
        write('DATA', name, 'IN', 'A', self.ttl, self.id, self.ip_address)
        self.write_name_servers(name)
        write('END')

    def handle_subdomains(self, qname):
        subdomain = qname[0:qname.find(self.domain)-1]

        subparts = subdomain.split('.')
        if len(subparts) < 4:
            if DEBUG: log('subparts less than 4')
            self.handle_self(qname)
            return

        ipaddress = subparts[-4:]
        if DEBUG: log('ip: %s' % ipaddress)
        for part in ipaddress:
            if re.match('^\d{1,3}$', part) is None:
                if DEBUG: log('%s is not a number' % part)
                self.handle_self(qname)
                return
            parti = int(part)
            if parti < 0 or parti > 255:
                if DEBUG: log('%d is too big/small' % parti)
                self.handle_self(qname)
                return

        write('DATA', qname, 'IN', 'A', self.ttl, self.id, '%s.%s.%s.%s' % (ipaddress[0], ipaddress[1], ipaddress[2], ipaddress[3]))
        self.write_name_servers(qname)
        write('END')

    def handle_nameservers(self, qname):
        ip = self.name_servers[qname]
        write('DATA', qname, 'IN', 'A', self.ttl, self.id, ip)
        write('END')

    def write_name_servers(self, qname):
        for nameServer in self.name_servers:
            write('DATA', qname, 'IN', 'NS', self.ttl, self.id, nameServer)

    def handle_soa(self, qname):
        write('DATA', qname, 'IN', 'SOA', self.ttl, self.id, self.soa)
        write('END')

    def handle_unknown(self, qtype, qname):
        write('LOG', 'Unknown type: %s, domain: %s' % (qtype, qname))
        write('END')


if __name__ == '__main__':

    backend = DynamicBackend()
    backend.configure()
    backend.run()

Commits for nip.io/src/backend.py

Diff revisions: vs.
Revision Author Commited Message
59b962 ... Diff Diff rs picture rs Wed 18 Dec, 2013 20:56:13 +0000

add pid to log

7e7ae2 ... Diff Diff rs picture rs Sat 30 Nov, 2013 12:02:58 +0000

change qname to lowercase

a59335 ... Diff Diff rs picture rs Fri 06 Jul, 2012 11:12:34 +0000

Minor bugfixes:
- only reply SOA queries for ourselves
- don’t send a FAIL for unknown queries, just END it instead

58ac8f ... rs picture rs Fri 08 Jun, 2012 15:22:15 +0000

a working backend