From c19f8f6b5e5ab91a65022812bdb31bbabd6ceb52 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 22 Apr 2019 11:02:31 +0800 Subject: [PATCH 1/2] Use a randomized port number for the first run --- src/base/bittorrent/session.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index a049eb478..cb9ec40b5 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -337,7 +337,7 @@ Session::Session(QObject *parent) , m_isAltGlobalSpeedLimitEnabled(BITTORRENT_SESSION_KEY("UseAlternativeGlobalSpeedLimit"), false) , m_isBandwidthSchedulerEnabled(BITTORRENT_SESSION_KEY("BandwidthSchedulerEnabled"), false) , m_saveResumeDataInterval(BITTORRENT_SESSION_KEY("SaveResumeDataInterval"), 60) - , m_port(BITTORRENT_SESSION_KEY("Port"), 8999) + , m_port(BITTORRENT_SESSION_KEY("Port"), -1) , m_useRandomPort(BITTORRENT_SESSION_KEY("UseRandomPort"), false) , m_networkInterface(BITTORRENT_SESSION_KEY("Interface")) , m_networkInterfaceName(BITTORRENT_SESSION_KEY("InterfaceName")) @@ -375,6 +375,9 @@ Session::Session(QObject *parent) , m_extraLimit(0) , m_recentErroredTorrentsTimer(new QTimer(this)) { + if (useRandomPort() || (port() < 0)) + m_port = Utils::Random::rand(1024, 65535); + initResumeFolder(); m_recentErroredTorrentsTimer->setSingleShot(true); @@ -2537,15 +2540,12 @@ void Session::setSaveResumeDataInterval(const uint value) int Session::port() const { - static int randomPort = Utils::Random::rand(1024, 65535); - if (useRandomPort()) - return randomPort; return m_port; } void Session::setPort(const int port) { - if (port != this->port()) { + if (port != m_port) { m_port = port; configureListeningInterface(); } From 672b44acba7459df33a22e973dc4644f158319a6 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 23 Apr 2019 11:33:33 +0800 Subject: [PATCH 2/2] Let OS assign listening port This applies when "Use different port on startup" option is selected. --- src/base/bittorrent/session.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index cb9ec40b5..ac3fccb19 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -375,7 +375,7 @@ Session::Session(QObject *parent) , m_extraLimit(0) , m_recentErroredTorrentsTimer(new QTimer(this)) { - if (useRandomPort() || (port() < 0)) + if (port() < 0) m_port = Utils::Random::rand(1024, 65535); initResumeFolder(); @@ -1149,9 +1149,10 @@ void Session::configure(lt::settings_pack &settingsPack) QString chosenIP; #endif if (m_listenInterfaceChanged) { - const ushort port = this->port(); - const std::pair ports(port, port); - settingsPack.set_int(lt::settings_pack::max_retry_port_bind, ports.second - ports.first); + const int port = useRandomPort() ? 0 : this->port(); + if (port > 0) // user specified port + settingsPack.set_int(lt::settings_pack::max_retry_port_bind, 0); + for (QString ip : getListeningIPs()) { lt::error_code ec; std::string interfacesStr;