def setup_tunnel_seeder(self, hops): """ Setup the seeder. """ from Tribler.Core.Session import Session self.seed_config = self.config.copy() self.seed_config.set_state_dir(self.getStateDir(2)) self.seed_config.set_megacache_enabled(True) self.seed_config.set_tunnel_community_socks5_listen_ports(self.get_socks5_ports()) if self.session2 is None: self.session2 = Session(self.seed_config, ignore_singleton=True, autoload_discovery=False) self.session2.start() tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://localhost/announce") tdef.finalize() torrentfn = os.path.join(self.session2.config.get_state_dir(), "gen.torrent") tdef.save(torrentfn) self.seed_tdef = tdef if hops > 0: # Safe seeding enabled self.tunnel_community_seeder = self.load_tunnel_community_in_session(self.session2) self.tunnel_community_seeder.build_tunnels(hops) from twisted.internet import reactor, task while not list(self.tunnel_community_seeder.active_data_circuits()): yield task.deferLater(reactor, .05, lambda: None) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = self.session2.start_download_from_tdef(tdef, dscfg) d.set_state_callback(self.seeder_state_callback)
def setup_tunnel_seeder(self, hops): """ Setup the seeder. """ from Tribler.Core.Session import Session self.seed_config = self.config.copy() self.seed_config.set_state_dir(self.getStateDir(2)) self.seed_config.set_megacache_enabled(True) self.seed_config.set_tunnel_community_socks5_listen_ports(self.get_socks5_ports()) if self.session2 is None: self.session2 = Session(self.seed_config) self.session2.start() tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://localhost/announce") tdef.finalize() torrentfn = os.path.join(self.session2.config.get_state_dir(), "gen.torrent") tdef.save(torrentfn) self.seed_tdef = tdef if hops > 0: # Safe seeding enabled self.tunnel_community_seeder = self.load_tunnel_community_in_session(self.session2) self.tunnel_community_seeder.build_tunnels(hops) else: self.sanitize_network(self.session2) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = self.session2.start_download_from_tdef(tdef, dscfg) d.set_state_callback(self.seeder_state_callback)
def setup_tunnel_seeder(self, hops): """ Setup the seeder. """ from Tribler.Core.Session import Session self.seed_config = self.config.copy() self.seed_config.set_state_dir(self.getStateDir(2)) self.seed_config.set_tunnel_community_socks5_listen_ports( self.get_socks5_ports()) if self.session2 is None: self.session2 = Session(self.seed_config) self.session2.start() tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://localhost/announce") torrentfn = os.path.join(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 = self.load_tunnel_community_in_session( self.session2) self.tunnel_community_seeder.build_tunnels(hops) else: self.sanitize_network(self.session2) dscfg = DownloadStartupConfig() dscfg.set_dest_dir( TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = self.session2.start_download_from_tdef(tdef, dscfg) d.set_state_callback(self.seeder_state_callback)
def setupSeeder(self, hops=0, session=None): from Tribler.Core.Session import Session from Tribler.Core.TorrentDef import TorrentDef from Tribler.Core.DownloadConfig import DownloadStartupConfig self.setUpPreSession() self.config.set_libtorrent(True) self.config2 = self.config.copy() self.config2.set_state_dir(self.getStateDir(2)) if session is None: self.session2 = Session(self.config2, ignore_singleton=True, autoload_discovery=False) upgrader = self.session2.prestart() while not upgrader.is_done: time.sleep(0.1) self.session2.start() session = self.session2 tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://fake.net/announce") tdef.set_private() # disable dht tdef.finalize() torrentfn = os.path.join(session.get_state_dir(), "gen.torrent") tdef.save(torrentfn) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = session.start_download(tdef, dscfg) d.set_state_callback(self.seeder_state_callback) return torrentfn
def start_anon_download(self, hops=1): """ Start an anonymous download in the main Tribler session. """ dscfg = DownloadStartupConfig() dscfg.set_dest_dir(self.getDestDir()) dscfg.set_hops(hops) download = self.session.start_download_from_tdef(self.seed_tdef, dscfg) download.add_peer(("127.0.0.1", self.session2.config.get_libtorrent_port())) return download
def start_anon_download(self, hops=1): """ Start an anonymous download in the main Tribler session. """ dscfg = DownloadStartupConfig() dscfg.set_dest_dir(self.getDestDir()) dscfg.set_hops(hops) download = self.session.start_download_from_tdef(self.seed_tdef, dscfg) download.add_peer(("127.0.0.1", self.session2.config.get_libtorrent_port())) return download
def start_anon_download(self, hops=1): """ Start an anonymous download in the main Tribler session. """ dscfg = DownloadStartupConfig() dscfg.set_dest_dir(self.getDestDir()) dscfg.set_hops(hops) download = self.session.start_download_from_tdef(self.seed_tdef, dscfg) tc = self.session.lm.tunnel_community tc.bittorrent_peers[download] = [("127.0.0.1", self.session2.config.get_libtorrent_port())] return download
def start_anon_download(self, hops=1): """ Start an anonymous download in the main Tribler session. """ dscfg = DownloadStartupConfig() dscfg.set_dest_dir(self.getDestDir()) dscfg.set_hops(hops) download = self.session.start_download_from_tdef(self.seed_tdef, dscfg) tc = self.session.lm.tunnel_community tc.bittorrent_peers[download] = [ ("127.0.0.1", self.session2.config.get_libtorrent_port()) ] return download
def on_torrent_insert(self, source_str, infohash, name): """ Callback function called by the source when a new torrent is discovered """ self._logger.debug('Received torrent %s from %s', infohash, source_str) if source_str not in self.sources: self._logger.debug('Skipping torrent %s (unknown source %s)', infohash, source_str) return # Did we already get this torrent from another source? if infohash in self.torrents: self.torrents[infohash].sources.add(source_str) self._logger.debug('Skipping torrent %s (already known)', infohash) return # If a download already exists or already has a checkpoint, skip this torrent if self.session.get_download(unhexlify(infohash)) or \ os.path.exists(os.path.join(self.session.get_downloads_pstate_dir(), infohash + '.state')): self._logger.debug( 'Skipping torrent %s (download already running or scheduled to run)', infohash) return if len(self.torrents) >= self.settings.max_torrents_listed: self._logger.debug('Skipping torrent %s (limit reached)', infohash) return self.torrents[infohash] = CreditMiningTorrent(infohash, name) self.torrents[infohash].sources.add(source_str) self._logger.info('Starting torrent %s', infohash) magnet = u'magnet:?xt=urn:btih:%s&dn=%s' % (infohash, name) dl_config = DownloadStartupConfig() dl_config.set_hops(self.settings.hops) dl_config.set_dest_dir(self.settings.save_path) dl_config.set_credit_mining(True) dl_config.set_user_stopped(True) self.session.lm.add(TorrentDefNoMetainfo(unhexlify(infohash), name, magnet), dl_config, hidden=True)
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 = DownloadStartupConfig() anon_hops = 0 if 'anon_hops' in parameters and len(parameters['anon_hops']) > 0: if parameters['anon_hops'][0].isdigit(): anon_hops = int(parameters['anon_hops'][0]) safe_seeding = False if 'safe_seeding' in parameters and len(parameters['safe_seeding']) > 0 \ and parameters['safe_seeding'][0] == "1": safe_seeding = True 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 'destination' in parameters and len(parameters['destination']) > 0: if not os.path.isdir(parameters['destination'][0]): return None, "Invalid destination directory specified" download_config.set_dest_dir(parameters['destination'][0]) if 'selected_files[]' in parameters: selected_files_list = [ unicode(f, 'utf-8') for f in parameters['selected_files[]'] ] download_config.set_selected_files(selected_files_list) return download_config, None
def on_torrent_insert(self, source_str, infohash, name): """ Callback function called by the source when a new torrent is discovered """ self._logger.debug('Received torrent %s from %s', infohash, source_str) if source_str not in self.sources: self._logger.debug('Skipping torrent %s (unknown source %s)', infohash, source_str) return # Did we already get this torrent from another source? if infohash in self.torrents: self.torrents[infohash].sources.add(source_str) self._logger.debug('Skipping torrent %s (already known)', infohash) return # If a download already exists or already has a checkpoint, skip this torrent if self.session.get_download(unhexlify(infohash)) or \ os.path.exists(os.path.join(self.session.get_downloads_pstate_dir(), infohash + '.state')): self._logger.debug('Skipping torrent %s (download already running or scheduled to run)', infohash) return if len(self.torrents) >= self.settings.max_torrents_listed: self._logger.debug('Skipping torrent %s (limit reached)', infohash) return self.torrents[infohash] = CreditMiningTorrent(infohash, name) self.torrents[infohash].sources.add(source_str) self._logger.info('Starting torrent %s', infohash) magnet = u'magnet:?xt=urn:btih:%s&dn=%s' % (infohash, name) dl_config = DownloadStartupConfig() dl_config.set_hops(self.settings.hops) dl_config.set_dest_dir(self.settings.save_path) dl_config.set_credit_mining(True) dl_config.set_user_stopped(True) self.session.lm.add(TorrentDefNoMetainfo(unhexlify(infohash), name, magnet), dl_config, hidden=True)
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 = DownloadStartupConfig() anon_hops = 0 if 'anon_hops' in parameters and len(parameters['anon_hops']) > 0: if parameters['anon_hops'][0].isdigit(): anon_hops = int(parameters['anon_hops'][0]) safe_seeding = False if 'safe_seeding' in parameters and len(parameters['safe_seeding']) > 0 \ and parameters['safe_seeding'][0] == "1": safe_seeding = True 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 'destination' in parameters and len(parameters['destination']) > 0: dest_dir = cast_to_unicode_utf8(parameters['destination'][0]) download_config.set_dest_dir(dest_dir) if 'selected_files[]' in parameters: selected_files_list = [cast_to_unicode_utf8(f) for f in parameters['selected_files[]']] download_config.set_selected_files(selected_files_list) return download_config, None
def setupSeeder(self, hops=0, session=None): from Tribler.Core.Session import Session from Tribler.Core.TorrentDef import TorrentDef from Tribler.Core.DownloadConfig import DownloadStartupConfig self.setUpPreSession() self.config.set_libtorrent(True) self.config2 = self.config.copy() self.config2.set_state_dir(self.getStateDir(2)) if session is None: self.session2 = Session(self.config2, ignore_singleton=True, autoload_discovery=False) upgrader = self.session2.prestart() while not upgrader.is_done: time.sleep(0.1) self.session2.start() session = self.session2 tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://fake.net/announce") tdef.set_private() # disable dht tdef.finalize() torrentfn = os.path.join(session.get_state_dir(), "gen.torrent") tdef.save(torrentfn) dscfg = DownloadStartupConfig() dscfg.set_dest_dir( TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = session.start_download(tdef, dscfg) d.set_state_callback(self.seeder_state_callback) return torrentfn
def setup_tunnel_seeder(self, hops): """ Setup the seeder. """ from Tribler.Core.Session import Session self.seed_config = self.config.copy() self.seed_config.set_state_dir(self.getStateDir(2)) self.seed_config.set_megacache(True) self.seed_config.set_tunnel_community_socks5_listen_ports( self.get_socks5_ports()) if self.session2 is None: self.session2 = Session(self.seed_config, ignore_singleton=True, autoload_discovery=False) self.session2.start() tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://localhost/announce") tdef.set_private() # disable dht tdef.finalize() torrentfn = os.path.join(self.session2.get_state_dir(), "gen.torrent") tdef.save(torrentfn) self.seed_tdef = tdef dscfg = DownloadStartupConfig() dscfg.set_dest_dir( TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = self.session2.start_download_from_tdef(tdef, dscfg) d.set_state_callback(self.seeder_state_callback) if hops > 0: # Safe seeding enabled self.tunnel_community_seeder = self.load_tunnel_community_in_session( self.session2)