2

In stem.process.launch_tor_with_config How can we specify the circuit to be used by Tor?

surbhi
  • 387
  • 5
  • 16

1 Answers1

0

In the config dict for launch_tor_with_config(), you can specify which entry and exit nodes to use/exclude, as well as excluding specific relays from being used in circuits (assuming StrictNodes == 1). But there is no way to define specific circuits to use from within the config there.

You can use the extend_circuit() function from the stem.Controller library to create circuits with a specified path, listing the relay fingerprints you want the circuit to use.

To define your own circuits and ensure that Tor doesn't automatically replace them:

(1) In the config that you use to launch_tor_with_config(), set NewCircuitPeriod and MaxCircuitDirtiness to very high values, so that circuits are not automatically destroyed. The default is that they are marked dirty and not used for new connections 600 seconds (10 mins) after their first use. This means that your custom circuits will likely be automatically replaced after 10 mins, unless you tell Tor not to. The maximum value for MaxCircuitDirtiness is 30 days. The config option MaxCircuitDirtiness is specified in seconds, so this would be 60⋅60⋅24⋅30 = 2592000 seconds. If your application needed to run continuously for longer than 30 days, you would have to manually destroy/create new circuits every month or so ...

(2) Create the desired circuits with extend_circuit(). As described in the documentation, you basically just provide a list of relay fingerprints to define the path you want for the circuit. For example:

controller.extend_circuit('0', ['718BCEA286B531757ACAFF93AE04910EA73DE617',
                                '30BAB8EE7606CBD12F3CC269AE976E0153E7A58D',
                                '2765D8A8C4BBA3F89585A9FFE0E8575615880BEB'])

... the first argument '0' tells it to create a new circuit using the path specified in the list.

(3) close all other circuits that were created by default with Stem.controller.close_circuit()

If you do the above, the only circuits that should exist are the ones with the paths you created, and they would not be marked dirty and get replaced for 30 days.

J. Taylor
  • 4,567
  • 3
  • 35
  • 55