torpyorg / torpy

Pure python Tor client implementation
Apache License 2.0
411 stars 53 forks source link

torpy.cell_socket.TorSocketConnectError: timed out #12

Closed gmagno closed 3 years ago

gmagno commented 4 years ago

Hello, hope you are doing great during these times.

I was wondering if this is an issue with my setup or some bug :thinking: Thanks in advance.

$ pipx install torpy
  installed package torpy 1.1.3, Python 3.8.6
  These apps are now globally available
    - torpy_cli
    - torpy_socks
done! ✨ 🌟 ✨
$ torpy_cli --url https://ifconfig.me --header "User-Agent" "curl/7.37.0"                                                289ms  Tue 13 Oct 2020 13:59:41 UTC
Downloading new consensus from moria1 authority
Connecting to guard node 128.31.0.39:9101 (moria1; None)...
timed out
[ignored]
Traceback (most recent call last):
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/cell_socket.py", line 63, in connect
    self._socket.connect((self._router.ip, self._router.or_port))
  File "/usr/lib/python3.8/ssl.py", line 1342, in connect
    self._real_connect(addr, False)
  File "/usr/lib/python3.8/ssl.py", line 1329, in _real_connect
    super().connect(addr)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/utils.py", line 78, in newfn
    return func(*args, **kwargs)
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 163, in renew
    raw_string = self._authorities.download_consensus(prev_hash)
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 105, in download_consensus
    with TorGuard(authority) as guard:
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/guard.py", line 65, in __init__
    self.__tor_socket.connect()
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/cell_socket.py", line 69, in connect
    raise TorSocketConnectError(e)
torpy.cell_socket.TorSocketConnectError: timed out
Retry with another authority...
Downloading new consensus from Serge authority
Connecting to guard node 66.111.2.131:9001 (Serge; None)...
Creating new circuit #80000001 with 66.111.2.131:9001 (Serge; None) router...
Building 0 hops circuit...
Creating stream #1 attached to #80000001 circuit...
Stream #1: connecting to hsdir
Stream #1: connected (remote ip '')
Stream #1: remote disconnected (reason = DONE)
Stream #1: closing (state = Disconnected)...
Destroy circuit #80000001
Closing guard connections...
Downloading fp-sk from dizum
Connecting to guard node 45.66.33.45:443 (dizum; None)...
Creating new circuit #80000002 with 45.66.33.45:443 (dizum; None) router...
Building 0 hops circuit...
Creating stream #2 attached to #80000002 circuit...
Stream #2: connecting to hsdir
Stream #2: connected (remote ip '')
Stream #2: remote disconnected (reason = DONE)
Stream #2: closing (state = Disconnected)...
Destroy circuit #80000002
Closing guard connections...
Downloading fp-sk from moria1
Connecting to guard node 128.31.0.39:9101 (moria1; None)...
timed out
[ignored]
Traceback (most recent call last):
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/cell_socket.py", line 63, in connect
    self._socket.connect((self._router.ip, self._router.or_port))
  File "/usr/lib/python3.8/ssl.py", line 1342, in connect
    self._real_connect(addr, False)
  File "/usr/lib/python3.8/ssl.py", line 1329, in _real_connect
    super().connect(addr)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/utils.py", line 78, in newfn
    return func(*args, **kwargs)
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 175, in renew
    if not self.verify(new_doc):
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 200, in verify
    pubkey = self._get_pubkey(sign['identity'], sign['signing_key_digest'])
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 206, in _get_pubkey
    key_certificate = self._authorities.download_fp_sk(identity, signing_key_digest)
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 121, in download_fp_sk
    with TorGuard(authority) as guard:
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/guard.py", line 65, in __init__
    self.__tor_socket.connect()
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/cell_socket.py", line 69, in connect
    raise TorSocketConnectError(e)
