broken-zone-responder.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python3
  2. """Static DNS responder with per-query logging."""
  3. import socket, sys, time
  4. pkt = open(sys.argv[1], "rb").read()
  5. port = int(sys.argv[2])
  6. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  7. s.bind(("0.0.0.0", port))
  8. # Determine this container's own IP (connect to a public address; no packet
  9. # is sent — UDP connect just selects the local address). This is more
  10. # reliable than `docker inspect` from the host, which can be empty while the
  11. # container is starting on a slow WSL daemon.
  12. try:
  13. probe = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  14. probe.connect(("1.1.1.1", 53))
  15. my_ip = probe.getsockname()[0]
  16. probe.close()
  17. except Exception:
  18. my_ip = "0.0.0.0"
  19. print(f"responder ip={my_ip} on :{port}, serving {len(pkt)} bytes", flush=True)
  20. n = 0
  21. while True:
  22. data, addr = s.recvfrom(4096)
  23. n += 1
  24. # decode question name (best-effort) for logging
  25. print(f"[{time.time():.0f}] query #{n} from {addr[0]} ({len(data)} bytes)", flush=True)
  26. if len(data) < 2:
  27. continue
  28. resp = data[:2] + pkt[2:]
  29. s.sendto(resp, addr)