0

我一直在尝试向NavigaTor(用 python 2 编写)添加一些功能,它使用 stem 库来构建 TOR 电路。基本上,如果电路发生故障,我会尝试使用新路径构建一个新路径(默认情况下,Navigator 不会这样做,如果电路发生故障,则会返回)。相关代码如下:

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()

在一个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

现在,我的问题是,如果电路在第一次尝试中成功,那么这个东西就可以正常工作。但是,如果 build_status 失败,并且循环使用新的有效路径再次运行,则代码在以下行崩溃:

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

使用 IndexError:列表索引超出范围。在调试时,我观察到 probe.circs 仍然是空的,即使 _circuit_handler 事件应该附加到它上面。对于第二个电路,这种情况每次都会发生,即使如果第一个电路成功,它也可以正常工作。我在这里做错了什么?

4

1 回答 1

0

弄清楚了,_cid 在电路故障时没有被重置。添加 self._cid = None 来修复。

于 2020-05-23T15:54:12.707 回答