Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement shadowban functionality #545

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/base/bittorrent/peer_shadowban_plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once

#include <QString>
#include <libtorrent/extensions.hpp>
#include <libtorrent/peer_connection_handle.hpp>

#if (LIBTORRENT_VERSION_NUM >= 20000)
using client_data = lt::client_data_t;
#else
using client_data = void *;
#endif

using filter_function = std::function<bool(const lt::peer_info &, bool, bool *)>;

class peer_shadowban_plugin final : public lt::peer_plugin
{
public:
peer_shadowban_plugin(lt::peer_connection_handle p)
: m_peer_connection(p)
{
}

bool on_request(lt::peer_request const &r) override
{
if (is_shadowbanned_peer())
{
return true;
}
return peer_plugin::on_request(r);
}

protected:
bool is_shadowbanned_peer()
{
lt::peer_info info;
m_peer_connection.get_peer_info(info);

QString peer_ip = QString::fromStdString(info.ip.address().to_string());
QStringList shadowbannedIPs =
CachedSettingValue<QStringList>(u"State/ShadowBannedIPs"_s, QStringList(), Algorithm::sorted<QStringList>).get();

return shadowbannedIPs.contains(peer_ip);
}

private:
lt::peer_connection_handle m_peer_connection;
};

class peer_shadowban_action_plugin : public lt::torrent_plugin
{
public:
peer_shadowban_action_plugin()
{
}

std::shared_ptr<lt::peer_plugin> new_connection(lt::peer_connection_handle const &p) override
{
return std::make_shared<peer_shadowban_plugin>(p);
}
};

std::shared_ptr<lt::torrent_plugin> create_peer_shadowban_plugin(const lt::torrent_handle &th, client_data)
{
// ignore private torrents
if (th.torrent_file() && th.torrent_file()->priv())
return nullptr;

return std::make_shared<peer_shadowban_action_plugin>();
}
6 changes: 6 additions & 0 deletions src/base/bittorrent/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ namespace BitTorrent
virtual bool isAutoBanBTPlayerPeerEnabled() const = 0;
virtual void setAutoBanBTPlayerPeer(bool value) = 0;

// Shadowban IP
virtual bool isShadowBanEnabled() const = 0;
virtual void setShadowBan(bool value) = 0;
virtual QStringList shadowBannedIPs() const = 0;
virtual void setShadowBannedIPs(const QStringList &newList) = 0;

// Trackers list
virtual bool isAutoUpdateTrackersEnabled() const = 0;
virtual void setAutoUpdateTrackersEnabled(bool enabled) = 0;
Expand Down
58 changes: 58 additions & 0 deletions src/base/bittorrent/sessionimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
#include "nativesessionextension.h"
#include "peer_blacklist.hpp"
#include "peer_filter_session_plugin.hpp"
#include "peer_shadowban_plugin.hpp"
#include "portforwarderimpl.h"
#include "resumedatastorage.h"
#include "torrentimpl.h"
Expand Down Expand Up @@ -537,6 +538,8 @@ SessionImpl::SessionImpl(QObject *parent)
, m_publicTrackers(BITTORRENT_SESSION_KEY(u"PublicTrackersList"_s))
, m_autoBanUnknownPeer(BITTORRENT_SESSION_KEY(u"AutoBanUnknownPeer"_s), false)
, m_autoBanBTPlayerPeer(BITTORRENT_SESSION_KEY(u"AutoBanBTPlayerPeer"_s), false)
, m_shadowBan(BITTORRENT_SESSION_KEY(u"ShadowBan"_s), false)
, m_shadowBannedIPs(u"State/ShadowBannedIPs"_s, QStringList(), Algorithm::sorted<QStringList>)
, m_isAutoUpdateTrackersEnabled(BITTORRENT_SESSION_KEY(u"AutoUpdateTrackersEnabled"_s), false)
, m_seedingLimitTimer {new QTimer(this)}
, m_resumeDataTimer {new QTimer(this)}
Expand Down Expand Up @@ -1648,6 +1651,8 @@ void SessionImpl::initializeNativeSession()
if (isAutoBanBTPlayerPeerEnabled())
m_nativeSession->add_extension(&create_drop_bittorrent_media_player_plugin);
m_nativeSession->add_extension(std::make_shared<peer_filter_session_plugin>());
if (isShadowBanEnabled())
m_nativeSession->add_extension(&create_peer_shadowban_plugin);