torpy.cell_socket.TorSocketConnectError: timed out
Retry with another authority...
Downloading new consensus from Faravahar authority
Connecting to guard node 154.35.175.225:443 (Faravahar; None)...
Creating new circuit #80000003 with 154.35.175.225:443 (Faravahar; None) router...
Building 0 hops circuit...
Creating stream #3 attached to #80000003 circuit...
Stream #3: connecting to hsdir
Stream #3: connected (remote ip '')
Stream #3: remote disconnected (reason = DONE)
Stream #3: closing (state = Disconnected)...
Destroy circuit #80000003
Closing guard connections...
Downloading fp-sk from dannenberg
Connecting to guard node 193.23.244.244:443 (dannenberg; None)...
Creating new circuit #80000004 with 193.23.244.244:443 (dannenberg; None) router...
Building 0 hops circuit...
Creating stream #4 attached to #80000004 circuit...
Stream #4: connecting to hsdir
Stream #4: connected (remote ip '')
Stream #4: remote disconnected (reason = DONE)
Stream #4: closing (state = Disconnected)...
Destroy circuit #80000004
Closing guard connections...
Downloading fp-sk from longclaw
Connecting to guard node 199.58.81.140:443 (longclaw; None)...
Creating new circuit #80000005 with 199.58.81.140:443 (longclaw; None) router...
Building 0 hops circuit...
Creating stream #5 attached to #80000005 circuit...
Stream #5: connecting to hsdir
Stream #5: connected (remote ip '')
Stream #5: remote disconnected (reason = DONE)
Stream #5: closing (state = Disconnected)...
Destroy circuit #80000005
Closing guard connections...
Downloading fp-sk from tor26
Connecting to guard node 86.59.21.38:443 (tor26; None)...
timed out
[ignored]
Traceback (most recent call last):
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/cell_socket.py", line 63, in connect
    self._socket.connect((self._router.ip, self._router.or_port))
  File "/usr/lib/python3.8/ssl.py", line 1342, in connect
    self._real_connect(addr, False)
  File "/usr/lib/python3.8/ssl.py", line 1329, in _real_connect
    super().connect(addr)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/utils.py", line 78, in newfn
    return func(*args, **kwargs)
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 175, in renew
    if not self.verify(new_doc):
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 200, in verify
    pubkey = self._get_pubkey(sign['identity'], sign['signing_key_digest'])
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 206, in _get_pubkey
    key_certificate = self._authorities.download_fp_sk(identity, signing_key_digest)
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 121, in download_fp_sk
    with TorGuard(authority) as guard:
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/guard.py", line 65, in __init__
    self.__tor_socket.connect()
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/cell_socket.py", line 69, in connect
    raise TorSocketConnectError(e)
torpy.cell_socket.TorSocketConnectError: timed out
Retry with another authority...
Traceback (most recent call last):
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/cell_socket.py", line 63, in connect
    self._socket.connect((self._router.ip, self._router.or_port))
  File "/usr/lib/python3.8/ssl.py", line 1342, in connect
    self._real_connect(addr, False)
  File "/usr/lib/python3.8/ssl.py", line 1329, in _real_connect
    super().connect(addr)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/gm/.local/bin/torpy_cli", line 8, in <module>
    sys.exit(main())
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/cli/console.py", line 59, in main
    data = args.request_func(args.url, method=args.method, data=args.data, headers=args.headers, hops=args.hops,
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/http/urlopener.py", line 142, in do_request
    with tor_opener(hops_count=hops, auth_data=auth_data, debuglevel=verbose) as opener:
  File "/usr/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/http/urlopener.py", line 136, in tor_opener
    tor = TorClient(auth_data=auth_data)
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/client.py", line 37, in __init__
    self._consensus = consensus or TorConsensus()
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 146, in __init__
    self.renew()
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/utils.py", line 78, in newfn
    return func(*args, **kwargs)
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 175, in renew
    if not self.verify(new_doc):
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 200, in verify
    pubkey = self._get_pubkey(sign['identity'], sign['signing_key_digest'])
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 206, in _get_pubkey
    key_certificate = self._authorities.download_fp_sk(identity, signing_key_digest)
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/consesus.py", line 121, in download_fp_sk
    with TorGuard(authority) as guard:
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/guard.py", line 65, in __init__
    self.__tor_socket.connect()
  File "/home/gm/.local/pipx/venvs/torpy/lib/python3.8/site-packages/torpy/cell_socket.py", line 69, in connect
    raise TorSocketConnectError(e)
torpy.cell_socket.TorSocketConnectError: timed out
jbrown299 commented 4 years ago

Does the error happen all the time or only sometimes? For me everything works fine. But I see some improvements for client to minimize this errors. Will do a little bit later.

gmagno commented 4 years ago

Now that I'm connected to an European ISP it seems to be working fine. In Brazil it was failing consistently.

Could this be a coincidence? Or some limitations imposed by the ISP?

jbrown299 commented 3 years ago

So yeah, that is tor network blocking problem. I implemented some optimization for caching circuits to minimize connections. Rest of this problem will be resolved with bridges support (https://github.com/torpyorg/torpy/issues/7).