Skip to content

Commit

Permalink
Merge pull request #329 from meejah/ticket293-purpose-circ
Browse files Browse the repository at this point in the history
Ticket293 purpose circ
  • Loading branch information
meejah committed Dec 12, 2018
2 parents 1af0983 + 32a8301 commit 5d82d07
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/releases.rst
Expand Up @@ -23,6 +23,7 @@ unreleased

* add :func:`TorControlProtocol.when_disconnected` (will replace `.on_disconnect`)
* add `detach=` kwarg to :func:`Tor.create_onion_service`
* add `purpose=` kwarg to :func:`TorState.build_circuit`


v18.3.0
Expand Down
29 changes: 27 additions & 2 deletions test/test_torstate.py
Expand Up @@ -1399,7 +1399,6 @@ def close(self):
circ = FakeCircuit()

def _build(*args, **kw):
print("DING {} {}".format(args, kw))
return defer.succeed(circ)
self.state.build_circuit = _build

Expand All @@ -1411,7 +1410,6 @@ def _build(*args, **kw):
# be closed
d = build_timeout_circuit(self.state, clock, path, timeout, using_guards=False)
clock.advance(1)
print("DING {}".format(self.state))
d.cancel()

with self.assertRaises(CircuitBuildTimedOutError):
Expand Down Expand Up @@ -1509,3 +1507,30 @@ def check_reason(fail):
d.addErrback(check_reason)

return d

def test_build_circuit_with_purpose(self):
class FakeRouter:
def __init__(self, i):
self.id_hex = i
self.flags = []

path = []
for x in range(3):
path.append(FakeRouter("$%040d" % x))
path[0].flags = ['guard']

d = self.state.build_circuit(path, using_guards=True, purpose="general")
d.addCallback(self.circuit_callback)

self.assertEqual(self.transport.value(), b'EXTENDCIRCUIT 0 0000000000000000000000000000000000000000,0000000000000000000000000000000000000001,0000000000000000000000000000000000000002 purpose=general\r\n')
self.send(b"250 EXTENDED 1234")
# we can't just .send(b'650 CIRC 1234 BUILT') this because we
# didn't fully hook up the protocol to the state, e.g. via
# post_bootstrap etc.
self.state.circuits[1234].update(['1234', 'FAILED', 'REASON=TIMEOUT'])

def check_reason(fail):
self.assertEqual(fail.value.reason, 'TIMEOUT')
d.addErrback(check_reason)

return d
5 changes: 4 additions & 1 deletion txtorcon/torstate.py
Expand Up @@ -545,7 +545,7 @@ def _find_circuit_after_extend(self, x):
circ.update([str(circ_id), 'EXTENDED'])
return circ

def build_circuit(self, routers=None, using_guards=True):
def build_circuit(self, routers=None, using_guards=True, purpose=None):
"""
Builds a circuit consisting of exactly the routers specified,
in order. This issues an EXTENDCIRCUIT call to Tor with all
Expand Down Expand Up @@ -586,6 +586,9 @@ def build_circuit(self, routers=None, using_guards=True):
cmd += router.decode('utf8')
else:
cmd += router.id_hex[1:]

if purpose is not None:
cmd += " purpose={}".format(purpose)
d = self.protocol.queue_command(cmd)
d.addCallback(self._find_circuit_after_extend)
return d
Expand Down

0 comments on commit 5d82d07

Please sign in to comment.