From 43965eb877ed24d94b8cfa09f948b7877bcffc24 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 27 Oct 2020 13:23:01 +0100 Subject: Allow blacklist and whitelist at the same time. Whitelist takes precedence. --- src/main.cpp | 16 ++++++---------- src/nam.cpp | 9 ++++++++- src/nam.h | 7 +++---- src/slxbrowser.cpp | 11 ++++++----- src/slxbrowser.h | 3 +-- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6bd0897..2213913 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,8 +40,8 @@ int main(int argc, char** argv) QCommandLineOption fullscreen("fullscreen", "Run browser in full screen."); QCommandLineOption maximized("maximized", "Maximize window on startup"); QCommandLineOption reloadInterval("reload-interval", "Reload displayed page every X seconds.", "seconds"); - QCommandLineOption whitelist("whitelist", "Path to a file of allowed URLs. Mutually exclusive with blacklist.", "file"); - QCommandLineOption blacklist("blacklist", "Path to a file of disallowed URLs. Mutually exclusive with whitelist.", "file"); + QCommandLineOption whitelist("whitelist", "Path to a file of allowed URLs.", "file"); + QCommandLineOption blacklist("blacklist", "Path to a file of disallowed URLs.", "file"); parser.addOption(ignoreSsl); parser.addOption(fullscreen); parser.addOption(maximized); @@ -54,10 +54,6 @@ int main(int argc, char** argv) QMessageBox::critical(nullptr, "Error", "Need one argument: file name"); return 1; } - if (parser.isSet(whitelist) && parser.isSet(blacklist)) { - QMessageBox::critical(nullptr, "Error", "Need either blacklist or whitelist, not both"); - return 2; - } BrowserSettings settings; settings.url = list[0]; settings.fullscreen = parser.isSet(fullscreen); @@ -65,11 +61,11 @@ int main(int argc, char** argv) settings.ignoreSslErrors = parser.isSet(ignoreSsl); settings.reloadInterval = parser.value(reloadInterval).toInt(); if (parser.isSet(whitelist)) { - settings.urlList = loadUrlList(parser.value(whitelist)); - } else if (parser.isSet(blacklist)) { - settings.urlList = loadUrlList(parser.value(blacklist)); + settings.whiteList = loadUrlList(parser.value(whitelist)); + } + if (parser.isSet(blacklist)) { + settings.blackList = loadUrlList(parser.value(blacklist)); } - settings.isWhitelist = parser.isSet(whitelist); SlxBrowser main(settings); main.show(); app.installEventFilter(new KeyHandler(&main)); diff --git a/src/nam.cpp b/src/nam.cpp index 2f3fdd1..798a8e6 100644 --- a/src/nam.cpp +++ b/src/nam.cpp @@ -33,7 +33,14 @@ QNetworkReply* SlxNetworkAccessManager::createRequest(QNetworkAccessManager::Ope } else if (url.host().isEmpty()) { ok = true; } else { - ok = (_list.match(url.toDisplayString(QUrl::NormalizePathSegments)).hasMatch() == _isWhitelist); + auto str = url.toDisplayString(QUrl::NormalizePathSegments); + if (_white.isValid() && _white.match(str).hasMatch()) { + ok = true; + } else if (_black.isValid() && _black.match(str).hasMatch()) { + ok = false; + } else { + ok = true; + } } if (!ok) { return new SlxDisabledNetworkReply(this, req, op); diff --git a/src/nam.h b/src/nam.h index 0036904..ec2bb17 100644 --- a/src/nam.h +++ b/src/nam.h @@ -12,14 +12,13 @@ class SlxNetworkAccessManager : public QNetworkAccessManager { Q_OBJECT public: - SlxNetworkAccessManager(QRegularExpression list, bool isWhitelist, QObject *parent = nullptr) - : QNetworkAccessManager(parent), _list(list), _isWhitelist(isWhitelist) {} + SlxNetworkAccessManager(QRegularExpression blackList, QRegularExpression whiteList, QObject *parent = nullptr) + : QNetworkAccessManager(parent), _black(blackList), _white(whiteList) {} protected: QNetworkReply* createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &originalReq, QIODevice *outgoingData = nullptr) override; private: - QRegularExpression _list; - bool _isWhitelist; + QRegularExpression _black, _white; }; diff --git a/src/slxbrowser.cpp b/src/slxbrowser.cpp index c4c2067..8cc8749 100644 --- a/src/slxbrowser.cpp +++ b/src/slxbrowser.cpp @@ -51,14 +51,15 @@ SlxBrowser::SlxBrowser(BrowserSettings settings) // QWebPage *page = _browser->page(); QNetworkAccessManager *nam; - if (_settings.urlList.isEmpty()) { + if (_settings.whiteList.isEmpty() && _settings.blackList.isEmpty()) { nam = new QNetworkAccessManager(this); } else { - if (_settings.isWhitelist) { - // Just to be safe - _settings.urlList << _settings.url; + if (_settings.blackList.isEmpty()) { + _settings.blackList << "*"; } - nam = new SlxNetworkAccessManager(urlListToRegExp(_settings.urlList), _settings.isWhitelist); + // Just to be safe + _settings.whiteList << _settings.url; + nam = new SlxNetworkAccessManager(urlListToRegExp(_settings.blackList), urlListToRegExp(_settings.whiteList)); } connect(nam, &QNetworkAccessManager::sslErrors, this, &SlxBrowser::sslErrors); connect(nam, &QNetworkAccessManager::finished, this, &SlxBrowser::requestFinished); diff --git a/src/slxbrowser.h b/src/slxbrowser.h index ae1356c..fd0cf06 100644 --- a/src/slxbrowser.h +++ b/src/slxbrowser.h @@ -18,8 +18,7 @@ struct BrowserSettings bool maximized; bool ignoreSslErrors; int reloadInterval; - bool isWhitelist; - QStringList urlList; + QStringList blackList, whiteList; }; class SlxBrowser : public QMainWindow -- cgit v1.2.3-55-g7522