4

I am currently using the following set-up to change my ip address in Mac OS X:

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9051) as controller:
    controller.authenticate()
    controller.signal(Signal.NEWNYM)

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.1'
}

proxies = {
    "http": "http://127.0.0.1:8118"
}

r_ip = requests.get("http://icanhazip.com", proxies=proxies, headers=headers_tor)
print(r_ip.text)

I first run Tor in my command line, then execute the above. I have noticed that the IP address doesn't change immediately but rather several seconds needs to go by before running the chunk of code above to generate a new ip address. Furthermore, on the terminal page where I am running Tor, it outputs messages like:

Aug 25 04:13:53.000 [notice] Rate limiting NEWNYM request: delaying by 7 second(s)

Is there a way to change the IP address without a lag?

user321627
  • 1,916
  • 1
  • 11
  • 31

1 Answers1

5

According to the Tor control specifications, the NEWNYM signal instructs your tor client to open new circuits. As a result, you will (most likely) get a circuit with a different exit node than before, hence a different IP-address.

This always needs some time and you cannot really speed up the circuit construction itself. What you could do, is changing the "CircuitBuildTimeout" (see the Tor manual) to something else than the default value of 60 seconds, so if it takes longer than the specified amount of time, tor tries to build a different circuit. Note, that this might raise privacy issues since you would prefer fast routers.

The circuit construction always introduces a significant load to the Tor network. To minder this, "tor MAY rate-limit the response" to the NEWNYM signal (section 3.7 of the tor control specification). So you cannot build circuits to often. Apart from changing the source code to disable this mechanism, there is no way to circumvent this intended limitation.

If you need a fixed number of different IP addresses, you could specify the "HTTPTunnelPort" config options multiple times with different port. For this, you need to add following lines to your torrc file (usually at /etc/tor/torrc for linux.. For Mac, the file seems to be a bit harder to find as descirbed here):

HTTPTunnelPort 8118
HTTPTunnelPort 8119 

etc. Then, tor will open one circuit per port on startup, hence this part takes longer. Your requests will have different IP-address, depending on what port you send them. E.g.: port 8118 -> 5.5.5.5 port 8119 -> 4.4.4.4. To switch between the addresses, change the port in the proxy config in your python script.

This is good for switching between a fixed number of addresses. However, it only changes when to wait for the circuit construction. For changing after each request, using NEWNYM seems better.

Additionally, you can use stem's get_newnym_wait or is_newnym_available to see if tor allows you to build a new circuit and if not, how long you would need to wait.

Julian
  • 76
  • 3
  • 1
    thanks for your response, this is really really great. been looking everywhere for something like this. i have a question for you: I actually need a fixed number of ip addresses to begin with. with the "HTTPTunnelPort" config, could you elaborate how it works? will it be first "saving" all the addresses, then using them one by one? – user321627 Aug 26 '18 at 23:38
  • Your welcome! I modified the answer to show the config in more detail. May I ask you what exactly you want to achieve? If you want to switch between multiple IP's, allowing reuse of them, it seems to be a good idea to open multiple ports. If you want to use a different IP for each request, it is not. Then, it is probably better to use newnym. – Julian Aug 27 '18 at 19:33