-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathdnsserver.py
More file actions
executable file
·99 lines (66 loc) · 2.17 KB
/
dnsserver.py
File metadata and controls
executable file
·99 lines (66 loc) · 2.17 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
#!/usr/bin/env python
"""
DNS Server Example
A simple little DNS Server example using
`dnslib <https://pypi.python.org/pypi/dnslib>`_
to handle the DNS protocol parsing and packet
construction (*a really nice library btw with
great integration into circuits*).
Answers ``A 127.0.0.1`` for any query of any type!
To run this example::
pip install dnslib
./dnsserver.py
Usage (*using dig*)::
dig @localhost -p 1053 test.com
"""
import sys
from dnslib import QTYPE, RR, A, DNSHeader, DNSRecord
from circuits import Component, Debugger, Event
from circuits.net.events import write
from circuits.net.sockets import UDPServer
class query(Event):
"""query Event"""
class DNS(Component):
"""DNS Protocol Handling"""
def read(self, peer, data):
self.fire(query(peer, DNSRecord.parse(data)))
class Dummy(Component):
"""
A Dummy DNS Handler
This just returns an A record response
of 127.0.0.1 for any query of any type!
"""
def query(self, peer, request):
id = request.header.id
qname = request.q.qname
print(
f'DNS Request for qname({qname!s:s})',
file=sys.stderr,
)
reply = DNSRecord(
DNSHeader(id=id, qr=1, aa=1, ra=1),
q=request.q,
)
# Add A Record
reply.add_answer(RR(qname, QTYPE.A, rdata=A('127.0.0.1')))
# Send To Client
self.fire(write(peer, reply.pack()))
class DNSServer(Component):
"""
DNS Server
This ties everything together in a nice
configurable way with protocol, transport
and dummy handler as well as optional debugger.
"""
def init(self, bind=None, verbose=False):
self.bind = bind or ('0.0.0.0', 53)
if verbose:
Debugger().register(self)
self.transport = UDPServer(self.bind).register(self)
self.protocol = DNS().register(self)
self.dummy = Dummy().register(self)
def started(self, manager):
print('DNS Server Started!', file=sys.stderr)
def ready(self, server, bind):
print('Ready! Listening on {:s}:{:d}'.format(*bind), file=sys.stderr)
DNSServer(('0.0.0.0', 1053), verbose=True).run()