I'm using the stem
to control a tor node created with stem.process.launch_tor_with_config
. I've also created a stem.control.Controller
that is operating on the aforementioned process' control port.
How can I change the exit node? I looked at stem.controller.Controller.new_circuit
, but this appears to change the intermediate nodes, preserving the endpoint.
Does anybody know how this could be done?
Thanks!
Edit:
So I think I may be misunderstanding something fundamental, but I can't seem to wrap my head around it. I tried calling Controller.get_circuits()
and found a list of CircuitEvent
objects. Does this mean that a single process can handle multiple circuits? If so, how do I select one for use?
Note that I'm directing HTTP requests to through Privoxy, which in turn is forwarding it to the tor process' SOCKS port.
Edit 2:
I found something that works, but I don't know how it works, which worries me. I'll gladly award an answer to anyone who can either:
Here's what I've done:
for circuit in controller.get_circuits():
controller.close_circuit(circuit.id)
There it is. The external IP changed, so I know I've done something but hell if I know exactly what.
Open the folder where you have installed Tor, browse through browser > Tor browser > Tor > data. There you will find a file named "torrc.". Open the file with Notepad. At the end of the document, on a new line, write this: ExitNodes {US}.
Double-click the Tor Browser folder to open it. Mac: Open Finder, press Command + Shift + G, and enter or paste this address into the field: ~/Library/Application Support/TorBrowser-Data . Click Go to open the folder. Navigate to the "torrc" file. This is the configuration file you'll need to edit. To find it:
We will use python's stem library released by official tor project to communicate with TOR process using control file. You can install stem library with pip in virtual environment (recommended). TOR requires SOCKS proxy for the communication.
Though TOR give us ability to renew IP address, this feature should not be abused as changing IP address not only puts high load on the tor network but also could endanger tor endpoints due to potential blacklisting by sites if used in abusive way.
You have a couple options to use a specific exit...
https://stem.torproject.org/tutorials/to_russia_with_love.html
https://stem.torproject.org/api/control.html#stem.control.Controller.extend_circuit
If the question you're trying to ask is really 'how do I get a new IP address' then that's a question we're more reluctant to answer. Partly because it's primarily for ban evasion or SEO, and partly because repeated circuit creation puts a high load on the Tor network.
As for why your IP seems to change when you call close_circuit(), that's because Tor then needs to recreate a new circuit on your behalf for the following request. There is no guarantee that the IP will be new, and doing so involves a fair bit of traffic to telescope your connection through three fresh hops.
I'm not often on StackOverflow so if you have further questions about scripting against Tor then I would suggest the tor-dev@ email list...
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-dev/
Cheers! -Damian (stem's author)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With