def tdef_to_metadata_dict(tdef): """ Helper function to create a TorrentMetadata-compatible dict from TorrentDef """ # We only want to determine the type of the data. XXX filtering is done by the receiving side try: tags = default_category_filter.calculateCategory( tdef.metainfo, tdef.get_name_as_unicode()) except UnicodeDecodeError: tags = "Unknown" try: torrent_date = datetime.fromtimestamp(tdef.get_creation_date()) except ValueError: torrent_date = EPOCH return { "infohash": tdef.get_infohash(), "title": tdef.get_name_as_unicode()[:300], "tags": tags[:200], "size": tdef.get_length(), "torrent_date": torrent_date if torrent_date >= EPOCH else EPOCH, "tracker_info": get_uniformed_tracker_url( ensure_unicode(tdef.get_tracker() or '', 'utf-8')) or '', }
def add_tracker(self, tracker_url): """ Adds a new tracker into the tracker info dict and the database. :param tracker_url: The new tracker URL to be added. """ sanitized_tracker_url = get_uniformed_tracker_url(tracker_url) if sanitized_tracker_url is None: self._logger.warning(u"skip invalid tracker: %s", repr(tracker_url)) return with db_session: num = count(g for g in self.tracker_store if g.url == sanitized_tracker_url) if num > 0: self._logger.debug(u"skip existing tracker: %s", repr(tracker_url)) return # insert into database self.tracker_store(url=sanitized_tracker_url, last_check=0, failures=0, alive=True, torrents={})
def update_tracker_info(self, tracker_url, is_successful): """ Updates a tracker information. :param tracker_url: The given tracker_url. :param is_successful: If the check was successful. """ if tracker_url == "DHT": return sanitized_tracker_url = get_uniformed_tracker_url(tracker_url) tracker = self.tracker_store.get( lambda g: g.url == sanitized_tracker_url) if not tracker: self._logger.error( "Trying to update the tracker info of an unknown tracker URL") return current_time = int(time.time()) failures = 0 if is_successful else tracker.failures + 1 is_alive = failures < MAX_TRACKER_FAILURES # update the dict tracker.last_check = current_time tracker.failures = failures tracker.alive = is_alive
def __init__(self, *args, **kwargs): # Sanitize and canonicalize the tracker URL sanitized = get_uniformed_tracker_url(kwargs['url']) if sanitized: kwargs['url'] = sanitized else: raise MalformedTrackerURLException( "Could not canonicalize tracker URL (%s)" % kwargs['url']) super(TrackerState, self).__init__(*args, **kwargs)
def remove_tracker(self, tracker_url): """ Remove a given tracker from the database. URL is sanitized first and removed from the database. If the URL is ill formed then try removing the non- sanitized version. :param tracker_url: The URL of the tracker to be deleted. """ sanitized_tracker_url = get_uniformed_tracker_url(tracker_url) with db_session: options = self.tracker_store.select( lambda g: g.url in [tracker_url, sanitized_tracker_url]) for option in options[:]: option.delete()
def load_blacklist(self): """ Load the tracker blacklist from tracker_blacklist.txt in the session state directory. Entries are newline separated and are supposed to be sanitized. """ blacklist_file = Path(self.state_dir / "tracker_blacklist.txt").absolute() if blacklist_file.exists(): with open(blacklist_file) as blacklist_file_handle: # Note that get_uniformed_tracker_url will strip the newline at the end of .readlines() self.blacklist.extend([ get_uniformed_tracker_url(url) for url in blacklist_file_handle.readlines() ]) else: self._logger.info("No tracker blacklist file found at %s.", blacklist_file)
def get_old_trackers(self): with contextlib.closing(sqlite3.connect(self.tribler_db)) as connection, connection: cursor = connection.cursor() trackers = {} for tracker_id, tracker, last_check, failures, is_alive in cursor.execute(self.select_trackers_sql): try: tracker_url_sanitized = get_uniformed_tracker_url(tracker) if not tracker_url_sanitized: continue except Exception as e: self._logger.warning("Encountered malformed tracker: %s", e) # Skip malformed trackers continue trackers[tracker_url_sanitized] = ({ "last_check": last_check, "failures": failures, "alive": is_alive}) return trackers
def get_tracker_info(self, tracker_url): """ Gets the tracker information with the given tracker URL. :param tracker_url: The given tracker URL. :return: The tracker info dict if exists, None otherwise. """ sanitized_tracker_url = get_uniformed_tracker_url( tracker_url) if tracker_url != "DHT" else tracker_url with db_session: tracker = list( self.tracker_store.select( lambda g: g.url == sanitized_tracker_url)) if tracker: return { 'id': tracker[0].url, 'last_check': tracker[0].last_check, 'failures': tracker[0].failures, 'is_alive': tracker[0].alive } return None
def test_skip_split_error(): result = get_uniformed_tracker_url(";") assert not result
def test_uniform_scheme_correct_udp(): result = get_uniformed_tracker_url("udp://tracker.openbittorrent.com:80") assert result == "udp://tracker.openbittorrent.com:80"
def test_skip_value_error(): result = get_uniformed_tracker_url("ftp://tracker.1337\xffx.org:80/announce") assert not result
def test_uniform_http_no_path(): result = get_uniformed_tracker_url("http://tracker.openbittorrent.com") assert not result
def test_skip_wrong_url_scheme(self): result = get_uniformed_tracker_url(u'wss://tracker.1337x.org:80/announce') self.assertIsNone(result)
def test_skip_split_error(self): result = get_uniformed_tracker_url(";") self.assertIsNone(result)
def test_uniform_trailing_hex(): result = get_uniformed_tracker_url("udp://tracker.1337x.org:80\xff") assert not result
def test_uniform_http_default_port_given(): result = get_uniformed_tracker_url("http://torrent.ubuntu.com:80/announce") assert result == "http://torrent.ubuntu.com/announce"
def test_uniform_trailing_zero_hex(): result = get_uniformed_tracker_url("udp://tracker.1337x.org:80\x00") assert result == "udp://tracker.1337x.org:80"
def test_uniform_http_no_path_nbsp(): result = get_uniformed_tracker_url("http://google.nl\xa0") assert not result
def test_uniform_udp_remove_path(): result = get_uniformed_tracker_url("udp://tracker.openbittorrent.com:6969/announce") assert result == "udp://tracker.openbittorrent.com:6969"
def test_uniform_udp_no_port(): result = get_uniformed_tracker_url("udp://tracker.openbittorrent.com") assert not result
def test_uniform_scheme_correct_http_training_slash(): result = get_uniformed_tracker_url("http://torrent.ubuntu.com:6969/announce/") assert result == "http://torrent.ubuntu.com:6969/announce"
def test_skip_truncated_url(self): result = get_uniformed_tracker_url(u'http://tracker.1337x.org:80/anno...') self.assertIsNone(result)
def add_tracker(self, tracker_url): sanitized_url = get_uniformed_tracker_url(tracker_url) if sanitized_url: tracker = db.TrackerState.get_for_update( url=sanitized_url) or db.TrackerState(url=sanitized_url) self.health.trackers.add(tracker)
def test_skip_truncated_url(): result = get_uniformed_tracker_url("http://tracker.1337x.org:80/anno...") assert not result
def test_skip_value_error(self): result = get_uniformed_tracker_url("ftp://tracker.1337\xffx.org:80/announce") self.assertIsNone(result)
def test_skip_wrong_url_scheme(): result = get_uniformed_tracker_url("wss://tracker.1337x.org:80/announce") assert not result
def test_uniform_bad_urlenc(): result = get_uniformed_tracker_url("http://btjunkie.org/?do=upload") assert not result
def test_uniform_scheme_unknown(): result = get_uniformed_tracker_url("unknown://tracker.openbittorrent.com/announce") assert not result
def test_uniform_empty(): result = get_uniformed_tracker_url('') assert not result
def test_uniform_empty(self): result = get_uniformed_tracker_url(u'') self.assertIsNone(result)