Esempio n. 1
0
 async def test_remove_with_files(self):
     """
     Testing whether the API returns 200 if a download is being removed
     """
     # Create a copy of the file, so we can remove it later
     source_file = TESTS_DATA_DIR / 'video.avi'
     tmpdir = self.temporary_directory()
     copied_file = tmpdir / Path(source_file).name
     shutil.copyfile(source_file, copied_file)
     video_tdef, _ = self.create_local_torrent(copied_file)
     dcfg = DownloadConfig()
     dcfg.set_dest_dir(tmpdir)
     download = self.session.dlmgr.start_download(tdef=video_tdef,
                                                  config=dcfg)
     infohash = get_hex_infohash(video_tdef)
     while not download.handle:
         await sleep(0.1)
     await sleep(2)
     await self.do_request('downloads/%s' % infohash,
                           post_data={"remove_data": True},
                           expected_code=200,
                           request_type='DELETE',
                           expected_json={
                               u"removed":
                               True,
                               u"infohash":
                               u"c9a19e7fe5d9a6c106d6ea3c01746ac88ca3c7a5"
                           })
     while copied_file.exists():
         await sleep(0.1)
     self.assertEqual(len(self.session.dlmgr.get_downloads()), 0)
     self.assertFalse(copied_file.exists())
Esempio n. 2
0
    def create_dconfig_from_params(parameters):
        """
        Create a download configuration based on some given parameters. Possible parameters are:
        - anon_hops: the number of hops for the anonymous download. 0 hops is equivalent to a plain download
        - safe_seeding: whether the seeding of the download should be anonymous or not (0 = off, 1 = on)
        - destination: the destination path of the torrent (where it is saved on disk)
        """
        download_config = DownloadConfig()

        anon_hops = parameters.get('anon_hops', 0)
        safe_seeding = bool(parameters.get('safe_seeding', 0))

        if anon_hops > 0 and not safe_seeding:
            return None, "Cannot set anonymous download without safe seeding enabled"

        if anon_hops > 0:
            download_config.set_hops(anon_hops)

        if safe_seeding:
            download_config.set_safe_seeding(True)

        if parameters.get('destination'):
            dest_dir = parameters['destination']
            download_config.set_dest_dir(dest_dir)

        if 'selected_files' in parameters:
            download_config.set_selected_files(parameters['selected_files'])

        return download_config, None
Esempio n. 3
0
async def hidden_seeder_session(seed_config, video_tdef):
    seed_config.set_libtorrent_enabled(False)
    seeder_session = Session(seed_config)
    seeder_session.upgrader_enabled = False
    await seeder_session.start()

    # Also load the tunnel community in the seeder session
    await load_tunnel_community_in_session(seeder_session, start_lt=True)
    seeder_session.tunnel_community.build_tunnels(1)

    dscfg_seed = DownloadConfig()
    dscfg_seed.set_dest_dir(TESTS_DATA_DIR)
    dscfg_seed.set_hops(1)
    upload = seeder_session.dlmgr.start_download(tdef=video_tdef,
                                                 config=dscfg_seed)

    def seeder_state_callback(ds):
        """
        The callback of the seeder download. For now, this only logs the state of the download that's seeder and is
        useful for debugging purposes.
        """
        seeder_session.tunnel_community.monitor_downloads([ds])
        d = ds.get_download()
        print(
            f"seeder: {repr(d.get_def().get_name())} {dlstatus_strings[ds.get_status()]} {ds.get_progress()}"
        )
        return 2

    upload.set_state_callback(seeder_state_callback)

    await upload.wait_for_status(DLSTATUS_SEEDING)
    yield seeder_session
    await seeder_session.shutdown()
