Skip to content

Commit

Permalink
Add DNS resolver cache
Browse files Browse the repository at this point in the history
  • Loading branch information
oldnapalm committed Jul 11, 2024
1 parent d0707a1 commit cd8024e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 5 additions & 1 deletion fake_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
Expand Down
16 changes: 10 additions & 6 deletions standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd8024e

Please sign in to comment.