예제 #1
0
    def initialize(self):
        # Start upnp
        self.get_session().start_upnp()

        if has_bep33_support():
            # Also listen to DHT log notifications - we need the dht_pkt_alert and extract the BEP33 bloom filters
            dht_health_session = self.get_session(
                self.tribler_session.config.get_default_number_hops())
            dht_health_session.set_alert_mask(
                self.default_alert_mask
                | lt.alert.category_t.dht_log_notification)
            self.dht_health_manager = DHTHealthManager(dht_health_session)

        # Make temporary directory for metadata collecting through DHT
        self.metadata_tmpdir = mkdtemp(suffix='tribler_metainfo_tmpdir')

        # Register tasks
        self.register_task("process_alerts",
                           self._task_process_alerts,
                           interval=1)
        if self.dht_readiness_timeout > 0:
            self._dht_ready_task = self.register_task("check_dht_ready",
                                                      self._check_dht_ready)
        self.register_task("request_torrent_updates",
                           self._request_torrent_updates,
                           interval=1)
        self.register_task('task_cleanup_metacache',
                           self._task_cleanup_metainfo_cache,
                           interval=60,
                           delay=0)

        self.set_download_states_callback(self.sesscb_states_callback)
예제 #2
0
    def initialize(self):
        # Create the checkpoints directory
        (self.state_dir / STATEDIR_CHECKPOINT_DIR).mkdir(exist_ok=True)

        # Start upnp
        if self.config.upnp:
            self.get_session().start_upnp()

        if has_bep33_support() and self.download_defaults.number_hops <= len(self.socks_listen_ports or []):
            # Also listen to DHT log notifications - we need the dht_pkt_alert and extract the BEP33 bloom filters
            dht_health_session = self.get_session(self.download_defaults.number_hops)
            dht_health_session.set_alert_mask(self.default_alert_mask | lt.alert.category_t.dht_log_notification)
            self.dht_health_manager = DHTHealthManager(dht_health_session)

        # Make temporary directory for metadata collecting through DHT
        self.metadata_tmpdir = self.metadata_tmpdir or Path.mkdtemp(suffix='tribler_metainfo_tmpdir')

        # Register tasks
        self.register_task("process_alerts", self._task_process_alerts, interval=1)
        if self.dht_readiness_timeout > 0 and self.config.dht:
            self._dht_ready_task = self.register_task("check_dht_ready", self._check_dht_ready)
        self.register_task("request_torrent_updates", self._request_torrent_updates, interval=1)
        self.register_task('task_cleanup_metacache', self._task_cleanup_metainfo_cache, interval=60, delay=0)

        self.set_download_states_callback(self.sesscb_states_callback)
예제 #3
0
async def test_check_torrent_health(rest_api, mock_dlmgr, udp_tracker, metadata_store):
    """
    Test the endpoint to fetch the health of a chant-managed, infohash-only torrent
    """
    infohash = b'a' * 20
    tracker_url = f'udp://localhost:{udp_tracker.port}/announce'
    udp_tracker.tracker_info.add_info_about_infohash(infohash, 12, 11, 1)

    with db_session:
        tracker_state = metadata_store.TrackerState(url=tracker_url)
        torrent_state = metadata_store.TorrentState(trackers=tracker_state, infohash=infohash)
        metadata_store.TorrentMetadata(
            infohash=infohash, title='ubuntu-torrent.iso', size=42, tracker_info=tracker_url, health=torrent_state
        )
    url = f'metadata/torrents/{hexlify(infohash)}/health?timeout={TORRENT_CHECK_TIMEOUT}&refresh=1'

    # Add mock DHT response - we both need to account for the case when BEP33 is used and the old lookup method
    mock_dlmgr.get_metainfo = lambda _, **__: succeed(None)
    dht_health_dict = {"infohash": hexlify(infohash), "seeders": 1, "leechers": 2}
    mock_dlmgr.dht_health_manager.get_health = lambda *_, **__: succeed({"DHT": [dht_health_dict]})

    # Left for compatibility with other tests in this object
    await udp_tracker.start()

    json_response = await do_request(rest_api, url)
    assert "health" in json_response
    assert f"udp://localhost:{udp_tracker.port}" in json_response['health']
    if has_bep33_support():
        assert "DHT" in json_response['health']

    json_response = await do_request(rest_api, url + '&nowait=1')
    assert json_response == {'checking': '1'}
