diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-07 13:20:54 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-07 18:19:19 +0200 |
commit | 2a5ea0269a1f9511c51d661a6c7d7bdc7d0176fa (patch) | |
tree | 7649cf4c1c20bbe8c801d642148992eea314d3ec /src/webengine/urlinterceptor.cpp | |
parent | Add hint on enabling plugins to makepkg (diff) | |
download | smolbote-2a5ea0269a1f9511c51d661a6c7d7bdc7d0176fa.tar.xz |
Expand HTTP header settings #4
- add doc/Usage/Filter.asciidoc to explain the usage of the filter headers
- add HTTP headers to Profile (section "headers")
- Use request interceptor to apply filter headers, then profile headers
- add insert/delete actions to ProfileEditor
Diffstat (limited to 'src/webengine/urlinterceptor.cpp')
-rw-r--r-- | src/webengine/urlinterceptor.cpp | 127 |
1 files changed, 16 insertions, 111 deletions
diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index db4aea9..cf9b85f 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -14,127 +14,32 @@ #include <QTextStream> #include <boost/algorithm/string.hpp> #include <configuration/configuration.h> +#include "filter.h" +#include <web/webprofile.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)); - } - } - list.close(); - } - - return rules; -} +// test DNT on https://browserleaks.com/donottrack -UrlRequestInterceptor::UrlRequestInterceptor(const std::unique_ptr<Configuration> &config, QObject *parent) +UrlRequestInterceptor::UrlRequestInterceptor(Filter* filter, WebProfile* profile, QObject* parent) : QWebEngineUrlRequestInterceptor(parent) { - QDir hostsD(config->value<QString>("filter.path").value()); - const QStringList hostFiles = hostsD.entryList(QDir::Files); - for(const QString &file : hostFiles) { - const QString absPath = hostsD.absoluteFilePath(file); - auto r = parse(absPath); -#ifdef QT_DEBUG - qDebug("Parsed %i rules from %s", r.count(), qUtf8Printable(absPath)); -#endif - - rules.unite(r); - } - - const auto header = config->value<std::vector<std::string>>("filter.header"); - if(header) { - for(const std::string &h : header.value()) { - std::vector<std::string> s; - boost::split(s, h, boost::is_any_of(":=")); - auto pair = std::make_pair(s.at(0), s.at(1)); - m_headers.emplace_back(pair); - } - } - - auto filtersPath = config->value<QString>("filter.adblock"); - if(filtersPath) - filters = parseAdBlockList(filtersPath.value()); + Q_CHECK_PTR(filter); + m_filter = filter; + Q_CHECK_PTR(profile); + m_profile = profile; } -// test DNT on https://browserleaks.com/donottrack void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { - for(const Header &header : m_headers) { - info.setHttpHeader(QByteArray::fromStdString(header.first), QByteArray::fromStdString(header.second)); + auto hostlistCheck = m_filter->hostlistRule(info.requestUrl().host()); + if(hostlistCheck) { + info.block(hostlistCheck.value().isBlocking); } - if(rules.contains(info.requestUrl().host())) { - info.block(rules.value(info.requestUrl().host()).isBlocking); - return; + // set headers + for(auto i = m_filter->headers().constBegin(); i != m_filter->headers().constEnd(); ++i) { + info.setHttpHeader(i.key(), i.value()); } - - const uint domainHash = qHash(info.firstPartyUrl().host()); - const QWebEngineUrlRequestInfo::ResourceType type = info.resourceType(); - const QUrl requestUrl = info.requestUrl(); - for(const FilterRule &rule : filters) { - if(rule.matchesDomain(domainHash) && rule.matchesType(type) && rule.matchesUrl(requestUrl)) { - info.block(rule.isBlocking()); -#ifdef QT_DEBUG - qDebug("--> blocked %s", qUtf8Printable(info.requestUrl().toString())); -#endif - break; - } + for(auto i = m_profile->headers().constBegin(); i != m_profile->headers().constEnd(); ++i) { + info.setHttpHeader(i.key(), i.value()); } } - -QHash<QString, UrlRequestInterceptor::HostRule> parse(const QString &filename) -{ - QHash<QString, UrlRequestInterceptor::HostRule> rules; - - QFile hostfile(filename); - if(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)) { - UrlRequestInterceptor::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; -} |