aboutsummaryrefslogtreecommitdiff
path: root/src/webengine
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-12-23 16:45:31 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2017-12-23 16:45:31 +0100
commitf7ed63179fa4f99322d6c7716e17466ec4e3e4ce (patch)
treef187d1f73300f1ac0882a4be569d567bb4ed29f7 /src/webengine
parentRemoved SingleApplication::SessionParam (diff)
downloadsmolbote-f7ed63179fa4f99322d6c7716e17466ec4e3e4ce.tar.xz
Request filter now properly takes hostlists
- hostslist directory is set in browser.filterPath
Diffstat (limited to 'src/webengine')
-rw-r--r--src/webengine/hostrule.cpp47
-rw-r--r--src/webengine/hostrule.h31
-rw-r--r--src/webengine/urlinterceptor.cpp67
-rw-r--r--src/webengine/urlinterceptor.h12
4 files changed, 123 insertions, 34 deletions
diff --git a/src/webengine/hostrule.cpp b/src/webengine/hostrule.cpp
new file mode 100644
index 0000000..1605b78
--- /dev/null
+++ b/src/webengine/hostrule.cpp
@@ -0,0 +1,47 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: git://neueland.iserlohn-fortress.net/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "hostrule.h"
+
+HostRule::HostRule(const QString &line)
+{
+ valid = parse(line);
+}
+
+QString HostRule::pattern() const
+{
+ return domain;
+}
+
+bool HostRule::shouldBlock(const QWebEngineUrlRequestInfo &info) const
+{
+ if(info.requestUrl().host().contains(domain)) {
+ return true;
+ }
+ return false;
+}
+
+bool HostRule::parse(const QString &line)
+{
+ QStringList parts = line.split(' ');
+ if(parts.first() == "0.0.0.0") {
+ blocking = true;
+ } else {
+ qDebug("Skipping rule: '%s'", qUtf8Printable(line));
+ }
+
+ domain = parts.last();
+
+#ifdef QT_DEBUG
+ if(domain.isEmpty()) {
+ qWarning("error parsing %s", qUtf8Printable(line));
+ }
+#endif
+
+ return true;
+}
diff --git a/src/webengine/hostrule.h b/src/webengine/hostrule.h
new file mode 100644
index 0000000..d1289a0
--- /dev/null
+++ b/src/webengine/hostrule.h
@@ -0,0 +1,31 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: git://neueland.iserlohn-fortress.net/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#ifndef HOSTRULE_H
+#define HOSTRULE_H
+
+#include <QString>
+#include <QWebEngineUrlRequestInfo>
+
+class HostRule
+{
+public:
+ HostRule(const QString &line);
+ QString pattern() const;
+ bool shouldBlock(const QWebEngineUrlRequestInfo &info) const;
+
+private:
+ bool valid = false;
+
+ bool parse(const QString &line);
+
+ QString domain;
+ bool blocking;
+};
+
+#endif // HOSTRULE_H
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 <QDir>
#include <QFile>
#include <QTextStream>
@@ -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;
}
diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h
index cf1597c..1ff786b 100644
--- a/src/webengine/urlinterceptor.h
+++ b/src/webengine/urlinterceptor.h
@@ -11,26 +11,22 @@
#include <QWebEngineUrlRequestInterceptor>
#include <vector>
-#include "filter/filter.h"
+#include "hostrule.h"
class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor
{
Q_OBJECT
public:
explicit UrlRequestInterceptor(const QString &path, QObject *parent = nullptr);
- //~UrlRequestInterceptor();
+ ~UrlRequestInterceptor();
void interceptRequest(QWebEngineUrlRequestInfo &info);
-signals:
-
public slots:
+ int parseHostfile(const QString &filename);
private:
-
- std::vector<FilterRule> m_rules;
+ std::vector<HostRule*> m_rules;
};
-bool shouldBlock(const QUrl &url, const QString &test);
-
#endif // URLREQUESTINTERCEPTOR_H