-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfocus_dns.py
More file actions
executable file
·450 lines (380 loc) · 12.9 KB
/
focus_dns.py
File metadata and controls
executable file
·450 lines (380 loc) · 12.9 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/usr/bin/env python
# coding: utf-8
from socketserver import BaseRequestHandler, ThreadingUDPServer
from io import BytesIO
import sys
import os
import re
from os.path import exists
import socket
from socket import timeout
import struct
import time
from datetime import datetime
import importlib
DEFAULT_FILE = os.path.join(os.path.dirname(__file__), "hosts")
FOCUS_ROOT = os.environ.get("FOCUS_ROOT")
BLACKLIST_LAST_CHECKED = 0
DEFAULT_BLACKLIST = """
def domain_news_ycombinator_com(dt):
# return dt.hour % 2 # every other hour
return False
def domain_reddit_com(dt):
# return dt.hour in (12, 21) # at noon-1pm, or from 9-10pm
return False
def domain_facebook_com(dt):
return False
def default(domain, dt):
# do something with regular expressions here?
return True
""".strip()
BLACKLIST_FILE = os.path.join(FOCUS_ROOT, "focus_blacklist.py")
# these are special characters that are common to domain names but must be
# replaced with an underscore in order for the domain name to be referenced
# as a function in focus_blacklist. for example, you cannot call
# test-site.com()...you must convert it to test_site_com()
DOMAIN_SPECIAL_CHARACTERS = "-."
sys.path.append(os.path.join(FOCUS_ROOT, "etc"))
try:
import focus_blacklist as blacklist
except ImportError:
blacklist = None
"""
A simple DNS proxy server, support wilcard hosts, IPv6, cache. Usage:
Edit /etc/hosts, add:
127.0.0.1 *.local
2404:6800:8005::62 *.blogspot.com
startup dnsproxy(here use Google DNS server as delegating server):
$ sudo python dnsproxy.py -s 8.8.8.8
Then set system dns server as 127.0.0.1, you can verify it by dig:
$ dig test.local
The result should contains 127.0.0.1.
author: marlonyao<yaolei135@gmail.com>
"""
def main():
import optparse
import sys
parser = optparse.OptionParser()
parser.add_option(
"-f",
"--hosts-file",
dest="hosts_file",
metavar="<file>",
default=DEFAULT_FILE,
help="specify hosts file, default %s" % DEFAULT_FILE,
)
parser.add_option(
"-H",
"--host",
dest="host",
default="127.0.0.1",
help="specify the address to listen on",
)
parser.add_option(
"-p",
"--port",
dest="port",
default=53,
type="int",
help="specify the port to listen on",
)
parser.add_option(
"-s",
"--server",
dest="dns_server",
metavar="SERVER",
help="Required: specify the delegating dns server",
)
parser.add_option(
"-b",
"--backup-server",
dest="backup_server",
metavar="BACKUPSERVER",
help="Required: specify the delegating backup dns server",
)
parser.add_option(
"-C",
"--no-cache",
dest="disable_cache",
default=False,
action="store_true",
help="disable dns cache",
)
opts, args = parser.parse_args()
if not opts.dns_server:
parser.print_help()
sys.exit(1)
dnsserver = DNSProxyServer(
opts.dns_server,
opts.backup_server,
disable_cache=opts.disable_cache,
host=opts.host,
port=opts.port,
hosts_file=opts.hosts_file,
)
dnsserver.serve_forever()
class Struct(object):
def __init__(self, **kwargs):
for name, value in list(kwargs.items()):
setattr(self, name, value)
def parse_dns_message(data):
message = BytesIO(data)
message.seek(4) # skip id, flag
c_qd, c_an, c_ns, c_ar = struct.unpack("!4H", message.read(8))
# parse question
question = parse_dns_question(message)
for i in range(1, c_qd): # skip other question
parse_dns_question(message)
records = []
for i in range(c_an + c_ns + c_ar):
records.append(parse_dns_record(message))
return Struct(question=question, records=records)
def parse_dns_question(message):
qname = parse_domain_name(message)
qtype, qclass = struct.unpack("!HH", message.read(4))
end_offset = message.tell()
return Struct(name=qname, type_=qtype, class_=qclass, end_offset=end_offset)
def parse_dns_record(message):
parse_domain_name(message) # skip name
message.seek(4, os.SEEK_CUR) # skip type, class
ttl_offset = message.tell()
ttl = struct.unpack("!I", message.read(4))[0]
rd_len = struct.unpack("!H", message.read(2))[0]
message.seek(rd_len, os.SEEK_CUR) # skip rd_content
return Struct(ttl_offset=ttl_offset, ttl=ttl)
def _parse_domain_labels(message):
labels = []
len = ord(message.read(1))
while len > 0:
if len >= 64: # domain name compression
len = len & 0x3f
offset = (len << 8) + ord(message.read(1))
mesg = BytesIO(message.getvalue())
mesg.seek(offset)
labels.extend(_parse_domain_labels(mesg))
return labels
else:
labels.append(message.read(len))
len = ord(message.read(1))
return labels
def parse_domain_name(message):
msgs = (i.decode() for i in _parse_domain_labels(message))
return ".".join(msgs)
def addr_p2n(addr):
try:
return socket.inet_pton(socket.AF_INET, addr)
except socket.error:
return socket.inet_pton(socket.AF_INET6, addr)
def refresh_blacklist():
global BLACKLIST_LAST_CHECKED, blacklist
# we also check for not exists because the pyc file may be left around.
# in that case, blacklist name will exist, but the file will not
if not blacklist or not exists(BLACKLIST_FILE):
print(("couldn't find %s, creating a default blacklist" % BLACKLIST_FILE))
with open(BLACKLIST_FILE, "w") as h:
h.write(DEFAULT_BLACKLIST)
import focus_blacklist as blacklist
# has it changed?
changed = os.stat(BLACKLIST_FILE).st_mtime
if changed > BLACKLIST_LAST_CHECKED:
print(("blacklist %s changed, reloading" % BLACKLIST_FILE))
importlib.reload(blacklist)
BLACKLIST_LAST_CHECKED = changed
def can_visit(domain):
""" determine if the domain is blacklisted at this time """
refresh_blacklist()
# here we do a cascading lookup for the function to run. example:
# for the domain "herp.derp.domain.com", first we try to find the
# following functions in the following order:
#
# herp_derp_domain_com()
# derp_domain_com()
# domain_com()
#
# and if one still isn't found, we go with default(), if it exists
parts = domain.split(".")
for i in range(len(parts) - 1):
domain_fn_name = "domain_" + ".".join(parts[i:])
domain_fn_name = re.sub(
"[" + DOMAIN_SPECIAL_CHARACTERS + "]", "_", domain_fn_name
)
fn = getattr(blacklist, domain_fn_name, None)
if fn:
return fn(datetime.now())
fn = getattr(blacklist, "default", None)
if fn:
return fn(domain, datetime.now())
return True
DNS_TYPE_A = 1
DNS_TYPE_AAAA = 28
DNS_CLASS_IN = 1
class DNSProxyHandler(BaseRequestHandler):
def handle(self):
reqdata, sock = self.request
req = parse_dns_message(reqdata)
q = req.question
if q.type_ in (DNS_TYPE_A, DNS_TYPE_AAAA) and (
q.class_ == DNS_CLASS_IN
):
if can_visit(q.name):
# Just continue and work normal
pass
else:
fail_ip = addr_p2n("127.0.0.1")
rspdata = self._generate_dns_response_data(reqdata, q, fail_ip)
sock.sendto(rspdata, self.client_address)
return
for packed_ip, host in self.server.host_lines:
if q.name.endswith(host):
rspdata = self._generate_dns_response_data(
reqdata, q, packed_ip
)
sock.sendto(rspdata, self.client_address)
return
# lookup cache
if not self.server.disable_cache:
cache = self.server.cache
cache_key = (q.name, q.type_, q.class_)
cache_entry = cache.get(cache_key)
if cache_entry:
rspdata = update_ttl(reqdata, cache_entry)
if rspdata:
sock.sendto(rspdata, self.client_address)
return
rspdata = self._get_response(reqdata)
if not rspdata:
return
if not self.server.disable_cache:
cache[cache_key] = Struct(
rspdata=rspdata, cache_time=int(time.time())
)
sock.sendto(rspdata, self.client_address)
def _get_response(self, data):
# socket for the remote DNS server
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
sock.connect((self.server.dns_server, 53))
sock.sendall(data)
sock.settimeout(60)
rspdata = sock.recv(65535)
return rspdata
except timeout:
print((
"Fetching DNS response from {} timed out".format(
self.server.dns_server
)
+ "\n"
+ "Fetching DNS response from {}".format(
self.server.backup_server
)
))
except socket.error as err:
print(err)
finally:
sock.close()
def _get_response_from_backup_dns_server(self, data):
"""
To fetch response from backup dns server
:data: DNS request data
:returns: response from backup dns server
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
sock.connect((self.server.backup_server, 53))
sock.sendall(data)
sock.settimeout(60)
rspdata = sock.recv(65535)
return rspdata
except timeout:
print((
"Fetching DNS response from backup_server {} timed out".format(
self.server.backup_server
)
))
except socket.error as err:
print(err)
finally:
sock.close()
def _generate_dns_response_data(self, reqdata, q, packed_ip):
# header, qd=1, an=1, ns=0, ar=0
rspdata = reqdata[:2] + b"\x81\x80\x00\x01\x00\x01\x00\x00\x00\x00"
rspdata += reqdata[12 : q.end_offset]
# answer
rspdata += b"\xc0\x0c" # pointer to domain name
# type, 1 for ip4, 28 for ip6
if len(packed_ip) == 4:
rspdata += b"\x00\x01" # 1 for ip4
else:
rspdata += b"\x00\x1c" # 28 for ip6
# class: 1, ttl: 2000(0x000007d0)
rspdata += b"\x00\x01\x00\x00\x07\xd0"
rspdata += b"\x00" + chr(len(packed_ip)).encode() # rd_len
rspdata += packed_ip
return rspdata
def update_ttl(reqdata, cache_entry):
rspdata, cache_time = cache_entry.rspdata, cache_entry.cache_time
rspbytes = bytearray(rspdata)
rspbytes[:2] = reqdata[:2] # update id
current_time = int(time.time())
time_interval = current_time - cache_time
rsp = parse_dns_message(rspdata)
for record in rsp.records:
if record.ttl <= time_interval:
return None
rspbytes[record.ttl_offset : record.ttl_offset + 4] = struct.pack(
"!I", record.ttl - time_interval
)
return rspbytes
def load_hosts(hosts_file):
"load hosts config, only extract config line contains wildcard domain name"
def parts_of_line(line):
parts = line.strip().split()[:2]
return parts
def wildcard_line(line):
parts = parts_of_line(line)
if len(parts) < 2:
return False
if not parts[1].startswith("*"):
return False
try:
packed_ip = addr_p2n(parts[0])
return packed_ip, parts[1][1:]
except socket.error:
return None
def entry_line(line):
if line.startswith('#'): # It is a comment, can be ignored
return None
parts = parts_of_line(line)
if len(parts) < 2:
return False
try:
packed_ip = addr_p2n(parts[0])
return packed_ip, parts[1]
except socket.error:
return None
with open(hosts_file) as hosts_in:
hostlines = []
for line in hosts_in:
hostline = wildcard_line(line) or entry_line(line)
if hostline:
hostlines.append(hostline)
return hostlines
class DNSProxyServer(ThreadingUDPServer):
def __init__(
self,
dns_server,
backup_server=None,
disable_cache=False,
host="127.0.0.1",
port=53,
hosts_file=DEFAULT_FILE,
):
self.dns_server = dns_server
self.backup_server = backup_server
self.hosts_file = hosts_file
self.host_lines = load_hosts(hosts_file)
self.disable_cache = disable_cache
self.cache = {}
ThreadingUDPServer.__init__(self, (host, port), DNSProxyHandler)
if __name__ == "__main__":
main()