Python:stem.controller Tor 控制端口配置 Tor 电路以进行流隔离

Python: stem.controller Tor Control port to configure Tor Circuits for Stream Isolation

This is the whonix documentation on stream isolation and tor circuits

This is the whonix documentation on interfacing and controlling Tor via sockets

我之前尝试过 python 请求库向 Tor's official link for checking exit node IP information inside a whonix workstation VM (read about whonix here 发出 GET 请求。请求总是显示相同的 IP 信息,独立于我 运行 脚本,关闭它,再次 运行 它。在第一个 link 中,作者描述了已经支持的应用程序。如果没有列出,用户必须提供支持。

我试图通过首先使用套接字与特定套接字进行通信,然后使用主干库来在我自己的脚本中提供支持。套接字本身没有效果,现在 stem 库在运行时给我错误。

from stem import Signal
from stem.control import Controller

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

此代码给出以下运行时错误:

Traceback (most recent call last):
  File "/path/to/stem/connection.py", line 1018, in get_protocolinfo
    protocolinfo_response = _msg(controller, 'PROTOCOLINFO 1')
  File "/path/to/stem/connection.py", line 1036, in _msg
    return controller.msg(message)
  File "/path/to/stem/control.py", line 658, in msg
    raise response
  File "/path/to/stem/control.py", line 937, in _reader_loop
    control_message = self._socket.recv()
  File "/path/to/stem/socket.py", line 464, in recv
    return self._recv(lambda s, sf: recv_message(sf))
  File "/path/to/stem/socket.py", line 278, in _recv
    return handler(my_socket, my_socket_file)
  File "/path/to/stem/socket.py", line 464, in <lambda>
    return self._recv(lambda s, sf: recv_message(sf))
  File "/path/to/stem/socket.py", line 693, in recv_message
    raise stem.SocketClosed('Received empty socket content.')
stem.SocketClosed: Received empty socket content.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/stem/connection.py", line 530, in authenticate
    protocolinfo_response = get_protocolinfo(controller)
  File "/path/to/stem/connection.py", line 1020, in get_protocolinfo
    raise stem.SocketError(exc)
stem.SocketError: Received empty socket content.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "my_program.py", line 103, in <module>
    controller.authenticate()
  File "/path/to/stem/control.py", line 1100, in authenticate
    stem.connection.authenticate(self, *args, **kwargs)
  File "/path/to/stem/connection.py", line 534, in authenticate
    raise AuthenticationFailure('socket connection failed (%s)' % exc)
stem.connection.AuthenticationFailure: socket connection failed (Received empty socket content.)

如何解决?是否有更好的解决方案来提供我尚未考虑的支持?

无需使用 stem 或连接到控制端口即可为您的电路启用流隔离。流隔离由提供给 Tor 的 SOCKS 代理的凭据决定。

如果您使用的是 Python requests,请参阅

要对连接使用相同的 IP,请在连接到 SOCKS 代理时使用相同的凭据。如果您想更改 IP(使用不同的电路),那么您只需要使用一组不同的凭据。

在下面的示例中,每个请求会话使用不同的电路,并且应该有不同的退出 IP 地址。

session1 = requests.session()
session1.proxies = {'http': 'socks5h://foo1:bar1@localhost:9050', 'https': 'socks5h://foo1:bar1@localhost:9050'}
r = session1.get('https://httpbin.org/ip')

session2 = requests.session()
session2.proxies = {'http': 'socks5h://foo2:bar2@localhost:9050', 'https': 'socks5h://foo2:bar2@localhost:9050'}
r = session2.get('https://httpbin.org/ip')

请记住,出口的数量是有限的,因此如果您快速创建许多流,最终您将构建使用与之前电路相同的出口 IP 的电路。