【问题标题】:Python: stem.controller Tor Control port to configure Tor Circuits for Stream IsolationPython:stem.controller Tor 控制端口,用于配置 Tor 电路以进行流隔离
【发布时间】:2021-09-20 07:06:24
【问题描述】:

This is the whonix documentation on stream isolation and tor circuits

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

我之前曾尝试使用 python 请求库向 whonix 工作站 VM (read about whonix here) 内的Tor's official link for checking exit node IP information 发出 GET 请求。该请求始终显示相同的 IP 信息,与我运行脚本、关闭它、再次运行它无关。在第一个链接中,作者描述了已经支持的应用程序。如果未列出,用户必须提供支持。

我试图在我自己的脚本中提供支持,方法是使用第一个套接字与特定套接字通信,然后是 stem 库。套接字本身没有任何作用,现在 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.)

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

【问题讨论】:

    标签: python networking tor


    【解决方案1】:

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

    如果您使用 Python requests,请参阅 this answer。

    要使用相同的 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 的电路。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-11
      • 2013-12-16
      • 1970-01-01
      • 2023-03-31
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      相关资源
      最近更新 更多