Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 6739-Added custom DNS server for hostname resolution(Needs fixing) #6815

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

* Release tags are now prefixed with `v` again to follow SemVer convention.
([#6810](https://github.com/mitmproxy/mitmproxy/pull/6810), @mhils)

* Adds DNS client for hostname resolution
([#6739](https://github.com/mitmproxy/mitmproxy/issues/6739), @smenon02)

## 17 April 2024: mitmproxy 10.3.0

Expand Down
54 changes: 54 additions & 0 deletions examples/addons/dns_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import asyncio
import socket


class DNSProtocol:
def __init__(self):
self.response_received = asyncio.Event()
self.response_data = None

def received(self, data):
self.response_data = data
self.response_received.set()


class DNSServer:
async def async_getaddrinfo(self, hostname):
try:
udp_server_address = "local"
udp_server_port = 12234

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(10) # Set timeout threshold for socket operations

try:
sock.sendto(hostname.encode(), (udp_server_address, udp_server_port))

data, _ = sock.recvfrom(1024)

ip_address = data.decode()
return ip_address
finally:
sock.close()
except socket.timeout as e:
print("Timeout waiting for data from UDP socket:", e)
return None
except Exception as e:
print("Error communicating with UDP server:", e)
return None

async def receive_from_socket(self, sock):
try:
data, _ = await asyncio.wait_for(sock.recvfrom(1024), timeout=5)
return data
except asyncio.TimeoutError as e:
print("Timeout waiting for data from UDP socket:", e)
return None
except Exception as e:
print("Error receiving data from UDP socket:", e)
return None
finally:
sock.close()


addons = [DNSServer()]