LogMsg(tr("Peer Exchange (PeX) support: %1").arg(isPeXEnabled() ? tr("ON") : tr("OFF")), Log::INFO);
LogMsg(tr("Anonymous mode: %1").arg(isAnonymousModeEnabled() ? tr("ON") : tr("OFF")), Log::INFO);
Expand Down Expand Up @@ -5051,6 +5056,59 @@ void SessionImpl::setAutoBanBTPlayerPeer(bool value)
}
}

bool SessionImpl::isShadowBanEnabled() const
{
return m_shadowBan;
}

void SessionImpl::setShadowBan(bool value)
{
if (value != isShadowBanEnabled()) {
m_shadowBan = value;
LogMsg(tr("Restart is required to toggle Auto Ban Shadow Ban"), Log::WARNING);
}
}

QStringList SessionImpl::shadowBannedIPs() const
{
return m_shadowBannedIPs;
}

void SessionImpl::setShadowBannedIPs(const QStringList &newList)
{
if (newList == m_shadowBannedIPs)
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
{
LogMsg(tr("Rejected invalid IP address while applying the list of shadow banned IP addresses. IP: \"%1\"")
.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_shadowBannedIPs)
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_shadowBannedIPs = filteredList;
configureDeferred();
}

bool SessionImpl::isListening() const
{
return m_nativeSessionExtension->isSessionListening();
Expand Down
8 changes: 8 additions & 0 deletions src/base/bittorrent/sessionimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ namespace BitTorrent
bool isAutoBanBTPlayerPeerEnabled() const override;
void setAutoBanBTPlayerPeer(bool value) override;

// Shadowban Peers
bool isShadowBanEnabled() const override;
void setShadowBan(bool value) override;
QStringList shadowBannedIPs() const override;
void setShadowBannedIPs(const QStringList &newList) override;

// Trackers list
bool isAutoUpdateTrackersEnabled() const override;
void setAutoUpdateTrackersEnabled(bool enabled) override;
Expand Down Expand Up @@ -751,6 +757,8 @@ namespace BitTorrent
CachedSettingValue<QString> m_publicTrackers;
CachedSettingValue<bool> m_autoBanUnknownPeer;
CachedSettingValue<bool> m_autoBanBTPlayerPeer;
CachedSettingValue<bool> m_shadowBan;
CachedSettingValue<QStringList> m_shadowBannedIPs;
CachedSettingValue<bool> m_isAutoUpdateTrackersEnabled;
QTimer *m_updateTimer;

Expand Down
10 changes: 10 additions & 0 deletions src/base/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,16 @@ void Preferences::setAutoBanBTPlayerPeer(const bool checked)
setValue(u"Preferences/Advanced/AutoBanBTPlayerPeer"_s, checked);
}

bool Preferences::getShadowBan() const
{
return value(u"Preferences/Advanced/ShadowBan"_s, false);
}

void Preferences::setShadowBan(const bool checked)
{
setValue(u"Preferences/Advanced/ShadowBan"_s, checked);
}

QString Preferences::customizeTrackersListUrl() const
{
return value(u"Preferences/Bittorrent/CustomizeTrackersListUrl"_s, u"https://cdn.jsdelivr.net/gh/ngosang/trackerslist/trackers_best.txt"_s);
Expand Down
2 changes: 2 additions & 0 deletions src/base/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ class Preferences final : public QObject
void setAutoBanUnknownPeer(const bool checked);
bool getAutoBanBTPlayerPeer() const;
void setAutoBanBTPlayerPeer(const bool checked);
bool getShadowBan() const;
void setShadowBan(const bool checked);
QString customizeTrackersListUrl() const;
void setCustomizeTrackersListUrl(const QString &trackersUrl);

Expand Down
3 changes: 3 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ qt_wrap_ui(UI_HEADERS
search/pluginsourcedialog.ui
search/searchjobwidget.ui
search/searchwidget.ui
shadowbanlistoptionsdialog.ui
shutdownconfirmdialog.ui
speedlimitdialog.ui
statsdialog.ui
Expand Down Expand Up @@ -98,6 +99,7 @@ add_library(qbt_gui STATIC
search/searchjobwidget.h
search/searchsortmodel.h
search/searchwidget.h
shadowbanlistoptionsdialog.h
shutdownconfirmdialog.h
speedlimitdialog.h
statsdialog.h
Expand Down Expand Up @@ -191,6 +193,7 @@ add_library(qbt_gui STATIC
search/searchjobwidget.cpp
search/searchsortmodel.cpp
search/searchwidget.cpp
shadowbanlistoptionsdialog.cpp
shutdownconfirmdialog.cpp
speedlimitdialog.cpp
statsdialog.cpp
Expand Down
3 changes: 3 additions & 0 deletions src/gui/gui.pri
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ HEADERS += \
$$PWD/search/searchjobwidget.h \
$$PWD/search/searchsortmodel.h \
$$PWD/search/searchwidget.h \
$$PWD/shadowbanlistoptionsdialog.h \
$$PWD/shutdownconfirmdialog.h \
$$PWD/speedlimitdialog.h \
$$PWD/statsdialog.h \
Expand Down Expand Up @@ -147,6 +148,7 @@ SOURCES += \
$$PWD/search/searchjobwidget.cpp \
$$PWD/search/searchsortmodel.cpp \
$$PWD/search/searchwidget.cpp \
$$PWD/shadowbanlistoptionsdialog.cpp \
$$PWD/shutdownconfirmdialog.cpp \
$$PWD/speedlimitdialog.cpp \
$$PWD/statsdialog.cpp \
Expand Down Expand Up @@ -209,6 +211,7 @@ FORMS += \
$$PWD/search/pluginsourcedialog.ui \
$$PWD/search/searchjobwidget.ui \
$$PWD/search/searchwidget.ui \
$$PWD/shadowbanlistoptionsdialog.ui \
$$PWD/shutdownconfirmdialog.ui \
$$PWD/speedlimitdialog.ui \
$$PWD/statsdialog.ui \
Expand Down
14 changes: 14 additions & 0 deletions src/gui/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "addnewtorrentdialog.h"
#include "advancedsettings.h"
#include "banlistoptionsdialog.h"
#include "shadowbanlistoptionsdialog.h"
#include "interfaces/iguiapplication.h"
#include "ipsubnetwhitelistoptionsdialog.h"
#include "rss/automatedrssdownloader.h"
Expand Down Expand Up @@ -1436,6 +1437,11 @@ bool OptionsDialog::isIPFilteringEnabled() const
return m_ui->checkIPFilter->isChecked();
}

bool OptionsDialog::isShadowBanEnabled() const
{
return m_ui->shadowBanEnabled->isChecked();
}

Net::ProxyType OptionsDialog::getProxyType() const
{
return m_ui->comboProxyType->currentData().value<Net::ProxyType>();
Expand Down Expand Up @@ -1977,6 +1983,14 @@ void OptionsDialog::on_banListButton_clicked()
dialog->open();
}

void OptionsDialog::on_shadowBanListButton_clicked()
{
auto *dialog = new ShadowBanListOptionsDialog(this);
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog, &QDialog::accepted, this, &OptionsDialog::enableApplyButton);
dialog->open();
}

void OptionsDialog::on_IPSubnetWhitelistButton_clicked()
{
auto *dialog = new IPSubnetWhitelistOptionsDialog(this);
Expand Down
2 changes: 2 additions & 0 deletions src/gui/optionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ private slots:
void on_IpFilterRefreshBtn_clicked();
void handleIPFilterParsed(bool error, int ruleCount);
void on_banListButton_clicked();
void on_shadowBanListButton_clicked();
void on_IPSubnetWhitelistButton_clicked();
void on_randomButton_clicked();
void on_addWatchedFolderButton_clicked();
Expand Down Expand Up @@ -180,6 +181,7 @@ private slots:
// IP Filter
bool isIPFilteringEnabled() const;
Path getFilter() const;
bool isShadowBanEnabled() const;
// Queueing system
bool isQueueingSystemEnabled() const;
int getMaxActiveDownloads() const;
Expand Down
33 changes: 33 additions & 0 deletions src/gui/optionsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2154,6 +2154,39 @@ readme[0-9].txt: filter 'readme1.txt', 'readme2.txt' but not 'readme10.txt'.</st
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupShadowBan">
<property name="title">
<string>Shadow Ban Settings</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_172">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_111">
<item>
<widget class="QCheckBox" name="shadowBanEnabled">
<property name="text">
<string>Enable Shadow Ban</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="shadowBanListButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Manually shadow banned IP addresses...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
Expand Down
Loading
Loading