Commit a7dce5ce authored by Matt Traudt's avatar Matt Traudt
Browse files

Don't assume we can resolve a domain name

Even for a valid domain that usually resolves, sometimes the local DNS
resolver won't be able to resolve it (duh). Catch the exception that gets
thrown in that instance and fail gracefully by returning an empty list.
parent 73c01a47
......@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed
- Update python minimal version in setup (#28043)
- Catch unhandled exception when we fail to resolve a domain name (#28141)
### Changed
......
......@@ -67,8 +67,16 @@ def touch_file(fname, times=None):
def resolve(hostname, ipv4_only=False, ipv6_only=False):
assert not (ipv4_only and ipv6_only)
results = []
try:
results = socket.getaddrinfo(hostname, 0):
except socket.gaierror:
log.warn(
'Unable to resolve %s hostname. Returning empty list of addresses',
hostname)
return []
ret = set()
for result in socket.getaddrinfo(hostname, 0):
for result in results:
fam, _, _, _, addr = result
if fam == socket.AddressFamily.AF_INET6 and not ipv4_only:
ret.add(addr[0])
......
......@@ -120,7 +120,10 @@ class Relay:
#
# Also, only use the first ipv4/6 we get even if there is more than
# one.
host = resolve(host)[0]
results = resolve(host)
if not len(results):
return False
host = results[0]
assert is_valid_ipv4_address(host) or is_valid_ipv6_address(host)
return self.exit_policy.can_exit_to(host, port)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment