def _create_peer_id(version): """Create a peer_id fingerprint. This creates the peer_id and modifies the release char to identify pre-release and development version. Using ``D`` for dev, daily or nightly builds, ``a, b, r`` for pre-releases and ``s`` for stable releases. Examples: ``--<client><client><major><minor><micro><release>--`` ``--DE200D--`` (development version of 2.0.0) ``--DE200s--`` (stable release of v2.0.0) ``--DE201b--`` (beta pre-release of v2.0.1) Args: version (str): The version string in PEP440 dotted notation. Returns: str: The formattted peer_id with Deluge prefix e.g. '--DE200s--' """ split = deluge.common.VersionSplit(version) # Fill list with zeros to length of 4 and use lt to create fingerprint. version_list = split.version + [0] * (4 - len(split.version)) peer_id = lt.generate_fingerprint('DE', *version_list) def substitute_chr(string, idx, char): """Fast substitute single char in string.""" return string[:idx] + char + string[idx + 1:] if split.dev: release_chr = 'D' elif split.suffix: # a (alpha), b (beta) or r (release candidate). release_chr = split.suffix[0].lower() else: release_chr = 's' peer_id = substitute_chr(peer_id, 6, release_chr) return peer_id
def __init__(self, listen_interface=None, read_only_config_keys=None): log.debug('Core init...') component.Component.__init__(self, 'Core') deluge_version = deluge.common.get_version() split_version = deluge.common.VersionSplit(deluge_version).version while len(split_version) < 4: split_version.append(0) deluge_fingerprint = lt.generate_fingerprint('DE', *split_version) user_agent = 'Deluge/{} libtorrent/{}'.format(deluge_version, self.get_libtorrent_version()) # Start the libtorrent session. log.debug('Starting session (fingerprint: %s, user_agent: %s)', deluge_fingerprint, user_agent) settings_pack = {'peer_fingerprint': deluge_fingerprint, 'user_agent': user_agent, 'ignore_resume_timestamps': True} self.session = lt.session(settings_pack, flags=0) # Load the settings, if available. self._load_session_state() # Enable libtorrent extensions # Allows peers to download the metadata from the swarm directly self.session.add_extension('ut_metadata') # Ban peers that sends bad data self.session.add_extension('smart_ban') # Create the components self.eventmanager = EventManager() self.preferencesmanager = PreferencesManager() self.alertmanager = AlertManager() self.pluginmanager = PluginManager(self) self.torrentmanager = TorrentManager() self.filtermanager = FilterManager(self) self.authmanager = AuthManager() # New release check information self.new_release = None # External IP Address from libtorrent self.external_ip = None self.eventmanager.register_event_handler('ExternalIPEvent', self._on_external_ip_event) # GeoIP instance with db loaded self.geoip_instance = None # These keys will be dropped from the set_config() RPC and are # configurable from the command-line. self.read_only_config_keys = read_only_config_keys log.debug('read_only_config_keys: %s', read_only_config_keys) # Get the core config self.config = ConfigManager('core.conf') self.config.save() # If there was an interface value from the command line, use it, but # store the one in the config so we can restore it on shutdown self.__old_interface = None if listen_interface: if deluge.common.is_ip(listen_interface): self.__old_interface = self.config['listen_interface'] self.config['listen_interface'] = listen_interface else: log.error('Invalid listen interface (must be IP Address): %s', listen_interface) # New release check information self.__new_release = None # Session status timer self.session_status = {} self.session_status_timer_interval = 0.5 self.session_status_timer = task.LoopingCall(self.session.post_session_stats) self.alertmanager.register_handler('session_stats_alert', self._on_alert_session_stats) self._session_rates = {(k_rate, k_bytes): 0 for k_rate, k_bytes in SESSION_RATES_MAPPING.items()} self.session_rates_timer_interval = 2 self.session_rates_timer = task.LoopingCall(self._update_session_rates)