internal renaming: hint objects vs hint structs
This commit is contained in:
parent
7aa55e6b65
commit
f2050c5fd4
|
@ -128,11 +128,11 @@ class Misc(unittest.TestCase):
|
||||||
self.assertIsInstance(portno, int)
|
self.assertIsInstance(portno, int)
|
||||||
|
|
||||||
class Hints(unittest.TestCase):
|
class Hints(unittest.TestCase):
|
||||||
def test_endpoint_from_hint(self):
|
def test_endpoint_from_hint_obj(self):
|
||||||
c = transit.Common(u"")
|
c = transit.Common(u"")
|
||||||
ep = c._endpoint_from_hint(transit.DirectTCPV1Hint("localhost", 1234))
|
ep = c._endpoint_from_hint_obj(transit.DirectTCPV1Hint("localhost", 1234))
|
||||||
self.assertIsInstance(ep, endpoints.HostnameEndpoint)
|
self.assertIsInstance(ep, endpoints.HostnameEndpoint)
|
||||||
ep = c._endpoint_from_hint("unknown:stuff:yowza:pivlor")
|
ep = c._endpoint_from_hint_obj("unknown:stuff:yowza:pivlor")
|
||||||
self.assertEqual(ep, None)
|
self.assertEqual(ep, None)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1212,7 +1212,7 @@ class Transit(unittest.TestCase):
|
||||||
connectors[0].callback("winner")
|
connectors[0].callback("winner")
|
||||||
self.assertEqual(results, ["winner"])
|
self.assertEqual(results, ["winner"])
|
||||||
|
|
||||||
def _endpoint_from_hint(self, hint):
|
def _endpoint_from_hint_obj(self, hint):
|
||||||
if hint == DIRECT_HINT_INTERNAL:
|
if hint == DIRECT_HINT_INTERNAL:
|
||||||
return "direct"
|
return "direct"
|
||||||
elif hint == RELAY_HINT_FIRST:
|
elif hint == RELAY_HINT_FIRST:
|
||||||
|
@ -1231,7 +1231,7 @@ class Transit(unittest.TestCase):
|
||||||
|
|
||||||
direct_connectors = []
|
direct_connectors = []
|
||||||
relay_connectors = []
|
relay_connectors = []
|
||||||
s._endpoint_from_hint = self._endpoint_from_hint
|
s._endpoint_from_hint_obj = self._endpoint_from_hint_obj
|
||||||
def _start_connector(ep, description, is_relay=False):
|
def _start_connector(ep, description, is_relay=False):
|
||||||
d = defer.Deferred()
|
d = defer.Deferred()
|
||||||
if ep == "direct":
|
if ep == "direct":
|
||||||
|
@ -1270,7 +1270,7 @@ class Transit(unittest.TestCase):
|
||||||
|
|
||||||
direct_connectors = []
|
direct_connectors = []
|
||||||
relay_connectors = []
|
relay_connectors = []
|
||||||
s._endpoint_from_hint = self._endpoint_from_hint
|
s._endpoint_from_hint_obj = self._endpoint_from_hint_obj
|
||||||
def _start_connector(ep, description, is_relay=False):
|
def _start_connector(ep, description, is_relay=False):
|
||||||
d = defer.Deferred()
|
d = defer.Deferred()
|
||||||
if ep == "direct":
|
if ep == "direct":
|
||||||
|
|
|
@ -79,6 +79,10 @@ def build_relay_handshake(key):
|
||||||
token = HKDF(key, 32, CTXinfo=b"transit_relay_token")
|
token = HKDF(key, 32, CTXinfo=b"transit_relay_token")
|
||||||
return b"please relay "+hexlify(token)+b"\n"
|
return b"please relay "+hexlify(token)+b"\n"
|
||||||
|
|
||||||
|
|
||||||
|
# These namedtuples are "hint objects". The JSON-serializable dictionaries
|
||||||
|
# are "hint dicts".
|
||||||
|
|
||||||
# DirectTCPV1Hint and TorTCPV1Hint mean the following protocol:
|
# DirectTCPV1Hint and TorTCPV1Hint mean the following protocol:
|
||||||
# * make a TCP connection (possibly via Tor)
|
# * make a TCP connection (possibly via Tor)
|
||||||
# * send the sender/receiver handshake bytes first
|
# * send the sender/receiver handshake bytes first
|
||||||
|
@ -92,7 +96,7 @@ TorTCPV1Hint = namedtuple("TorTCPV1Hint", ["hostname", "port"])
|
||||||
# the rest of the V1 protocol. Only one hint per relay is useful.
|
# the rest of the V1 protocol. Only one hint per relay is useful.
|
||||||
RelayV1Hint = namedtuple("RelayV1Hint", ["hints"])
|
RelayV1Hint = namedtuple("RelayV1Hint", ["hints"])
|
||||||
|
|
||||||
def describe_hint(hint):
|
def describe_hint_obj(hint):
|
||||||
if isinstance(hint, DirectTCPV1Hint):
|
if isinstance(hint, DirectTCPV1Hint):
|
||||||
return u"tcp:%s:%d" % (hint.hostname, hint.port)
|
return u"tcp:%s:%d" % (hint.hostname, hint.port)
|
||||||
elif isinstance(hint, TorTCPV1Hint):
|
elif isinstance(hint, TorTCPV1Hint):
|
||||||
|
@ -100,7 +104,7 @@ def describe_hint(hint):
|
||||||
else:
|
else:
|
||||||
return str(hint)
|
return str(hint)
|
||||||
|
|
||||||
def parse_hint_tcp(hint):
|
def parse_hint_argv(hint):
|
||||||
assert isinstance(hint, type(u""))
|
assert isinstance(hint, type(u""))
|
||||||
# return tuple or None for an unparseable hint
|
# return tuple or None for an unparseable hint
|
||||||
mo = re.search(r'^([a-zA-Z0-9]+):(.*)$', hint)
|
mo = re.search(r'^([a-zA-Z0-9]+):(.*)$', hint)
|
||||||
|
@ -573,11 +577,11 @@ class Common:
|
||||||
if transit_relay:
|
if transit_relay:
|
||||||
if not isinstance(transit_relay, type(u"")):
|
if not isinstance(transit_relay, type(u"")):
|
||||||
raise UsageError
|
raise UsageError
|
||||||
relay = RelayV1Hint(hints=[parse_hint_tcp(transit_relay)])
|
relay = RelayV1Hint(hints=[parse_hint_argv(transit_relay)])
|
||||||
self._transit_relays = [relay]
|
self._transit_relays = [relay]
|
||||||
else:
|
else:
|
||||||
self._transit_relays = []
|
self._transit_relays = []
|
||||||
self._their_direct_hints = []
|
self._their_direct_hints = [] # hintobjs
|
||||||
self._their_relay_hints = []
|
self._their_relay_hints = []
|
||||||
self._tor_manager = tor_manager
|
self._tor_manager = tor_manager
|
||||||
self._transit_key = None
|
self._transit_key = None
|
||||||
|
@ -658,7 +662,7 @@ class Common:
|
||||||
self._listener_d.addErrback(lambda f: None)
|
self._listener_d.addErrback(lambda f: None)
|
||||||
self._listener_d.cancel()
|
self._listener_d.cancel()
|
||||||
|
|
||||||
def _parse_tcp_v1_hint(self, hint):
|
def _parse_tcp_v1_hint(self, hint): # hint_struct -> hint_obj
|
||||||
hint_type = hint.get(u"type", u"")
|
hint_type = hint.get(u"type", u"")
|
||||||
if hint_type not in [u"direct-tcp-v1", u"tor-tcp-v1"]:
|
if hint_type not in [u"direct-tcp-v1", u"tor-tcp-v1"]:
|
||||||
log.msg("unknown hint type: %r" % (hint,))
|
log.msg("unknown hint type: %r" % (hint,))
|
||||||
|
@ -676,12 +680,12 @@ class Common:
|
||||||
return TorTCPV1Hint(hint[u"hostname"], hint[u"port"])
|
return TorTCPV1Hint(hint[u"hostname"], hint[u"port"])
|
||||||
|
|
||||||
def add_connection_hints(self, hints):
|
def add_connection_hints(self, hints):
|
||||||
for h in hints:
|
for h in hints: # hint structs
|
||||||
hint_type = h.get(u"type", u"")
|
hint_type = h.get(u"type", u"")
|
||||||
if hint_type in [u"direct-tcp-v1", u"tor-tcp-v1"]:
|
if hint_type in [u"direct-tcp-v1", u"tor-tcp-v1"]:
|
||||||
dh = self._parse_tcp_v1_hint(h)
|
dh = self._parse_tcp_v1_hint(h)
|
||||||
if dh:
|
if dh:
|
||||||
self._their_direct_hints.append(dh)
|
self._their_direct_hints.append(dh) # hint_obj
|
||||||
elif hint_type == u"relay-v1":
|
elif hint_type == u"relay-v1":
|
||||||
# TODO: each relay-v1 clause describes a different relay,
|
# TODO: each relay-v1 clause describes a different relay,
|
||||||
# with a set of equally-valid ways to connect to it. Treat
|
# with a set of equally-valid ways to connect to it. Treat
|
||||||
|
@ -769,14 +773,14 @@ class Common:
|
||||||
contenders.append(self._listener_d)
|
contenders.append(self._listener_d)
|
||||||
relay_delay = 0
|
relay_delay = 0
|
||||||
|
|
||||||
for hint in self._their_direct_hints:
|
for hint_obj in self._their_direct_hints:
|
||||||
# Check the hint type to see if we can support it (e.g. skip
|
# Check the hint type to see if we can support it (e.g. skip
|
||||||
# onion hints on a non-Tor client). Do not increase relay_delay
|
# onion hints on a non-Tor client). Do not increase relay_delay
|
||||||
# unless we have at least one viable hint.
|
# unless we have at least one viable hint.
|
||||||
ep = self._endpoint_from_hint(hint)
|
ep = self._endpoint_from_hint_obj(hint_obj)
|
||||||
if not ep:
|
if not ep:
|
||||||
continue
|
continue
|
||||||
description = "->%s" % describe_hint(hint)
|
description = "->%s" % describe_hint_obj(hint_obj)
|
||||||
d = self._start_connector(ep, description)
|
d = self._start_connector(ep, description)
|
||||||
contenders.append(d)
|
contenders.append(d)
|
||||||
relay_delay = self.RELAY_DELAY
|
relay_delay = self.RELAY_DELAY
|
||||||
|
@ -787,11 +791,11 @@ class Common:
|
||||||
# resolve quickly. Many direct hints will be to unused local-network
|
# resolve quickly. Many direct hints will be to unused local-network
|
||||||
# IP addresses, which won't answer, and would take the full TCP
|
# IP addresses, which won't answer, and would take the full TCP
|
||||||
# timeout (30s or more) to fail.
|
# timeout (30s or more) to fail.
|
||||||
for hint in self._their_relay_hints:
|
for hint_obj in self._their_relay_hints:
|
||||||
ep = self._endpoint_from_hint(hint)
|
ep = self._endpoint_from_hint_obj(hint_obj)
|
||||||
if not ep:
|
if not ep:
|
||||||
continue
|
continue
|
||||||
description = "->relay:%s" % describe_hint(hint)
|
description = "->relay:%s" % describe_hint_obj(hint_obj)
|
||||||
d = task.deferLater(self._reactor, relay_delay,
|
d = task.deferLater(self._reactor, relay_delay,
|
||||||
self._start_connector, ep, description,
|
self._start_connector, ep, description,
|
||||||
is_relay=True)
|
is_relay=True)
|
||||||
|
@ -822,7 +826,7 @@ class Common:
|
||||||
d.addCallback(lambda p: p.startNegotiation())
|
d.addCallback(lambda p: p.startNegotiation())
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def _endpoint_from_hint(self, hint):
|
def _endpoint_from_hint_obj(self, hint):
|
||||||
if self._tor_manager:
|
if self._tor_manager:
|
||||||
if isinstance(hint, (DirectTCPV1Hint, TorTCPV1Hint)):
|
if isinstance(hint, (DirectTCPV1Hint, TorTCPV1Hint)):
|
||||||
# our TorManager will return None for non-public IPv4
|
# our TorManager will return None for non-public IPv4
|
||||||
|
|
Loading…
Reference in New Issue
Block a user