简体   繁体   English

Python tor stem 事件未按预期工作

[英]Python tor stem Events not working as expected

I have been trying to add some functionality to NavigaTor (written in python 2), which uses stem library to build TOR circuits.我一直在尝试向NavigaTor (用 python 2 编写)添加一些功能,它使用 stem 库来构建 TOR 电路。 Basically, if a circuit fails, i am trying to build a new one with a new path (Navigator does not do that by default, and returns if the circuit fails).基本上,如果电路发生故障,我会尝试使用新路径构建一个新路径(默认情况下,Navigator 不会这样做,如果电路发生故障,则会返回)。 The relevant code is as follows:相关代码如下:

Probe = namedtuple('Probe', 'path circs cbt streams perf bw')
def _circuit_handler(event):
    """ Event handler for handling circuit states. """
    if not event.build_flags or 'IS_INTERNAL' not in event.build_flags:
        if event.id == self._cid:
            probe.circs.append(event)
            if self._circuit_built.is_set():
                if event.status in ('FAILED', 'CLOSED'):
                    self._circuit_finished.set()
            if not self._circuit_built.is_set():
                if event.status in ('FAILED', 'BUILT'):
                    self._circuit_built.set()
        elif event.status == 'LAUNCHED' and not self._cid:
            self._cid = event.id
            probe.circs.append(event)
            self._manager.circ_launched.release()

In a while loop, I am trying to build circuits until a circuit has successfully been created:在一个while循环中,我正在尝试构建电路,直到成功创建电路:

while(true):        #try until a valid circuit is received:
    probe = Probe(path=self.path, circs=[], \
        cbt=set(), streams=[],perf=[], bw=[])
    circ_path = [node.desc.fingerprint for node in self.path]       #Valid nodes already acquired and stored in path

    self._manager.circ_launched.acquire()
    self._controller.add_event_listener(_circuit_handler, EventType.CIRC)
    self._controller.add_event_listener(_cbt_check, EventType.INFO)
    circID = self._controller.extend_circuit(path=circ_path)
    self._circuit_built.wait()

    build_status = probe.circs[len(probe.circs) - 1].status
    assert build_status == 'BUILT' or build_status == 'FAILED', \
        'Wrong circuit status: %s.' % build_statusFirst

    if build_status == 'FAILED':
        self._controller.remove_event_listener(_circuit_handler)
        self._controller.remove_event_listener(_cbt_check)
        self.path = self._manager._get_new_path()
    else:
        break

Now, my problem is, that if the circuit is successful in the first attempt, this thing works fine.现在,我的问题是,如果电路在第一次尝试中成功,那么这个东西就可以正常工作。 But if the build_status is FAILED, and loop runs again with a new valid path, the code crashes at the line:但是,如果 build_status 失败,并且循环使用新的有效路径再次运行,则代码在以下行崩溃:

build_status = probe.circs[len(probe.circs) - 1].status

With IndexError: list index out of range.使用 IndexError:列表索引超出范围。 Upon debugging, I have observed that the probe.circs remains empty, even though the _circuit_handler event is supposed to append to it.在调试时,我观察到 probe.circs 仍然是空的,即使 _circuit_handler 事件应该是 append 到它。 This happens every single time for the second circuit, even though it works fine if the first circuit was successful.对于第二个电路,这种情况每次都会发生,即使如果第一个电路成功,它也可以正常工作。 What am I doing wrong here?我在这里做错了什么?

Figured it out, _cid was not being reset upon circuit failure.弄清楚了,_cid 在电路故障时没有被重置。 Added self._cid = None to fix.添加 self._cid = None 来修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM