@@ -62,6 +62,7 @@ def __init__(self):
6262 self .ip_address = ''
6363 self .ttl = ''
6464 self .name_servers = {}
65+ self .additional_cnames = {}
6566
6667 def configure (self ):
6768 fname = os .path .join (os .path .dirname (os .path .realpath (__file__ )), 'backend.conf' )
@@ -81,7 +82,19 @@ def configure(self):
8182 self .ttl = config .get ('main' , 'ttl' )
8283
8384 for entry in config .items ('nameservers' ):
84- self .name_servers [entry [0 ]] = entry [1 ]
85+ if not entry [0 ].endswith (self .domain ):
86+ name = entry [0 ] + '.' + self .domain
87+ else :
88+ name = entry [0 ]
89+ self .name_servers [name ] = entry [1 ]
90+
91+ for entry in config .items ('additional_cnames' ) or []:
92+ if not entry [0 ].endswith (self .domain ):
93+ name = entry [0 ] + '.' + self .domain
94+ else :
95+ name = entry [0 ]
96+ self .additional_cnames [name ] = entry [1 ]
97+ log ('add static CNAME: %s = %s' % (entry [0 ], entry [1 ]))
8598
8699 log ('Name servers: %s' % self .name_servers )
87100 log ('ID: %s' % self .id )
@@ -112,11 +125,13 @@ def run(self):
112125 qname = cmd [1 ].lower ()
113126 qtype = cmd [3 ]
114127
115- if ( qtype == 'A' or qtype == 'ANY' ) and qname .endswith (self .domain ):
128+ if qtype in ( 'CNAME' , 'A' , 'ANY' ) and qname .endswith (self .domain ):
116129 if qname == self .domain :
117130 self .handle_self (self .domain )
118131 elif qname in self .name_servers :
119132 self .handle_nameservers (qname )
133+ elif qname in self .additional_cnames :
134+ self .handle_additional_cnames (qname )
120135 else :
121136 self .handle_subdomains (qname )
122137 elif qtype == 'SOA' and qname .endswith (self .domain ):
@@ -159,7 +174,16 @@ def handle_subdomains(self, qname):
159174 self .write_name_servers (qname )
160175 write ('END' )
161176
177+ def handle_additional_cnames (self , qname ):
178+ if DEBUG :
179+ log ('Found CNAME: %s' % qname )
180+ cname = self .additional_cnames [qname ]
181+ write ('DATA' , qname , 'IN' , 'CNAME' , self .ttl , self .id , cname )
182+ write ('END' )
183+
162184 def handle_nameservers (self , qname ):
185+ if DEBUG :
186+ log ('Found name server: %s' % qname )
163187 ip = self .name_servers [qname ]
164188 write ('DATA' , qname , 'IN' , 'A' , self .ttl , self .id , ip )
165189 write ('END' )
0 commit comments