【问题标题】:Choose specific exit node in TOR (with Python code)在 TOR 中选择特定的退出节点(使用 Python 代码)
【发布时间】:2014-06-24 17:28:45
【问题描述】:

我想用 Python 编写一个简单的脚本,它将连接到 TOR 网络并选择我指定的某个国家的出口节点。例如,我希望将所有传出网络流量路由到西班牙(通过 TOR 网络)。有什么方法可以实现吗?

谢谢, 汤姆。

【问题讨论】:

  • 您是否有理由不为此使用内置功能? torrc 文件已经可以指定国家作为出口节点。
  • 我考虑过,但据我所知,torrc 文件是“硬编码”的,每次我想更改出口节点时,我都必须更改它并重置连接...问题是我想动态更改我的出口节点。假设我的 python 脚本创建了 2 个进程,我希望其中一个在西班牙有一个出口节点,另一个在法国,并且它们可能在运行时更改(在运行时切换出口节点)。你知道我该怎么做吗?

标签: python networking routing tor


【解决方案1】:

好的,我想我有一个答案,这不是我想要的,但它也同样有效。

我可以在我的机器上拥有一个以上的 TOR 中继,并在 torrc 文件中使用我想要的出口节点配置每个中继,例如提到的Robert Davey。在我的 python 脚本中,我可以创建一些进程并将每个进程连接到不同的继电器 - 从而给我一个不同的退出节点。可以在这里找到一个例子: https://tor.stackexchange.com/questions/327/how-may-i-run-multiple-tor-relay-instances-in-a-single-linux-machine

如果我想更改已经运行的中继中的出口节点,我可以编辑相关 TOR 中继的 torrc 文件,并使用 stem python 库使用 SIGHUP 信号对其进行更新:

from stem import Signal
from stem.control import Controller

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

【讨论】:

    【解决方案2】:

    您不需要为每个继电器编写一个 torrc 文件。您可以通过 stem 动态启动 tor 传递参数。例如:

    import stem.process
    
    tor_process = stem.process.launch_tor_with_config(
        tor_cmd = PATH_TO_TOR_LAUNCHER,
        config = {
            'SOCKSPort': str(SOCKS_PORT),
            'ControlPort': str(CTRL_PORT),
            'DataDirectory': DATA_DIR,
            'GeoIPFile': DATA_DIR + 'geoip',
            'GeoIPv6File': DATA_DIR + 'geoip6',
            'ExitNodes': '{es},{pt}'       # exiting through Spain or Portugal
            }
    )
    

    这些是 Tor 默认使用的地理位置数据库中的国家/地区代码:http://dev.maxmind.com/geoip/legacy/codes/iso3166/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      相关资源
      最近更新 更多