hints: avoid DNS lookups for all-numeric ipv4/ipv6 hints

This improves offline behavior for some tests, if we get spurious DNS lookup
errors.
This commit is contained in:
Brian Warner 2019-05-06 21:40:20 -07:00
parent 50c766f811
commit d9284cd4cb

View File

@ -3,7 +3,8 @@ import sys
import re import re
import six import six
from collections import namedtuple from collections import namedtuple
from twisted.internet.endpoints import HostnameEndpoint from twisted.internet.endpoints import TCP4ClientEndpoint, TCP6ClientEndpoint, HostnameEndpoint
from twisted.internet.abstract import isIPAddress, isIPv6Address
from twisted.python import log from twisted.python import log
# These namedtuples are "hint objects". The JSON-serializable dictionaries # These namedtuples are "hint objects". The JSON-serializable dictionaries
@ -82,6 +83,11 @@ def endpoint_from_hint_obj(hint, tor, reactor):
return None return None
return None return None
if isinstance(hint, DirectTCPV1Hint): if isinstance(hint, DirectTCPV1Hint):
# avoid DNS lookup unless necessary
if isIPAddress(hint.hostname):
return TCP4ClientEndpoint(reactor, hint.hostname, hint.port)
if isIPv6Address(hint.hostname):
return TCP6ClientEndpoint(reactor, hint.hostname, hint.port)
return HostnameEndpoint(reactor, hint.hostname, hint.port) return HostnameEndpoint(reactor, hint.hostname, hint.port)
return None return None