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