Add port forwarding option for embedded tracker

Closes #17781.
PR #17981.
This commit is contained in:
Chocobo1 2022-11-07 11:32:11 +08:00 committed by GitHub
parent c80238d66f
commit f6735401f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 54 additions and 10 deletions

View file

@ -80,6 +80,7 @@
#include "base/logger.h"
#include "base/net/downloadmanager.h"
#include "base/net/proxyconfigurationmanager.h"
#include "base/preferences.h"
#include "base/profile.h"
#include "base/torrentfileguard.h"
#include "base/torrentfilter.h"
@ -548,8 +549,6 @@ SessionImpl::SessionImpl(QObject *parent)
if (isExcludedFileNamesEnabled())
populateExcludedFileNamesRegExpList();
enableTracker(isTrackerEnabled());
connect(Net::ProxyConfigurationManager::instance()
, &Net::ProxyConfigurationManager::proxyConfigurationChanged
, this, &SessionImpl::configureDeferred);
@ -569,11 +568,14 @@ SessionImpl::SessionImpl(QObject *parent)
m_ioThread->start();
initMetrics();
loadStatistics();
// initialize PortForwarder instance
new PortForwarderImpl(m_nativeSession);
initMetrics();
loadStatistics();
// start embedded tracker
enableTracker(isTrackerEnabled());
prepareStartup();
}
@ -1980,16 +1982,27 @@ void SessionImpl::configurePeerClasses()
void SessionImpl::enableTracker(const bool enable)
{
const QString profile = u"embeddedTracker"_qs;
auto *portForwarder = Net::PortForwarder::instance();
if (enable)
{
if (!m_tracker)
m_tracker = new Tracker(this);
m_tracker->start();
const auto *pref = Preferences::instance();
if (pref->isTrackerPortForwardingEnabled())
portForwarder->setPorts(profile, {static_cast<quint16>(pref->getTrackerPort())});
else
portForwarder->removePorts(profile);
}
else
{
delete m_tracker;
portForwarder->removePorts(profile);
}
}

View file

