From f57286fd5164b363e64460a7bca1e64643426ba0 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Sat, 16 Dec 2017 13:27:08 +0000 Subject: [PATCH] [#3121] Fix the peer-id to be unique The associated ticket made us aware that the id '-DE2000-' might be blocked. --- deluge/core/core.py | 67 +++++++++++++++++++++++++++++++-------- deluge/tests/test_core.py | 7 ++++ 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/deluge/core/core.py b/deluge/core/core.py index f54b77e03..4b80a55d8 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -24,7 +24,7 @@ from twisted.web.client import getPage import deluge.common import deluge.component as component from deluge import path_chooser_common -from deluge._libtorrent import lt, LT_VERSION +from deluge._libtorrent import LT_VERSION, lt from deluge.common import PY2 from deluge.configmanager import ConfigManager, get_config_dir from deluge.core.alertmanager import AlertManager @@ -102,22 +102,19 @@ DELUGE_VER = deluge.common.get_version() class Core(component.Component): 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} + user_agent = 'Deluge/{} libtorrent/{}'.format(DELUGE_VER, LT_VERSION) + peer_id = self._create_peer_id(DELUGE_VER) + log.debug( + 'Starting session (peer_id: %s, user_agent: %s)', + peer_id, user_agent) + settings_pack = { + 'peer_fingerprint': peer_id, + 'user_agent': user_agent, + 'ignore_resume_timestamps': True, + } self.session = lt.session(settings_pack, flags=0) # Load the settings, if available. @@ -218,6 +215,48 @@ class Core(component.Component): """ self.session.apply_settings(settings) + @staticmethod + 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: + ``----`` + ``--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 _save_session_state(self): """Saves the libtorrent session state""" filename = 'session.state' diff --git a/deluge/tests/test_core.py b/deluge/tests/test_core.py index 4d1f2ba14..910ff2ed5 100644 --- a/deluge/tests/test_core.py +++ b/deluge/tests/test_core.py @@ -317,3 +317,10 @@ class CoreTestCase(BaseTestCase): self.assertEqual(old_value, new_value) self.core.read_only_config_keys = None + + def test__create_peer_id(self): + self.assertEqual(self.core._create_peer_id('2.0.0'), '-DE200s-') + self.assertEqual(self.core._create_peer_id('2.0.0.dev15'), '-DE200D-') + self.assertEqual(self.core._create_peer_id('2.0.1rc1'), '-DE201r-') + self.assertEqual(self.core._create_peer_id('2.11.0b2'), '-DE2B0b-') + self.assertEqual(self.core._create_peer_id('2.4.12b2.dev3'), '-DE24CD-')