-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion.py
69 lines (58 loc) · 2.06 KB
/
question.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
from dataclasses import dataclass
from header import *
import struct
@dataclass
class Question():
url : str
query_type : int
def build_question(self):
query_packet = "".encode()
for i in self.url.split("."):
encoded = i.encode()
bytelen = len(i)
query_packet += struct.pack(f"! {bytelen+1}B", bytelen, *encoded)
query_packet += struct.pack(
">B2H", 0, self.query_type, 1 # End of String,# Query Type,# Query Class
)
return query_packet
def parse_question(self,data):
self.url=self.parse_hostname(data)
self.query_type=self.parse_query_type(data)
query_class = self.parse_query_class(data)
return f"\nDomain : {self.url} Query Type : {query_class} Query Class : {self.query_type}"
def parse_hostname(self, data_recieved):
url = []
chunk_size = data_recieved[12]
index = 13
while chunk_size != 0:
for i in range(chunk_size):
url.append(data_recieved[index])
index += 1
url.append(46)
chunk_size = data_recieved[index]
index += 1
hostname = ""
for i in url:
hostname += chr(i)
return hostname
def parse_query_type(self, data_recieved):
index = 12
while data_recieved[index] != 0:
index += 1
index += 1
query_type = ""
query_type_list = list(data_recieved[index : index + 2])
index += 2
if (query_type_list[0] * 10 + query_type_list[1] * 1) == 1:
query_type += " A "
return query_type
def parse_query_class(self,data_recieved):
index = 12
while data_recieved[index] != 0:
index += 1
index += 3
query_class = ""
class_list = list(data_recieved[index : index + 2])
if (class_list[0] * 10 + class_list[1] * 1) == 1:
query_class += " IN "
return query_class