Esempio n. 4
0
    async def setup_tunnel_seeder(self, hops):
        """
        Setup the seeder.
        """
        from tribler_core.session import Session
        self.seed_config = self.config.copy()
        self.seed_config._state_dir = self.getRootStateDir(2)
        self.seed_config.set_libtorrent_enabled(hops == 0)
        self.seed_config.set_tunnel_community_socks5_listen_ports(
            self.get_ports(5))
        if self.session2 is None:
            self.session2 = Session(self.seed_config)
            self.session2.upgrader_enabled = False
            await self.session2.start()

        tdef = TorrentDef()
        tdef.add_content(TESTS_DATA_DIR / "video.avi")
        tdef.set_tracker("http://localhost/announce")
        torrentfn = self.session2.config.get_state_dir() / "gen.torrent"
        tdef.save(torrent_filepath=torrentfn)
        self.seed_tdef = tdef

        if hops > 0:  # Safe seeding enabled
            self.tunnel_community_seeder = await self.load_tunnel_community_in_session(
                self.session2, start_lt=True)
            self.tunnel_community_seeder.build_tunnels(hops)
        else:
            await self.sanitize_network(self.session2)

        dscfg = DownloadConfig()
        dscfg.set_dest_dir(
            TESTS_DATA_DIR)  # basedir of the file we are seeding
        dscfg.set_hops(hops)
        d = self.session2.dlmgr.start_download(tdef=tdef, config=dscfg)
        d.set_state_callback(self.seeder_state_callback)
Esempio n. 5
0
    def check_watch_folder(self):
        if not self.session.config.get_watch_folder_path().is_dir():
            return

        # Make sure that we pass a str to os.walk
        watch_dir = str(self.session.config.get_watch_folder_path())

        for root, _, files in os.walk(watch_dir):
            root = path_util.Path(root)
            for name in files:
                if not name.endswith(".torrent"):
                    continue

                try:
                    tdef = TorrentDef.load(root / name)
                    if not tdef.get_metainfo():
                        self.cleanup_torrent_file(root, name)
                        continue
                except:  # torrent appears to be corrupt
                    self.cleanup_torrent_file(root, name)
                    continue

                infohash = tdef.get_infohash()

                if not self.session.dlmgr.download_exists(infohash):
                    self._logger.info("Starting download from torrent file %s", name)
                    dl_config = DownloadConfig()

                    anon_enabled = self.session.config.get_default_anonymity_enabled()
                    default_num_hops = self.session.config.get_default_number_hops()
                    dl_config.set_hops(default_num_hops if anon_enabled else 0)
                    dl_config.set_safe_seeding(self.session.config.get_default_safeseeding_enabled())
                    dl_config.set_dest_dir(self.session.config.get_default_destination_dir())
                    self.session.dlmgr.start_download(tdef=tdef, config=dl_config)
Esempio n. 6
0
async def channel_seeder_session(seed_config, channel_tdef):
    seeder_session = Session(seed_config)
    seeder_session.upgrader_enabled = False
    await seeder_session.start()
    dscfg_seed = DownloadConfig()
    dscfg_seed.set_dest_dir(TESTS_DATA_DIR / 'sample_channel')
    upload = seeder_session.dlmgr.start_download(tdef=channel_tdef,
                                                 config=dscfg_seed)
    await upload.wait_for_status(DLSTATUS_SEEDING)
    yield seeder_session
    await seeder_session.shutdown()
Esempio n. 7
0
def start_anon_download(session, seed_session, tdef, hops=1):
    """
    Start an anonymous download in the main Tribler session.
    """
    session.config.set_libtorrent_dht_readiness_timeout(0)
    dscfg = DownloadConfig()
    dscfg.set_dest_dir(session.config.get_state_dir())
    dscfg.set_hops(hops)
    download = session.dlmgr.start_download(tdef=tdef, config=dscfg)
    session.tunnel_community.bittorrent_peers[download] = [
        ("127.0.0.1", seed_session.config.get_libtorrent_port())
    ]
    return download
Esempio n. 8
0
 def updated_my_channel(self, tdef):
     """
     Notify the core that we updated our channel.
     """
     with db_session:
         my_channel = self.session.mds.ChannelMetadata.get(
             infohash=database_blob(tdef.get_infohash()))
     if (my_channel and my_channel.status == COMMITTED
             and not self.session.dlmgr.download_exists(
                 bytes(my_channel.infohash))):
         dcfg = DownloadConfig(
             state_dir=self.session.config.get_state_dir())
         dcfg.set_dest_dir(self.session.mds.channels_dir)
         dcfg.set_channel_download(True)
         return self.session.dlmgr.start_download(tdef=tdef, config=dcfg)
