Merge pull request #11377 from Chocobo1/listeningIPs

Clean up "network interfaces configuration" code
This commit is contained in:
Mike Tzou 2019-10-24 15:02:18 +08:00 committed by GitHub
commit 0de5cbaa4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 40 deletions

View file

@ -30,6 +30,8 @@
#include <cctype> #include <cctype>
#include <libtorrent/error_code.hpp>
#include <QDataStream> #include <QDataStream>
#include <QFile> #include <QFile>
@ -89,7 +91,7 @@ namespace
bool parseIPAddress(const char *data, lt::address &address) bool parseIPAddress(const char *data, lt::address &address)
{ {
IPv4Parser parser; IPv4Parser parser;
boost::system::error_code ec; lt::error_code ec;
if (parser.tryParse(data)) if (parser.tryParse(data))
address = lt::address_v4(parser.parsed()); address = lt::address_v4(parser.parsed());

View file

@ -1012,7 +1012,7 @@ void Session::processBannedIPs(lt::ip_filter &filter)
{ {
// First, import current filter // First, import current filter
for (const QString &ip : asConst(m_bannedIPs.value())) { for (const QString &ip : asConst(m_bannedIPs.value())) {
boost::system::error_code ec; lt::error_code ec;
const lt::address addr = lt::address::from_string(ip.toLatin1().constData(), ec); const lt::address addr = lt::address::from_string(ip.toLatin1().constData(), ec);
Q_ASSERT(!ec); Q_ASSERT(!ec);
if (!ec) if (!ec)
@ -1131,31 +1131,30 @@ void Session::configure(lt::settings_pack &settingsPack)
if (port > 0) // user specified port if (port > 0) // user specified port
settingsPack.set_int(lt::settings_pack::max_retry_port_bind, 0); settingsPack.set_int(lt::settings_pack::max_retry_port_bind, 0);
for (QString ip : getListeningIPs()) { for (const QString &ip : asConst(getListeningIPs())) {
lt::error_code ec;
std::string interfacesStr;
if (ip.isEmpty()) { if (ip.isEmpty()) {
ip = QLatin1String("0.0.0.0"); const QString anyIP = QHostAddress(QHostAddress::AnyIPv4).toString();
interfacesStr = std::string((QString("%1:%2").arg(ip).arg(port)).toLatin1().constData()); const std::string endpoint = anyIP.toStdString() + ':' + std::to_string(port);
LogMsg(tr("qBittorrent is trying to listen on any interface port: %1" settingsPack.set_str(lt::settings_pack::listen_interfaces, endpoint);
, "e.g: qBittorrent is trying to listen on any interface port: TCP/6881") LogMsg(tr("Trying to listen on IP: %1, port: %2"
.arg(QString::number(port)) , "e.g: Trying to listen on IP: 192.168.0.1, port: 6881")
.arg(anyIP, QString::number(port))
, Log::INFO); , Log::INFO);
settingsPack.set_str(lt::settings_pack::listen_interfaces, interfacesStr);
break; break;
} }
const lt::address addr = lt::address::from_string(ip.toLatin1().constData(), ec); lt::error_code ec;
const lt::address addr = lt::address::from_string(ip.toStdString(), ec);
if (!ec) { if (!ec) {
interfacesStr = std::string((addr.is_v6() ? QString("[%1]:%2") : QString("%1:%2")) const std::string endpoint = (addr.is_v6()
.arg(ip).arg(port).toLatin1().constData()); ? ('[' + addr.to_string() + ']')
LogMsg(tr("qBittorrent is trying to listen on interface %1 port: %2" : addr.to_string())
, "e.g: qBittorrent is trying to listen on interface 192.168.0.1 port: TCP/6881") + ':' + std::to_string(port);
.arg(ip).arg(port) settingsPack.set_str(lt::settings_pack::listen_interfaces, endpoint);
LogMsg(tr("Trying to listen on IP: %1, port: %2"
, "e.g: Trying to listen on IP: 192.168.0.1, port: 6881")
.arg(ip, QString::number(port))
, Log::INFO); , Log::INFO);
settingsPack.set_str(lt::settings_pack::listen_interfaces, interfacesStr);
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
chosenIP = ip; chosenIP = ip;
#endif #endif
@ -1165,22 +1164,24 @@ void Session::configure(lt::settings_pack &settingsPack)
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
// On Vista+ versions and after Qt 5.5 QNetworkInterface::name() returns // On Vista+ versions and after Qt 5.5 QNetworkInterface::name() returns
// the interface's Luid and not the GUID. // the interface's LUID and not the GUID.
// Libtorrent expects GUIDs for the 'outgoing_interfaces' setting. // Libtorrent expects GUIDs for the 'outgoing_interfaces' setting.
if (!networkInterface().isEmpty()) { const QString netInterface = networkInterface();
const QString guid = convertIfaceNameToGuid(networkInterface()); if (!netInterface.isEmpty()) {
const QString guid = convertIfaceNameToGuid(netInterface);
if (!guid.isEmpty()) { if (!guid.isEmpty()) {
settingsPack.set_str(lt::settings_pack::outgoing_interfaces, guid.toStdString()); settingsPack.set_str(lt::settings_pack::outgoing_interfaces, guid.toStdString());
} }
else { else {
settingsPack.set_str(lt::settings_pack::outgoing_interfaces, chosenIP.toStdString()); settingsPack.set_str(lt::settings_pack::outgoing_interfaces, chosenIP.toStdString());
LogMsg(tr("Could not get GUID of configured network interface. Binding to IP %1").arg(chosenIP) LogMsg(tr("Could not get GUID of configured network interface. Binding to IP: %1").arg(chosenIP)
, Log::WARNING); , Log::WARNING);
} }
} }
#else #else
settingsPack.set_str(lt::settings_pack::outgoing_interfaces, networkInterface().toStdString()); settingsPack.set_str(lt::settings_pack::outgoing_interfaces, networkInterface().toStdString());
#endif // Q_OS_WIN #endif // Q_OS_WIN
m_listenInterfaceChanged = false; m_listenInterfaceChanged = false;
} }
@ -1611,7 +1612,7 @@ void Session::banIP(const QString &ip)
QStringList bannedIPs = m_bannedIPs; QStringList bannedIPs = m_bannedIPs;
if (!bannedIPs.contains(ip)) { if (!bannedIPs.contains(ip)) {
lt::ip_filter filter = m_nativeSession->get_ip_filter(); lt::ip_filter filter = m_nativeSession->get_ip_filter();
boost::system::error_code ec; lt::error_code ec;
const lt::address addr = lt::address::from_string(ip.toLatin1().constData(), ec); const lt::address addr = lt::address::from_string(ip.toLatin1().constData(), ec);
Q_ASSERT(!ec); Q_ASSERT(!ec);
if (ec) return; if (ec) return;
@ -2298,7 +2299,7 @@ void Session::networkConfigurationChange(const QNetworkConfiguration &cfg)
} }
} }
const QStringList Session::getListeningIPs() QStringList Session::getListeningIPs() const
{ {
QStringList IPs; QStringList IPs;
@ -4131,7 +4132,7 @@ void Session::handlePortmapAlert(const lt::portmap_alert *p)
void Session::handlePeerBlockedAlert(const lt::peer_blocked_alert *p) void Session::handlePeerBlockedAlert(const lt::peer_blocked_alert *p)
{ {
boost::system::error_code ec; lt::error_code ec;
#if LIBTORRENT_VERSION_NUM < 10200 #if LIBTORRENT_VERSION_NUM < 10200
const std::string ip = p->ip.to_string(ec); const std::string ip = p->ip.to_string(ec);
#else #else
@ -4165,7 +4166,7 @@ void Session::handlePeerBlockedAlert(const lt::peer_blocked_alert *p)
void Session::handlePeerBanAlert(const lt::peer_ban_alert *p) void Session::handlePeerBanAlert(const lt::peer_ban_alert *p)
{ {
boost::system::error_code ec; lt::error_code ec;
#if (LIBTORRENT_VERSION_NUM < 10200) #if (LIBTORRENT_VERSION_NUM < 10200)
const std::string ip = p->ip.address().to_string(ec); const std::string ip = p->ip.address().to_string(ec);
#else #else
@ -4243,9 +4244,9 @@ void Session::handleListenSucceededAlert(const lt::listen_succeeded_alert *p)
} }
#endif #endif
boost::system::error_code ec; lt::error_code ec;
LogMsg(tr("qBittorrent is successfully listening on interface %1 port: %2/%3" LogMsg(tr("Successfully listening on IP: %1, port: %2/%3"
, "e.g: qBittorrent is successfully listening on interface 192.168.0.1 port: TCP/6881") , "e.g: Successfully listening on IP: 192.168.0.1, port: TCP/6881")
#if (LIBTORRENT_VERSION_NUM < 10200) #if (LIBTORRENT_VERSION_NUM < 10200)
.arg(p->endpoint.address().to_string(ec).c_str(), proto, QString::number(p->endpoint.port())), Log::INFO); .arg(p->endpoint.address().to_string(ec).c_str(), proto, QString::number(p->endpoint.port())), Log::INFO);
#else #else
@ -4306,9 +4307,9 @@ void Session::handleListenFailedAlert(const lt::listen_failed_alert *p)
} }
#endif #endif
boost::system::error_code ec; lt::error_code ec;
LogMsg(tr("qBittorrent failed listening on interface %1 port: %2/%3. Reason: %4." LogMsg(tr("Failed to listen on IP: %1, port: %2/%3. Reason: %4"
, "e.g: qBittorrent failed listening on interface 192.168.0.1 port: TCP/6881. Reason: already in use.") , "e.g: Failed to listen on IP: 192.168.0.1, port: TCP/6881. Reason: already in use")
#if (LIBTORRENT_VERSION_NUM < 10200) #if (LIBTORRENT_VERSION_NUM < 10200)
.arg(p->endpoint.address().to_string(ec).c_str(), proto, QString::number(p->endpoint.port()) .arg(p->endpoint.address().to_string(ec).c_str(), proto, QString::number(p->endpoint.port())
, QString::fromLocal8Bit(p->error.message().c_str())), Log::CRITICAL); , QString::fromLocal8Bit(p->error.message().c_str())), Log::CRITICAL);
@ -4320,8 +4321,9 @@ void Session::handleListenFailedAlert(const lt::listen_failed_alert *p)
void Session::handleExternalIPAlert(const lt::external_ip_alert *p) void Session::handleExternalIPAlert(const lt::external_ip_alert *p)
{ {
boost::system::error_code ec; lt::error_code ec;
LogMsg(tr("External IP: %1", "e.g. External IP: 192.168.0.1").arg(p->external_address.to_string(ec).c_str()), Log::INFO); LogMsg(tr("Detected external IP: %1", "e.g. Detected external IP: 1.1.1.1")
.arg(p->external_address.to_string(ec).c_str()), Log::INFO);
} }
void Session::handleSessionStatsAlert(const lt::session_stats_alert *p) void Session::handleSessionStatsAlert(const lt::session_stats_alert *p)

View file

@ -520,7 +520,7 @@ namespace BitTorrent
void adjustLimits(); void adjustLimits();
void applyBandwidthLimits(); void applyBandwidthLimits();
void processBannedIPs(lt::ip_filter &filter); void processBannedIPs(lt::ip_filter &filter);
const QStringList getListeningIPs(); QStringList getListeningIPs() const;
void configureListeningInterface(); void configureListeningInterface();
void enableTracker(bool enable); void enableTracker(bool enable);
void enableBandwidthScheduler(); void enableBandwidthScheduler();

View file

@ -1071,7 +1071,11 @@ qulonglong TorrentHandle::eta() const
QVector<qreal> TorrentHandle::filesProgress() const QVector<qreal> TorrentHandle::filesProgress() const
{ {
#if (LIBTORRENT_VERSION_NUM < 10200)
std::vector<boost::int64_t> fp; std::vector<boost::int64_t> fp;
#else
std::vector<int64_t> fp;
#endif
m_nativeHandle.file_progress(fp, lt::torrent_handle::piece_granularity); m_nativeHandle.file_progress(fp, lt::torrent_handle::piece_granularity);
const int count = static_cast<int>(fp.size()); const int count = static_cast<int>(fp.size());
@ -1267,15 +1271,25 @@ int TorrentHandle::maxSeedingTime() const
qreal TorrentHandle::realRatio() const qreal TorrentHandle::realRatio() const
{ {
#if (LIBTORRENT_VERSION_NUM < 10200)
const boost::int64_t upload = m_nativeStatus.all_time_upload; const boost::int64_t upload = m_nativeStatus.all_time_upload;
// special case for a seeder who lost its stats, also assume nobody will import a 99% done torrent // special case for a seeder who lost its stats, also assume nobody will import a 99% done torrent
const boost::int64_t download = (m_nativeStatus.all_time_download < m_nativeStatus.total_done * 0.01) ? m_nativeStatus.total_done : m_nativeStatus.all_time_download; const boost::int64_t download = (m_nativeStatus.all_time_download < (m_nativeStatus.total_done * 0.01))
? m_nativeStatus.total_done
: m_nativeStatus.all_time_download;
#else
const int64_t upload = m_nativeStatus.all_time_upload;
// special case for a seeder who lost its stats, also assume nobody will import a 99% done torrent
const int64_t download = (m_nativeStatus.all_time_download < (m_nativeStatus.total_done * 0.01))
? m_nativeStatus.total_done
: m_nativeStatus.all_time_download;
#endif
if (download == 0) if (download == 0)
return (upload == 0) ? 0.0 : MAX_RATIO; return (upload == 0) ? 0 : MAX_RATIO;
const qreal ratio = upload / static_cast<qreal>(download); const qreal ratio = upload / static_cast<qreal>(download);
Q_ASSERT(ratio >= 0.0); Q_ASSERT(ratio >= 0);
return (ratio > MAX_RATIO) ? MAX_RATIO : ratio; return (ratio > MAX_RATIO) ? MAX_RATIO : ratio;
} }