예제 #4
0
    async def check_torrent_health(self,
                                   infohash,
                                   timeout=20,
                                   scrape_now=False):
        """
        Check the health of a torrent with a given infohash.
        :param infohash: Torrent infohash.
        :param timeout: The timeout to use in the performed requests
        :param scrape_now: Flag whether we want to force scraping immediately
        """
        tracker_set = []

        # We first check whether the torrent is already in the database and checked before
        with db_session:
            result = self.tribler_session.mds.TorrentState.get(
                infohash=database_blob(infohash))
            if result:
                torrent_id = result.infohash
                last_check = result.last_check
                time_diff = time.time() - last_check
                if time_diff < MIN_TORRENT_CHECK_INTERVAL and not scrape_now:
                    self._logger.debug(
                        "time interval too short, not doing torrent health check for %s",
                        hexlify(infohash))
                    return {
                        "db": {
                            "seeders": result.seeders,
                            "leechers": result.leechers,
                            "infohash": hexlify(infohash)
                        }
                    }

                # get torrent's tracker list from DB
                tracker_set = self.get_valid_trackers_of_torrent(torrent_id)

        tasks = []
        for tracker_url in tracker_set:
            session = self._create_session_for_request(tracker_url,
                                                       timeout=timeout)
            session.add_infohash(infohash)
            tasks.append(self.connect_to_tracker(session))

        if has_bep33_support():
            # Create a (fake) DHT session for the lookup if we have support for BEP33.
            session = FakeBep33DHTSession(self.tribler_session, infohash,
                                          timeout)

        else:
            # Otherwise, fallback on the normal DHT metainfo lookups.
            session = FakeDHTSession(self.tribler_session, infohash, timeout)

        self._session_list['DHT'].append(session)
        tasks.append(self.connect_to_tracker(session))

        res = await gather(*tasks, return_exceptions=True)
        return self.on_torrent_health_check_completed(infohash, res)
예제 #5
0
    async def test_check_torrent_health(self):
        """
        Test the endpoint to fetch the health of a chant-managed, infohash-only torrent
        """
        infohash = b'a' * 20
        tracker_url = 'udp://localhost:%s/announce' % self.udp_port
        self.udp_tracker.tracker_info.add_info_about_infohash(
            infohash, 12, 11, 1)

        with db_session:
            tracker_state = self.session.mds.TrackerState(url=tracker_url)
            torrent_state = self.session.mds.TorrentState(
                trackers=tracker_state, infohash=infohash)
            self.session.mds.TorrentMetadata(infohash=infohash,
                                             title='ubuntu-torrent.iso',
                                             size=42,
                                             tracker_info=tracker_url,
                                             health=torrent_state)
        url = 'metadata/torrents/%s/health?timeout=%s&refresh=1' % (
            hexlify(infohash), TORRENT_CHECK_TIMEOUT)

        # Initialize the torrent checker
        self.session.torrent_checker = TorrentChecker(self.session)
        await self.session.torrent_checker.initialize()

        # Add mock DHT response - we both need to account for the case when BEP33 is used and the old lookup method
        self.session.dlmgr = MockObject()
        self.session.dlmgr.get_metainfo = lambda _, **__: succeed(None)
        self.session.dlmgr.dht_health_manager = MockObject()
        dht_health_dict = {
            "infohash": hexlify(infohash),
            "seeders": 1,
            "leechers": 2
        }
        self.session.dlmgr.dht_health_manager.get_health = lambda *_, **__: succeed(
            {"DHT": [dht_health_dict]})
        self.session.dlmgr.get_channel_downloads = lambda: []

        # Left for compatibility with other tests in this object
        await self.udp_tracker.start()
        json_response = await self.do_request(url)
        self.assertIn("health", json_response)
        self.assertIn("udp://localhost:%s" % self.udp_port,
                      json_response['health'])
        if has_bep33_support():
            self.assertIn("DHT", json_response['health'])

        json_response = await self.do_request(url + '&nowait=1')
        self.assertDictEqual(json_response, {'checking': '1'})
예제 #6
0
    def on_tribler_statistics(self, data):
        if not data:
            return
        data = data["tribler_statistics"]
        self.window().general_tree_widget.clear()
        self.create_and_add_widget_item("Tribler version",
                                        self.tribler_version,
                                        self.window().general_tree_widget)
        self.create_and_add_widget_item(
            "Python version",
            sys.version.replace('\n', ''),
            self.window().general_tree_widget  # to fit in one line
        )
        self.create_and_add_widget_item("Libtorrent version",
                                        libtorrent.version,
                                        self.window().general_tree_widget)
        self.create_and_add_widget_item("BEP33 support", has_bep33_support(),
                                        self.window().general_tree_widget)
        self.create_and_add_widget_item("", "",
                                        self.window().general_tree_widget)

        self.create_and_add_widget_item("Number of channels",
                                        data["num_channels"],
                                        self.window().general_tree_widget)
        self.create_and_add_widget_item("Database size",
                                        format_size(data["db_size"]),
                                        self.window().general_tree_widget)
        self.create_and_add_widget_item("Number of known torrents",
                                        data["num_torrents"],
                                        self.window().general_tree_widget)
        self.create_and_add_widget_item("", "",
                                        self.window().general_tree_widget)

        disk_usage = psutil.disk_usage('/')
        self.create_and_add_widget_item("Total disk space",
                                        format_size(disk_usage.total),
                                        self.window().general_tree_widget)
        self.create_and_add_widget_item("Used disk space",
                                        format_size(disk_usage.used),
                                        self.window().general_tree_widget)
        self.create_and_add_widget_item("Free disk space",
                                        format_size(disk_usage.free),
                                        self.window().general_tree_widget)

        # Show GUI settings
        self.show_gui_settings()