From f7ed63179fa4f99322d6c7716e17466ec4e3e4ce Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 23 Dec 2017 16:45:31 +0100 Subject: Request filter now properly takes hostlists - hostslist directory is set in browser.filterPath --- src/webengine/urlinterceptor.cpp | 67 ++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 26 deletions(-) (limited to 'src/webengine/urlinterceptor.cpp') diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index 363d3ba..9598f36 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -7,6 +7,7 @@ */ #include "urlinterceptor.h" +#include #include #include @@ -17,46 +18,60 @@ UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *paren qDebug("Reading request blocklist: %s", qUtf8Printable(path)); #endif - int n_rules = 0; - - QFile filter(path); - if(filter.open(QIODevice::ReadOnly | QIODevice::Text)) { - QTextStream out(&filter); - while(!out.atEnd()) { - const QString &line = out.readLine(); - if(!line.startsWith('!')) { - FilterRule r(line); - // check if valid - m_rules.push_back(std::move(r)); - ++n_rules; - } - } - filter.close(); + QDir hostsD(path); + for(const QString &file : hostsD.entryList(QDir::Files)) { + qDebug("Parsing hosts.d/%s: %i", qUtf8Printable(file), parseHostfile(hostsD.absoluteFilePath(file))); } +} - qDebug("Added %i rules", n_rules); +UrlRequestInterceptor::~UrlRequestInterceptor() +{ + for(HostRule *r : m_rules) { + delete r; + } } void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { - for(const FilterRule &test : m_rules) { - const QUrl &url = info.requestUrl(); +//#ifdef QT_DEBUG +// qDebug("%s -> %s", qUtf8Printable(info.firstPartyUrl().toString()), qUtf8Printable(info.requestUrl().toString())); +//#endif - if(test.shouldBlock(url)) { + for(HostRule *test : m_rules) { + if(test->shouldBlock(info)) { info.block(true); -#ifdef QT_DEBUG - qDebug("blocked [%s] %s", qUtf8Printable(info.firstPartyUrl().toString()), qUtf8Printable(url.toString())); -#endif +//#ifdef QT_DEBUG +// qDebug("blocked on pattern %s", qUtf8Printable(test->pattern())); +//#endif return; } } } -bool shouldBlock(const QUrl &url, const QString &test) +int UrlRequestInterceptor::parseHostfile(const QString &filename) { - if(url.toString().contains(test)) { - return true; + int numRules = 0; + QFile file(filename); + + // try to open the file + if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { + + // with a QTextStream we can read lines without getting linebreaks at the end + QTextStream out(&file); + while(!out.atEnd()) { + const QString &line = out.readLine().trimmed(); + + // skip comments and empty lines + if(!line.startsWith('#') && !line.isEmpty()) { + HostRule *r = new HostRule(line); + m_rules.push_back(r); + ++numRules; + } + } + + // close once we're done with it + file.close(); } - return false; + return numRules; } -- cgit v1.2.1