Esempio n. 9
0
    async def get_metainfo(self, infohash, timeout=30, hops=None, url=None):
        """
        Lookup metainfo for a given infohash. The mechanism works by joining the swarm for the infohash connecting
        to a few peers, and downloading the metadata for the torrent.
        :param infohash: The (binary) infohash to lookup metainfo for.
        :param timeout: A timeout in seconds.
        :param hops: the number of tunnel hops to use for this lookup. If None, use config default.
        :param url: Optional URL. Can contain trackers info, etc.
        :return: The metainfo
        """
        infohash_hex = hexlify(infohash)
        if infohash in self.metainfo_cache:
            self._logger.info('Returning metainfo from cache for %s', infohash_hex)
            return self.metainfo_cache[infohash]['meta_info']

        self._logger.info('Trying to fetch metainfo for %s', infohash_hex)
        if infohash in self.metainfo_requests:
            download = self.metainfo_requests[infohash][0]
            self.metainfo_requests[infohash][1] += 1
        elif infohash in self.downloads:
            download = self.downloads[infohash]
        else:
            tdef = TorrentDefNoMetainfo(infohash, 'metainfo request', url=url)
            dcfg = DownloadConfig()
            dcfg.set_hops(self.tribler_session.config.get_default_number_hops() if hops is None else hops)
            dcfg.set_upload_mode(True)  # Upload mode should prevent libtorrent from creating files
            dcfg.set_dest_dir(self.metadata_tmpdir)
            try:
                download = self.start_download(tdef=tdef, config=dcfg, hidden=True, checkpoint_disabled=True)
            except TypeError:
                return
            self.metainfo_requests[infohash] = [download, 1]

        try:
            metainfo = download.tdef.get_metainfo() or await wait_for(shield(download.future_metainfo), timeout)
            self._logger.info('Successfully retrieved metainfo for %s', infohash_hex)
            self.metainfo_cache[infohash] = {'time': timemod.time(), 'meta_info': metainfo}
        except (CancelledError, asyncio.TimeoutError):
            metainfo = None
            self._logger.info('Failed to retrieve metainfo for %s', infohash_hex)

        if infohash in self.metainfo_requests:
            self.metainfo_requests[infohash][1] -= 1
            if self.metainfo_requests[infohash][1] <= 0:
                await self.remove_download(download, remove_content=True)
                self.metainfo_requests.pop(infohash)

        return metainfo
Esempio n. 10
0
async def test_seeding(enable_libtorrent, video_seeder_session, video_tdef,
                       session, tmpdir):
    """
    Test whether a torrent is correctly seeded
    """
    dscfg = DownloadConfig()
    dscfg.set_dest_dir(tmpdir)
    download = session.dlmgr.start_download(tdef=video_tdef, config=dscfg)
    download.add_peer(
        ("127.0.0.1", video_seeder_session.config.get_libtorrent_port()))
    await download.wait_for_status(DLSTATUS_SEEDING)

    with open(tmpdir / "video.avi", "rb") as f:
        realdata = f.read()
    with open(TESTS_DATA_DIR / 'video.avi', "rb") as f:
        expdata = f.read()

    assert realdata == expdata
Esempio n. 11
0
    async def add_torrent(self, piece_length=1024):
        [srchandle, sourcefn] = mkstemp(dir=TESTS_DIR)
        data = b''.join([i.to_bytes(2, byteorder='big') for i in range(1000)])
        os.write(srchandle, data)
        os.close(srchandle)

        tdef = TorrentDef()
        tdef.add_content(sourcefn)
        tdef.set_piece_length(piece_length)
        torrentfn = self.session.config.get_state_dir() / "gen.torrent"
        tdef.save(torrentfn)

        dscfg = DownloadConfig()
        destdir = Path(sourcefn).parent
        dscfg.set_dest_dir(destdir)

        download = self.session.dlmgr.start_download(tdef=tdef, config=dscfg)
        await download.wait_for_status(DLSTATUS_SEEDING)
        return tdef.get_infohash(), data
