-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_server2.py
56 lines (46 loc) · 1.82 KB
/
dns_server2.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
from dnslib import DNSRecord, QTYPE, RR, A, DNSHeader
import socket
import socketserver
# Get the local IP address
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
# DNS server configuration
DOMAIN_TO_IP = {
'a.com.': local_ip,
'b.com.': local_ip,
# DNS table with 5 entries
"olivetrees.com." : "73.032.34.33",
"example.com.": "93.184.216.34",
"google.com.": "172.217.16.142",
"github.com.": "140.82.121.4",
"stackoverflow.com.": "151.101.1.69",
"python.org.": "138.197.63.241",
}
class DNSHandler(socketserver.BaseRequestHandler):
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
try:
request = DNSRecord.parse(data)
# print(f"Received request for: {str(request.q.qname)}")
# Create a DNS response with the same ID and the appropriate flags
reply = DNSRecord(DNSHeader(id=request.header.id, qr=1, aa=1, ra=1), q=request.q)
qname = str(request.q.qname)
qtype = QTYPE[request.q.qtype]
if qname in DOMAIN_TO_IP:
if qname == "olivetrees.com.":
reply.add_answer(RR(qname + "73.03283493301709", QTYPE.A, rdata=A(DOMAIN_TO_IP[qname])))
else:
reply.add_answer(RR(qname, QTYPE.A, rdata=A(DOMAIN_TO_IP[qname])))
#print(f"Resolved {qname} to {DOMAIN_TO_IP[qname]}")
#else:
# print(f"No record found for {qname}")
socket.sendto(reply.pack(), self.client_address)
except Exception as e:
print(f"Error handling request: {e}")
def start_dns():
server = socketserver.UDPServer(("0.0.0.0", 53533), DNSHandler)
#print("DNS Server is running...")
server.serve_forever()
if __name__ == "__main__":
start_dns()