-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.py
344 lines (284 loc) · 10.5 KB
/
dns.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import logging
if __name__ == '__main__':
from lib.mplog import setup_logging
setup_logging()
import socket
import re
import sys
import os
import time
from multiprocessing import Process
logger = logging.getLogger("service.dns")
try:
import dnslib
except ImportError:
logger.critical("Unable to import dnslib, install with 'pip3 install dnslib'")
sys.exit(1)
import sanic
from sanic import Sanic
from lib import ipc
from lib import procs
from lib.constants import *
from lib import network
class DNSAPI:
def __init__(self):
self.dns_server = DNSServer()
self.proc = None
self.app = None
self.socket = None
self.config = {}
self.dynamic = {}
self.dns_running = False
def run(self):
self.get_config()
self.set_ip_mappings()
self.socket = ipc.unix_socket(SOCKET_DNS)
self.proc = Process(
target=self.dns_server.udp_server,
args=(
self.config["DNSAPI"].get("bind", "0.0.0.0"),
int(self.config["DNSAPI"].get("port", 53)),
self.dns_server,
)
)
self.proc.start()
self.app = Sanic("DNSAPI")
self.add_routes()
logger.info("Initialized DNS module")
self.app.run(sock=self.socket, access_log=False)
def add_routes(self):
self.app.add_route(
self.add_dynamic,
"/add/dynamic/<domain>/<ip_addr>",
methods=["POST"]
)
self.app.add_route(
self.add_dynamic_ms,
"/add/dynamic/ms/<domain>/<ip_addr>",
methods=["POST"]
)
self.app.add_route(
self.get_dynamic,
"/get/dynamic/<domain>",
methods=["GET"]
)
self.app.add_route(
self.set_dns_server_status,
"/set/dns/server/running/<status:[a-z]+>",
methods=["POST"]
)
self.app.add_route(self.stop, "/exit", methods=["POST"])
self.app.add_route(self.status, "/status", methods=["GET"])
self.app.add_route(self.revert, "/revert", methods=["POST"])
def set_ip_mappings(self):
assert "root" in self.config["DNSAPI"] and "publicip" in self.config["DNSAPI"]
publicip = self.config["DNSAPI"]["publicip"]
root_list = self.config["DNSAPI"]["root"]
roots = root_list.split(",")
for root in roots:
self.dns_server.add_static(root, publicip)
for sub in self.config["DNSAPI"].get("subdomains", "www,ns1,ds2").split(","):
self.dns_server.add_static(sub.strip() + "." + root, publicip)
self.dns_server.add_wildcard(r".*\." + root, publicip)
def get_config_entry(self, section):
response = ipc.sync_http_raw("GET", SOCK_CONFIG, "/get/section/{}".format(section))
ipc.assert_response_valid(response, dict)
self.config[section] = response["text"].copy()
def get_config(self):
self.get_config_entry("DNSAPI")
async def set_dns_server_status(self, _request, status):
logger.debug("Received a change in DNS status, server is: {}".format(status))
if status == "up":
self.dns_running = True
else:
self.dns_running = False
return sanic.response.json({"response": "OK"})
async def get_dynamic(self, request, domain):
ip = self.dynamic.get(domain, "")
logger.debug("dynamic query {} -> {}".format(domain, ip))
return sanic.response.json({"ip": ip})
async def add_dynamic_ms(self, request, domain, ip_addr):
logger.debug("Set dynamic MS {} -> {}".format(domain, ip_addr))
self.dynamic[domain] = [self.config["DNSAPI"]["publicip"], ip_addr]
return sanic.response.json(RETURN_OK)
async def add_dynamic(self, request, domain, ip_addr):
logger.debug("Set dynamic {} -> {}".format(domain, ip_addr))
self.dynamic[domain] = ip_addr
return sanic.response.json(RETURN_OK)
async def status(self, request):
if self.dns_running is True:
return sanic.response.json({"status": "up"})
return sanic.response.json({"status": "down"})
async def stop(self, request):
self.app.stop()
self.socket.close()
self.proc.terminate()
return sanic.response.json({"status": "stopped"})
async def revert(self, request):
self.dynamic = {}
return sanic.response.json({"response": "OK"})
class DNSServer:
"""
Authoritative DNS server that only responds to queries under 1 root domain.
This DNS can be dynamically changed during runtime to give different
responses based on external factors.
"""
def __init__(self):
self.port = 53
self.ttl = 5
# Search order is:
# 1. static
# 2. dynamic
# 3. wildcard
self.static = {}
self.wildcard = []
def add_wildcard(self, domain, ip_addr):
"""
Add a regular expression for a domain that should be match, like
r'[a-zA-Z0-9]\.example\.com'.
"""
self.wildcard.append({"domain": domain, "ip": ip_addr})
def add_static(self, domain, ip_addr):
""" Add a domain to IP-address mapping. """
self.static[domain] = ip_addr
def find_wildcard(self, qname):
ret = None
for i in self.wildcard:
match = re.match(i.get("domain", ""), qname)
if match:
ret = i.get("ip")
break
return ret
@staticmethod
def domain2ip(qdomain, ips, ttl):
ans = []
if isinstance(ips, list):
for ip_addr in ips:
ans.append(
dnslib.RR(
qdomain,
rdata=dnslib.A(ip_addr),
ttl=ttl
)
)
elif isinstance(ips, str):
ans.append(
dnslib.RR(
qdomain,
rdata=dnslib.A(ips),
ttl=ttl
)
)
return ans
@staticmethod
def get_dynamic(domain):
response = ipc.sync_http_raw("GET", SOCK_DNS, "/get/dynamic/{}".format(domain))
try:
resp = response["text"]
except:
logger.warning("Unable to check for dynamic domain")
return ""
if isinstance(resp, dict):
return resp.get("ip", "")
return ""
@staticmethod
def match_internal_ip(domain):
match = re.match("^(([0-9]{1,3}\.){4}).*", domain)
if match:
ip = match.group(1)[:-1]
if network.internal_ip(ip):
return ip
return None
def find_answers(self, question):
ans = []
# Multiple questions in query is not supported, but we still continue with response
if len(question.questions) > 1:
logger.warning("Received a DNS query with multiple questions, that is not supported")
# Always answer first 'A' question
for i in range(0, len(question.questions)):
if question.questions[i].qtype != dnslib.QTYPE.A:
continue
qstr = str(question.questions[i].get_qname()).lower()
logger.info("DNS query: %s", qstr)
domain = qstr[:-1]
if domain in self.static:
ans = DNSServer.domain2ip(domain, self.static[domain], self.ttl)
else:
dynamic = DNSServer.get_dynamic(domain)
if dynamic != "":
ans = DNSServer.domain2ip(domain, dynamic, self.ttl)
else:
# We first try and match to an IP address, like 127.0.0.1.root
ip = DNSServer.match_internal_ip(domain)
if ip != None:
ans = DNSServer.domain2ip(domain, ip, self.ttl)
else:
ret = self.find_wildcard(domain)
ans = DNSServer.domain2ip(domain, ret, self.ttl)
return ans
return ans
def answer(self, data, client):
try:
question = dnslib.DNSRecord.parse(data)
except:
logger.warning("Unable to parse DNS packet from: %s", client)
return None
ans = self.find_answers(question)
ret = question.reply()
for i in ans:
ret.add_answer(i)
return ret.pack()
def udp_server(self, ip, port, dns):
logger.info("Starting DNS server at %s:%i", ip, port)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = (ip, port)
try:
sock.bind(server_address)
except Exception as e:
logger.warning("Unable to bind to socket, message: {}".format(e))
sys.exit(1)
# This server might start up before the DNS management server so we must try a couple of
# times.
for i in range(0, 5):
try:
response = ipc.sync_http_raw("POST", SOCK_DNS, "/set/dns/server/running/up")
if reponse["text"]["status"] == "OK":
break
except:
time.sleep(0.5)
continue
break
while True:
try:
data, client = sock.recvfrom(4096)
logger.debug("Connection from: {}".format(client[0]))
except:
logger.info("Closing the connection")
sock.close()
break
if data:
data = dns.answer(data, client[0])
if data is not None:
sock.sendto(data, client)
if __name__ == '__main__':
def server_up():
resp = ipc.sync_http_raw("GET", SOCK_DNS, "/status")
if ipc.response_valid(resp, dict) and resp["text"].get("status") == "up":
logger.info("DNS service has successfully started")
else:
logger.error("Unable to start DNS service")
_resp = ipc.sync_http_raw("POST", SOCK_DNS, "/exit")
sys.exit(1)
resp = procs.wait_service_up(SOCK_CONFIG)
if resp is True:
import threading
threading.Timer(5, server_up, ()).start()
dns = DNSAPI()
dns.run()
else:
logger.error("Config service was not ready")
sys.exit(1)