def start(self): with Cash() as cash: data, sender = self.socket_listener.recvfrom(512) while data is not None: if data: request = parse_request(data) records = self.get_from_cash(cash, request) if records is not None: res = cash_record_as_bytes(records, request) self.socket_listener.sendto(res, sender) else: resp = self.get_from_ns(request) cash.add_to_cash(resp) records = self.get_from_cash(cash, request) if records is not None: res = cash_record_as_bytes(records, request) self.socket_listener.sendto(res, sender) else: find_req = Request(request.header, [ Question(request.questions[0].domain, request.questions.type) ], request.byte_header) byte_request = find_req.to_bytes() self.socket_resolver.sendto( byte_request, (self.address_resolver, self.port_resolver)) answer, s = self.socket_resolver.recvfrom(512) cash.add_to_cash(parse_answers(answer)) self.socket_listener.sendto(answer, sender) data, sender = self.socket_listener.recvfrom(512) cash.del_ttl_expire()
def find_any_record_on_ns(response: Response, domain): req = Request(Header("aaaa", Flags(BitArray(b"\x01\x00")), 1, 0, 0, 0), [Question(domain, RecordTypes.ANY)], str_to_hex("aa 1a 01 00 00 01 00 00 00 00 00 00")) req_bytes = req.to_bytes() with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: s.sendto(req_bytes, (response.answers[0].data, 53)) data, sender = s.recvfrom(512) response = parse_answers(data) return response
def __init__(self, q_name, q_type): if q_type not in RecordTypes.ValidRequestType: raise ValueError("Invalid type value") self.data = str_to_hex(insert_info_into_request(q_name, q_type)) self.address = "127.0.0.1" self.port = 53 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: s.sendto(self.data, (self.address, self.port)) data, sender = s.recvfrom(256) answer = parse_answers(data) print(answer)
def find_ns(self, request: Request) -> Response: ns_request = Request( request.header, [Question(request.questions[0].domain, RecordTypes.NS)], request.byte_header) byte_request = ns_request.to_bytes() self.socket_resolver.sendto( byte_request, (self.address_resolver, self.port_resolver)) answer, sender = self.socket_resolver.recvfrom(512) answer = parse_answers(answer) return answer
def find_ip_for_ns(self, response: Response): domain = response.get_all_info()[0].data request = Request( Header("aaaa", Flags(BitArray(b"\x01\x00")), 1, 0, 0, 0), [Question(domain, RecordTypes.A)], str_to_hex("aa aa 01 00 00 01 00 00 00 00 00 00")) req_bytes = request.to_bytes() self.socket_resolver.sendto( req_bytes, (self.address_resolver, self.port_resolver)) data, sender = self.socket_resolver.recvfrom(512) data = parse_answers(data) return data