reachability-monitor.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:securedrop-reachability-monitor 作者: freedomofpress 项目源码 文件源码
def check_instances(self, instances, timeout=30):
        """Visits each SD found in the directory and records its version
        string or the string 'unreachable', as well as relevant circuit
        information and descriptor information."""
        opener = build_opener(SocksiPyHandler(socks.SOCKS5, "127.0.0.1", 9050))

        for instance in instances:
            hs_url = instance.get("ths_address")

            try:
                response = opener.open("http://"+hs_url,
                                       timeout=timeout).read().decode()
                version_str = search("Powered by SecureDrop [0-9.]+",
                                     response).group(0)
                instance["version"] = version_str.split()[-1][:-1]
            except (socks.SOCKS5Error, socks.GeneralProxyError,
                    urllib.error.URLError):
                instance["version"] = "unreachable"
                try:
                    # The reason that we don't call the
                    # get_hidden_service_descriptor method on all URLs is that
                    # it's unreliable for services that are actually up.
                    # Basically, the method will never return or timeout. With
                    # services that cannot be reached, it usually quickly
                    # fails with the stem.DescriptorUnavailable exception. This
                    # seems to be the leading cause of unreachability.
                    hs_desc = self.controller.get_hidden_service_descriptor(hs_url)
                    instance["intro_pts"] = hs_desc.introduction_points_content.decode()
                except stem.DescriptorUnavailable:
                    instance["intro_pts"] = "descriptor unavailable"
                    print(instance)
                    continue
                pass

            intro_circs = []
            rend_circs = []

            for circuit in self.controller.get_circuits():
                if circuit.purpose == "HS_CLIENT_INTRO":
                    intro_circs.append(dict(path=circuit.path,
                                           reason=circuit.reason,
                                           remote_reason=circuit.remote_reason))
                if circuit.purpose == "HS_CLIENT_REND":
                    rend_circs.append(dict(path=circuit.path,
                                           state=circuit.hs_state,
                                           reason=circuit.reason,
                                           remote_reason=circuit.remote_reason))
                self.controller.close_circuit(circuit.id)

            instance["intro_circs"] = intro_circs
            instance["rend_circs"] = rend_circs

            if instance["version"] == "unreachable":
                print(instance)

        return instances
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号