diff --git a/fake_dns.py b/fake_dns.py index b0aba136..bfa83c2e 100644 --- a/fake_dns.py +++ b/fake_dns.py @@ -22,7 +22,11 @@ def response(self): if namemap.__contains__(name): ip = namemap[name] else: - ip = DNSServer.resolver.resolve(name)[0].to_text() + answer = DNSServer.resolver.cache.data.get((name, 1, 1)) + if not answer: + answer = DNSServer.resolver.resolve(name) + DNSServer.resolver.cache.put((name, 1, 1), answer) + ip = answer[0].to_text() 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:] diff --git a/standalone.py b/standalone.py index f9a2d8a3..c30f18a7 100644 --- a/standalone.py +++ b/standalone.py @@ -46,12 +46,16 @@ from urllib3.util import connection orig_create_connection = connection.create_connection def patched_create_connection(address, *args, **kwargs): - try: - host, port = address - answer = resolver.resolve(host)[0].to_text() - address = (answer, port) - except Exception as exc: - print('dns.resolver: %s' % repr(exc)) + host, port = address + answer = resolver.cache.data.get((host, 1, 1)) + if not answer: + try: + answer = resolver.resolve(host) + resolver.cache.put((host, 1, 1), answer) + except Exception as exc: + print('dns.resolver: %s' % repr(exc)) + if answer: + address = (answer[0].to_text(), port) return orig_create_connection(address, *args, **kwargs) connection.create_connection = patched_create_connection CDN_PROXY = True