Multiple instances of tor.exe, each with own identity via python

Question:

I’m testing my own ddos protection feature implemented in my server (this is necessary). Currently I have a terrible loop for making multiple tor requests, each with it’s own identity.

os.system("taskkill /f /im tor.exe")
os.startfile("C:/Tor/Browser/TorBrowser/Tor/tor.exe")

session = requests.session()
session.proxies = {}
session.proxies['http'] = 'socks5h://localhost:9050'
session.proxies['https'] = 'socks5h://localhost:9050'

Now I want to multithread this for faster speeds, since each tor connection takes ages to load.

If I google how to run multiple tor instances, I get info on how to do this from within the tor browser itself, never how to do it programmatically, Is there a way to do this on windows python3 specifically?

Any help appreciated

Asked By: BinkyNichols

||

Answers:

The key point to understand about running multiple separate Tor processes is that each one will need to listen on it’s own ControlPort and SocksPort so that your clients can issue requests through each individual instance.

If you use Stem, stem.process.launch_tor_with_config would be the recommended way to launch multiple Tor processes. By using this method, you can pass the necessary config options dynamically to each client without having to create individual files, and you’ll have better process management over the Tor instances.

If you want to use os, you will need to create one config file per instance and pass that to tor when you start it.

At minimum, create one torrc config file for each instance you want to run with the following:

torrc.1

ControlPort 9800
SocksPort 9801

torrc.2

ControlPort 9802
SocksPort 9803

Each individual client will connect on the different socks ports to issue requests.

To start them, use:

os.system("C:/Tor/Browser/TorBrowser/Tor/tor.exe -f C:/path/to/torrc.1")
os.system("C:/Tor/Browser/TorBrowser/Tor/tor.exe -f C:/path/to/torrc.2")

Then create one or more clients per instance:

session1 = requests.session()
session1.proxies = {}
session1.proxies['http'] = 'socks5h://localhost:9801'
session1.proxies['https'] = 'socks5h://localhost:9801'

session2 = requests.session()
session2.proxies = {}
session2.proxies['http'] = 'socks5h://localhost:9803'
session2.proxies['https'] = 'socks5h://localhost:9803'
Answered By: drew010

first of All, install stem like this in terminal

>>>pip install stem

write this code in a text file and rename the file like this myfile.py
include stem and requests first like this in start of file and write following code

import requests
import stem.process
x = 6
for i in range(1, x):
    cp = str(10000+i)
    sp = str(11000+i)
    
    tp1 = stem.process.launch_tor_with_config(tor_cmd = 'C:\Users\<Tor Directory>\Browser\TorBrowser\Tor\tor.exe', config = {
        'ControlPort': cp,
        'SocksPort' : sp,
        'DataDirectory': 'C:/<Any Path for data directories>/proxies/'+str(i)+'/',
        'Log': [
          'NOTICE stdout',
          'ERR file C:/<Any Path for Error file>/tor_error_log.txt',
        ],
      },
    )
    proxies = {
        'http': 'socks5h://127.0.0.1:'+str(sp),
        'https': 'socks5h://127.0.0.1:'+str(sp)
    }
    r1 = requests.get('http://ipinfo.io/json', proxies=proxies)
    print('n')
    print(r1.content)
    print('n')

now go into the folder that contains myfile.py and run command prompt(cmd) or any terminal there and launch the file like this.

>>>python myfile.py

this will launch 5 tor processes on these ports 11001,11002,11003,11004,11005
you can access the tor proxy(socks5) by using ip address 127.0.0.1 and any of the above ports from any program.

if you open task manager you will see 5 tor processes running that consumes 10-20mb of ram each process

if you get an error like this while running myfile.py in terminal,

can not bind listening port. working with config files left us in broken state. Dying

then just close all processes of tor and launch myfile.py again. this error happens because you have already a tor process running on the port.

to create more tor processes, close all tor instances from task manager, change the value of variable x in start of file like this

x = any integer like 10,20,30,50

save myfile.py and run this file again.

cheers!

Answered By: Haider Abbas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.