forked from qbittorrent/qBittorrent
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
109 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <fstream> | ||
#include <string> | ||
|
||
#include <QFileInfo> | ||
#include <QRegularExpression> | ||
#include <QString> | ||
#include <QVector> | ||
|
||
#include <libtorrent/peer_info.hpp> | ||
|
||
#include "base/logger.h" | ||
|
||
namespace { | ||
|
||
bool qregex_has_match(const QRegularExpression& re, const QString& str) | ||
{ | ||
auto m = re.match(str); | ||
return m.hasMatch(); | ||
} | ||
|
||
} | ||
|
||
class peer_filter | ||
{ | ||
public: | ||
explicit peer_filter(const QString& filter_file) | ||
{ | ||
QString log_tag = QFileInfo(filter_file).fileName(); | ||
|
||
std::ifstream ifs(filter_file.toStdString()); | ||
std::string peer_id, client; | ||
while (ifs >> peer_id >> client) { | ||
QRegularExpression peer_id_re(QString::fromStdString(peer_id)); | ||
QRegularExpression client_re(QString::fromStdString(client)); | ||
|
||
QString msg_tmpl("'%1': invalid %2 matching expression '%3' detected, ignoring rule"); | ||
|
||
if (!peer_id_re.isValid()) | ||
LogMsg(msg_tmpl.arg(log_tag).arg("peer id").arg(peer_id_re.pattern()), Log::WARNING); | ||
|
||
if (!client_re.isValid()) | ||
LogMsg(msg_tmpl.arg(log_tag).arg("client name").arg(client_re.pattern()), Log::WARNING); | ||
|
||
if (peer_id_re.isValid() && client_re.isValid()) | ||
m_filters.append({peer_id_re, client_re}); | ||
} | ||
} | ||
|
||
bool match_peer(const lt::peer_info& info, bool skip_name) const | ||
{ | ||
QString peer_id = QString::fromLatin1(info.pid.data(), 8); | ||
QString client = QString::fromStdString(info.client); | ||
return std::any_of(m_filters.begin(), m_filters.end(), | ||
[&](const auto& filter) { | ||
return qregex_has_match(filter[0], peer_id) && | ||
(skip_name || qregex_has_match(filter[1], client)); | ||
}); | ||
} | ||
|
||
bool is_empty() const { return m_filters.isEmpty(); } | ||
|
||
private: | ||
QVector<QVector<QRegularExpression>> m_filters; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters