webfp / tor-browser-selenium

Tor Browser automation with Selenium.
MIT License
548 stars 100 forks source link

Use preffered exit node #158

Closed askumar2903 closed 10 months ago

askumar2903 commented 3 years ago

I edited the torrc file and added " ExitNodes {us} StrictNodes 1 ". But when launching TB using selenium it does not work. Do you have any other ways with which we can pick our preferred exit node?

I also tried to set the torrc_path in tbdriver.py even that did not do anything.

gunesacar commented 2 years ago

@askumar2903 Thanks for reporting and sorry for the delay in responding.

If you are okay with using system tor, the simplest method would be configure system tor to use the preferred exit country. If that wouldn't help with your use case, you may configure and start new a tor process using Stem. Stem lets you configure tor pretty extensively.

I'll work writing an example script to demonstrate the Stem method.

t0m4s commented 2 years ago

@askumar2903 : if you're using launch_tbb_tor_with_stem(), the modifications done in your torrc file won't be taken in account. Here is an example to pass the parameters you want to stem :

socks_port = free_port()
control_port = free_port()
tor_data_dir = tempfile.mkdtemp()

torrc = {'ControlPort': str(control_port),
    'SOCKSPort': str(socks_port),
    'DataDirectory': tor_data_dir,
    'ExitNodes': TheExitNodeYouWant}
TBB_PATH = YouPathTo/tor-browser_en-US/
GECKO = YourPathTo/geckodriver
tor_binary = join(TBB_PATH, cm.DEFAULT_TOR_BINARY_PATH)
tor_process = launch_tbb_tor_with_stem(tbb_path=TBB_PATH, torrc=torrc, tor_binary=tor_binary)
Controller.from_port(port=control_port).authenticate()
driver = TorBrowserDriver(TBB_PATH, socks_port=socks_port, control_port=control_port, tor_cfg=cm.USE_STEM, executable_path=GECKO, tbb_logfile_path='/dev/null')

To exit cleanly :

driver.close()
tor_process.kill()
shutil.rmtree(tor_data_dir)