diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index f1b22cf9f3d..63382143bf4 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -370,6 +370,7 @@ Session::Session(QObject *parent) , m_includeOverheadInLimits(BITTORRENT_SESSION_KEY("IncludeOverheadInLimits"), false) , m_announceIP(BITTORRENT_SESSION_KEY("AnnounceIP")) , m_maxConcurrentHTTPAnnounces(BITTORRENT_SESSION_KEY("MaxConcurrentHTTPAnnounces"), 50) + , m_isReannounceWhenAddressChangedEnabled(BITTORRENT_SESSION_KEY("ReannounceWhenAddressChanged"), false) , m_stopTrackerTimeout(BITTORRENT_SESSION_KEY("StopTrackerTimeout"), 5) , m_maxConnections(BITTORRENT_SESSION_KEY("MaxConnections"), 500, lowerLimited(0, -1)) , m_maxUploads(BITTORRENT_SESSION_KEY("MaxUploads"), 20, lowerLimited(0, -1)) @@ -1010,9 +1011,9 @@ void Session::adjustLimits() void Session::applyBandwidthLimits() { - lt::settings_pack settingsPack = m_nativeSession->get_settings(); - applyBandwidthLimits(settingsPack); - m_nativeSession->apply_settings(settingsPack); + lt::settings_pack settingsPack = m_nativeSession->get_settings(); + applyBandwidthLimits(settingsPack); + m_nativeSession->apply_settings(settingsPack); } void Session::configure() @@ -2739,6 +2740,9 @@ void Session::setPort(const int port) { m_port = port; configureListeningInterface(); + + if (isReannounceWhenAddressChangedEnabled()) + reannounceToAllTrackers(); } } @@ -3029,7 +3033,6 @@ void Session::setMaxConnectionsPerTorrent(int max) // Apply this to all session torrents for (const lt::torrent_handle &handle : m_nativeSession->get_torrents()) { - if (!handle.is_valid()) continue; try { handle.set_max_connections(max); @@ -3054,7 +3057,6 @@ void Session::setMaxUploadsPerTorrent(int max) // Apply this to all session torrents for (const lt::torrent_handle &handle : m_nativeSession->get_torrents()) { - if (!handle.is_valid()) continue; try { handle.set_max_uploads(max); @@ -3585,6 +3587,25 @@ void Session::setMaxConcurrentHTTPAnnounces(const int value) configureDeferred(); } +bool Session::isReannounceWhenAddressChangedEnabled() const +{ + return m_isReannounceWhenAddressChangedEnabled; +} + +void Session::setReannounceWhenAddressChangedEnabled(const bool enabled) +{ + if (enabled == m_isReannounceWhenAddressChangedEnabled) + return; + + m_isReannounceWhenAddressChangedEnabled = enabled; +} + +void Session::reannounceToAllTrackers() const +{ + for (const lt::torrent_handle &torrent : m_nativeSession->get_torrents()) + torrent.force_reannounce(0, -1, lt::torrent_handle::ignore_min_interval); +} + int Session::stopTrackerTimeout() const { return m_stopTrackerTimeout; @@ -4632,8 +4653,7 @@ void Session::handleListenSucceededAlert(const lt::listen_succeeded_alert *p) .arg(toString(p->address), proto, QString::number(p->port)), Log::INFO); // Force reannounce on all torrents because some trackers blacklist some ports - for (const lt::torrent_handle &torrent : m_nativeSession->get_torrents()) - torrent.force_reannounce(); + reannounceToAllTrackers(); } void Session::handleListenFailedAlert(const lt::listen_failed_alert *p) @@ -4647,8 +4667,16 @@ void Session::handleListenFailedAlert(const lt::listen_failed_alert *p) void Session::handleExternalIPAlert(const lt::external_ip_alert *p) { + const QString externalIP {toString(p->external_address)}; LogMsg(tr("Detected external IP: %1", "e.g. Detected external IP: 1.1.1.1") - .arg(toString(p->external_address)), Log::INFO); + .arg(externalIP), Log::INFO); + + if (m_lastExternalIP != externalIP) + { + if (isReannounceWhenAddressChangedEnabled() && !m_lastExternalIP.isEmpty()) + reannounceToAllTrackers(); + m_lastExternalIP = externalIP; + } } void Session::handleSessionStatsAlert(const lt::session_stats_alert *p) diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 9754ce52973..7c2bbfba948 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -402,6 +402,9 @@ namespace BitTorrent void setAnnounceIP(const QString &ip); int maxConcurrentHTTPAnnounces() const; void setMaxConcurrentHTTPAnnounces(int value); + bool isReannounceWhenAddressChangedEnabled() const; + void setReannounceWhenAddressChangedEnabled(bool enabled); + void reannounceToAllTrackers() const; int stopTrackerTimeout() const; void setStopTrackerTimeout(int value); int maxConnections() const; @@ -690,6 +693,7 @@ namespace BitTorrent CachedSettingValue m_includeOverheadInLimits; CachedSettingValue m_announceIP; CachedSettingValue m_maxConcurrentHTTPAnnounces; + CachedSettingValue m_isReannounceWhenAddressChangedEnabled; CachedSettingValue m_stopTrackerTimeout; CachedSettingValue m_maxConnections; CachedSettingValue m_maxUploads; @@ -798,6 +802,8 @@ namespace BitTorrent QList m_moveStorageQueue; + QString m_lastExternalIP; + static Session *m_instance; }; } diff --git a/src/gui/advancedsettings.cpp b/src/gui/advancedsettings.cpp index c070ed6fb52..af0e91cb90f 100644 --- a/src/gui/advancedsettings.cpp +++ b/src/gui/advancedsettings.cpp @@ -83,6 +83,7 @@ namespace NOTIFICATION_TIMEOUT, #endif CONFIRM_REMOVE_ALL_TAGS, + REANNOUNCE_WHEN_ADDRESS_CHANGED, DOWNLOAD_TRACKER_FAVICON, SAVE_PATH_HISTORY_LENGTH, ENABLE_SPEED_WIDGET, @@ -284,6 +285,8 @@ void AdvancedSettings::saveAdvancedSettings() #if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) && defined(QT_DBUS_LIB) mainWindow->setNotificationTimeout(m_spinBoxNotificationTimeout.value()); #endif + // Reannounce to all trackers when ip/port changed + session->setReannounceWhenAddressChangedEnabled(m_checkBoxReannounceWhenAddressChanged.isChecked()); // Misc GUI properties mainWindow->setDownloadTrackerFavicon(m_checkBoxTrackerFavicon.isChecked()); AddNewTorrentDialog::setSavePathHistoryLength(m_spinBoxSavePathHistoryLength.value()); @@ -664,6 +667,9 @@ void AdvancedSettings::loadAdvancedSettings() m_spinBoxNotificationTimeout.setSuffix(tr(" ms", " milliseconds")); addRow(NOTIFICATION_TIMEOUT, tr("Notification timeout [0: infinite]"), &m_spinBoxNotificationTimeout); #endif + // Reannounce to all trackers when ip/port changed + m_checkBoxReannounceWhenAddressChanged.setChecked(session->isReannounceWhenAddressChangedEnabled()); + addRow(REANNOUNCE_WHEN_ADDRESS_CHANGED, tr("Reannounce to all trackers when IP or port changed"), &m_checkBoxReannounceWhenAddressChanged); // Download tracker's favicon m_checkBoxTrackerFavicon.setChecked(mainWindow->isDownloadTrackerFavicon()); addRow(DOWNLOAD_TRACKER_FAVICON, tr("Download tracker's favicon"), &m_checkBoxTrackerFavicon); diff --git a/src/gui/advancedsettings.h b/src/gui/advancedsettings.h index 27ed46f1eb9..fae2e4b77fc 100644 --- a/src/gui/advancedsettings.h +++ b/src/gui/advancedsettings.h @@ -66,7 +66,7 @@ private slots: m_spinBoxSendBufferWatermarkFactor, m_spinBoxSocketBacklogSize, m_spinBoxMaxConcurrentHTTPAnnounces, m_spinBoxStopTrackerTimeout, m_spinBoxSavePathHistoryLength, m_spinBoxPeerTurnover, m_spinBoxPeerTurnoverCutoff, m_spinBoxPeerTurnoverInterval; QCheckBox m_checkBoxOsCache, m_checkBoxRecheckCompleted, m_checkBoxResolveCountries, m_checkBoxResolveHosts, - m_checkBoxProgramNotifications, m_checkBoxTorrentAddedNotifications, m_checkBoxTrackerFavicon, m_checkBoxTrackerStatus, + m_checkBoxProgramNotifications, m_checkBoxTorrentAddedNotifications, m_checkBoxReannounceWhenAddressChanged, m_checkBoxTrackerFavicon, m_checkBoxTrackerStatus, m_checkBoxConfirmTorrentRecheck, m_checkBoxConfirmRemoveAllTags, m_checkBoxAnnounceAllTrackers, m_checkBoxAnnounceAllTiers, m_checkBoxMultiConnectionsPerIp, m_checkBoxValidateHTTPSTrackerCertificate, m_checkBoxBlockPeersOnPrivilegedPorts, m_checkBoxPieceExtentAffinity, m_checkBoxSuggestMode, m_checkBoxSpeedWidgetEnabled, m_checkBoxIDNSupport; diff --git a/src/webui/api/appcontroller.cpp b/src/webui/api/appcontroller.cpp index 3a3c01b0473..4dcf2d84d62 100644 --- a/src/webui/api/appcontroller.cpp +++ b/src/webui/api/appcontroller.cpp @@ -277,6 +277,8 @@ void AppController::preferencesAction() data["recheck_completed_torrents"] = pref->recheckTorrentsOnCompletion(); // Resolve peer countries data["resolve_peer_countries"] = pref->resolvePeerCountries(); + // Reannounce to all trackers when ip/port changed + data["reannounce_when_address_changed"] = session->isReannounceWhenAddressChangedEnabled(); // libtorrent preferences // Async IO threads @@ -716,6 +718,9 @@ void AppController::setPreferencesAction() // Resolve peer countries if (hasKey("resolve_peer_countries")) pref->resolvePeerCountries(it.value().toBool()); + // Reannounce to all trackers when ip/port changed + if (hasKey("reannounce_when_address_changed")) + session->setReannounceWhenAddressChangedEnabled(it.value().toBool()); // libtorrent preferences // Async IO threads diff --git a/src/webui/www/private/views/preferences.html b/src/webui/www/private/views/preferences.html index d8e845baa7e..6a3397d99d4 100644 --- a/src/webui/www/private/views/preferences.html +++ b/src/webui/www/private/views/preferences.html @@ -922,6 +922,14 @@ + + + + + + + + @@ -1882,6 +1890,7 @@ $('saveResumeDataInterval').setProperty('value', pref.save_resume_data_interval); $('recheckTorrentsOnCompletion').setProperty('checked', pref.recheck_completed_torrents); $('resolvePeerCountries').setProperty('checked', pref.resolve_peer_countries); + $('reannounceWhenAddressChanged').setProperty('checked', pref.reannounce_when_address_changed); // libtorrent section $('asyncIOThreads').setProperty('value', pref.async_io_threads); $('hashingThreads').setProperty('value', pref.hashing_threads); @@ -2270,6 +2279,7 @@ settings.set('save_resume_data_interval', $('saveResumeDataInterval').getProperty('value')); settings.set('recheck_completed_torrents', $('recheckTorrentsOnCompletion').getProperty('checked')); settings.set('resolve_peer_countries', $('resolvePeerCountries').getProperty('checked')); + settings.set('reannounce_when_address_changed', $('reannounceWhenAddressChanged').getProperty('checked')); // libtorrent section settings.set('async_io_threads', $('asyncIOThreads').getProperty('value'));