From 77b3cd57f930ec70e6f618da70985e23e5cf76fc Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 19 Oct 2018 01:44:06 +0200 Subject: Integrate FilterTree into browser (#6) - change filter.path to filter.hosts to represent that the setting is only used for hostlist-format lists - change FilterTree::match to use QUrl and not QString --- src/webengine/filter.cpp | 74 +++++++++++----------------------------- src/webengine/filter.h | 9 ++--- src/webengine/urlinterceptor.cpp | 5 +-- 3 files changed, 24 insertions(+), 64 deletions(-) (limited to 'src/webengine') diff --git a/src/webengine/filter.cpp b/src/webengine/filter.cpp index b250843..3ff6a04 100644 --- a/src/webengine/filter.cpp +++ b/src/webengine/filter.cpp @@ -15,53 +15,6 @@ #include #include "util.h" - -QHash parseHostlist(const QString &filename) -{ - QHash rules; - - if(QFile hostfile(filename); hostfile.open(QIODevice::ReadOnly | QIODevice::Text)) { - - // with a QTextStream we can read lines without getting linebreaks at the end - QTextStream hostfile_stream(&hostfile); - - while(!hostfile_stream.atEnd()) { - - // read line and remove any whitespace at the end - const QString &line = hostfile_stream.readLine().trimmed(); - - // skip comments and empty lines - if(line.isEmpty() || line.startsWith('#')) - continue; - - // everything else should be a rule - // format is - // 0.0.0.0 hostname - const QStringList &parts = line.split(' '); - const QString &redirect = parts.at(0); - - for(auto i = parts.constBegin() + 1; i != parts.constEnd(); ++i) { - if(!rules.contains(*i)) { - Filter::HostRule rule{}; - rule.isBlocking = (redirect == "0.0.0.0"); - rules.insert(*i, rule); - } - } - - // for(const QString &host : parts.mid(1)) { - // if(!rules.contains(host)) { - // UrlRequestInterceptor::HostRule rule{}; - // rule.isBlocking = redirect == "0.0.0.0"; - // rules.insert(host, rule); - // } - // } - } - - hostfile.close(); - } - - return rules; -} /* inline std::vector parseAdBlockList(const QString &filename) { @@ -95,9 +48,17 @@ Filter::Filter::Filter(const std::unique_ptr &config, QObject* pa } } - const QStringList hostfiles = Util::files(config->value("filter.path").value()); + const QStringList hostfiles = Util::files(config->value("filter.hosts").value()); + //qDebug("filter.path=[%s]", qUtf8Printable(config->value("filter.hosts").value())); for(const QString &hostfile : hostfiles) { - m_hostlist.unite(parseHostlist(hostfile)); + QFile f(hostfile); + if(f.open(QIODevice::ReadOnly | QIODevice::Text)) { +#ifdef QT_DEBUG + qDebug("Loading hostlist filters [%s]", qUtf8Printable(hostfile)); +#endif + loadHostlist(f, &filters); + f.close(); + } } /* @@ -107,10 +68,15 @@ Filter::Filter::Filter(const std::unique_ptr &config, QObject* pa */ } -std::optional Filter::hostlistRule(const QString& url) const +void Filter::filterRequest(QWebEngineUrlRequestInfo& info) const { - if(!m_hostlist.contains(url)) - return std::nullopt; - - return std::optional(m_hostlist.value(url)); + auto matches = filters.match(info.firstPartyUrl().toString(), info.requestUrl().toString()); + for(const auto &rule : matches) { + switch(rule->action()) { + case FilterLeaf::Block: + qDebug("block %s", qUtf8Printable(info.requestUrl().toString())); + info.block(true); + break; + } + } } diff --git a/src/webengine/filter.h b/src/webengine/filter.h index 3eac5ee..c610b29 100644 --- a/src/webengine/filter.h +++ b/src/webengine/filter.h @@ -13,6 +13,7 @@ #include #include #include +#include "urlfilter/filtertree.h" class Configuration; class Filter : public QObject @@ -26,11 +27,7 @@ public: explicit Filter(const std::unique_ptr &config, QObject *parent = nullptr); ~Filter() override = default; - const QHash hostlist() const - { - return qAsConst(m_hostlist); - } - std::optional hostlistRule(const QString &url) const; + void filterRequest(QWebEngineUrlRequestInfo &info) const; const QMap headers() const { @@ -38,7 +35,7 @@ public: } private: - QHash m_hostlist; + FilterTree filters; QMap m_headers; }; diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index cf9b85f..e6bd523 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -30,10 +30,7 @@ UrlRequestInterceptor::UrlRequestInterceptor(Filter* filter, WebProfile* profile void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { - auto hostlistCheck = m_filter->hostlistRule(info.requestUrl().host()); - if(hostlistCheck) { - info.block(hostlistCheck.value().isBlocking); - } + m_filter->filterRequest(info); // set headers for(auto i = m_filter->headers().constBegin(); i != m_filter->headers().constEnd(); ++i) { -- cgit v1.2.1