diff options
Diffstat (limited to 'src/webengine')
-rw-r--r-- | src/webengine/urlinterceptor.cpp | 47 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.h | 9 |
2 files changed, 42 insertions, 14 deletions
diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index a66ab57..cf50e59 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -7,12 +7,34 @@ */ #include "urlinterceptor.h" +#include "web/urlfilter/adblockrule.h" #include <QDir> +#include <QJsonArray> +#include <QJsonDocument> #include <QTextStream> -#include <configuration/configuration.h> #include <boost/algorithm/string.hpp> -#include <QJsonDocument> -#include <QJsonArray> +#include <configuration/configuration.h> + +inline std::vector<FilterRule> parseAdBlockList(const QString &filename) +{ + std::vector<FilterRule> rules; + QFile list(filename); + + if(list.open(QIODevice::ReadOnly | QIODevice::Text), true) { + QTextStream l(&list); + QString line; + while(l.readLineInto(&line)) { + AdBlockRule rule(line); + if(rule.isEnabled()) { + rules.emplace_back(std::move(rule)); + //qDebug("added rule: %s", qUtf8Printable(line)); + } + } + list.close(); + } + + return rules; +} UrlRequestInterceptor::UrlRequestInterceptor(const std::unique_ptr<Configuration> &config, QObject *parent) : QWebEngineUrlRequestInterceptor(parent) @@ -39,8 +61,9 @@ UrlRequestInterceptor::UrlRequestInterceptor(const std::unique_ptr<Configuration } } - - + auto filtersPath = config->value<QString>("filter.adblock"); + if(filtersPath) + filters = std::move(parseAdBlockList(filtersPath.value())); } // test DNT on https://browserleaks.com/donottrack @@ -55,14 +78,16 @@ void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) return; } + for(const FilterRule &rule : filters) { + if(rule.matchesDomain(info.firstPartyUrl().host()) && rule.matchesType(info.resourceType()) && rule.matchesUrl(info.requestUrl())) { + info.block(rule.isBlocking()); #ifdef QT_DEBUG -// qDebug("request>>>"); -// qDebug("firstParty url=%s", qUtf8Printable(info.firstPartyUrl().toString())); -// qDebug("firstParty host=%s", qUtf8Printable(info.firstPartyUrl().host())); -// qDebug("request url=%s", qUtf8Printable(info.requestUrl().toString())); -// qDebug("request host=%s", qUtf8Printable(info.requestUrl().host())); -// qDebug("<<<"); + qDebug("--> blocked %s", qUtf8Printable(info.requestUrl().toString())); + qDebug("- %s", qUtf8Printable(rule.toString())); #endif + break; + } + } } QHash<QString, UrlRequestInterceptor::HostRule> parse(const QString &filename) diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h index 2f91e30..a4a1b6e 100644 --- a/src/webengine/urlinterceptor.h +++ b/src/webengine/urlinterceptor.h @@ -6,13 +6,14 @@ * SPDX-License-Identifier: GPL-3.0 */ -#ifndef URLREQUESTINTERCEPTOR_H -#define URLREQUESTINTERCEPTOR_H +#ifndef SMOLBOTE_URLREQUESTINTERCEPTOR_H +#define SMOLBOTE_URLREQUESTINTERCEPTOR_H #include <QWebEngineUrlRequestInterceptor> #include <memory> #include <QVector> #include <QByteArray> +#include "web/urlfilter/filterrule.h" typedef std::pair<std::string, std::string> Header; @@ -32,9 +33,11 @@ public: private: QHash<QString, HostRule> rules; + std::vector<FilterRule> filters; std::vector<Header> m_headers; }; QHash<QString, UrlRequestInterceptor::HostRule> parse(const QString &filename); +inline std::vector<FilterRule> parseAdBlockList(const QString &filename); -#endif // URLREQUESTINTERCEPTOR_H +#endif // SMOLBOTE_URLREQUESTINTERCEPTOR_H |