3proxy/tests/cases/dnspr.py
Vladimir Dubrovin daa0e36e41 Cover the port mappers, tlspr, dnspr, auto and the UDP data path
tcppm and udppm forward a port each, so test both directions of each: a
request through the TCP mapper reaching its target, a datagram through the
UDP one coming back answered, and a mapper whose rules deny the client
answering nothing.

tlspr takes its destination from the name in the handshake, so point that
name at 127.0.0.1 with nsrecord and give the certificate the same name: the
name then both chooses where the request goes and is what the client checks.

dnspr answers from its cache, including the documented 0.0.0.0 record, which
is handed out as an address rather than withheld.

auto is asked to serve an HTTP proxy request, SOCKS4, SOCKS5 and a TLS
handshake on one port, and to make nothing of a request that is none of
them. Its protocols reach different places, so there are two origins.

The SOCKS UDP association was only checked for the port it binds. Send
datagrams through it as well, large and small, and check a second
association gets its own port - and that one bound inside an intport range
still relays.

A UDP service has no socket to connect to, so readiness is found by asking
until it answers rather than racing it.
2026-08-26 10:54:04 +03:00

41 lines
1.4 KiB
Python

"""dnspr: a caching DNS proxy, answering from what it has been told."""
def run(t):
port = t.free_port()
t.start("dnspr", f"""
log
flush
nserver 127.0.0.1
nscache 1024
nsrecord host.test 10.11.12.13
nsrecord other.test 10.11.12.14
nsrecord blocked.test 0.0.0.0
auth iponly
allow *
dnspr -p{port}
""")
# wait for the service: a datagram sent too early is simply lost
for _ in range(100):
if t.dns_query(port, "host.test"):
break
t.eq(["10.11.12.13"], t.dns_query(port, "host.test"),
"a static record is answered")
t.eq(["10.11.12.14"], t.dns_query(port, "other.test"),
"and so is another one")
# asking twice must give the same answer, which is what the cache is for
t.eq(["10.11.12.13"], t.dns_query(port, "host.test"),
"the same name answers the same way again")
# 0.0.0.0 is the documented way to make a name never resolve: the
# address is handed out, and it is the client that then gets nowhere
t.eq(["0.0.0.0"], t.dns_query(port, "blocked.test"),
"a name pointed at 0.0.0.0 answers with that address")
# a name it knows nothing about cannot be answered from here: the
# configured server does not exist, so there is nothing to forward to
t.ne(["10.11.12.13"], t.dns_query(port, "unknown.test") or [],
"an unknown name does not borrow another answer")