use correctly first-party url when blocking with adblock
This commit is contained in:
parent
ef1b32bb52
commit
d9c8246858
3 changed files with 34 additions and 7 deletions
|
@ -12,7 +12,8 @@
|
|||
// How to use:
|
||||
// curl -i -X POST --data '
|
||||
// {
|
||||
// "url": "http://gompoozu.net",
|
||||
// "url": "http://gompoozu.net/cwqcwq/js.js",
|
||||
// "fp_url": "http://bbc.com",
|
||||
// "url_type": "main_frame",
|
||||
// "filter": true,
|
||||
// "cosmetic": true
|
||||
|
@ -60,6 +61,7 @@ else {
|
|||
const jsonStruct = JSON.parse(jsonData.toString());
|
||||
|
||||
const askUrl = jsonStruct['url'];
|
||||
const askFpUrl = jsonStruct['fp_url'];
|
||||
const askFilter = jsonStruct['filter'];
|
||||
const askCosmetic = jsonStruct['cosmetic'];
|
||||
const askUrlType = jsonStruct['url_type'];
|
||||
|
@ -71,6 +73,7 @@ else {
|
|||
const adblockMatch = engine.match(adblock.Request.fromRawDetails({
|
||||
type: askUrlType,
|
||||
url: askUrl,
|
||||
sourceUrl: askFpUrl
|
||||
}));
|
||||
|
||||
resultJson["filter"] = adblockMatch;
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
|
||||
AdBlockManager::AdBlockManager(QObject* parent)
|
||||
: QObject(parent), m_loaded(false), m_enabled(false), m_interceptor(new AdBlockUrlInterceptor(this)),
|
||||
m_serverProcess(nullptr) {
|
||||
m_serverProcess(nullptr), m_cacheBlocks({}) {
|
||||
m_adblockIcon = new AdBlockIcon(this);
|
||||
m_adblockIcon->setObjectName(QSL("m_adblockIconAction"));
|
||||
m_unifiedFiltersFile = qApp->userDataFolder() + QDir::separator() + QSL("adblock-unified-filters.txt");
|
||||
|
@ -38,21 +38,37 @@ AdBlockManager::~AdBlockManager() {
|
|||
}
|
||||
}
|
||||
|
||||
BlockingResult AdBlockManager::block(const AdblockRequestInfo& request) const {
|
||||
BlockingResult AdBlockManager::block(const AdblockRequestInfo& request) {
|
||||
if (!isEnabled()) {
|
||||
return { false };
|
||||
}
|
||||
|
||||
const QString url_string = request.requestUrl().toEncoded().toLower();
|
||||
const QString firstparty_url_string = request.firstPartyUrl().toEncoded().toLower();
|
||||
const QString url_scheme = request.requestUrl().scheme().toLower();
|
||||
const QPair<QString, QString> url_pair = { firstparty_url_string, url_string };
|
||||
|
||||
if (!canRunOnScheme(url_scheme)) {
|
||||
return { false };
|
||||
}
|
||||
else {
|
||||
if (m_serverProcess != nullptr && m_serverProcess->state() == QProcess::ProcessState::Running) {
|
||||
if (m_cacheBlocks.contains(url_pair)) {
|
||||
qDebugNN << LOGSEC_ADBLOCK
|
||||
<< "Found blocking data in cache, URL:"
|
||||
<< QUOTE_W_SPACE_DOT(url_pair);
|
||||
|
||||
return m_cacheBlocks.value(url_pair);
|
||||
}
|
||||
|
||||
try {
|
||||
auto result = askServerIfBlocked(url_string);
|
||||
auto result = askServerIfBlocked(firstparty_url_string, url_string);
|
||||
|
||||
m_cacheBlocks.insert(url_pair, result);
|
||||
|
||||
qDebugNN << LOGSEC_ADBLOCK
|
||||
<< "Inserted blocking data to cache for:"
|
||||
<< QUOTE_W_SPACE_DOT(url_pair);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -177,11 +193,12 @@ void AdBlockManager::showDialog() {
|
|||
AdBlockDialog(qApp->mainFormWidget()).exec();
|
||||
}
|
||||
|
||||
BlockingResult AdBlockManager::askServerIfBlocked(const QString& url) const {
|
||||
BlockingResult AdBlockManager::askServerIfBlocked(const QString& fp_url, const QString& url) const {
|
||||
QJsonObject req_obj;
|
||||
QByteArray out;
|
||||
QElapsedTimer tmr;
|
||||
|
||||
req_obj["fp_url"] = fp_url;
|
||||
req_obj["url"] = url;
|
||||
req_obj["filter"] = true;
|
||||
|
||||
|
@ -312,6 +329,8 @@ QProcess* AdBlockManager::restartServer(int port) {
|
|||
}
|
||||
|
||||
void AdBlockManager::updateUnifiedFiltersFile() {
|
||||
m_cacheBlocks.clear();
|
||||
|
||||
if (QFile::exists(m_unifiedFiltersFile)) {
|
||||
QFile::remove(m_unifiedFiltersFile);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
#include <QObject>
|
||||
|
||||
#include <QHash>
|
||||
|
||||
class QUrl;
|
||||
class QProcess;
|
||||
class AdblockRequestInfo;
|
||||
|
@ -15,6 +17,8 @@ struct BlockingResult {
|
|||
bool m_blocked;
|
||||
QString m_blockedByFilter;
|
||||
|
||||
BlockingResult() : m_blocked(false), m_blockedByFilter(QString()) {}
|
||||
|
||||
BlockingResult(bool blocked, QString blocked_by_filter = {})
|
||||
: m_blocked(blocked), m_blockedByFilter(std::move(blocked_by_filter)) {}
|
||||
|
||||
|
@ -38,7 +42,7 @@ class AdBlockManager : public QObject {
|
|||
AdBlockIcon* adBlockIcon() const;
|
||||
|
||||
// General methods for adblocking.
|
||||
BlockingResult block(const AdblockRequestInfo& request) const;
|
||||
BlockingResult block(const AdblockRequestInfo& request);
|
||||
QString elementHidingRulesForDomain(const QUrl& url) const;
|
||||
|
||||
QStringList filterLists() const;
|
||||
|
@ -58,7 +62,7 @@ class AdBlockManager : public QObject {
|
|||
void enabledChanged(bool enabled);
|
||||
|
||||
private:
|
||||
BlockingResult askServerIfBlocked(const QString& url) const;
|
||||
BlockingResult askServerIfBlocked(const QString& fp_url, const QString& url) const;
|
||||
QString askServerForCosmeticRules(const QString& url) const;
|
||||
QProcess* restartServer(int port);
|
||||
|
||||
|
@ -69,6 +73,7 @@ class AdBlockManager : public QObject {
|
|||
AdBlockUrlInterceptor* m_interceptor;
|
||||
QString m_unifiedFiltersFile;
|
||||
QProcess* m_serverProcess;
|
||||
QHash<QPair<QString, QString>, BlockingResult> m_cacheBlocks;
|
||||
};
|
||||
|
||||
inline AdBlockIcon* AdBlockManager::adBlockIcon() const {
|
||||
|
|
Loading…
Add table
Reference in a new issue