1

我正在使用 Windows 10。我正在运行 Tor Win32 服务。

这是我来自 C:...\Tor Browser\Browser\TorBrowser\Data\Tor\torrc 的 torrc 文件

ControlPort 9051
CookieAuthentication 1
CacheDirectoryGroupReadable 1

这是我的 python 3.9 代码:

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9051) as controller:
  controller.authenticate()
  controller.signal(Signal.NEWNYM)

运行脚本会生成此堆栈跟踪:

Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "C:\Users\Shaun\PycharmProjects\scm-hunter-analysis\venv\lib\site-packages\stem\control.py", line 1112, in authenticate
    stem.connection.authenticate(self, *args, **kwargs)
  File "C:\Users\Shaun\PycharmProjects\scm-hunter-analysis\venv\lib\site-packages\stem\connection.py", line 629, in authenticate
    raise auth_exc
  File "C:\Users\Shaun\PycharmProjects\scm-hunter-analysis\venv\lib\site-packages\stem\connection.py", line 591, in authenticate
    authenticate_safecookie(controller, cookie_path, False)
  File "C:\Users\Shaun\PycharmProjects\scm-hunter-analysis\venv\lib\site-packages\stem\connection.py", line 904, in authenticate_safecookie
    cookie_data = _read_cookie(cookie_path, True)
  File "C:\Users\Shaun\PycharmProjects\scm-hunter-analysis\venv\lib\site-packages\stem\connection.py", line 1085, in _read_cookie
    raise UnreadableCookieFile(exc_msg, cookie_path, is_safecookie)
stem.connection.UnreadableCookieFile: Authentication failed: '/run/tor/control.authcookie' doesn't exist

为什么我不能使用 stem 进行身份验证?该错误消息使我感到困惑,因为我不知道运行目录在哪里。

我尝试设置CookieAuthentication 0,重新启动服务,然后重新设置CookieAuthentication 1并重新启动。它会产生相同的错误。

4

1 回答 1

0

这对我有用,首先生成一个密码(例如welcome)和哈希:

$ tor --hash-password welcome
Feb 14 11:31:48.321 [warn] Tor was compiled with zstd 1.4.5, but is running with zstd 1.4.4. For safety, we'll avoid using advanced zstd functionality.
16:05B7B9E8F3D0AB3160E030928F9517EDA5348ECD1CDCE2D95F0D230016

然后转到/etc/tor/torrc,取消注释HashedControlPassword设置并复制上一步生成的哈希:

HashedControlPassword 16:05B7B9E8F3D0AB3160E030928F9517EDA5348ECD1CDCE2D95F0D230016

然后重启服务:

$ sudo service tor restart

最后一步,在控制器验证方法中传递您的新密码Welcome :

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9051) as controller:
    controller.authenticate("welcome")
    controller.signal(Signal.NEWNYM)

然后确保它有效:

import requests

proxies = {'http':  'socks5://127.0.0.1:9050',
           'https': 'socks5://127.0.0.1:9050'}

print("first IP:", requests.get('https://ident.me', proxies=proxies).text)

with Controller.from_port(port = 9051) as controller:
    controller.authenticate("welcome")
    controller.signal(Signal.NEWNYM)
    
print("second IP:", requests.get('https://ident.me', proxies=proxies).text)
于 2021-02-14T10:46:38.567 回答