@ -203,12 +203,12 @@ Tracker::Tracker(QObject *parent)
bool Tracker::start()
{
const QHostAddress ip = QHostAddress::Any;
const int port = Preferences::instance()->getTrackerPort();
if (m_server->isListening())
{
if (m_server->serverPort() == port)
if (const int oldPort = m_server->serverPort()
; oldPort == port)
{
// Already listening on the right port, just return
return true;
@ -218,9 +218,9 @@ bool Tracker::start()
m_server->close();
}
// Listen on the predefined port
// Listen on port
const QHostAddress ip = QHostAddress::Any;
const bool listenSuccess = m_server->listen(ip, port);
if (listenSuccess)
{
LogMsg(tr("Embedded Tracker: Now listening on IP: %1, port: %2")

View file

@ -1164,6 +1164,16 @@ void Preferences::setTrackerPort(const int port)
setValue(u"Preferences/Advanced/trackerPort"_qs, port);
}
bool Preferences::isTrackerPortForwardingEnabled() const
{
return value(u"Preferences/Advanced/trackerPortForwarding"_qs, false);
}
void Preferences::setTrackerPortForwardingEnabled(const bool enabled)
{
setValue(u"Preferences/Advanced/trackerPortForwarding"_qs, enabled);
}
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
bool Preferences::isUpdateCheckEnabled() const
{

View file

@ -299,6 +299,8 @@ public:
#endif
int getTrackerPort() const;
void setTrackerPort(int port);
bool isTrackerPortForwardingEnabled() const;
void setTrackerPortForwardingEnabled(bool enabled);
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
bool isUpdateCheckEnabled() const;
void setUpdateCheckEnabled(bool enabled);

View file

@ -97,6 +97,7 @@ namespace
// embedded tracker
TRACKER_STATUS,
TRACKER_PORT,
TRACKER_PORT_FORWARDING,
// libtorrent section
LIBTORRENT_HEADER,
ASYNC_IO_THREADS,
@ -292,7 +293,9 @@ void AdvancedSettings::saveAdvancedSettings() const
// Tracker
pref->setTrackerPort(m_spinBoxTrackerPort.value());
pref->setTrackerPortForwardingEnabled(m_checkBoxTrackerPortForwarding.isChecked());
session->setTrackerEnabled(m_checkBoxTrackerStatus.isChecked());
// Choking algorithm
session->setChokingAlgorithm(m_comboBoxChokingAlgorithm.currentData().value<BitTorrent::ChokingAlgorithm>());
// Seed choking algorithm
@ -732,6 +735,9 @@ void AdvancedSettings::loadAdvancedSettings()
m_spinBoxTrackerPort.setMaximum(65535);
m_spinBoxTrackerPort.setValue(pref->getTrackerPort());
addRow(TRACKER_PORT, tr("Embedded tracker port"), &m_spinBoxTrackerPort);
// Tracker port forwarding
m_checkBoxTrackerPortForwarding.setChecked(pref->isTrackerPortForwardingEnabled());
addRow(TRACKER_PORT_FORWARDING, tr("Enable port forwarding for embedded tracker"), &m_checkBoxTrackerPortForwarding);
// Choking algorithm
m_comboBoxChokingAlgorithm.addItem(tr("Fixed slots"), QVariant::fromValue(BitTorrent::ChokingAlgorithm::FixedSlots));
m_comboBoxChokingAlgorithm.addItem(tr("Upload rate based"), QVariant::fromValue(BitTorrent::ChokingAlgorithm::RateBased));

View file

@ -68,7 +68,7 @@ private:
m_spinBoxSavePathHistoryLength, m_spinBoxPeerTurnover, m_spinBoxPeerTurnoverCutoff, m_spinBoxPeerTurnoverInterval, m_spinBoxRequestQueueSize;
QCheckBox m_checkBoxOsCache, m_checkBoxRecheckCompleted, m_checkBoxResolveCountries, m_checkBoxResolveHosts,
m_checkBoxProgramNotifications, m_checkBoxTorrentAddedNotifications, m_checkBoxReannounceWhenAddressChanged, m_checkBoxTrackerFavicon, m_checkBoxTrackerStatus,
m_checkBoxConfirmTorrentRecheck, m_checkBoxConfirmRemoveAllTags, m_checkBoxAnnounceAllTrackers, m_checkBoxAnnounceAllTiers,
m_checkBoxTrackerPortForwarding, m_checkBoxConfirmTorrentRecheck, m_checkBoxConfirmRemoveAllTags, m_checkBoxAnnounceAllTrackers, m_checkBoxAnnounceAllTiers,
m_checkBoxMultiConnectionsPerIp, m_checkBoxValidateHTTPSTrackerCertificate, m_checkBoxSSRFMitigation, m_checkBoxBlockPeersOnPrivilegedPorts, m_checkBoxPieceExtentAffinity,
m_checkBoxSuggestMode, m_checkBoxSpeedWidgetEnabled, m_checkBoxIDNSupport;
QComboBox m_comboBoxInterface, m_comboBoxInterfaceAddress, m_comboBoxDiskIOReadMode, m_comboBoxDiskIOWriteMode, m_comboBoxUtpMixedMode, m_comboBoxChokingAlgorithm,

View file

@ -372,6 +372,7 @@ void AppController::preferencesAction()
// Embedded tracker
data[u"enable_embedded_tracker"_qs] = session->isTrackerEnabled();
data[u"embedded_tracker_port"_qs] = pref->getTrackerPort();
data[u"embedded_tracker_port_forwarding"_qs] = pref->isTrackerPortForwardingEnabled();
// Choking algorithm
data[u"upload_slots_behavior"_qs] = static_cast<int>(session->chokingAlgorithm());
// Seed choking algorithm
@ -899,6 +900,8 @@ void AppController::setPreferencesAction()
// Embedded tracker
if (hasKey(u"embedded_tracker_port"_qs))
pref->setTrackerPort(it.value().toInt());
if (hasKey(u"embedded_tracker_port_forwarding"_qs))
pref->setTrackerPortForwardingEnabled(it.value().toBool());
if (hasKey(u"enable_embedded_tracker"_qs))
session->setTrackerEnabled(it.value().toBool());
// Choking algorithm

View file

@ -52,7 +52,7 @@
#include "base/utils/version.h"
#include "api/isessionmanager.h"
inline const Utils::Version<3, 2> API_VERSION {2, 8, 17};
inline const Utils::Version<3, 2> API_VERSION {2, 8, 18};
class APIController;
class AuthController;

View file

@ -1016,6 +1016,14 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
<input type="text" id="embeddedTrackerPort" style="width: 15em;" />
</td>
</tr>
<tr>
<td>
<label for="embeddedTrackerPortForwarding">QBT_TR(Enable port forwarding for embedded tracker:)QBT_TR[CONTEXT=OptionsDialog]</label>
</td>
<td>
<input type="checkbox" id="embeddedTrackerPortForwarding" />
</td>
</tr>
</table>
</fieldset>
<fieldset class="settings">
@ -2099,6 +2107,7 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
$('blockPeersOnPrivilegedPorts').setProperty('checked', pref.block_peers_on_privileged_ports);
$('enableEmbeddedTracker').setProperty('checked', pref.enable_embedded_tracker);
$('embeddedTrackerPort').setProperty('value', pref.embedded_tracker_port);
$('embeddedTrackerPortForwarding').setProperty('checked', pref.embedded_tracker_port_forwarding);
$('uploadSlotsBehavior').setProperty('value', pref.upload_slots_behavior);
$('uploadChokingAlgorithm').setProperty('value', pref.upload_choking_algorithm);
$('announceAllTrackers').setProperty('checked', pref.announce_to_all_trackers);
@ -2526,6 +2535,7 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
settings.set('block_peers_on_privileged_ports', $('blockPeersOnPrivilegedPorts').getProperty('checked'));
settings.set('enable_embedded_tracker', $('enableEmbeddedTracker').getProperty('checked'));
settings.set('embedded_tracker_port', $('embeddedTrackerPort').getProperty('value'));
settings.set('embedded_tracker_port_forwarding', $('embeddedTrackerPortForwarding').getProperty('checked'));
settings.set('upload_slots_behavior', $('uploadSlotsBehavior').getProperty('value'));
settings.set('upload_choking_algorithm', $('uploadChokingAlgorithm').getProperty('value'));
settings.set('announce_to_all_trackers', $('announceAllTrackers').getProperty('checked'));