-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_server.py
74 lines (68 loc) · 2.84 KB
/
dns_server.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
import socket
import uasyncio as asyncio
from utils import log_with_timestamp # Updated import
class DNSQuery:
def __init__(self, data):
self.data = data
self.domain = ''
self.parse_domain()
def parse_domain(self):
kind = (self.data[2] >> 3) & 15
if kind == 0:
ini = 12
lon = self.data[ini]
while lon != 0:
self.domain += self.data[ini+1:ini+lon+1].decode('utf-8') + '.'
ini += lon + 1
lon = self.data[ini]
log_with_timestamp(f"[DEBUG] DNSQuery: Parsed domain: {self.domain}")
def response(self, ip):
packet = self.data[:2] + b'\x81\x80'
packet += self.data[4:6] + self.data[4:6] + b'\x00\x00\x00\x00'
packet += self.data[12:]
packet += b'\xc0\x0c'
packet += b'\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04'
packet += bytes(map(int, ip.split('.')))
return packet
class DNSServer:
def __init__(self, ip):
self.ip = ip
self.socket = None
self.running = False
async def start(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.setblocking(False)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
self.socket.bind(('0.0.0.0', 53))
log_with_timestamp("[INFO] DNS Server: Started on port 53")
self.running = True
while self.running:
try:
yield asyncio.core._io_queue.queue_read(self.socket)
data, addr = self.socket.recvfrom(512)
if data:
log_with_timestamp(f"[DEBUG] DNS Server: Received request from {addr}")
request = DNSQuery(data)
response = request.response(self.ip)
self.socket.sendto(response, addr)
log_with_timestamp(f"[INFO] DNS Server: Responded {request.domain} -> {self.ip}")
await asyncio.sleep_ms(1) # Cooperative yield
except asyncio.CancelledError:
log_with_timestamp("[INFO] DNS Server: Received cancellation signal")
break
except Exception as e:
log_with_timestamp(f"[ERROR] DNS Server: {e}")
await asyncio.sleep_ms(1) # Cooperative yield
except Exception as e:
log_with_timestamp(f"[ERROR] DNS Server: Failed to start: {e}")
finally:
await self.stop()
async def stop(self):
self.running = False
if self.socket:
try:
self.socket.close()
except Exception as e:
log_with_timestamp(f"[ERROR] DNS Server: Error closing socket: {e}")
log_with_timestamp("[INFO] DNS Server: Stopped")