torproject / stem

Python controller library for Tor
https://stem.torproject.org/
GNU Lesser General Public License v3.0
257 stars 75 forks source link

On Windows 10 unable to open cookie auth file from utf8 encoded directory #57

Closed HelloZeroNet closed 3 years ago

HelloZeroNet commented 4 years ago

Stem version: 1.8.0 Python version: 3.7.3 Tor: 0.4.2.6 (git-971a6beff5a53434) running on Windows 8 [or later] with Libevent 2.1.8-stable, OpenSSL 1.1.1d, Zlib 1.2.11, Liblzma N/A, and Libzstd N/A.

Traceback (most recent call last):
  File "C:\Drive-E\Web\Today\ZeroNet-win-dist-win64 árvíztűrő tükörfúrógép\ZeroNet-win-dist-win64\test2.py", line 20, in <module>
    controller.authenticate()
  File "C:\Python3\lib\site-packages\stem\control.py", line 1112, in authenticate
    stem.connection.authenticate(self, *args, **kwargs)
  File "C:\Python3\lib\site-packages\stem\connection.py", line 534, in authenticate
    protocolinfo_response = get_protocolinfo(controller)
  File "C:\Python3\lib\site-packages\stem\connection.py", line 1029, in get_protocolinfo
    stem.response.convert('PROTOCOLINFO', protocolinfo_response)
  File "C:\Python3\lib\site-packages\stem\response\__init__.py", line 124, in convert
    message._parse_message(**kwargs)
  File "C:\Python3\lib\site-packages\stem\response\protocolinfo.py", line 110, in _parse_message
    self.cookie_path = line.pop_mapping(True, True, get_bytes = True)[1].decode(sys.getfilesystemencoding())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 44: invalid continuation byte

Starting tor.exe:

@ c:\Drive-E\Web\Today\ZeroNet-win-dist-win64 árvíztűrő tükörfúrógép\ZeroNet-win-dist-win64\core\tools\tor
$ tor.exe -f torrc --defaults-torrc torrc-defaults --ignore-missing-torrc

torrc-defaults file:

DataDirectory data
HiddenServiceStatistics 0
GeoIPFile geoip\geoip
GeoIPv6File geoip\geoip6

ControlPort 49051
SOCKSPort 49050

CookieAuthentication 1

Script:

import stem
import stem.connection

from stem.control import Controller

controller = Controller.from_port(port=49051)
controller.authenticate()

PROTOCOLINFO result:

import socket

conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

conn.connect(("127.0.0.1", 49051))

conn.sendall(b"PROTOCOLINFO\r\n")
print(conn.recv(1024 * 64).decode("utf8"))
250-PROTOCOLINFO 1

250-AUTH METHODS=COOKIE,SAFECOOKIE COOKIEFILE="c:\\Drive-E\\Web\\Today\\ZeroNet-win-dist-win64 \341rv\355zt\373r\365 t\374k\366rf\372r\363g\351p\\ZeroNet-win-dist-win64\\core\\tools\\tor\\data\\control_auth_cookie"

250-VERSION Tor="0.4.2.6"

250 OK
atagar commented 3 years ago

Hi ZeroNet, sorry for taking so long to get to this. The problem is that your filesystem is configured as unicode whereas that path is latin-1. For example, the first non-ascii character is an 'a' with an umlaut...

>>> b"\341".decode('utf-8')  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 0: unexpected end of data

>>> b"\341".decode('latin-1')
'á'

I just pushed a patch so we attempt to parse these paths as both unicode and latin-1 when our filesystem encoding doesn't work.

Thanks for pointing this out!