Skip to content

Commit

Permalink
Avoid instantiating Resolver when hostname is ip address for DoH
Browse files Browse the repository at this point in the history
  • Loading branch information
URenko committed Aug 28, 2024
1 parent be1cace commit 7ea7630
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
22 changes: 12 additions & 10 deletions dns/asyncquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,21 +560,23 @@ async def https(
else:
url = where

if bootstrap_address is None:
parsed = urllib.parse.urlparse(url)
if parsed.hostname is None:
raise ValueError("no hostname in URL")
if dns.inet.is_address(parsed.hostname):
bootstrap_address = parsed.hostname
if parsed.port is not None:
port = parsed.port

if http_version == HTTPVersion.H3 or (
http_version == HTTPVersion.DEFAULT and not have_doh
):
if bootstrap_address is None:
parsed = urllib.parse.urlparse(url)
resolver = _maybe_get_resolver(resolver)
if parsed.hostname is None:
raise ValueError("no hostname in URL")
if dns.inet.is_address(parsed.hostname):
bootstrap_address = parsed.hostname
else:
answers = await resolver.resolve_name(parsed.hostname, family)
bootstrap_address = random.choice(list(answers.addresses()))
if parsed.port is not None:
port = parsed.port
assert parsed.hostname is not None # for mypy
answers = await resolver.resolve_name(parsed.hostname, family)
bootstrap_address = random.choice(list(answers.addresses()))
return await _http3(
q,
bootstrap_address,
Expand Down
22 changes: 12 additions & 10 deletions dns/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,21 +449,23 @@ def https(
else:
url = where

if bootstrap_address is None:
parsed = urllib.parse.urlparse(url)
if parsed.hostname is None:
raise ValueError("no hostname in URL")
if dns.inet.is_address(parsed.hostname):
bootstrap_address = parsed.hostname
if parsed.port is not None:
port = parsed.port

if http_version == HTTPVersion.H3 or (
http_version == HTTPVersion.DEFAULT and not have_doh
):
if bootstrap_address is None:
parsed = urllib.parse.urlparse(url)
resolver = _maybe_get_resolver(resolver)
if parsed.hostname is None:
raise ValueError("no hostname in URL")
if dns.inet.is_address(parsed.hostname):
bootstrap_address = parsed.hostname
else:
answers = resolver.resolve_name(parsed.hostname, family)
bootstrap_address = random.choice(list(answers.addresses()))
if parsed.port is not None:
port = parsed.port
assert parsed.hostname is not None # for mypy
answers = resolver.resolve_name(parsed.hostname, family)
bootstrap_address = random.choice(list(answers.addresses()))
return _http3(
q,
bootstrap_address,
Expand Down

0 comments on commit 7ea7630

Please sign in to comment.