Aviril Smith
Aviril Smith

Reputation: 19

Why does TorRequests fail with "Tunnel connection failed: 400 Bad Request

I'm trying to use the TorRequests library to make an HTTPS GET request through the Tor network. Here is the Python script I'm using:

from torpy.http.requests import TorRequests

with TorRequests() as tor_requests:
    with tor_requests.get_session() as sess:
        r = sess.get('https://socialearning.org/').text
        print(r)

However, I encounter the following error when trying to access https websites when using the below script:

Traceback (most recent call last):
  File "/root/frst/lib/python3.12/site-packages/urllib3/connectionpool.py", line 766, in urlopen
    conn = self._get_conn(timeout=pool_timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/urllib3/connectionpool.py", line 292, in _get_conn
    return conn or self._new_conn()
                   ^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/torpy/http/adapter.py", line 111, in _new_conn
    return self._prepare_proxy(conn) #self._prepare_conn(conn)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/urllib3/connectionpool.py", line 1042, in _prepare_proxy
    conn.connect()
  File "/root/frst/lib/python3.12/site-packages/urllib3/connection.py", line 721, in connect
    self._tunnel()
  File "/usr/lib/python3.12/http/client.py", line 979, in _tunnel
    raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
OSError: Tunnel connection failed: 400 Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/frst/lib/python3.12/site-packages/requests/adapters.py", line 667, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/urllib3/connectionpool.py", line 841, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/urllib3/util/retry.py", line 474, in increment
    raise reraise(type(error), error, _stacktrace)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/urllib3/util/util.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/root/frst/lib/python3.12/site-packages/urllib3/connectionpool.py", line 766, in urlopen
    conn = self._get_conn(timeout=pool_timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/urllib3/connectionpool.py", line 292, in _get_conn
    return conn or self._new_conn()
                   ^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/torpy/http/adapter.py", line 111, in _new_conn
    return self._prepare_proxy(conn) #self._prepare_conn(conn)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/urllib3/connectionpool.py", line 1042, in _prepare_proxy
    conn.connect()
  File "/root/frst/lib/python3.12/site-packages/urllib3/connection.py", line 721, in connect
    self._tunnel()
  File "/usr/lib/python3.12/http/client.py", line 979, in _tunnel
    raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError('Tunnel connection failed: 400 Bad Request'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/linux_folder/kali-anonsurf/lab.py", line 4, in <module>
    r = sess.get('https://socialearning.org/').text
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/requests/sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/frst/lib/python3.12/site-packages/requests/adapters.py", line 682, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', OSError('Tunnel connection failed: 400 Bad Request'))

Steps I've Taken:

  1. Verified that the Tor service is running on my machine.

  2. Ensured that the TorRequests library is installed and up-to-date.

  3. Checked that I can manually connect to Tor using other tools (like curl or torsocks).

  4. Change self._prepare_conn(conn) to self._prepare_proxy(conn) in File: "/root/frst/lib/python3.12/site-packages/torpy/http/adapter.py"

Questions:

  1. What could cause the "400 Bad Request" error in this context

  2. Is there a specific configuration or dependency that needs to be adjusted for TorRequests to work properly

  3. Are there any alternative methods to achieve this functionality if TorRequests is problematic

Upvotes: 0

Views: 51

Answers (0)

Related Questions