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

Add shadowban operation and status api #568

Merged
merged 4 commits into from
Aug 17, 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
15 changes: 15 additions & 0 deletions src/base/bittorrent/peerinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@

#include <QBitArray>

#include "base/algorithm.h"
#include "base/bittorrent/ltqbitarray.h"
#include "base/net/geoipmanager.h"
#include "base/settingvalue.h"
#include "base/unicodestrings.h"
#include "base/utils/bytearray.h"
#include "peeraddress.h"
Expand Down Expand Up @@ -117,6 +119,19 @@ bool PeerInfo::isSeed() const
return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::seed);
}

bool PeerInfo::isShadowBanned() const
{
if (!CachedSettingValue<bool>(u"BitTorrent/Session/ShadowBan"_s, false))
{
return false;
}
QString peer_ip = address().ip.toString();
QStringList shadowbannedIPs =
CachedSettingValue<QStringList>(u"State/ShadowBannedIPs"_s, QStringList(), Algorithm::sorted<QStringList>).get();

return shadowbannedIPs.contains(peer_ip);
}

bool PeerInfo::optimisticUnchoke() const
{
return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::optimistic_unchoke);
Expand Down
2 changes: 2 additions & 0 deletions src/base/bittorrent/peerinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace BitTorrent
bool isOnParole() const;
bool isSeed() const;

bool isShadowBanned() const;

bool optimisticUnchoke() const;
bool isSnubbed() const;
bool isUploadOnly() const;
Expand Down
1 change: 1 addition & 0 deletions src/base/bittorrent/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ namespace BitTorrent
virtual void setMaxRatioAction(MaxRatioAction act) = 0;

virtual void banIP(const QString &ip) = 0;
virtual void shadowbanIP(const QString &ip) = 0;

virtual bool isKnownTorrent(const InfoHash &infoHash) const = 0;
virtual bool addTorrent(const QString &source, const AddTorrentParams &params = {}) = 0;
Expand Down
17 changes: 17 additions & 0 deletions src/base/bittorrent/sessionimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2485,6 +2485,23 @@ void SessionImpl::banIP(const QString &ip)
m_bannedIPs = bannedIPs;
}

void SessionImpl::shadowbanIP(const QString &ip)
{
if (m_shadowBannedIPs.get().contains(ip))
return;

lt::error_code ec;
lt::make_address(ip.toLatin1().constData(), ec); // Only check IP valid.
Q_ASSERT(!ec);
if (ec)
return;

QStringList shadowBannedIPs = m_shadowBannedIPs;
shadowBannedIPs.append(ip);
shadowBannedIPs.sort();
m_shadowBannedIPs = shadowBannedIPs;
}

// Delete a torrent from the session, given its hash
// and from the disk, if the corresponding deleteOption is chosen
bool SessionImpl::deleteTorrent(const TorrentID &id, const DeleteOption deleteOption)
Expand Down
1 change: 1 addition & 0 deletions src/base/bittorrent/sessionimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ namespace BitTorrent
void setMaxRatioAction(MaxRatioAction act) override;

void banIP(const QString &ip) override;
void shadowbanIP(const QString &ip) override;

bool isKnownTorrent(const InfoHash &infoHash) const override;
bool addTorrent(const QString &source, const AddTorrentParams &params = {}) override;
Expand Down
4 changes: 3 additions & 1 deletion src/webui/api/synccontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace
const QString KEY_PEER_PORT = u"port"_s;
const QString KEY_PEER_PROGRESS = u"progress"_s;
const QString KEY_PEER_RELEVANCE = u"relevance"_s;
const QString KEY_PEER_SHADOWBANNED = u"shadowbanned"_s;
const QString KEY_PEER_TOT_DOWN = u"downloaded"_s;
const QString KEY_PEER_TOT_UP = u"uploaded"_s;
const QString KEY_PEER_UP_SPEED = u"up_speed"_s;
Expand Down Expand Up @@ -754,7 +755,8 @@ void SyncController::torrentPeersAction()
{KEY_PEER_CONNECTION_TYPE, pi.connectionType()},
{KEY_PEER_FLAGS, pi.flags()},
{KEY_PEER_FLAGS_DESCRIPTION, pi.flagsDescription()},
{KEY_PEER_RELEVANCE, pi.relevance()}
{KEY_PEER_RELEVANCE, pi.relevance()},
{KEY_PEER_SHADOWBANNED, pi.isShadowBanned()}
};

if (torrent->hasMetadata())
Expand Down
13 changes: 13 additions & 0 deletions src/webui/api/transfercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,16 @@ void TransferController::banPeersAction()
BitTorrent::Session::instance()->banIP(addr.ip.toString());
}
}

void TransferController::shadowbanPeersAction()
{
requireParams({u"peers"_s});

const QStringList peers = params()[u"peers"_s].split(u'|');
for (const QString &peer : peers)
{
const BitTorrent::PeerAddress addr = BitTorrent::PeerAddress::parse(peer.trimmed());
if (!addr.ip.isNull())
BitTorrent::Session::instance()->shadowbanIP(addr.ip.toString());
}
}
1 change: 1 addition & 0 deletions src/webui/api/transfercontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ private slots:
void setUploadLimitAction();
void setDownloadLimitAction();
void banPeersAction();
void shadowbanPeersAction();
};
Loading