Merge pull request #5877 from dzmat/to_pull_request

locally banned IP list managment (Closes #1837 )
This commit is contained in:
sledgehammer999
2017-03-07 23:14:20 +02:00
committed by GitHub
14 changed files with 456 additions and 6 deletions

View File

@@ -52,6 +52,7 @@ HEADERS += \
$$PWD/utils/fs.h \
$$PWD/utils/gzip.h \
$$PWD/utils/misc.h \
$$PWD/utils/net.h \
$$PWD/utils/random.h \
$$PWD/utils/string.h \
$$PWD/unicodestrings.h \
@@ -108,6 +109,7 @@ SOURCES += \
$$PWD/utils/fs.cpp \
$$PWD/utils/gzip.cpp \
$$PWD/utils/misc.cpp \
$$PWD/utils/net.cpp \
$$PWD/utils/random.cpp \
$$PWD/utils/string.cpp \
$$PWD/torrentfileguard.cpp \

View File

@@ -1,4 +1,3 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
@@ -74,6 +73,7 @@
#include "base/torrentfileguard.h"
#include "base/torrentfilter.h"
#include "base/unicodestrings.h"
#include "base/utils/net.h"
#include "base/utils/misc.h"
#include "base/utils/fs.h"
#include "base/utils/random.h"
@@ -275,7 +275,15 @@ Session::Session(QObject *parent)
, m_isDisableAutoTMMWhenDefaultSavePathChanged(BITTORRENT_SESSION_KEY("DisableAutoTMMTriggers/DefaultSavePathChanged"), true)
, m_isDisableAutoTMMWhenCategorySavePathChanged(BITTORRENT_SESSION_KEY("DisableAutoTMMTriggers/CategorySavePathChanged"), true)
, m_isTrackerEnabled(BITTORRENT_KEY("TrackerEnabled"), false)
, m_bannedIPs("State/BannedIPs")
, m_bannedIPs("State/BannedIPs"
, QStringList()
, [](const QStringList &value)
{
QStringList tmp = value;
tmp.sort();
return tmp;
}
)
, m_wasPexEnabled(m_isPeXEnabled)
, m_numResumeData(0)
, m_extraLimit(0)
@@ -1376,6 +1384,7 @@ void Session::banIP(const QString &ip)
m_nativeSession->set_ip_filter(filter);
bannedIPs << ip;
bannedIPs.sort();
m_bannedIPs = bannedIPs;
}
}
@@ -2379,6 +2388,45 @@ void Session::setIPFilterFile(QString path)
}
}
void Session::setBannedIPs(const QStringList &newList)
{
if (newList == m_bannedIPs)
return; // do nothing
// here filter out incorrect IP
QStringList filteredList;
for (const QString &ip : newList) {
if (Utils::Net::isValidIP(ip)) {
// the same IPv6 addresses could be written in different forms;
// QHostAddress::toString() result format follows RFC5952;
// thus we avoid duplicate entries pointing to the same address
filteredList << QHostAddress(ip).toString();
}
else {
Logger::instance()->addMessage(
tr("%1 is not a valid IP address and was rejected while applying the list of banned addresses.")
.arg(ip)
, Log::WARNING);
}
}
// now we have to sort IPs and make them unique
filteredList.sort();
filteredList.removeDuplicates();
// Again ensure that the new list is different from the stored one.
if (filteredList == m_bannedIPs)
return; // do nothing
// store to session settings
// also here we have to recreate filter list including 3rd party ban file
// and install it again into m_session
m_bannedIPs = filteredList;
m_IPFilteringChanged = true;
configureDeferred();
}
QStringList Session::bannedIPs() const
{
return m_bannedIPs;
}
int Session::maxConnectionsPerTorrent() const
{
return m_maxConnectionsPerTorrent;
@@ -2976,15 +3024,20 @@ void Session::configureDeferred()
}
// Enable IP Filtering
// this method creates ban list from scratch combining user ban list and 3rd party ban list file
void Session::enableIPFilter()
{
qDebug("Enabling IPFilter");
// 1. Clear existing ban list
// 2. Add manual ban list
// 3. Add 3rd party ban list
m_nativeSession->set_ip_filter(libt::ip_filter());
processBannedIPs();
if (!m_filterParser) {
m_filterParser = new FilterParserThread(m_nativeSession, this);
connect(m_filterParser.data(), SIGNAL(IPFilterParsed(int)), SLOT(handleIPFilterParsed(int)));
connect(m_filterParser.data(), SIGNAL(IPFilterError()), SLOT(handleIPFilterError()));
}
m_filterParser->processFilterFile(IPFilterFile());
}

View File

@@ -326,6 +326,8 @@ namespace BitTorrent
void setUTPRateLimited(bool limited);
bool isTrackerFilteringEnabled() const;
void setTrackerFilteringEnabled(bool enabled);
QStringList bannedIPs() const;
void setBannedIPs(const QStringList &list);
TorrentHandle *findTorrent(const InfoHash &hash) const;
QHash<InfoHash, TorrentHandle *> torrents() const;

42
src/base/utils/net.cpp Normal file
View File

@@ -0,0 +1,42 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2016 Alexandr Milovantsev <dzmat@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "net.h"
#include <QHostAddress>
#include <QString>
namespace Utils
{
namespace Net
{
bool isValidIP(const QString &ip)
{
return !QHostAddress(ip).isNull();
}
}
}

41
src/base/utils/net.h Normal file
View File

@@ -0,0 +1,41 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2016 Alexandr Milovantsev <dzmat@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#ifndef BASE_UTILS_NET_H
#define BASE_UTILS_NET_H
class QString;
namespace Utils
{
namespace Net
{
bool isValidIP(const QString &ip);
}
}
#endif // BASE_UTILS_NET_H