feat: xhttp xmux
This commit is contained in:
16 files changed
+231
-18
No files matched your search
+48
-17
@@ -21,6 +21,8 @@ import sys
|
||||
import time
|
||||
|
||||
TIMEOUT = 4.0
|
||||
# A UDP query is sent again after this long without an answer (ask_udp).
|
||||
RESEND_AFTER = 1.5
|
||||
MAX_MESSAGE = 65535
|
||||
|
||||
# --log (debug.perAppDns): a line per lookup sent on, to the journal.
|
||||
@@ -48,7 +50,7 @@ def question(query: bytes) -> str:
|
||||
return f"{'.'.join(labels) or '.'} {QTYPES.get(qtype, qtype)}"
|
||||
|
||||
|
||||
def log_lookup(port: int, proto: str, query: bytes, reply: bytes | None, started: float):
|
||||
def log_lookup(port: int, proto: str, query: bytes, reply: bytes | None, started: float, asked: int = 1):
|
||||
if not LOG:
|
||||
return
|
||||
if reply is None:
|
||||
@@ -58,7 +60,8 @@ def log_lookup(port: int, proto: str, query: bytes, reply: bytes | None, started
|
||||
else:
|
||||
outcome = RCODES.get(reply[3] & 0x0F, f"rcode {reply[3] & 0x0F}")
|
||||
took = (time.monotonic() - started) * 1000
|
||||
print(f"per-app-dns: :{port} {proto} {question(query)}: {outcome} in {took:.0f} ms", flush=True)
|
||||
again = f", asked {asked} times" if asked > 1 else ""
|
||||
print(f"per-app-dns: :{port} {proto} {question(query)}: {outcome} in {took:.0f} ms{again}", flush=True)
|
||||
|
||||
|
||||
# The source address of the queries sent on (--bind): on a TProxy route's local route the
|
||||
@@ -80,23 +83,51 @@ def upstream_socket(server: str, kind: int, mark: int) -> socket.socket:
|
||||
return sock
|
||||
|
||||
|
||||
async def ask_udp(query: bytes, servers: list[str], mark: int) -> bytes | None:
|
||||
"""The first answer any server gives, in order; None when none does."""
|
||||
async def ask_udp_once(query: bytes, server: str, mark: int) -> bytes:
|
||||
loop = asyncio.get_running_loop()
|
||||
sock = upstream_socket(server, socket.SOCK_DGRAM, mark)
|
||||
try:
|
||||
await loop.sock_connect(sock, (server, 53))
|
||||
await loop.sock_sendall(sock, query)
|
||||
return await loop.sock_recv(sock, MAX_MESSAGE)
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
|
||||
async def ask_udp(query: bytes, servers: list[str], mark: int) -> tuple[bytes | None, int]:
|
||||
"""(the first answer any server gives, in order, or None; how many times it was asked).
|
||||
|
||||
Unanswered after RESEND_AFTER, a query goes again from a new socket, the first still
|
||||
waiting: through a proxy one is often lost (a tunnel that stalled, a session it was
|
||||
opening), and the app asks again only after its own timeout, glibc's 5 s. Silence, not
|
||||
SERVFAIL, when none answers: on SERVFAIL glibc gives up, on silence it retries.
|
||||
"""
|
||||
loop = asyncio.get_running_loop()
|
||||
asked = 0
|
||||
for server in servers:
|
||||
attempts = []
|
||||
deadline = loop.time() + TIMEOUT
|
||||
# Set as each attempt goes out; the first goes out at once.
|
||||
resend_at = loop.time()
|
||||
try:
|
||||
sock = upstream_socket(server, socket.SOCK_DGRAM, mark)
|
||||
except OSError:
|
||||
continue
|
||||
try:
|
||||
await loop.sock_connect(sock, (server, 53))
|
||||
await loop.sock_sendall(sock, query)
|
||||
return await asyncio.wait_for(loop.sock_recv(sock, MAX_MESSAGE), TIMEOUT)
|
||||
except (OSError, asyncio.TimeoutError):
|
||||
continue
|
||||
while loop.time() < deadline:
|
||||
pending = [a for a in attempts if not a.done()]
|
||||
if not pending and len(attempts) == 2:
|
||||
break
|
||||
if not pending or (len(attempts) == 1 and loop.time() >= resend_at):
|
||||
attempts.append(asyncio.ensure_future(ask_udp_once(query, server, mark)))
|
||||
asked += 1
|
||||
resend_at = loop.time() + RESEND_AFTER
|
||||
pending = [a for a in attempts if not a.done()]
|
||||
wake = min(deadline, resend_at) if len(attempts) == 1 else deadline
|
||||
done, _ = await asyncio.wait(pending, timeout=max(0, wake - loop.time()), return_when=asyncio.FIRST_COMPLETED)
|
||||
for attempt in done:
|
||||
if attempt.exception() is None:
|
||||
return attempt.result(), asked
|
||||
finally:
|
||||
sock.close()
|
||||
return None
|
||||
for attempt in attempts:
|
||||
attempt.cancel()
|
||||
return None, asked
|
||||
|
||||
|
||||
class UdpListener(asyncio.DatagramProtocol):
|
||||
@@ -114,8 +145,8 @@ class UdpListener(asyncio.DatagramProtocol):
|
||||
|
||||
async def answer(self, query, addr):
|
||||
started = time.monotonic()
|
||||
reply = await ask_udp(query, self.servers, self.mark)
|
||||
log_lookup(self.port, "udp", query, reply, started)
|
||||
reply, asked = await ask_udp(query, self.servers, self.mark)
|
||||
log_lookup(self.port, "udp", query, reply, started, asked)
|
||||
if reply is not None and self.transport is not None:
|
||||
self.transport.sendto(reply, addr)
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
"""per_app_dns.py: the question in a log line, and a lost UDP query asked again."""
|
||||
|
||||
import asyncio
|
||||
import struct
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
import per_app_dns as dns
|
||||
|
||||
|
||||
def query(name: str, qtype: int = 1) -> bytes:
|
||||
labels = b"".join(bytes([len(p)]) + p.encode() for p in name.split("."))
|
||||
return struct.pack("!HHHHHH", 7, 0x100, 1, 0, 0, 0) + labels + b"\0" + struct.pack("!HH", qtype, 1)
|
||||
|
||||
|
||||
class QuestionTest(unittest.TestCase):
|
||||
def test_question(self):
|
||||
self.assertEqual(dns.question(query("pbs.twimg.com", 65)), "pbs.twimg.com HTTPS")
|
||||
self.assertEqual(dns.question(query("x.com", 999)), "x.com 999")
|
||||
# The app's name, shown safely; garbage is "?".
|
||||
self.assertEqual(dns.question(query("a\x1bb.com")), "a?b.com A")
|
||||
self.assertEqual(dns.question(b"\0" * 5), "?")
|
||||
self.assertEqual(dns.question(query("x.com")[:-3]), "?")
|
||||
|
||||
|
||||
class AskUdpTest(unittest.TestCase):
|
||||
def ask(self, behaviours, servers=("192.0.2.53",)):
|
||||
"""ask_udp with each attempt, in order, answering after its delay (None: never, an
|
||||
exception: failing)."""
|
||||
behaviours = iter(behaviours)
|
||||
|
||||
async def once(q, server, mark):
|
||||
delay, result = next(behaviours)
|
||||
await asyncio.sleep(delay)
|
||||
if isinstance(result, Exception):
|
||||
raise result
|
||||
if result is None:
|
||||
await asyncio.sleep(3600)
|
||||
return result
|
||||
|
||||
async def run():
|
||||
loop = asyncio.get_running_loop()
|
||||
started = loop.time()
|
||||
reply = await dns.ask_udp(b"q", list(servers), 0)
|
||||
return reply, loop.time() - started
|
||||
|
||||
with mock.patch.object(dns, "ask_udp_once", once), mock.patch.multiple(dns, TIMEOUT=0.4, RESEND_AFTER=0.15):
|
||||
return asyncio.run(run())
|
||||
|
||||
def check(self, behaviours, reply, asked, took, **kw):
|
||||
result, elapsed = self.ask(behaviours, **kw)
|
||||
self.assertEqual(result, (reply, asked))
|
||||
# A busy builder runs late, never early.
|
||||
self.assertGreaterEqual(elapsed, took - 0.01)
|
||||
self.assertLess(elapsed, took + 0.1)
|
||||
|
||||
def test_answered_at_once(self):
|
||||
self.check([(0, b"a")], b"a", 1, 0)
|
||||
|
||||
def test_lost_query_is_asked_again(self):
|
||||
# The first never answers; the second, sent at 0.15, answers 0.05 later.
|
||||
self.check([(0, None), (0.05, b"b")], b"b", 2, 0.2)
|
||||
|
||||
def test_the_first_may_still_answer(self):
|
||||
self.check([(0.3, b"late"), (1, b"never")], b"late", 2, 0.3)
|
||||
|
||||
def test_failing_socket_is_asked_again_at_once(self):
|
||||
self.check([(0, OSError()), (0, b"b")], b"b", 2, 0)
|
||||
|
||||
def test_silence(self):
|
||||
# Asked twice at most per server, then the next server.
|
||||
self.check([(0, None), (0, None)], None, 2, 0.4)
|
||||
self.check([(0, None), (0, None), (0, b"c")], b"c", 3, 0.4, servers=("192.0.2.53", "192.0.2.54"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user