Esempio n. 12
0
    async def add_torrent(self):
        [srchandle, sourcefn] = mkstemp(dir=TESTS_DIR)
        self.data = b'\xFF' * self.size  # pylint: disable=attribute-defined-outside-init
        os.write(srchandle, self.data)
        os.close(srchandle)

        tdef = TorrentDef()
        tdef.add_content(sourcefn)
        tdef.set_piece_length(self.piece_len)
        torrentfn = self.session.config.get_state_dir() / "gen.torrent"
        tdef.save(torrentfn)

        dscfg = DownloadConfig()
        destdir = Path(sourcefn).parent
        dscfg.set_dest_dir(destdir)

        self.download = self.session.dlmgr.start_download(tdef=tdef,
                                                          config=dscfg)  # pylint: disable=attribute-defined-outside-init
        await self.download.wait_for_status(DLSTATUS_SEEDING)
        self.infohash = tdef.get_infohash()  # pylint: disable=attribute-defined-outside-init
Esempio n. 13
0
    async def download_channel(self, channel):
        """
        Download a channel with a given infohash and title.
        :param channel: The channel metadata ORM object.
        """

        metainfo = await self.session.dlmgr.get_metainfo(bytes(
            channel.infohash),
                                                         timeout=60,
                                                         hops=0)
        if metainfo is None:
            # Timeout looking for the channel metainfo. Probably, there are no seeds.
            # TODO: count the number of tries we had with the channel, so we can stop trying eventually
            return
        try:
            if metainfo[b'info'][b'name'].decode('utf-8') != channel.dirname:
                # Malformed channel
                # TODO: stop trying to download this channel until it is updated with a new infohash
                return
        except (KeyError, TypeError):
            return

        dcfg = DownloadConfig(state_dir=self.session.config.get_state_dir())
        dcfg.set_dest_dir(self.session.mds.channels_dir)
        dcfg.set_channel_download(True)
        tdef = TorrentDef(metainfo=metainfo)

        download = self.session.dlmgr.start_download(tdef=tdef,
                                                     config=dcfg,
                                                     hidden=True)
        try:
            await download.future_finished
        except CancelledError:
            pass
        else:
            self.channels_processing_queue[channel.infohash] = (
                PROCESS_CHANNEL_DIR, channel)
        return download
Esempio n. 14
0
    def test_downloadconfig(self):
        dlcfg = DownloadConfig()

        self.assertIsInstance(dlcfg.get_dest_dir(), Path)
        dlcfg.set_dest_dir(self.session_base_dir)
        self.assertEqual(dlcfg.get_dest_dir(), self.session_base_dir)

        dlcfg.set_hops(4)
        self.assertEqual(dlcfg.get_hops(), 4)

        dlcfg.set_safe_seeding(False)
        self.assertFalse(dlcfg.get_safe_seeding())

        dlcfg.set_selected_files([1])
        self.assertEqual(dlcfg.get_selected_files(), [1])

        dlcfg.set_channel_download(True)
        self.assertTrue(dlcfg.get_channel_download())

        dlcfg.set_add_to_channel(True)
        self.assertTrue(dlcfg.get_add_to_channel())

        dlcfg.set_bootstrap_download(True)
        self.assertTrue(dlcfg.get_bootstrap_download())
