diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-19 01:44:06 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-19 01:44:06 +0200 |
commit | 77b3cd57f930ec70e6f618da70985e23e5cf76fc (patch) | |
tree | 712dbb43ec463a8524286d9e6e0fb2d2f8b9d6ab /src/webengine | |
parent | urlfilter: destroy FilterLeaves only when destroying the FilterTree (diff) | |
download | smolbote-77b3cd57f930ec70e6f618da70985e23e5cf76fc.tar.xz |
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
Diffstat (limited to 'src/webengine')
-rw-r--r-- | src/webengine/filter.cpp | 74 | ||||
-rw-r--r-- | src/webengine/filter.h | 9 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.cpp | 5 |
3 files changed, 24 insertions, 64 deletions
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 <configuration/configuration.h> #include "util.h" - -QHash<QString, Filter::HostRule> parseHostlist(const QString &filename) -{ - QHash<QString, Filter::HostRule> 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 <redirect> <host> - // 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<FilterRule> parseAdBlockList(const QString &filename) { @@ -95,9 +48,17 @@ Filter::Filter::Filter(const std::unique_ptr<Configuration> &config, QObject* pa } } - const QStringList hostfiles = Util::files(config->value<QString>("filter.path").value()); + const QStringList hostfiles = Util::files(config->value<QString>("filter.hosts").value()); + //qDebug("filter.path=[%s]", qUtf8Printable(config->value<QString>("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<Configuration> &config, QObject* pa */ } -std::optional<Filter::HostRule> Filter::hostlistRule(const QString& url) const +void Filter::filterRequest(QWebEngineUrlRequestInfo& info) const { - if(!m_hostlist.contains(url)) - return std::nullopt; - - return std::optional<Filter::HostRule>(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 <QByteArray> #include <QVector> #include <optional> +#include "urlfilter/filtertree.h" class Configuration; class Filter : public QObject @@ -26,11 +27,7 @@ public: explicit Filter(const std::unique_ptr<Configuration> &config, QObject *parent = nullptr); ~Filter() override = default; - const QHash<QString, HostRule> hostlist() const - { - return qAsConst(m_hostlist); - } - std::optional<HostRule> hostlistRule(const QString &url) const; + void filterRequest(QWebEngineUrlRequestInfo &info) const; const QMap<QByteArray, QByteArray> headers() const { @@ -38,7 +35,7 @@ public: } private: - QHash<QString, HostRule> m_hostlist; + FilterTree filters; QMap<QByteArray, QByteArray> 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) { |