From e25c011d5db32104ccdb3e8949082345efdba805 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 11 Dec 2017 18:16:50 +0100 Subject: Simple filterlist --- src/webengine/urlinterceptor.cpp | 49 ++++++++++++++++++++++++++++++---------- src/webengine/urlinterceptor.h | 18 +++++++++------ 2 files changed, 48 insertions(+), 19 deletions(-) (limited to 'src/webengine') diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index 19bc64f..cbf1c86 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -19,31 +19,56 @@ ******************************************************************************/ #include "urlinterceptor.h" -#include "filter/filtercollection.h" +#include +#include -UrlRequestInterceptor::UrlRequestInterceptor(QObject *parent) : +UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *parent) : QWebEngineUrlRequestInterceptor(parent) { +#ifdef QT_DEBUG + qDebug("Reading request blocklist: %s", qUtf8Printable(path)); +#endif + + int n_rules = 0; + + QFile filter(path); + if(filter.open(QIODevice::ReadOnly | QIODevice::Text)) { + QTextStream out(&filter); + while(!out.atEnd()) { + const QString &line = out.readLine(); + if(!line.startsWith('!')) { + FilterRule r(line); + // check if valid + m_rules.push_back(std::move(r)); + ++n_rules; + } + } + filter.close(); + } + + qDebug("Added %i rules", n_rules); } void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { -#ifdef DEBUG_VERBOSE - qDebug("%s -[%i]-> %s", qUtf8Printable(info.firstPartyUrl().toString()), info.resourceType(), qUtf8Printable(info.requestUrl().toString())); -#endif - for(auto s : m_manager->subscriptions()) { - bool shouldBlock = s->match(info); - if(shouldBlock) { + for(const FilterRule &test : m_rules) { + const QUrl &url = info.requestUrl(); + + if(test.shouldBlock(url)) { info.block(true); -#ifdef DEBUG_VERBOSE - qDebug(">> blocked"); + +#ifdef QT_DEBUG + qDebug("blocked [%s] %s", qUtf8Printable(info.firstPartyUrl().toString()), qUtf8Printable(url.toString())); #endif return; } } } -void UrlRequestInterceptor::setSubscription(BlockerManager *manager) +bool shouldBlock(const QUrl &url, const QString &test) { - m_manager = manager; + if(url.toString().contains(test)) { + return true; + } + return false; } diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h index acd076a..4466523 100644 --- a/src/webengine/urlinterceptor.h +++ b/src/webengine/urlinterceptor.h @@ -18,27 +18,31 @@ ** ******************************************************************************/ -#ifndef ADBLOCKINTERCEPTOR_H -#define ADBLOCKINTERCEPTOR_H +#ifndef URLREQUESTINTERCEPTOR_H +#define URLREQUESTINTERCEPTOR_H #include -#include "filter/blockermanager.h" +#include +#include "filter/filter.h" class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor { Q_OBJECT public: - explicit UrlRequestInterceptor(QObject *parent = 0); + explicit UrlRequestInterceptor(const QString &path, QObject *parent = nullptr); + //~UrlRequestInterceptor(); void interceptRequest(QWebEngineUrlRequestInfo &info); - void setSubscription(BlockerManager *manager); signals: public slots: private: - BlockerManager *m_manager; + + std::vector m_rules; }; -#endif // ADBLOCKINTERCEPTOR_H +bool shouldBlock(const QUrl &url, const QString &test); + +#endif // URLREQUESTINTERCEPTOR_H -- cgit v1.2.1