Esempio n. 15
0
class Bootstrap(TaskManager):
    """
    A class to create a bootstrap downloads for inital file aka bootstrap file.
    Bootstrap class will be initialized at the start of Tribler by downloading/seeding bootstrap file.
    """
    def __init__(self, config_dir, dht=None):
        super(Bootstrap, self).__init__()

        self._logger = logging.getLogger(self.__class__.__name__)
        self.dcfg = DownloadConfig(state_dir=config_dir)
        self.dcfg.set_bootstrap_download(True)
        self.bootstrap_dir = config_dir / 'bootstrap'
        if not self.bootstrap_dir.exists():
            os.mkdir(self.bootstrap_dir)
        self.dcfg.set_dest_dir(self.bootstrap_dir)
        self.dcfg.set_safe_seeding(True)
        self.bootstrap_file = self.bootstrap_dir / "bootstrap.blocks"
        self.dht = dht

        self.bootstrap_finished = False
        self.infohash = None
        self.download = None
        self.bootstrap_nodes = {}

        self.register_task('fetch_bootstrap_peers',
                           self.fetch_bootstrap_peers,
                           interval=5)

    def start_by_infohash(self, download_function, infohash):
        """
        Download bootstrap file from current seeders
        :param download_function: function to download via tdef
        :return: download on bootstrap file
        """
        self._logger.debug("Starting bootstrap downloading %s", infohash)
        tdef = TorrentDefNoMetainfo(unhexlify(infohash),
                                    name='bootstrap.blocks')
        self.download = download_function(tdef=tdef,
                                          config=self.dcfg,
                                          hidden=True)
        self.infohash = infohash

    async def fetch_bootstrap_peers(self):
        if not self.download:
            return {}

        for peer in self.download.get_peerlist():
            mid = peer['id']
            if (mid not in self.bootstrap_nodes
                    or not self.bootstrap_nodes[mid]) and mid != "0" * 40:
                if self.dht:
                    try:
                        nodes = await self.dht.connect_peer(
                            bytes(unhexlify(mid)))
                    except DHTError as e:
                        self._logger.error("Failed to get DHT response:%s", e)
                        continue

                    if not nodes:
                        return
                    for node in nodes:
                        self.bootstrap_nodes[hexlify(node.mid)] = hexlify(
                            node.public_key.key_to_bin())
        return self.bootstrap_nodes

    async def shutdown(self):
        await self.shutdown_task_manager()
Esempio n. 16
0
    async def create_torrent(self, request):
        parameters = await request.json()
        params = {}

        if 'files' in parameters and parameters['files']:
            file_path_list = [
                ensure_unicode(f, 'utf-8') for f in parameters['files']
            ]
        else:
            return RESTResponse({"error": "files parameter missing"},
                                status=HTTP_BAD_REQUEST)

        if 'description' in parameters and parameters['description']:
            params['comment'] = parameters['description']

        if 'trackers' in parameters and parameters['trackers']:
            tracker_url_list = parameters['trackers']
            params['announce'] = tracker_url_list[0]
            params['announce-list'] = tracker_url_list

        name = 'unknown'
        if 'name' in parameters and parameters['name']:
            name = parameters['name']
            params['name'] = name

        export_dir = None
        if 'export_dir' in parameters and parameters['export_dir']:
            export_dir = Path(parameters['export_dir'])

        from tribler_core.version import version_id
        params['created by'] = '%s version: %s' % ('Tribler', version_id)

        params['nodes'] = False
        params['httpseeds'] = False
        params['encoding'] = False
        params['piece length'] = 0  # auto

        try:
            result = await self.session.dlmgr.create_torrent_file(
                file_path_list, recursive_bytes(params))
        except (IOError, UnicodeDecodeError, RuntimeError) as e:
            self._logger.exception(e)
            return return_handled_exception(request, e)

        metainfo_dict = bdecode_compat(result['metainfo'])

        if export_dir and export_dir.exists():
            save_path = export_dir / ("%s.torrent" % name)
            with open(save_path, "wb") as fd:
                fd.write(result['metainfo'])

        # Download this torrent if specified
        if 'download' in request.query and request.query[
                'download'] and request.query['download'] == "1":
            download_config = DownloadConfig()
            download_config.set_dest_dir(result['base_path'] if len(
                file_path_list) == 1 else result['base_dir'])
            try:
                self.session.dlmgr.start_download(
                    tdef=TorrentDef(metainfo_dict), config=download_config)
            except DuplicateDownloadException:
                self._logger.warning(
                    "The created torrent is already being downloaded.")

        return RESTResponse(
            json.dumps({
                "torrent":
                base64.b64encode(result['metainfo']).decode('utf-8')
            }))