From e53906ccae7610b00ee12c3c0c45710907d7ff81 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 24 Jul 2018 12:09:07 +0200 Subject: UrlRequestInterceptor: add filter rules --- src/webengine/urlinterceptor.cpp | 47 ++++++++++++++++++++++++++++++---------- src/webengine/urlinterceptor.h | 9 +++++--- 2 files changed, 42 insertions(+), 14 deletions(-) (limited to 'src') 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 +#include +#include #include -#include #include -#include -#include +#include + +inline std::vector parseAdBlockList(const QString &filename) +{ + std::vector 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 &config, QObject *parent) : QWebEngineUrlRequestInterceptor(parent) @@ -39,8 +61,9 @@ UrlRequestInterceptor::UrlRequestInterceptor(const std::unique_ptrvalue("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 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 #include #include #include +#include "web/urlfilter/filterrule.h" typedef std::pair Header; @@ -32,9 +33,11 @@ public: private: QHash rules; + std::vector filters; std::vector
m_headers; }; QHash parse(const QString &filename); +inline std::vector parseAdBlockList(const QString &filename); -#endif // URLREQUESTINTERCEPTOR_H +#endif // SMOLBOTE_URLREQUESTINTERCEPTOR_H -- cgit v1.2.1