diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 5b1c1f18d..fdf086534 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -244,7 +244,7 @@ Session *Session::m_instance = nullptr; Session::Session(QObject *parent) : QObject(parent) , m_deferredConfigureScheduled(false) - , m_IPFilteringChanged(false) + , m_IPFilteringChanged(true) , m_listenInterfaceChanged(true) , m_isDHTEnabled(BITTORRENT_SESSION_KEY("DHTEnabled"), true) , m_isLSDEnabled(BITTORRENT_SESSION_KEY("LSDEnabled"), true) @@ -364,91 +364,12 @@ Session::Session(QObject *parent) m_seedingLimitTimer->setInterval(10000); connect(m_seedingLimitTimer, &QTimer::timeout, this, &Session::processShareLimits); - // Set severity level of libtorrent session - const LTAlertCategory alertMask = lt::alert::error_notification - | lt::alert::peer_notification - | lt::alert::port_mapping_notification - | lt::alert::storage_notification - | lt::alert::tracker_notification - | lt::alert::status_notification - | lt::alert::ip_block_notification - | lt::alert::performance_warning - | lt::alert::file_progress_notification; - - const std::string peerId = lt::generate_fingerprint(PEER_ID, QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD); - lt::settings_pack pack; - pack.set_int(lt::settings_pack::alert_mask, alertMask); - pack.set_str(lt::settings_pack::peer_fingerprint, peerId); - pack.set_bool(lt::settings_pack::listen_system_port_fallback, false); - pack.set_str(lt::settings_pack::user_agent, USER_AGENT); - pack.set_bool(lt::settings_pack::use_dht_as_fallback, false); - // Speed up exit - pack.set_int(lt::settings_pack::stop_tracker_timeout, 1); - pack.set_int(lt::settings_pack::auto_scrape_interval, 1200); // 20 minutes - pack.set_int(lt::settings_pack::auto_scrape_min_interval, 900); // 15 minutes - pack.set_int(lt::settings_pack::connection_speed, 20); // default is 10 - pack.set_bool(lt::settings_pack::no_connect_privileged_ports, false); - // libtorrent 1.1 enables UPnP & NAT-PMP by default - // turn them off before `lt::session` ctor to avoid split second effects - pack.set_bool(lt::settings_pack::enable_upnp, false); - pack.set_bool(lt::settings_pack::enable_natpmp, false); - pack.set_bool(lt::settings_pack::upnp_ignore_nonrouters, true); - -#if (LIBTORRENT_VERSION_NUM < 10200) - // Disable support for SSL torrents for now - pack.set_int(lt::settings_pack::ssl_listen, 0); - // To prevent ISPs from blocking seeding - pack.set_bool(lt::settings_pack::lazy_bitfields, true); - // Disk cache pool is rarely tested in libtorrent and doesn't free buffers - // Soon to be deprecated there - // More info: https://github.com/arvidn/libtorrent/issues/2251 - pack.set_bool(lt::settings_pack::use_disk_cache_pool, false); -#endif - - configure(pack); - - m_nativeSession = new lt::session {pack, LTSessionFlags {0}}; - m_nativeSession->set_alert_notify([this]() - { -#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) - QMetaObject::invokeMethod(this, &Session::readAlerts, Qt::QueuedConnection); -#else - QMetaObject::invokeMethod(this, "readAlerts", Qt::QueuedConnection); -#endif - }); - - configurePeerClasses(); - - // Enabling plugins - //m_nativeSession->add_extension(<::create_metadata_plugin); - m_nativeSession->add_extension(<::create_ut_metadata_plugin); - if (isPeXEnabled()) - m_nativeSession->add_extension(<::create_ut_pex_plugin); - m_nativeSession->add_extension(<::create_smart_ban_plugin); - - LogMsg(tr("Peer ID: ") + QString::fromStdString(peerId)); - LogMsg(tr("HTTP User-Agent is '%1'").arg(USER_AGENT)); - LogMsg(tr("DHT support [%1]").arg(isDHTEnabled() ? tr("ON") : tr("OFF")), Log::INFO); - LogMsg(tr("Local Peer Discovery support [%1]").arg(isLSDEnabled() ? tr("ON") : tr("OFF")), Log::INFO); - LogMsg(tr("PeX support [%1]").arg(isPeXEnabled() ? tr("ON") : tr("OFF")), Log::INFO); - LogMsg(tr("Anonymous mode [%1]").arg(isAnonymousModeEnabled() ? tr("ON") : tr("OFF")), Log::INFO); - LogMsg(tr("Encryption support [%1]").arg((encryption() == 0) ? tr("ON") : - ((encryption() == 1) ? tr("FORCED") : tr("OFF"))), Log::INFO); + initializeNativeSession(); + configureComponents(); if (isBandwidthSchedulerEnabled()) enableBandwidthScheduler(); - if (isIPFilteringEnabled()) { - // Manually banned IPs are handled in that function too(in the slots) - enableIPFilter(); - } - else { - // Add the banned IPs - lt::ip_filter filter; - processBannedIPs(filter); - m_nativeSession->set_ip_filter(filter); - } - m_categories = map_cast(m_storedCategories); if (isSubcategoriesEnabled()) { // if subcategories support changed manually @@ -987,13 +908,23 @@ void Session::applyBandwidthLimits() m_nativeSession->apply_settings(settingsPack); } -// Set BitTorrent session configuration void Session::configure() { - qDebug("Configuring session"); lt::settings_pack settingsPack = m_nativeSession->get_settings(); - configure(settingsPack); + loadLTSettings(settingsPack); m_nativeSession->apply_settings(settingsPack); + + configureComponents(); + + m_deferredConfigureScheduled = false; +} + +void Session::configureComponents() +{ + // This function contains components/actions that: + // 1. Need to be setup at start up + // 2. When deferred configure is called + configurePeerClasses(); if (m_IPFilteringChanged) { @@ -1003,9 +934,76 @@ void Session::configure() disableIPFilter(); m_IPFilteringChanged = false; } +} - m_deferredConfigureScheduled = false; - qDebug("Session configured"); +void Session::initializeNativeSession() +{ + const LTAlertCategory alertMask = lt::alert::error_notification + | lt::alert::file_progress_notification + | lt::alert::ip_block_notification + | lt::alert::peer_notification + | lt::alert::performance_warning + | lt::alert::port_mapping_notification + | lt::alert::status_notification + | lt::alert::storage_notification + | lt::alert::tracker_notification; + const std::string peerId = lt::generate_fingerprint(PEER_ID, QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD); + + lt::settings_pack pack; + pack.set_int(lt::settings_pack::alert_mask, alertMask); + pack.set_str(lt::settings_pack::peer_fingerprint, peerId); + pack.set_bool(lt::settings_pack::listen_system_port_fallback, false); + pack.set_str(lt::settings_pack::user_agent, USER_AGENT); + pack.set_bool(lt::settings_pack::use_dht_as_fallback, false); + // Speed up exit + pack.set_int(lt::settings_pack::stop_tracker_timeout, 1); + pack.set_int(lt::settings_pack::auto_scrape_interval, 1200); // 20 minutes + pack.set_int(lt::settings_pack::auto_scrape_min_interval, 900); // 15 minutes + pack.set_int(lt::settings_pack::connection_speed, 20); // default is 10 + pack.set_bool(lt::settings_pack::no_connect_privileged_ports, false); + // libtorrent 1.1 enables UPnP & NAT-PMP by default + // turn them off before `lt::session` ctor to avoid split second effects + pack.set_bool(lt::settings_pack::enable_upnp, false); + pack.set_bool(lt::settings_pack::enable_natpmp, false); + pack.set_bool(lt::settings_pack::upnp_ignore_nonrouters, true); + +#if (LIBTORRENT_VERSION_NUM < 10200) + // Disable support for SSL torrents for now + pack.set_int(lt::settings_pack::ssl_listen, 0); + // To prevent ISPs from blocking seeding + pack.set_bool(lt::settings_pack::lazy_bitfields, true); + // Disk cache pool is rarely tested in libtorrent and doesn't free buffers + // Soon to be deprecated there + // More info: https://github.com/arvidn/libtorrent/issues/2251 + pack.set_bool(lt::settings_pack::use_disk_cache_pool, false); +#endif + + loadLTSettings(pack); + m_nativeSession = new lt::session {pack, LTSessionFlags {0}}; + + LogMsg(tr("Peer ID: ") + QString::fromStdString(peerId)); + LogMsg(tr("HTTP User-Agent is '%1'").arg(USER_AGENT)); + LogMsg(tr("DHT support [%1]").arg(isDHTEnabled() ? tr("ON") : tr("OFF")), Log::INFO); + LogMsg(tr("Local Peer Discovery support [%1]").arg(isLSDEnabled() ? tr("ON") : tr("OFF")), Log::INFO); + LogMsg(tr("PeX support [%1]").arg(isPeXEnabled() ? tr("ON") : tr("OFF")), Log::INFO); + LogMsg(tr("Anonymous mode [%1]").arg(isAnonymousModeEnabled() ? tr("ON") : tr("OFF")), Log::INFO); + LogMsg(tr("Encryption support [%1]").arg((encryption() == 0) ? tr("ON") : + ((encryption() == 1) ? tr("FORCED") : tr("OFF"))), Log::INFO); + + m_nativeSession->set_alert_notify([this]() + { +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) + QMetaObject::invokeMethod(this, &Session::readAlerts, Qt::QueuedConnection); +#else + QMetaObject::invokeMethod(this, "readAlerts", Qt::QueuedConnection); +#endif + }); + + // Enabling plugins + m_nativeSession->add_extension(<::create_smart_ban_plugin); + m_nativeSession->add_extension(<::create_ut_metadata_plugin); + if (isPeXEnabled()) + m_nativeSession->add_extension(<::create_ut_pex_plugin); } void Session::processBannedIPs(lt::ip_filter &filter) @@ -1117,7 +1115,7 @@ void Session::initMetrics() Q_ASSERT(m_metricIndices.disk.diskJobTime >= 0); } -void Session::configure(lt::settings_pack &settingsPack) +void Session::loadLTSettings(lt::settings_pack &settingsPack) { // from libtorrent doc: // It will not take affect until the listen_interfaces settings is updated @@ -3573,16 +3571,17 @@ void Session::initResumeFolder() void Session::configureDeferred() { - if (!m_deferredConfigureScheduled) { + if (m_deferredConfigureScheduled) + return; + m_deferredConfigureScheduled = true; + #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) - QMetaObject::invokeMethod(this - , qOverload<>(&Session::configure) - , Qt::QueuedConnection); + QMetaObject::invokeMethod(this + , qOverload<>(&Session::configure) + , Qt::QueuedConnection); #else - QMetaObject::invokeMethod(this, "configure", Qt::QueuedConnection); + QMetaObject::invokeMethod(this, "configure", Qt::QueuedConnection); #endif - m_deferredConfigureScheduled = true; - } } // Enable IP Filtering diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 6f647d895..deddab567 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -512,7 +512,9 @@ namespace BitTorrent // Session configuration Q_INVOKABLE void configure(); - void configure(lt::settings_pack &settingsPack); + void configureComponents(); + void initializeNativeSession(); + void loadLTSettings(lt::settings_pack &settingsPack); void configurePeerClasses(); void adjustLimits(lt::settings_pack &settingsPack); void applyBandwidthLimits(lt::settings_pack &settingsPack) const;