This repository has been archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjianbing-dns.py
executable file
·89 lines (64 loc) · 2.22 KB
/
jianbing-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
#!/usr/bin/env python
# -*- coding:utf8 -*-
import struct
import dnslib
from gevent.server import StreamServer, DatagramServer
import stardict
def notfound(word):
yield "No word '%s' found, did you mean:" % word
for i, w in enumerate(stardict.get_close_matches(word)):
yield " %d. %s %s" % (i+1, w, stardict.check(w).replace('\n', ' '))
def split_len(seq, length):
for i in xrange(0, len(seq), length):
yield seq[i:i+length]
def make_jianbing(query):
response = query.reply()
response.header.ra = 0
word = query.q.qname.label[0]
desc = stardict.check(word)
desc = [desc] if desc else notfound(word)
# force change rtype to TXT
response.rr = []
for txt in desc:
# if len(txt) > 255
for part in split_len(txt, 255):
response.add_answer(
dnslib.RR(query.q.qname, dnslib.QTYPE.TXT,
rdata=dnslib.TXT(part), ttl=5))
# no Recursion Available
return response
class UdpJianbingServer(DatagramServer):
def handle(self, data, address):
# print '%s: got %r' % (address[0], data)
query = dnslib.DNSRecord.parse(data)
# print query
response = make_jianbing(query)
wire = response.pack()
if len(wire) >= 512:
response.rr = []
response.header.a = 0
response.header.tc = 1
wire = response.pack()
self.socket.sendto(wire, address)
class TcpJianbingServer(StreamServer):
def handle(self, socket, address):
# print ('New connection from %s:%s' % address)
# using a makefile because we want to use readline()
fileobj = socket.makefile()
ldata = fileobj.read(2)
(l,) = struct.unpack("!H", ldata)
data = fileobj.read(l)
# print '%r' % data
query = dnslib.DNSRecord.parse(data)
# print query
response = make_jianbing(query)
wire = response.pack()
wire = struct.pack('!H', len(wire)) + wire
fileobj.write(wire)
fileobj.flush()
if __name__ == '__main__':
print 'Already here'
udpserver = UdpJianbingServer(':53')
udpserver.start()
tcpserver = TcpJianbingServer(':53')
tcpserver.serve_forever()