1

I'm working on a web crawler to generate some web traffic, figured I'll be needing to use tor (reset_identity()) after I visit the desired website. Also I need my connection to exit through Germany specifically. I've been working with couple of libraries (stem and torrequest) each of which I've hit a dead end...

import torrequest
import requests

with torrequest.TorRequest(password=None) as tr:
    response = requests.get('http://ipecho.net/plain')
    print("My Original IP Address:", response.text)

    tr.reset_identity()  # Reset Tor
    response = tr.get('http://ipecho.net/plain')
    print("New Ip Address", response.text)

Works just fine, I get two ip addresses: first is my ip address and the second is the tor exit node. But I still need it to exit through Germany, So I used launch_tor_with_config().

torrequest.launch_tor_with_config(
    tor_cmd='/usr/bin/tor',
    config={
        'ExitNodes': '{DE}'  # exiting through Germany
    }
)

But from here on I have no idea how to handle the get request. I tried:

import requests
import stem.process

tor_process = stem.process.launch_tor_with_config(tor_cmd='/usr/bin/tor',
                                                  config={
                                                      'ExitNodes': '{DE}'  # exiting through Germany
                                                  }
                                                  )

response = tor_process.communicate(requests.get('http://ipecho.net/plain'))
print("New Ip Address", response.text)

And it finishes with code 1:

Traceback (most recent call last):
  File "/root/PycharmProjects/webtraffic/webtraffic1.py", line 10, in <module>
    response = tor_process.communicate(requests.get('http://ipecho.net/plain'))
  File "/usr/lib/python3.7/subprocess.py", line 964, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/usr/lib/python3.7/subprocess.py", line 1667, in _communicate
    self.stdin.flush()
ValueError: flush of closed file

Process finished with exit code 1

I'm using guest kali linux on windows 10 host with python3.7.

Thanks a bunch on advance !!

1 Answers1

1

After about a day and a half of work I've found the solution. I couldn't proceed with launch_tor_with_config() as hard as I've tried. Instead I've gone for a workaround. Unlike what the web says changing?/adding 'ExitNodes': '{de}' in your tor-browser_en-US/Browser/TorBrowser/Data/Tor/torrc won't help, neither will /etc/tor/torrc. What I've done is change the torrequest code, adding 'ExitNodes': '{de}' as follows:

  def _launch_tor(self):
    return launch_tor_with_config(
      config={
        'SocksPort': str(self.proxy_port),
        'ControlPort': str(self.ctrl_port),
        'ExitNodes': '{de}'
      },
      take_ownership=True)

Best regards.