From 737d688e5b173ef5155db3e4fc9e8debf9b33a11 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 31 May 2020 21:53:52 +0300 Subject: enable smolblok Build both HostlistFilter and AdblockFitler plugins by default. --- lib/smolblok/README.md | 8 ++ lib/smolblok/filtermanager.hpp | 41 ++++++ lib/smolblok/meson.build | 15 ++ lib/smolblok/smolblok.cpp | 40 ++++++ lib/smolblok/smolblok.hpp | 80 +++++++++++ lib/smolblok/test/loader.cpp | 25 ++++ lib/smolblok/test/main.cpp | 22 +++ lib/smolblok/test/sample-filters.txt | 10 ++ meson.build | 14 +- plugins/AdblockFilter/filterlist.cpp | 128 +++++++++++++++++ plugins/AdblockFilter/filterlist.h | 54 ++++++++ plugins/AdblockFilter/meson.build | 38 +++++ plugins/AdblockFilter/options.cpp | 94 +++++++++++++ plugins/AdblockFilter/options.h | 45 ++++++ .../AdblockFilter/plugin/AdblockPlusPlugin.json | 4 + plugins/AdblockFilter/plugin/plugin.cpp | 54 ++++++++ plugins/AdblockFilter/plugin/plugin.h | 25 ++++ plugins/AdblockFilter/rule.h | 153 +++++++++++++++++++++ plugins/AdblockFilter/test/filterlist.cpp | 118 ++++++++++++++++ plugins/AdblockFilter/test/options.cpp | 42 ++++++ plugins/AdblockFilter/test/rule.cpp | 86 ++++++++++++ plugins/HostlistFilter/corpus/apple.txt | 1 + plugins/HostlistFilter/corpus/banana.txt | 1 + plugins/HostlistFilter/corpus/kiwi.txt | 1 + plugins/HostlistFilter/corpus/orange.txt | 1 + plugins/HostlistFilter/filterlist.cpp | 58 ++++++++ plugins/HostlistFilter/filterlist.h | 58 ++++++++ plugins/HostlistFilter/meson.build | 43 ++++++ plugins/HostlistFilter/plugin/plugin.cpp | 32 +++++ plugins/HostlistFilter/plugin/plugin.h | 28 ++++ .../plugin/smolblokHostlistPlugin.json | 4 + plugins/HostlistFilter/test/filterlist.cpp | 29 ++++ plugins/HostlistFilter/test/hostlist.txt | 6 + plugins/HostlistFilter/test/plugin.cpp | 27 ++++ plugins/HostlistFilter/test/rule.cpp | 57 ++++++++ plugins/smolblok_hostlist/corpus/apple.txt | 1 - plugins/smolblok_hostlist/corpus/banana.txt | 1 - plugins/smolblok_hostlist/corpus/kiwi.txt | 1 - plugins/smolblok_hostlist/corpus/orange.txt | 1 - plugins/smolblok_hostlist/filterlist.cpp | 58 -------- plugins/smolblok_hostlist/filterlist.h | 58 -------- plugins/smolblok_hostlist/meson.build | 50 ------- plugins/smolblok_hostlist/plugin/plugin.cpp | 32 ----- plugins/smolblok_hostlist/plugin/plugin.h | 28 ---- .../plugin/smolblokHostlistPlugin.json | 4 - plugins/smolblok_hostlist/test/filterlist.cpp | 29 ---- plugins/smolblok_hostlist/test/hostlist.txt | 6 - plugins/smolblok_hostlist/test/plugin.cpp | 27 ---- plugins/smolblok_hostlist/test/rule.cpp | 57 -------- src/browser.cpp | 10 +- src/browser.h | 4 +- staging/adblock/filterlist.cpp | 128 ----------------- staging/adblock/filterlist.h | 54 -------- staging/adblock/meson.build | 38 ----- staging/adblock/options.cpp | 94 ------------- staging/adblock/options.h | 45 ------ staging/adblock/plugin/AdblockPlusPlugin.json | 4 - staging/adblock/plugin/plugin.cpp | 54 -------- staging/adblock/plugin/plugin.h | 25 ---- staging/adblock/rule.h | 153 --------------------- staging/adblock/test/filterlist.cpp | 118 ---------------- staging/adblock/test/options.cpp | 42 ------ staging/adblock/test/rule.cpp | 86 ------------ staging/smolblok/README.md | 8 -- staging/smolblok/filtermanager.hpp | 41 ------ staging/smolblok/meson.build | 17 --- staging/smolblok/smolblok.cpp | 40 ------ staging/smolblok/smolblok.hpp | 80 ----------- staging/smolblok/test/loader.cpp | 25 ---- staging/smolblok/test/main.cpp | 22 --- staging/smolblok/test/sample-filters.txt | 10 -- 71 files changed, 1445 insertions(+), 1448 deletions(-) create mode 100644 lib/smolblok/README.md create mode 100644 lib/smolblok/filtermanager.hpp create mode 100644 lib/smolblok/meson.build create mode 100644 lib/smolblok/smolblok.cpp create mode 100644 lib/smolblok/smolblok.hpp create mode 100644 lib/smolblok/test/loader.cpp create mode 100644 lib/smolblok/test/main.cpp create mode 100644 lib/smolblok/test/sample-filters.txt create mode 100644 plugins/AdblockFilter/filterlist.cpp create mode 100644 plugins/AdblockFilter/filterlist.h create mode 100644 plugins/AdblockFilter/meson.build create mode 100644 plugins/AdblockFilter/options.cpp create mode 100644 plugins/AdblockFilter/options.h create mode 100644 plugins/AdblockFilter/plugin/AdblockPlusPlugin.json create mode 100644 plugins/AdblockFilter/plugin/plugin.cpp create mode 100644 plugins/AdblockFilter/plugin/plugin.h create mode 100644 plugins/AdblockFilter/rule.h create mode 100644 plugins/AdblockFilter/test/filterlist.cpp create mode 100644 plugins/AdblockFilter/test/options.cpp create mode 100644 plugins/AdblockFilter/test/rule.cpp create mode 100644 plugins/HostlistFilter/corpus/apple.txt create mode 100644 plugins/HostlistFilter/corpus/banana.txt create mode 100644 plugins/HostlistFilter/corpus/kiwi.txt create mode 100644 plugins/HostlistFilter/corpus/orange.txt create mode 100644 plugins/HostlistFilter/filterlist.cpp create mode 100644 plugins/HostlistFilter/filterlist.h create mode 100644 plugins/HostlistFilter/meson.build create mode 100644 plugins/HostlistFilter/plugin/plugin.cpp create mode 100644 plugins/HostlistFilter/plugin/plugin.h create mode 100644 plugins/HostlistFilter/plugin/smolblokHostlistPlugin.json create mode 100644 plugins/HostlistFilter/test/filterlist.cpp create mode 100644 plugins/HostlistFilter/test/hostlist.txt create mode 100644 plugins/HostlistFilter/test/plugin.cpp create mode 100644 plugins/HostlistFilter/test/rule.cpp delete mode 100644 plugins/smolblok_hostlist/corpus/apple.txt delete mode 100644 plugins/smolblok_hostlist/corpus/banana.txt delete mode 100644 plugins/smolblok_hostlist/corpus/kiwi.txt delete mode 100644 plugins/smolblok_hostlist/corpus/orange.txt delete mode 100644 plugins/smolblok_hostlist/filterlist.cpp delete mode 100644 plugins/smolblok_hostlist/filterlist.h delete mode 100644 plugins/smolblok_hostlist/meson.build delete mode 100644 plugins/smolblok_hostlist/plugin/plugin.cpp delete mode 100644 plugins/smolblok_hostlist/plugin/plugin.h delete mode 100644 plugins/smolblok_hostlist/plugin/smolblokHostlistPlugin.json delete mode 100644 plugins/smolblok_hostlist/test/filterlist.cpp delete mode 100644 plugins/smolblok_hostlist/test/hostlist.txt delete mode 100644 plugins/smolblok_hostlist/test/plugin.cpp delete mode 100644 plugins/smolblok_hostlist/test/rule.cpp delete mode 100644 staging/adblock/filterlist.cpp delete mode 100644 staging/adblock/filterlist.h delete mode 100644 staging/adblock/meson.build delete mode 100644 staging/adblock/options.cpp delete mode 100644 staging/adblock/options.h delete mode 100644 staging/adblock/plugin/AdblockPlusPlugin.json delete mode 100644 staging/adblock/plugin/plugin.cpp delete mode 100644 staging/adblock/plugin/plugin.h delete mode 100644 staging/adblock/rule.h delete mode 100644 staging/adblock/test/filterlist.cpp delete mode 100644 staging/adblock/test/options.cpp delete mode 100644 staging/adblock/test/rule.cpp delete mode 100644 staging/smolblok/README.md delete mode 100644 staging/smolblok/filtermanager.hpp delete mode 100644 staging/smolblok/meson.build delete mode 100644 staging/smolblok/smolblok.cpp delete mode 100644 staging/smolblok/smolblok.hpp delete mode 100644 staging/smolblok/test/loader.cpp delete mode 100644 staging/smolblok/test/main.cpp delete mode 100644 staging/smolblok/test/sample-filters.txt diff --git a/lib/smolblok/README.md b/lib/smolblok/README.md new file mode 100644 index 0000000..1793009 --- /dev/null +++ b/lib/smolblok/README.md @@ -0,0 +1,8 @@ +## smolblok + +### What is this +This is a C++ library for URL filtering for Qt applications using QtWebEngine. + +### Supported formats +- AdblockPlus without element hiding rules + diff --git a/lib/smolblok/filtermanager.hpp b/lib/smolblok/filtermanager.hpp new file mode 100644 index 0000000..6ee4d3f --- /dev/null +++ b/lib/smolblok/filtermanager.hpp @@ -0,0 +1,41 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#pragma once + +#include +#include + +class FilterManager : public QWebEngineUrlRequestInterceptor +{ +public: + FilterManager(QObject *parent = nullptr) + : QWebEngineUrlRequestInterceptor(parent) + { + } + ~FilterManager() + { + qDeleteAll(filters); + } + + void addFilterList(FilterList *list) { + filters.append(list); + } + + void interceptRequest(QWebEngineUrlRequestInfo &info) override + { + for(const auto *filter : qAsConst(filters)) { + if(filter->filter(info)) { + return; + } + } + } + +private: + QList filters; +}; diff --git a/lib/smolblok/meson.build b/lib/smolblok/meson.build new file mode 100644 index 0000000..ea9e715 --- /dev/null +++ b/lib/smolblok/meson.build @@ -0,0 +1,15 @@ +dep_smolblok = declare_dependency( + include_directories: [ '.', smolbote_interfaces ], + link_with: library('smolblok', [ 'smolblok.cpp' ], include_directories: smolbote_interfaces, dependencies: dep_qt5) +) + +poi_sourceset.add(dep_smolblok) + +smolblok_load = executable('smolblok-load', + dependencies: [ dep_qt5, dep_spdlog, dep_smolblok ], + sources: [ 'test/loader.cpp' ] +) + +test('load', smolblok_load, suite: 'smolblok', should_fail: true) +test('load', smolblok_load, suite: 'smolblok', args: files('meson.build'), should_fail: true) + diff --git a/lib/smolblok/smolblok.cpp b/lib/smolblok/smolblok.cpp new file mode 100644 index 0000000..465c348 --- /dev/null +++ b/lib/smolblok/smolblok.cpp @@ -0,0 +1,40 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "smolblok.hpp" +#include +#include + +bool smolblok::addSubscriptions(const QString &filename) +{ + if(filename.isEmpty()) { + return false; + } + + QSettings listconf(filename, QSettings::IniFormat); + + for(auto &group : listconf.childGroups()) { + listconf.beginGroup(group); + const auto *loader = m_formats.value(listconf.value("Format").toString()).instance; + if(loader != nullptr) { + QFile f(listconf.value("File").toString()); + if(!f.exists()) { + continue; + } + + auto *list = loader->load(f); + f.seek(0); + if(loader->parse(list, f)) { + m_subscriptions.addFilterList(list); + } + } + listconf.endGroup(); + } + return false; +} + diff --git a/lib/smolblok/smolblok.hpp b/lib/smolblok/smolblok.hpp new file mode 100644 index 0000000..dc79232 --- /dev/null +++ b/lib/smolblok/smolblok.hpp @@ -0,0 +1,80 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_SMOLBLOK_HPP +#define SMOLBOTE_SMOLBLOK_HPP + +#include "filtermanager.hpp" +#include "smolbote/filterinterface.hpp" +#include +#include + +class smolblok +{ +public: + smolblok() = default; + ~smolblok() + { + for(auto &plugin : m_formats) { + delete plugin.loader; + } + } + + auto registerFormatPlugin(const QString &format, const QString &filename) + { + struct { + bool loaded = false; + QString error; + } ret; + + if(format.isEmpty() || filename.isEmpty()) { + ret.error = "Format or filename is empty"; + return ret; + } + + auto *plugin = new QPluginLoader(filename); + if(!plugin->load()) { + ret.error = plugin->errorString(); + delete plugin; + return ret; + } + + auto *instance = qobject_cast(plugin->instance()); + if(instance == nullptr) { + ret.error = "Unable to cast"; + delete plugin; + return ret; + } + + m_formats[format] = PluginInfo{ plugin, instance }; + ret.loaded = true; + return ret; + } + + auto formats() const + { + return m_formats.keys(); + } + + bool addSubscriptions(const QString &filename); + QWebEngineUrlRequestInterceptor *interceptor() + { + return &m_subscriptions; + } + +private: + struct PluginInfo { + QPluginLoader *loader = nullptr; + FilterPlugin *instance = nullptr; + }; + + QHash m_formats; + FilterManager m_subscriptions; +}; + +#endif // SMOLBOTE_SMOLBLOK_HPP diff --git a/lib/smolblok/test/loader.cpp b/lib/smolblok/test/loader.cpp new file mode 100644 index 0000000..9e27a26 --- /dev/null +++ b/lib/smolblok/test/loader.cpp @@ -0,0 +1,25 @@ +#include "smolblok.hpp" +#include + +int main(int argc, char** argv) +{ + if(argc != 2) { + spdlog::error("usage: {} path/to/plugin.so", argv[0]); + return -1; + } + + smolblok filter; + { + const auto r = filter.registerFormatPlugin("unused", argv[1]); + if(r.loaded) { + spdlog::info("Loaded plugin {}", argv[1]); + } else { + spdlog::error("Failed loading plugin {}", argv[1]); + spdlog::error(qUtf8Printable(r.error)); + return -1; + } + } + + return 0; +} + diff --git a/lib/smolblok/test/main.cpp b/lib/smolblok/test/main.cpp new file mode 100644 index 0000000..5624ee9 --- /dev/null +++ b/lib/smolblok/test/main.cpp @@ -0,0 +1,22 @@ +#define CATCH_CONFIG_MAIN + +#include "smolblok.hpp" +#include + +SCENARIO("smolblok") +{ + smolblok s; + + GIVEN("invalid plugins") + { + REQUIRE(!s.registerFormatPlugin("", "")); + REQUIRE(!s.registerFormatPlugin("Format", "missing.dll")); + } + + GIVEN("invalid subscriptions") + { + REQUIRE(!s.addSubscriptions("")); + REQUIRE(!s.addSubscriptions("missing.txt")); + } +} + diff --git a/lib/smolblok/test/sample-filters.txt b/lib/smolblok/test/sample-filters.txt new file mode 100644 index 0000000..59e0e7b --- /dev/null +++ b/lib/smolblok/test/sample-filters.txt @@ -0,0 +1,10 @@ +[easylist-noelemhide] +Format = AdblockPlus +File = easylist_noelemhide.txt +Href = https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt + +[StevenBlack] +Format = Hostlist +File = stevenblack.txt +Href = https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts + diff --git a/meson.build b/meson.build index 8290125..926fe8b 100644 --- a/meson.build +++ b/meson.build @@ -89,9 +89,6 @@ dep_qt5 = dependency('qt5', dep_spdlog = dependency('spdlog', fallback: ['spdlog', 'spdlog_dep'], version: '>=1.3.1') -optional_deps = [] -poi_cpp_args = [] - dep_breakpad = dependency('breakpad-client', include_type: 'system', required: get_option('crashhandler')) dep_threads = dependency('threads', include_type: 'system', required: get_option('crashhandler')) @@ -111,6 +108,7 @@ subdir('lib/configuration') subdir('lib/downloads') subdir('lib/pluginloader') subdir('lib/session_formats') +subdir('lib/smolblok') subdir('src') subdir('lang') @@ -118,20 +116,18 @@ subdir('doc') subdir('tools') subdir('plugins/ProfileEditor') +subdir('plugins/HostlistFilter') +subdir('plugins/AdblockFilter') subdir('test/firefox-bookmarks-json-parser') -subdir('test/matcherbenchmark') - -subdir('staging/smolblok') -subdir('plugins/smolblok_hostlist') ssconfig = poi_sourceset.apply(cdata) poi_exe = executable(get_option('poi'), - cpp_args: ['-DQAPPLICATION_CLASS=QApplication', poi_cpp_args], + cpp_args: ['-DQAPPLICATION_CLASS=QApplication'], sources: [ssconfig.sources()], include_directories: [ plugininterfaces_include, include_directories('src') ], - dependencies: [ dep_qt5, dep_spdlog, dep_SingleApplication, dep_args, optional_deps, dep_bookmarks, dep_configuration, dep_downloads, dep_pluginloader, ssconfig.dependencies(), lib_session_formats ], + dependencies: [ dep_qt5, dep_spdlog, dep_SingleApplication, dep_args, dep_bookmarks, dep_configuration, dep_downloads, dep_pluginloader, ssconfig.dependencies(), lib_session_formats ], install: true, ) diff --git a/plugins/AdblockFilter/filterlist.cpp b/plugins/AdblockFilter/filterlist.cpp new file mode 100644 index 0000000..76953fc --- /dev/null +++ b/plugins/AdblockFilter/filterlist.cpp @@ -0,0 +1,128 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "filterlist.h" +#include +#include + +/** + * Documentation: + * + * https://adblockplus.org/filter-cheatsheet + * https://help.eyeo.com/adblockplus/how-to-write-filters + * + * https://github.com/gorhill/uBlock/wiki/Introduction-to-basic-filtering-syntax + * https://github.com/gorhill/uBlock/wiki/Static-filter-syntax + * + */ + +const QLatin1String comment_lastModified("! Last modified: "); +const QLatin1String comment_expires("! Expires: "); + +using namespace AdblockPlus; + +Filterlist::Filterlist(QIODevice &from) +{ + if(from.isReadable() && from.isTextModeEnabled()) { + while(from.bytesAvailable() > 0) { + const auto line = from.readLine(512); + + if(line[0] == '!') { + parseComment(line); + + } else if(line.contains("##") || line.contains("#@#")) { + // ## is element hiding rule + // #@# is element hiding exception rule + + } else { + parseRule(line); + } + } + } +} + +void Filterlist::parseComment(const QString &line) +{ + if(line.startsWith(comment_lastModified)) { + lastModified = QDateTime::fromString(line.mid(comment_lastModified.size()), "dd MMM yyyy HH:mm 'UTC'"); + expires = lastModified; + + } else if(line.startsWith(comment_expires)) { + const QRegularExpression time_re("(?:(\\d+) days)|(?:(\\d+) hours)"); + const auto match = time_re.match(line); + if(match.hasMatch()) { + expires = expires.addDays(match.captured(1).toInt()); + expires = expires.addSecs(match.captured(2).toInt() * 60 * 60); + } + } +} + +Rule *Filterlist::parseRule(const QByteArray &line) +{ + QString pattern = line; + Options opt; + + if(pattern.startsWith(QLatin1String("@@"))) { + pattern.remove(0, 2); + opt.exception = true; + } + + // parse options + if(pattern.contains('$')) { + const auto list = pattern.split('$'); + pattern = list.at(0); + const auto options = list.at(1); + + if(!opt.parseAbp(&options)) { + return nullptr; + } + } + + if(pattern.startsWith("||") && pattern.endsWith("^")) { + // domain match + pattern = pattern.mid(2, pattern.length() - 3); + return new MatcherRule(pattern, opt, MatcherRule::DomainMatch); + + } else if(pattern.startsWith("|") && pattern.endsWith("|")) { + // string equals + pattern = pattern.mid(1, pattern.length() - 2); + return new MatcherRule(pattern, opt, MatcherRule::UrlEquals); + + } else if(pattern.startsWith("|")) { + // string starts with + pattern = pattern.mid(1, pattern.length() - 1); + return new MatcherRule(pattern, opt, MatcherRule::UrlStartsWith); + + } else if(pattern.endsWith("|")) { + // string ends with + pattern = pattern.mid(0, pattern.length() - 1); + return new MatcherRule(pattern, opt, MatcherRule::UrlEndsWith); + + } else if(pattern.startsWith("/") && pattern.endsWith("/")) { + // regular expression + pattern = pattern.mid(1, pattern.length() - 2); + return new RegexRule(pattern, opt); + + } else if(!pattern.isEmpty()) { + if(pattern.contains('*')) { + // wildcard pattern + pattern = QRegularExpression::wildcardToRegularExpression(pattern); + return new RegexRule(pattern, opt); + } else { + // contains pattern + return new MatcherRule(pattern, opt); + } + } + + return nullptr; +} + +bool Filterlist::filter(QWebEngineUrlRequestInfo &info) const +{ + return false; +} diff --git a/plugins/AdblockFilter/filterlist.h b/plugins/AdblockFilter/filterlist.h new file mode 100644 index 0000000..dba2138 --- /dev/null +++ b/plugins/AdblockFilter/filterlist.h @@ -0,0 +1,54 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "rule.h" +#include +#include +#include +#include +#include +#include + +namespace AdblockPlus +{ +class Filterlist : public FilterList +{ +public: + explicit Filterlist(QIODevice &from); + ~Filterlist() + { + qDeleteAll(m_rules); + } + + bool filter(QWebEngineUrlRequestInfo &info) const override; + bool isUpToDate() const override + { + const auto current = QDateTime::currentDateTime(); + return expires > current; + } + + QDateTime modified() const + { + return lastModified; + } + QDateTime expiresOn() const + { + return expires; + } + + [[nodiscard]] static Rule *parseRule(const QByteArray &line); + +private: + void parseComment(const QString &line); + + QDateTime lastModified; + QDateTime expires; + QVector m_rules; +}; + +} // namespace AdblockPlus diff --git a/plugins/AdblockFilter/meson.build b/plugins/AdblockFilter/meson.build new file mode 100644 index 0000000..942f325 --- /dev/null +++ b/plugins/AdblockFilter/meson.build @@ -0,0 +1,38 @@ +lib_adblockfilter = static_library('adblockfilter', + [ 'filterlist.cpp', 'options.cpp' ], + include_directories: smolbote_interfaces, + dependencies: [ dep_qt5 ] +) + +dep_adblockfilter = declare_dependency( + include_directories: ['.', smolbote_interfaces], + link_with: lib_adblockfilter +) + +#AdblockPlusFilterPlugin = shared_library('AdblockPlusPlugin', +# [ 'plugin/plugin.cpp', +# mod_qt5.preprocess(include_directories: smolbote_interfaces, +# moc_headers: 'plugin/plugin.h', dependencies: dep_qt5) +# ], +# include_directories: smolbote_interfaces, +# link_with: lib_adblockfilter, +# dependencies: dep_qt5, +# install: true, +# install_dir: get_option('libdir')/'smolbote/plugins' +#) + +test('adblock: rule', executable('libadblockfilter_rule', + sources: 'test/rule.cpp', + dependencies: [ dep_qt5, dep_catch, dep_adblockfilter ] +)) + +test('adblock: options', executable('libadblockfilter_options', + sources: 'test/options.cpp', + dependencies: [ dep_qt5, dep_catch, dep_adblockfilter ] +)) + +test('adblock: filterlist', executable('libadblockfilter_filterlist', + sources: 'test/filterlist.cpp', + dependencies: [ dep_qt5, dep_catch, dep_adblockfilter ] +)) + diff --git a/plugins/AdblockFilter/options.cpp b/plugins/AdblockFilter/options.cpp new file mode 100644 index 0000000..08f30ee --- /dev/null +++ b/plugins/AdblockFilter/options.cpp @@ -0,0 +1,94 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "options.h" + +using namespace AdblockPlus; + +constexpr std::array abpTypeOptions = { + "document", // ResourceTypeMainFrame 0 Top level page. + "subdocument", // ResourceTypeSubFrame 1 Frame or iframe. + "stylesheet", // ResourceTypeStylesheet 2 A CSS stylesheet. + "script", // ResourceTypeScript 3 An external script. + "image", // ResourceTypeImage 4 An image (JPG, GIF, PNG, and so on). + "font", // ResourceTypeFontResource 5 A font. + "other", // ResourceTypeSubResource 6 An "other" subresource. + "object", // ResourceTypeObject 7 An object (or embed) tag for a plugin or a resource that a plugin requested. + "media", // ResourceTypeMedia 8 A media resource. + "__worker", // ResourceTypeWorker 9 The main resource of a dedicated worker. + "__sharedworker", // ResourceTypeSharedWorker 10 The main resource of a shared worker. + "__prefetch", // ResourceTypePrefetch 11 An explicitly requested prefetch. + "__favicon", // ResourceTypeFavicon 12 A favicon. + "xmlhttprequest", // ResourceTypeXhr 13 An XMLHttpRequest. + "ping", // ResourceTypePing 14 A ping request for . + "__serviceworker", // ResourceTypeServiceWorker 15 The main resource of a service worker. + "__cspreport", // ResourceTypeCspReport 16 A report of Content Security Policy (CSP) violations. + "__pluginresource", // ResourceTypePluginResource 17 A resource requested by a plugin. + "__preloadmainframe", // ResourceTypeNavigationPreloadMainFrame 19 A main-frame service worker navigation preload request. + "__preloadsubframe", // ResourceTypeNavigationPreloadSubFrame 20 A sub-frame service worker navigation preload request. + "__unknown" // ResourceTypeUnknown 255 Unknown request type. +}; + +auto parseTypeOption(QStringRef &option) +{ + struct { + bool found = false; + int index = -1; + bool exception = false; + } ret; + + // Possible inverse type options include ~script, ~image, ~stylesheet, ~object, + // ~xmlhttprequest, ~subdocument, ~ping, ~websocket, ~webrtc, ~document, ~elemhide, ~other + if(option[0] == '~') { + ret.exception = true; + option = option.mid(1); + } + + // TODO: map all ResourceType's to their respective strings + // TODO: websocket, webrtc, elemhide, generichide, genericblock, popup + + for(std::size_t i = 0; i < std::size(abpTypeOptions); ++i) { + if(option == abpTypeOptions[i]) { + ret.index = i; + ret.found = true; + return ret; + } + } + return ret; +} + +bool Options::parseAbp(const QStringRef &options) +{ + std::bitset<32> checked_flags; + + for(auto &option : options.split(',')) { + if(option == "match-case") { + matchcase = true; + + } else if(option == "third-party") { + thirdparty = !exception; + } else if(const auto r = parseTypeOption(option); r.found) { + if(!r.exception) { + flags.set(r.index, true); + checked_flags.set(r.index, true); + } else { + flags.set(r.index, false); + checked_flags.set(r.index, true); + for(auto i = 0; i < 32; ++i) { + if(!checked_flags[i]) { + flags.set(i, true); + } + } + } + } else { + return false; + } + } + + return true; +} diff --git a/plugins/AdblockFilter/options.h b/plugins/AdblockFilter/options.h new file mode 100644 index 0000000..efc47a6 --- /dev/null +++ b/plugins/AdblockFilter/options.h @@ -0,0 +1,45 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_ADBLOCK_OPTIONS_H +#define SMOLBOTE_ADBLOCK_OPTIONS_H + +#include +#include +#include +#include + +namespace AdblockPlus +{ + +struct Options { + // request handling options + bool exception = false; + bool redirect = false; + + // pattern options + bool matchcase = false; + + // request type options + bool firstparty = true; + bool thirdparty = true; + + // request types + bool matchesType(QWebEngineUrlRequestInfo::ResourceType type) + { + return flags.test(type); + } + bool parseAbp(const QStringRef &options); + + // TODO private: + std::bitset<32> flags; +}; + +} // namespace AdblockPlus + +#endif // SMOLBOTE_ADBLOCK_OPTIONS_H diff --git a/plugins/AdblockFilter/plugin/AdblockPlusPlugin.json b/plugins/AdblockFilter/plugin/AdblockPlusPlugin.json new file mode 100644 index 0000000..053826a --- /dev/null +++ b/plugins/AdblockFilter/plugin/AdblockPlusPlugin.json @@ -0,0 +1,4 @@ +{ + "name": "AdblockPlus Filter Plugin", + "author": "Aqua " +} diff --git a/plugins/AdblockFilter/plugin/plugin.cpp b/plugins/AdblockFilter/plugin/plugin.cpp new file mode 100644 index 0000000..028c83f --- /dev/null +++ b/plugins/AdblockFilter/plugin/plugin.cpp @@ -0,0 +1,54 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "plugin.h" +#include "../filterlist.h" +#include + +using namespace AdblockPlus; + +Filter* AdblockPlusFilterPlugin::load(QIODevice* from) const +{ + if(!from->isOpen()) + return nullptr; + + + QTextStream stream(from); + + if(stream.readLine().trimmed() != QLatin1String("[Adblock Plus 2.0]")) { + return nullptr; + } + + auto *list = new FilterList; + QString line; + int total, comments, rules, unsupported, failed; + + while(stream.readLineInto(&line)) { + if(!line.isEmpty()) { + ++total; + + switch(list->parse(line)) + { + case FilterList::Comment: + ++comments; + break; + case FilterList::Rule: + ++rules; + break; + case FilterList::Unsupported: + ++unsupported; + break; + case FilterList::Failed: + break; + } + } + } + + return list; +} + diff --git a/plugins/AdblockFilter/plugin/plugin.h b/plugins/AdblockFilter/plugin/plugin.h new file mode 100644 index 0000000..db419bd --- /dev/null +++ b/plugins/AdblockFilter/plugin/plugin.h @@ -0,0 +1,25 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef ADBLOCKPLUSFILTER_PLUGIN_H +#define ADBLOCKPLUSFILTER_PLUGIN_H + +#include + +class AdblockPlusFilterPlugin : public QObject, public FilterPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID FilterPluginIid FILE "AdblockPlusPlugin.json") + Q_INTERFACES(FilterPlugin) + +public: + Filter* load(QIODevice* from) const override; +}; + +#endif // ADBLOCKPLUSFILTER_PLUGIN_H + diff --git a/plugins/AdblockFilter/rule.h b/plugins/AdblockFilter/rule.h new file mode 100644 index 0000000..aaab49a --- /dev/null +++ b/plugins/AdblockFilter/rule.h @@ -0,0 +1,153 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_ADBLOCK_RULE_H +#define SMOLBOTE_ADBLOCK_RULE_H + +#include "options.h" +#include +#include +#include +#include + +namespace AdblockPlus +{ +class Rule +{ +public: + virtual ~Rule() = default; + /** + * requestUrl: requested URL + * initiatorUrl: URL of document that initiated navigation + * firstPartyUrl: URL of the page that issued the request + */ + virtual bool hasMatch(const QStringRef &requestUrl, + const QStringRef &initiatorUrl, + const QStringRef &firstPartyUrl, + QWebEngineUrlRequestInfo::ResourceType resourceType = QWebEngineUrlRequestInfo::ResourceTypeMainFrame) const = 0; + + bool shouldRedirect() const + { + return options.redirect; + } + bool shouldBlock() const + { + return !options.exception; + } + +protected: + Rule(const Options &opt) + : options(opt) + { + } + const Options options; +}; + +// The separator character can be anything but +// a letter, a digit, or one of the following: _, -, ., %. +// The end of the address is also accepted as a separator. +inline bool isSeparator(const QChar &c) +{ + return !c.isLetter() && !c.isDigit() && c != '_' && c != '-' && c != '.' && c != '%'; +} + +class MatcherRule : public Rule +{ +public: + enum MatchPosition { + DomainMatch, + UrlStartsWith, + UrlEndsWith, + UrlContains, + UrlEquals + }; + + MatcherRule(const QString &pattern, const Options &opt, const MatchPosition pos = UrlContains) + : Rule(opt) + , position(pos) + , matcher(pattern, opt.matchcase ? Qt::CaseSensitive : Qt::CaseInsensitive) + , patternLength(pattern.length()) + { + } + explicit MatcherRule(const MatcherRule &) = delete; + MatcherRule &operator=(const MatcherRule &) = delete; + + explicit MatcherRule(MatcherRule &&) = delete; + MatcherRule &operator=(MatcherRule &&) = delete; + + ~MatcherRule() = default; + bool hasMatch(const QStringRef &url, + const QStringRef &initiatorUrl, + const QStringRef &firstPartyUrl, + QWebEngineUrlRequestInfo::ResourceType resourceType = QWebEngineUrlRequestInfo::ResourceTypeMainFrame) const override + { + const auto index = matcher.indexIn(url); + if(index == -1) { + return false; + } + + switch(position) { + case DomainMatch: + // match if + // there is only one : left of the index + // and + // character after pattern is separator or end of string + return (url.left(index).count(':') <= 1 && (index + patternLength == url.length() || isSeparator(url[index + patternLength]))); + case UrlStartsWith: + return (index == 0); + case UrlEndsWith: + return (index == url.length() - patternLength); + case UrlContains: + return (index != -1); + case UrlEquals: + return (index == 0 && patternLength == url.length()); + } + + return false; + } + +private: + const MatchPosition position; + const QStringMatcher matcher; + const int patternLength; +}; + +class RegexRule : public Rule +{ +public: + RegexRule(const QString &rule, const Options &opt) + : Rule(opt) + , regex(rule) + { + if(!opt.matchcase) { + regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption); + } + } + explicit RegexRule(const RegexRule &) = delete; + RegexRule &operator=(const RegexRule &) = delete; + + explicit RegexRule(RegexRule &&) = delete; + RegexRule &operator=(RegexRule &&) = delete; + + ~RegexRule() = default; + bool hasMatch(const QStringRef &url, + const QStringRef &initiatorUrl, + const QStringRef &firstPartyUrl, + QWebEngineUrlRequestInfo::ResourceType resourceType = QWebEngineUrlRequestInfo::ResourceTypeMainFrame) const override + { + const auto match = regex.match(url); + return match.hasMatch(); + } + +private: + QRegularExpression regex; +}; + +} // namespace AdblockPlus + +#endif // SMOLBOTE_ADBLOCK_RULE_H diff --git a/plugins/AdblockFilter/test/filterlist.cpp b/plugins/AdblockFilter/test/filterlist.cpp new file mode 100644 index 0000000..d3ec513 --- /dev/null +++ b/plugins/AdblockFilter/test/filterlist.cpp @@ -0,0 +1,118 @@ +#define CATCH_CONFIG_MAIN +#include "filterlist.h" +#include +#include + +using namespace AdblockPlus; + +QByteArray sampleList = + R"(! comment on line +! Last modified: 1 Jan 2000 00:00 UTC +! Expires: 4 days (update frequency) +)"; + +TEST_CASE("placeholder") +{ + QBuffer buffer(&sampleList); + buffer.open(QIODevice::ReadOnly | QIODevice::Text); + + AdblockPlus::Filterlist list(buffer); + REQUIRE(!list.isUpToDate()); +} + +TEST_CASE("domain match") +{ + const QString defaultUrl = ""; + + const QString block1 = "http://ads.example.com/foo.gif"; + const QString block2 = "http://server1.ads.example.com/foo.gif"; + const QString block3 = "https://ads.example.com:8000/"; + const QString block4 = "https://ads.example.com"; + + const QString allow1 = "http://ads.example.com.ua/foo.gif"; + const QString allow2 = "http://example.com/redirect/http://ads.example.com/"; + + auto *rule = Filterlist::parseRule("||ads.example.com^"); + REQUIRE(rule->shouldBlock()); + REQUIRE(!rule->shouldRedirect()); + + REQUIRE(rule->hasMatch(&block1, &defaultUrl, &defaultUrl)); + REQUIRE(rule->hasMatch(&block2, &defaultUrl, &defaultUrl)); + REQUIRE(rule->hasMatch(&block3, &defaultUrl, &defaultUrl)); + REQUIRE(rule->hasMatch(&block4, &defaultUrl, &defaultUrl)); + + REQUIRE(!rule->hasMatch(&allow1, &defaultUrl, &defaultUrl)); + REQUIRE(!rule->hasMatch(&allow2, &defaultUrl, &defaultUrl)); + + delete rule; +} + +TEST_CASE("string equals") +{ + const QString defaultUrl = ""; + + const QString block = "http://example.com/"; + + const QString allow1 = "http://example.com/foo.gif"; + const QString allow2 = "http://example.info/redirect/http://example.com/"; + + auto *rule = Filterlist::parseRule("|http://example.com/|"); + REQUIRE(rule->shouldBlock()); + REQUIRE(!rule->shouldRedirect()); + + REQUIRE(rule->hasMatch(&block, &defaultUrl, &defaultUrl)); + + REQUIRE(!rule->hasMatch(&allow1, &defaultUrl, &defaultUrl)); + REQUIRE(!rule->hasMatch(&allow2, &defaultUrl, &defaultUrl)); + + delete rule; +} + +TEST_CASE("string starts with") +{ + const QString defaultUrl = ""; + + auto *rule = Filterlist::parseRule("|http://baddomain.example/"); + REQUIRE(rule->shouldBlock()); + REQUIRE(!rule->shouldRedirect()); + + const QString blocks = "http://baddomain.example/banner.gif"; + const QString allows = "http://gooddomain.example/analyze?http://baddomain.example"; + + REQUIRE(rule->hasMatch(&blocks, &defaultUrl, &defaultUrl)); + REQUIRE(!rule->hasMatch(&allows, &defaultUrl, &defaultUrl)); + delete rule; +} + +TEST_CASE("string ends with") +{ + const QString defaultUrl = ""; + + auto *rule = Filterlist::parseRule("swf|"); + REQUIRE(rule->shouldBlock()); + REQUIRE(!rule->shouldRedirect()); + + const QString blocks = "http://example.com/annoyingflash.swf"; + const QString allows = "http://example.com/swf/index.html"; + + REQUIRE(rule->hasMatch(&blocks, &defaultUrl, &defaultUrl)); + REQUIRE(!rule->hasMatch(&allows, &defaultUrl, &defaultUrl)); + delete rule; +} + +TEST_CASE("regular expressions") +{ + const QString defaultUrl = ""; + + auto *rule = Filterlist::parseRule("/banner\\d+/"); + const QString matches1 = "banner123"; + const QString matches2 = "banner321"; + + const QString ignores = "banners"; + + REQUIRE(rule->hasMatch(&matches1, &defaultUrl, &defaultUrl)); + REQUIRE(rule->hasMatch(&matches2, &defaultUrl, &defaultUrl)); + REQUIRE(!rule->hasMatch(&ignores, &defaultUrl, &defaultUrl)); + delete rule; +} + diff --git a/plugins/AdblockFilter/test/options.cpp b/plugins/AdblockFilter/test/options.cpp new file mode 100644 index 0000000..67dc143 --- /dev/null +++ b/plugins/AdblockFilter/test/options.cpp @@ -0,0 +1,42 @@ +#define CATCH_CONFIG_MAIN +#include "options.h" +#include + +using namespace AdblockPlus; + +SCENARIO("parsing adblock options") +{ + Options opt; + + GIVEN("an unknown option") + { + const QString unknown = "unknown"; + THEN("the option is not parsed") + { + QStringRef unknown_ref(&unknown); + REQUIRE(!opt.parseAbp(unknown_ref)); + } + } + + GIVEN("match-case,document,~subdocument") + { + const QString options = "match-case,document,~subdocument"; + REQUIRE(opt.parseAbp(&options)); + + WHEN("match-case") + { + REQUIRE(opt.matchcase); + } + + WHEN("testing set/unset options") + { + REQUIRE(opt.matchesType(QWebEngineUrlRequestInfo::ResourceTypeMainFrame)); + REQUIRE(!opt.matchesType(QWebEngineUrlRequestInfo::ResourceTypeSubFrame)); + } + + WHEN("testing other options") + { + REQUIRE(opt.matchesType(QWebEngineUrlRequestInfo::ResourceTypeStylesheet)); + } + } +} diff --git a/plugins/AdblockFilter/test/rule.cpp b/plugins/AdblockFilter/test/rule.cpp new file mode 100644 index 0000000..07186b9 --- /dev/null +++ b/plugins/AdblockFilter/test/rule.cpp @@ -0,0 +1,86 @@ +#define CATCH_CONFIG_MAIN +#include "rule.h" +#include + +using namespace AdblockPlus; + +SCENARIO("MatcherRule") +{ + GIVEN("options with case sensitive pattern") + { + const QString defaultUrl = ""; + + const Options opt { .matchcase=true }; + const QString patternContains("this string contains the pattern in it"); + const QString patternBegins("pattern starts this string"); + const QString patternEnds("this string ends with pattern"); + const QString patternMissing("and this one does not"); + + WHEN("contains") + { + MatcherRule rule("pattern", opt); + REQUIRE(rule.shouldBlock()); + + THEN("pattern is matched anywhere in the URL") + { + REQUIRE(rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl)); + REQUIRE(rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl)); + REQUIRE(rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl)); + REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl)); + } + } + + WHEN("startsWith") + { + MatcherRule rule("pattern", opt, MatcherRule::UrlStartsWith); + REQUIRE(rule.shouldBlock()); + + THEN("pattern is matched if at the start of the URL") + { + REQUIRE(!rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl)); + REQUIRE(rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl)); + REQUIRE(!rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl)); + REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl)); + } + } + + WHEN("endsWith") + { + MatcherRule rule("pattern", opt, MatcherRule::UrlEndsWith); + REQUIRE(rule.shouldBlock()); + + THEN("pattern is matched if at the end of the URL") + { + REQUIRE(!rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl)); + REQUIRE(!rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl)); + REQUIRE(rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl)); + REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl)); + } + } + } +} + +SCENARIO("RegexRule") +{ + GIVEN("options with case sensitive pattern") + { + const QString defaultUrl; + + const Options opt { .matchcase=true }; + const QString patternContains("this string contains the pattern in it"); + const QString patternMissing("and this one does not"); + + WHEN("contains") + { + RegexRule rule("pattern", opt); + REQUIRE(rule.shouldBlock()); + + THEN("pattern is matched anywhere in the URL") + { + REQUIRE(rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl)); + REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl)); + } + } + } +} + diff --git a/plugins/HostlistFilter/corpus/apple.txt b/plugins/HostlistFilter/corpus/apple.txt new file mode 100644 index 0000000..3a8973b --- /dev/null +++ b/plugins/HostlistFilter/corpus/apple.txt @@ -0,0 +1 @@ +127.0.0.1 localhost.localdomain diff --git a/plugins/HostlistFilter/corpus/banana.txt b/plugins/HostlistFilter/corpus/banana.txt new file mode 100644 index 0000000..c30aa84 --- /dev/null +++ b/plugins/HostlistFilter/corpus/banana.txt @@ -0,0 +1 @@ +0.0.0.0 blockeddomain.com diff --git a/plugins/HostlistFilter/corpus/kiwi.txt b/plugins/HostlistFilter/corpus/kiwi.txt new file mode 100644 index 0000000..77c325c --- /dev/null +++ b/plugins/HostlistFilter/corpus/kiwi.txt @@ -0,0 +1 @@ +# This is a comment, and after it comes a blank line diff --git a/plugins/HostlistFilter/corpus/orange.txt b/plugins/HostlistFilter/corpus/orange.txt new file mode 100644 index 0000000..583273d --- /dev/null +++ b/plugins/HostlistFilter/corpus/orange.txt @@ -0,0 +1 @@ +0.0.0.0 blockeddomain.first blockeddomain.second diff --git a/plugins/HostlistFilter/filterlist.cpp b/plugins/HostlistFilter/filterlist.cpp new file mode 100644 index 0000000..a0fd414 --- /dev/null +++ b/plugins/HostlistFilter/filterlist.cpp @@ -0,0 +1,58 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "filterlist.h" +#include +#include + +using namespace Hostlist; + +#ifdef FUZZER +extern "C" int LLVMFuzzerTestOneInput(const char *Data, long long Size) +{ + Filterlist::parseRule(QString::fromLatin1(Data, Size)); + return 0; +} +#endif + +std::map Filterlist::parseRule(const QString &line) +{ + if(line.isEmpty() || line.at(0) == '#') { + return {}; + } + + auto parts = line.trimmed().split(' '); + if(parts.size() < 2) { + return {}; + } + + const auto redirect = (parts[0] == "0.0.0.0") ? QString() : parts[0]; + + std::map r; + for(int i = 1; i < parts.size(); ++i) { + r.emplace(qHash(parts[i], 0), Filterlist::Rule{ parts[i], redirect }); + } + return r; +} + +bool Filterlist::load(QIODevice &from) +{ + if(!from.isReadable() || !from.isTextModeEnabled()) { + return false; + } + + while(from.bytesAvailable() > 0) { + const auto line = from.readLine(512).trimmed(); + auto r = parseRule(line); + if(!r.empty()) { + qDebug("merging in %lu rules", r.size()); + rules.merge(r); + } + } + return true; +} diff --git a/plugins/HostlistFilter/filterlist.h b/plugins/HostlistFilter/filterlist.h new file mode 100644 index 0000000..7301f20 --- /dev/null +++ b/plugins/HostlistFilter/filterlist.h @@ -0,0 +1,58 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#pragma once + +#include +#include + +namespace Hostlist +{ + +class Filterlist final : public FilterList +{ +public: + typedef uint DomainHash; + struct Rule { + QString domain; + QString redirect; + }; + + Filterlist() = default; + ~Filterlist() = default; + + [[nodiscard]] bool findMatch(const QString &domain) const + { + const auto hash = qHash(domain, 0); + const auto found = rules.find(hash); + if(found != rules.end()) { + return true; + } + return false; + } + int count() const + { + return rules.size(); + } + + [[nodiscard]] bool filter(QWebEngineUrlRequestInfo &info) const + { + return false; + } + [[nodiscard]] bool isUpToDate() const + { + return true; + } + + bool load(QIODevice &device); + [[nodiscard]] static std::map parseRule(const QString &line); + +private: + std::map rules; +}; +} // namespace Hostlist diff --git a/plugins/HostlistFilter/meson.build b/plugins/HostlistFilter/meson.build new file mode 100644 index 0000000..1562524 --- /dev/null +++ b/plugins/HostlistFilter/meson.build @@ -0,0 +1,43 @@ +dep_hostlistfilter = declare_dependency( + include_directories: [ '.', smolbote_interfaces ], + link_with: static_library('hostlistfilter', [ 'filterlist.cpp' ], include_directories: smolbote_interfaces, dependencies: [dep_qt5]) +) + +# plugin +plugin = shared_library('smolblokHostlistPlugin', + [ 'plugin/plugin.cpp', mod_qt5.preprocess(include_directories: smolbote_interfaces, moc_headers: 'plugin/plugin.h', dependencies: dep_qt5) ], + include_directories: smolbote_interfaces, + dependencies: [ dep_hostlistfilter, dep_qt5 ], + install: true, + install_dir: get_option('libdir')/'smolbote/plugins' +) + +# tests +test('rule', executable('rule', sources: 'test/rule.cpp', dependencies: [dep_qt5, dep_catch, dep_hostlistfilter]), suite: 'hostlist') + +test('filterlist', executable('filterlist', + sources: 'test/filterlist.cpp', + dependencies: [dep_qt5, dep_catch, dep_hostlistfilter]), + env: 'HOSTLIST_TXT='+meson.current_source_dir()/'test/hostlist.txt', + suite: 'hostlist' +) +test('plugin', executable('filterlist-plugin', + sources: [ 'test/plugin.cpp', 'plugin/plugin.cpp', + mod_qt5.preprocess(include_directories: smolbote_interfaces, moc_headers: 'plugin/plugin.h', dependencies: dep_qt5) ], + dependencies: [dep_qt5, dep_catch, dep_hostlistfilter]), + env: 'HOSTLIST_TXT='+meson.current_source_dir()/'test/hostlist.txt', + suite: 'hostlist' +) + +test('smolblok-load', smolblok_load, workdir: meson.build_root(), args: plugin.full_path(), suite: 'hostlist') + +# fuzzer +if meson.get_compiler('cpp').has_multi_arguments('-g', '-fsanitize=fuzzer') +executable('hostlist-fuzzer', + sources: 'filterlist.cpp', + include_directories: smolbote_interfaces, + dependencies: dep_qt5, + cpp_args: [ '-g', '-fsanitize=fuzzer', '-DFUZZER' ], + link_args: [ '-fsanitize=fuzzer' ] +) +endif diff --git a/plugins/HostlistFilter/plugin/plugin.cpp b/plugins/HostlistFilter/plugin/plugin.cpp new file mode 100644 index 0000000..28a7706 --- /dev/null +++ b/plugins/HostlistFilter/plugin/plugin.cpp @@ -0,0 +1,32 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "plugin.h" +#include "filterlist.h" + +FilterList* HostlistFilterPlugin::load(QIODevice &from) const +{ + if(!from.isOpen()) + return nullptr; + + auto *list = new Hostlist::Filterlist; + return list; +} + +bool HostlistFilterPlugin::parse(FilterList *list, QIODevice &from) const +{ + if(list == nullptr || !from.isOpen()) { + return false; + } + auto *l = dynamic_cast(list); + if(l == nullptr) { + return false; + } + return l->load(from); +} + diff --git a/plugins/HostlistFilter/plugin/plugin.h b/plugins/HostlistFilter/plugin/plugin.h new file mode 100644 index 0000000..53b5d36 --- /dev/null +++ b/plugins/HostlistFilter/plugin/plugin.h @@ -0,0 +1,28 @@ +/* + * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBLOK_FILTER_PLUGIN_H +#define SMOLBLOK_FILTER_PLUGIN_H + +#include + +class HostlistFilterPlugin : public QObject, public FilterPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID FilterPluginIid FILE "smolblokHostlistPlugin.json") + Q_INTERFACES(FilterPlugin) + +public: + ~HostlistFilterPlugin() = default; + + FilterList *load(QIODevice &from) const override; + bool parse(FilterList *list, QIODevice &from) const override; +}; + +#endif // SMOLBLOK_FILTER_PLUGIN_H + diff --git a/plugins/HostlistFilter/plugin/smolblokHostlistPlugin.json b/plugins/HostlistFilter/plugin/smolblokHostlistPlugin.json new file mode 100644 index 0000000..aa53cdd --- /dev/null +++ b/plugins/HostlistFilter/plugin/smolblokHostlistPlugin.json @@ -0,0 +1,4 @@ +{ + "name": "smolblok Hostlist filter plugin", + "author": "Aqua " +} diff --git a/plugins/HostlistFilter/test/filterlist.cpp b/plugins/HostlistFilter/test/filterlist.cpp new file mode 100644 index 0000000..4aa532b --- /dev/null +++ b/plugins/HostlistFilter/test/filterlist.cpp @@ -0,0 +1,29 @@ +#define CATCH_CONFIG_MAIN +#include "filterlist.h" +#include +#include + +using namespace Hostlist; + +TEST_CASE("Hostlist") +{ + Filterlist list; + + const QString filename(qgetenv("HOSTLIST_TXT")); + REQUIRE(!filename.isEmpty()); + + QFile f(filename); + REQUIRE(f.open(QIODevice::ReadOnly | QIODevice::Text)); + + REQUIRE(list.load(f)); + f.close(); + + REQUIRE(list.count() == 4); + + REQUIRE(list.findMatch("blockeddomain.first")); + REQUIRE(list.findMatch("blockeddomain.second")); + + REQUIRE(list.findMatch("localhost.localdomain")); + + REQUIRE(!list.findMatch("other.domain")); +} diff --git a/plugins/HostlistFilter/test/hostlist.txt b/plugins/HostlistFilter/test/hostlist.txt new file mode 100644 index 0000000..a0b4e5c --- /dev/null +++ b/plugins/HostlistFilter/test/hostlist.txt @@ -0,0 +1,6 @@ +# This is a comment, and after it comes a blank line + +127.0.0.1 localhost.localdomain + +0.0.0.0 blockeddomain.com +0.0.0.0 blockeddomain.first blockeddomain.second diff --git a/plugins/HostlistFilter/test/plugin.cpp b/plugins/HostlistFilter/test/plugin.cpp new file mode 100644 index 0000000..fad34f2 --- /dev/null +++ b/plugins/HostlistFilter/test/plugin.cpp @@ -0,0 +1,27 @@ +#define CATCH_CONFIG_MAIN +#include "plugin/plugin.h" +#include +#include + +TEST_CASE("Hostlist") +{ + HostlistFilterPlugin plugin; + + const QString filename(qgetenv("HOSTLIST_TXT")); + REQUIRE(!filename.isEmpty()); + QFile f(filename); + + // shouldn't be able to load an unopened QIODevice + REQUIRE(plugin.load(f) == nullptr); + + REQUIRE(f.open(QIODevice::ReadOnly | QIODevice::Text)); + + auto *list = plugin.load(f); + REQUIRE(list != nullptr); + f.seek(0); + REQUIRE_FALSE(plugin.parse(nullptr, f)); + REQUIRE(plugin.parse(list, f)); + f.close(); + REQUIRE_FALSE(plugin.parse(list, f)); +} + diff --git a/plugins/HostlistFilter/test/rule.cpp b/plugins/HostlistFilter/test/rule.cpp new file mode 100644 index 0000000..b5ba6e0 --- /dev/null +++ b/plugins/HostlistFilter/test/rule.cpp @@ -0,0 +1,57 @@ +#define CATCH_CONFIG_MAIN +#include "filterlist.h" +#include + +using namespace Hostlist; + +SCENARIO("Hostlist::Rule") +{ + GIVEN("an invalid rule") + { + const auto rule = Filterlist::parseRule("0.0.0.0 "); + REQUIRE(rule.empty()); + } + GIVEN("127.0.0.1 localhost.localdomain") + { + auto rule = Filterlist::parseRule("127.0.0.1 localhost.localdomain"); + + REQUIRE(!rule.empty()); + REQUIRE(rule.size() == 1); + + // note: you need to force it to hash a string, rather than the address itself + const auto index = qHash(QString("localhost.localdomain"), 0); + REQUIRE(rule[index].domain == "localhost.localdomain"); + REQUIRE(rule[index].redirect == "127.0.0.1"); + } + + GIVEN("0.0.0.0 blockeddomain.com") + { + auto rule = Filterlist::parseRule("0.0.0.0 blockeddomain.com"); + + REQUIRE(!rule.empty()); + REQUIRE(rule.size() == 1); + + const auto index = qHash(QString("blockeddomain.com"), 0); + REQUIRE(rule[index].domain == "blockeddomain.com"); + REQUIRE(rule[index].redirect.isEmpty()); + ; + } + + GIVEN("0.0.0.0 blockeddomain.first blockeddomain.second") + { + auto rule = Filterlist::parseRule("0.0.0.0 blockeddomain.first blockeddomain.second"); + + REQUIRE(!rule.empty()); + REQUIRE(rule.size() == 2); + { + const auto index = qHash(QString("blockeddomain.first"), 0); + REQUIRE(rule[index].domain == "blockeddomain.first"); + REQUIRE(rule[index].redirect.isEmpty()); + } + { + const auto index = qHash(QString("blockeddomain.second"), 0); + REQUIRE(rule[index].domain == "blockeddomain.second"); + REQUIRE(rule[index].redirect.isEmpty()); + } + } +} diff --git a/plugins/smolblok_hostlist/corpus/apple.txt b/plugins/smolblok_hostlist/corpus/apple.txt deleted file mode 100644 index 3a8973b..0000000 --- a/plugins/smolblok_hostlist/corpus/apple.txt +++ /dev/null @@ -1 +0,0 @@ -127.0.0.1 localhost.localdomain diff --git a/plugins/smolblok_hostlist/corpus/banana.txt b/plugins/smolblok_hostlist/corpus/banana.txt deleted file mode 100644 index c30aa84..0000000 --- a/plugins/smolblok_hostlist/corpus/banana.txt +++ /dev/null @@ -1 +0,0 @@ -0.0.0.0 blockeddomain.com diff --git a/plugins/smolblok_hostlist/corpus/kiwi.txt b/plugins/smolblok_hostlist/corpus/kiwi.txt deleted file mode 100644 index 77c325c..0000000 --- a/plugins/smolblok_hostlist/corpus/kiwi.txt +++ /dev/null @@ -1 +0,0 @@ -# This is a comment, and after it comes a blank line diff --git a/plugins/smolblok_hostlist/corpus/orange.txt b/plugins/smolblok_hostlist/corpus/orange.txt deleted file mode 100644 index 583273d..0000000 --- a/plugins/smolblok_hostlist/corpus/orange.txt +++ /dev/null @@ -1 +0,0 @@ -0.0.0.0 blockeddomain.first blockeddomain.second diff --git a/plugins/smolblok_hostlist/filterlist.cpp b/plugins/smolblok_hostlist/filterlist.cpp deleted file mode 100644 index a0fd414..0000000 --- a/plugins/smolblok_hostlist/filterlist.cpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "filterlist.h" -#include -#include - -using namespace Hostlist; - -#ifdef FUZZER -extern "C" int LLVMFuzzerTestOneInput(const char *Data, long long Size) -{ - Filterlist::parseRule(QString::fromLatin1(Data, Size)); - return 0; -} -#endif - -std::map Filterlist::parseRule(const QString &line) -{ - if(line.isEmpty() || line.at(0) == '#') { - return {}; - } - - auto parts = line.trimmed().split(' '); - if(parts.size() < 2) { - return {}; - } - - const auto redirect = (parts[0] == "0.0.0.0") ? QString() : parts[0]; - - std::map r; - for(int i = 1; i < parts.size(); ++i) { - r.emplace(qHash(parts[i], 0), Filterlist::Rule{ parts[i], redirect }); - } - return r; -} - -bool Filterlist::load(QIODevice &from) -{ - if(!from.isReadable() || !from.isTextModeEnabled()) { - return false; - } - - while(from.bytesAvailable() > 0) { - const auto line = from.readLine(512).trimmed(); - auto r = parseRule(line); - if(!r.empty()) { - qDebug("merging in %lu rules", r.size()); - rules.merge(r); - } - } - return true; -} diff --git a/plugins/smolblok_hostlist/filterlist.h b/plugins/smolblok_hostlist/filterlist.h deleted file mode 100644 index 7301f20..0000000 --- a/plugins/smolblok_hostlist/filterlist.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#pragma once - -#include -#include - -namespace Hostlist -{ - -class Filterlist final : public FilterList -{ -public: - typedef uint DomainHash; - struct Rule { - QString domain; - QString redirect; - }; - - Filterlist() = default; - ~Filterlist() = default; - - [[nodiscard]] bool findMatch(const QString &domain) const - { - const auto hash = qHash(domain, 0); - const auto found = rules.find(hash); - if(found != rules.end()) { - return true; - } - return false; - } - int count() const - { - return rules.size(); - } - - [[nodiscard]] bool filter(QWebEngineUrlRequestInfo &info) const - { - return false; - } - [[nodiscard]] bool isUpToDate() const - { - return true; - } - - bool load(QIODevice &device); - [[nodiscard]] static std::map parseRule(const QString &line); - -private: - std::map rules; -}; -} // namespace Hostlist diff --git a/plugins/smolblok_hostlist/meson.build b/plugins/smolblok_hostlist/meson.build deleted file mode 100644 index c9ff303..0000000 --- a/plugins/smolblok_hostlist/meson.build +++ /dev/null @@ -1,50 +0,0 @@ -lib_hostlistfilter = static_library('hostlistfilter', - [ 'filterlist.cpp' ], - include_directories: smolbote_interfaces, - dependencies: [dep_qt5] -) - -dep_hostlistfilter = declare_dependency( - include_directories: [ '.', smolbote_interfaces ], - link_with: lib_hostlistfilter -) - -# plugin -plugin = shared_library('smolblokHostlistPlugin', - [ 'plugin/plugin.cpp', - mod_qt5.preprocess(include_directories: smolbote_interfaces, moc_headers: 'plugin/plugin.h', dependencies: dep_qt5) ], - include_directories: smolbote_interfaces, - dependencies: [ dep_hostlistfilter, dep_qt5 ], - install: true, - install_dir: get_option('libdir')/'smolbote/plugins' -) - -# tests -test('rule', executable('rule', sources: 'test/rule.cpp', dependencies: [dep_qt5, dep_catch, dep_hostlistfilter]), suite: 'hostlist') - -test('filterlist', executable('filterlist', - sources: 'test/filterlist.cpp', - dependencies: [dep_qt5, dep_catch, dep_hostlistfilter]), - env: 'HOSTLIST_TXT='+meson.current_source_dir()/'test/hostlist.txt', - suite: 'hostlist' -) -test('plugin', executable('filterlist-plugin', - sources: [ 'test/plugin.cpp', 'plugin/plugin.cpp', - mod_qt5.preprocess(include_directories: smolbote_interfaces, moc_headers: 'plugin/plugin.h', dependencies: dep_qt5) ], - dependencies: [dep_qt5, dep_catch, dep_hostlistfilter]), - env: 'HOSTLIST_TXT='+meson.current_source_dir()/'test/hostlist.txt', - suite: 'hostlist' -) - -test('smolblok-load', smolblok_load, workdir: meson.build_root(), args: plugin.full_path(), suite: 'hostlist') - -# fuzzer -if meson.get_compiler('cpp').has_multi_arguments('-g', '-fsanitize=fuzzer') -executable('hostlist-fuzzer', - sources: 'filterlist.cpp', - include_directories: smolbote_interfaces, - dependencies: dep_qt5, - cpp_args: [ '-g', '-fsanitize=fuzzer', '-DFUZZER' ], - link_args: [ '-fsanitize=fuzzer' ] -) -endif diff --git a/plugins/smolblok_hostlist/plugin/plugin.cpp b/plugins/smolblok_hostlist/plugin/plugin.cpp deleted file mode 100644 index 28a7706..0000000 --- a/plugins/smolblok_hostlist/plugin/plugin.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "plugin.h" -#include "filterlist.h" - -FilterList* HostlistFilterPlugin::load(QIODevice &from) const -{ - if(!from.isOpen()) - return nullptr; - - auto *list = new Hostlist::Filterlist; - return list; -} - -bool HostlistFilterPlugin::parse(FilterList *list, QIODevice &from) const -{ - if(list == nullptr || !from.isOpen()) { - return false; - } - auto *l = dynamic_cast(list); - if(l == nullptr) { - return false; - } - return l->load(from); -} - diff --git a/plugins/smolblok_hostlist/plugin/plugin.h b/plugins/smolblok_hostlist/plugin/plugin.h deleted file mode 100644 index 53b5d36..0000000 --- a/plugins/smolblok_hostlist/plugin/plugin.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBLOK_FILTER_PLUGIN_H -#define SMOLBLOK_FILTER_PLUGIN_H - -#include - -class HostlistFilterPlugin : public QObject, public FilterPlugin -{ - Q_OBJECT - Q_PLUGIN_METADATA(IID FilterPluginIid FILE "smolblokHostlistPlugin.json") - Q_INTERFACES(FilterPlugin) - -public: - ~HostlistFilterPlugin() = default; - - FilterList *load(QIODevice &from) const override; - bool parse(FilterList *list, QIODevice &from) const override; -}; - -#endif // SMOLBLOK_FILTER_PLUGIN_H - diff --git a/plugins/smolblok_hostlist/plugin/smolblokHostlistPlugin.json b/plugins/smolblok_hostlist/plugin/smolblokHostlistPlugin.json deleted file mode 100644 index aa53cdd..0000000 --- a/plugins/smolblok_hostlist/plugin/smolblokHostlistPlugin.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "smolblok Hostlist filter plugin", - "author": "Aqua " -} diff --git a/plugins/smolblok_hostlist/test/filterlist.cpp b/plugins/smolblok_hostlist/test/filterlist.cpp deleted file mode 100644 index 4aa532b..0000000 --- a/plugins/smolblok_hostlist/test/filterlist.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#define CATCH_CONFIG_MAIN -#include "filterlist.h" -#include -#include - -using namespace Hostlist; - -TEST_CASE("Hostlist") -{ - Filterlist list; - - const QString filename(qgetenv("HOSTLIST_TXT")); - REQUIRE(!filename.isEmpty()); - - QFile f(filename); - REQUIRE(f.open(QIODevice::ReadOnly | QIODevice::Text)); - - REQUIRE(list.load(f)); - f.close(); - - REQUIRE(list.count() == 4); - - REQUIRE(list.findMatch("blockeddomain.first")); - REQUIRE(list.findMatch("blockeddomain.second")); - - REQUIRE(list.findMatch("localhost.localdomain")); - - REQUIRE(!list.findMatch("other.domain")); -} diff --git a/plugins/smolblok_hostlist/test/hostlist.txt b/plugins/smolblok_hostlist/test/hostlist.txt deleted file mode 100644 index a0b4e5c..0000000 --- a/plugins/smolblok_hostlist/test/hostlist.txt +++ /dev/null @@ -1,6 +0,0 @@ -# This is a comment, and after it comes a blank line - -127.0.0.1 localhost.localdomain - -0.0.0.0 blockeddomain.com -0.0.0.0 blockeddomain.first blockeddomain.second diff --git a/plugins/smolblok_hostlist/test/plugin.cpp b/plugins/smolblok_hostlist/test/plugin.cpp deleted file mode 100644 index fad34f2..0000000 --- a/plugins/smolblok_hostlist/test/plugin.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#define CATCH_CONFIG_MAIN -#include "plugin/plugin.h" -#include -#include - -TEST_CASE("Hostlist") -{ - HostlistFilterPlugin plugin; - - const QString filename(qgetenv("HOSTLIST_TXT")); - REQUIRE(!filename.isEmpty()); - QFile f(filename); - - // shouldn't be able to load an unopened QIODevice - REQUIRE(plugin.load(f) == nullptr); - - REQUIRE(f.open(QIODevice::ReadOnly | QIODevice::Text)); - - auto *list = plugin.load(f); - REQUIRE(list != nullptr); - f.seek(0); - REQUIRE_FALSE(plugin.parse(nullptr, f)); - REQUIRE(plugin.parse(list, f)); - f.close(); - REQUIRE_FALSE(plugin.parse(list, f)); -} - diff --git a/plugins/smolblok_hostlist/test/rule.cpp b/plugins/smolblok_hostlist/test/rule.cpp deleted file mode 100644 index b5ba6e0..0000000 --- a/plugins/smolblok_hostlist/test/rule.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#define CATCH_CONFIG_MAIN -#include "filterlist.h" -#include - -using namespace Hostlist; - -SCENARIO("Hostlist::Rule") -{ - GIVEN("an invalid rule") - { - const auto rule = Filterlist::parseRule("0.0.0.0 "); - REQUIRE(rule.empty()); - } - GIVEN("127.0.0.1 localhost.localdomain") - { - auto rule = Filterlist::parseRule("127.0.0.1 localhost.localdomain"); - - REQUIRE(!rule.empty()); - REQUIRE(rule.size() == 1); - - // note: you need to force it to hash a string, rather than the address itself - const auto index = qHash(QString("localhost.localdomain"), 0); - REQUIRE(rule[index].domain == "localhost.localdomain"); - REQUIRE(rule[index].redirect == "127.0.0.1"); - } - - GIVEN("0.0.0.0 blockeddomain.com") - { - auto rule = Filterlist::parseRule("0.0.0.0 blockeddomain.com"); - - REQUIRE(!rule.empty()); - REQUIRE(rule.size() == 1); - - const auto index = qHash(QString("blockeddomain.com"), 0); - REQUIRE(rule[index].domain == "blockeddomain.com"); - REQUIRE(rule[index].redirect.isEmpty()); - ; - } - - GIVEN("0.0.0.0 blockeddomain.first blockeddomain.second") - { - auto rule = Filterlist::parseRule("0.0.0.0 blockeddomain.first blockeddomain.second"); - - REQUIRE(!rule.empty()); - REQUIRE(rule.size() == 2); - { - const auto index = qHash(QString("blockeddomain.first"), 0); - REQUIRE(rule[index].domain == "blockeddomain.first"); - REQUIRE(rule[index].redirect.isEmpty()); - } - { - const auto index = qHash(QString("blockeddomain.second"), 0); - REQUIRE(rule[index].domain == "blockeddomain.second"); - REQUIRE(rule[index].redirect.isEmpty()); - } - } -} diff --git a/src/browser.cpp b/src/browser.cpp index ff948e4..a04f87d 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -19,7 +19,6 @@ #include "smolbote/plugininterface.hpp" #include "subwindow/subwindow.h" #include "util.h" -#include "webengine/urlinterceptor.h" #include "webengine/webprofile.h" #include "webengine/webprofilemanager.h" #include "webengine/webview.h" @@ -70,6 +69,14 @@ Browser::Browser(int &argc, char *argv[], bool allowSecondary) } } + // content filter - register format plugins + if(const auto hostlist_plugin = conf.value("smolblok.plugins.hostlist")) { + content_filter.registerFormatPlugin("hostlist", hostlist_plugin.value()); + } + if(const auto adblock_plugin = conf.value("smolblok.plugins.adblock")) { + content_filter.registerFormatPlugin("adblock", adblock_plugin.value()); + } + // load profiles { const auto profiles = Util::files(conf.value("profile.path").value(), { "*.profile" }); @@ -92,6 +99,7 @@ Browser::Browser(int &argc, char *argv[], bool allowSecondary) // downloads m_downloads = std::make_unique(conf.value("downloads.path").value()); m_profileManager->walk([this](const QString &, WebProfile *profile, QSettings *) { + profile->setUrlRequestInterceptor(content_filter.interceptor()); connect(profile, &QWebEngineProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); }); diff --git a/src/browser.h b/src/browser.h index 0a0b201..69cf804 100644 --- a/src/browser.h +++ b/src/browser.h @@ -9,8 +9,9 @@ #ifndef SMOLBOTE_BROWSER_H #define SMOLBOTE_BROWSER_H -#include "webengine/webprofilemanager.h" +#include "smolblok.hpp" #include "smolbote/session.hpp" +#include "webengine/webprofilemanager.h" #include #include #include @@ -79,6 +80,7 @@ private: std::shared_ptr m_bookmarks; std::unique_ptr m_downloads; std::unique_ptr> m_profileManager{ nullptr }; + smolblok content_filter; QVector m_windows; QVector m_plugins; diff --git a/staging/adblock/filterlist.cpp b/staging/adblock/filterlist.cpp deleted file mode 100644 index 1846ff6..0000000 --- a/staging/adblock/filterlist.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "filterlist.h" -#include -#include - -/** - * Documentation: - * - * https://adblockplus.org/filter-cheatsheet - * https://help.eyeo.com/adblockplus/how-to-write-filters - * - * https://github.com/gorhill/uBlock/wiki/Introduction-to-basic-filtering-syntax - * https://github.com/gorhill/uBlock/wiki/Static-filter-syntax - * - */ - -const QLatin1String comment_lastModified("! Last modified: "); -const QLatin1String comment_expires("! Expires: "); - -using namespace AdblockPlus; - -FilterList::FilterList(QIODevice &from) -{ - if(from.isReadable() && from.isTextModeEnabled()) { - while(from.bytesAvailable() > 0) { - const auto line = from.readLine(512); - - if(line[0] == '!') { - parseComment(line); - - } else if(line.contains("##") || line.contains("#@#")) { - // ## is element hiding rule - // #@# is element hiding exception rule - - } else { - parseRule(line); - } - } - } -} - -void FilterList::parseComment(const QString &line) -{ - if(line.startsWith(comment_lastModified)) { - lastModified = QDateTime::fromString(line.mid(comment_lastModified.size()), "dd MMM yyyy HH:mm 'UTC'"); - expires = lastModified; - - } else if(line.startsWith(comment_expires)) { - const QRegularExpression time_re("(?:(\\d+) days)|(?:(\\d+) hours)"); - const auto match = time_re.match(line); - if(match.hasMatch()) { - expires = expires.addDays(match.captured(1).toInt()); - expires = expires.addSecs(match.captured(2).toInt() * 60 * 60); - } - } -} - -Rule *FilterList::parseRule(const QByteArray &line) -{ - QString pattern = line; - Options opt; - - if(pattern.startsWith(QLatin1String("@@"))) { - pattern.remove(0, 2); - opt.exception = true; - } - - // parse options - if(pattern.contains('$')) { - const auto list = pattern.split('$'); - pattern = list.at(0); - const auto options = list.at(1); - - if(!opt.parseAbp(&options)) { - return nullptr; - } - } - - if(pattern.startsWith("||") && pattern.endsWith("^")) { - // domain match - pattern = pattern.mid(2, pattern.length() - 3); - return new MatcherRule(pattern, opt, MatcherRule::DomainMatch); - - } else if(pattern.startsWith("|") && pattern.endsWith("|")) { - // string equals - pattern = pattern.mid(1, pattern.length() - 2); - return new MatcherRule(pattern, opt, MatcherRule::UrlEquals); - - } else if(pattern.startsWith("|")) { - // string starts with - pattern = pattern.mid(1, pattern.length() - 1); - return new MatcherRule(pattern, opt, MatcherRule::UrlStartsWith); - - } else if(pattern.endsWith("|")) { - // string ends with - pattern = pattern.mid(0, pattern.length() - 1); - return new MatcherRule(pattern, opt, MatcherRule::UrlEndsWith); - - } else if(pattern.startsWith("/") && pattern.endsWith("/")) { - // regular expression - pattern = pattern.mid(1, pattern.length() - 2); - return new RegexRule(pattern, opt); - - } else if(!pattern.isEmpty()) { - if(pattern.contains('*')) { - // wildcard pattern - pattern = QRegularExpression::wildcardToRegularExpression(pattern); - return new RegexRule(pattern, opt); - } else { - // contains pattern - return new MatcherRule(pattern, opt); - } - } - - return nullptr; -} - -bool FilterList::filter(QWebEngineUrlRequestInfo &info) const -{ - return false; -} diff --git a/staging/adblock/filterlist.h b/staging/adblock/filterlist.h deleted file mode 100644 index 24464c8..0000000 --- a/staging/adblock/filterlist.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "rule.h" -#include -#include -#include -#include -#include -#include - -namespace AdblockPlus -{ -class FilterList : public Filter -{ -public: - explicit FilterList(QIODevice &from); - ~FilterList() - { - qDeleteAll(m_rules); - } - - bool filter(QWebEngineUrlRequestInfo &info) const override; - bool isUpToDate() const override - { - const auto current = QDateTime::currentDateTime(); - return expires > current; - } - - QDateTime modified() const - { - return lastModified; - } - QDateTime expiresOn() const - { - return expires; - } - - [[nodiscard]] static Rule *parseRule(const QByteArray &line); - -private: - void parseComment(const QString &line); - - QDateTime lastModified; - QDateTime expires; - QVector m_rules; -}; - -} // namespace AdblockPlus diff --git a/staging/adblock/meson.build b/staging/adblock/meson.build deleted file mode 100644 index 942f325..0000000 --- a/staging/adblock/meson.build +++ /dev/null @@ -1,38 +0,0 @@ -lib_adblockfilter = static_library('adblockfilter', - [ 'filterlist.cpp', 'options.cpp' ], - include_directories: smolbote_interfaces, - dependencies: [ dep_qt5 ] -) - -dep_adblockfilter = declare_dependency( - include_directories: ['.', smolbote_interfaces], - link_with: lib_adblockfilter -) - -#AdblockPlusFilterPlugin = shared_library('AdblockPlusPlugin', -# [ 'plugin/plugin.cpp', -# mod_qt5.preprocess(include_directories: smolbote_interfaces, -# moc_headers: 'plugin/plugin.h', dependencies: dep_qt5) -# ], -# include_directories: smolbote_interfaces, -# link_with: lib_adblockfilter, -# dependencies: dep_qt5, -# install: true, -# install_dir: get_option('libdir')/'smolbote/plugins' -#) - -test('adblock: rule', executable('libadblockfilter_rule', - sources: 'test/rule.cpp', - dependencies: [ dep_qt5, dep_catch, dep_adblockfilter ] -)) - -test('adblock: options', executable('libadblockfilter_options', - sources: 'test/options.cpp', - dependencies: [ dep_qt5, dep_catch, dep_adblockfilter ] -)) - -test('adblock: filterlist', executable('libadblockfilter_filterlist', - sources: 'test/filterlist.cpp', - dependencies: [ dep_qt5, dep_catch, dep_adblockfilter ] -)) - diff --git a/staging/adblock/options.cpp b/staging/adblock/options.cpp deleted file mode 100644 index 08f30ee..0000000 --- a/staging/adblock/options.cpp +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "options.h" - -using namespace AdblockPlus; - -constexpr std::array abpTypeOptions = { - "document", // ResourceTypeMainFrame 0 Top level page. - "subdocument", // ResourceTypeSubFrame 1 Frame or iframe. - "stylesheet", // ResourceTypeStylesheet 2 A CSS stylesheet. - "script", // ResourceTypeScript 3 An external script. - "image", // ResourceTypeImage 4 An image (JPG, GIF, PNG, and so on). - "font", // ResourceTypeFontResource 5 A font. - "other", // ResourceTypeSubResource 6 An "other" subresource. - "object", // ResourceTypeObject 7 An object (or embed) tag for a plugin or a resource that a plugin requested. - "media", // ResourceTypeMedia 8 A media resource. - "__worker", // ResourceTypeWorker 9 The main resource of a dedicated worker. - "__sharedworker", // ResourceTypeSharedWorker 10 The main resource of a shared worker. - "__prefetch", // ResourceTypePrefetch 11 An explicitly requested prefetch. - "__favicon", // ResourceTypeFavicon 12 A favicon. - "xmlhttprequest", // ResourceTypeXhr 13 An XMLHttpRequest. - "ping", // ResourceTypePing 14 A ping request for . - "__serviceworker", // ResourceTypeServiceWorker 15 The main resource of a service worker. - "__cspreport", // ResourceTypeCspReport 16 A report of Content Security Policy (CSP) violations. - "__pluginresource", // ResourceTypePluginResource 17 A resource requested by a plugin. - "__preloadmainframe", // ResourceTypeNavigationPreloadMainFrame 19 A main-frame service worker navigation preload request. - "__preloadsubframe", // ResourceTypeNavigationPreloadSubFrame 20 A sub-frame service worker navigation preload request. - "__unknown" // ResourceTypeUnknown 255 Unknown request type. -}; - -auto parseTypeOption(QStringRef &option) -{ - struct { - bool found = false; - int index = -1; - bool exception = false; - } ret; - - // Possible inverse type options include ~script, ~image, ~stylesheet, ~object, - // ~xmlhttprequest, ~subdocument, ~ping, ~websocket, ~webrtc, ~document, ~elemhide, ~other - if(option[0] == '~') { - ret.exception = true; - option = option.mid(1); - } - - // TODO: map all ResourceType's to their respective strings - // TODO: websocket, webrtc, elemhide, generichide, genericblock, popup - - for(std::size_t i = 0; i < std::size(abpTypeOptions); ++i) { - if(option == abpTypeOptions[i]) { - ret.index = i; - ret.found = true; - return ret; - } - } - return ret; -} - -bool Options::parseAbp(const QStringRef &options) -{ - std::bitset<32> checked_flags; - - for(auto &option : options.split(',')) { - if(option == "match-case") { - matchcase = true; - - } else if(option == "third-party") { - thirdparty = !exception; - } else if(const auto r = parseTypeOption(option); r.found) { - if(!r.exception) { - flags.set(r.index, true); - checked_flags.set(r.index, true); - } else { - flags.set(r.index, false); - checked_flags.set(r.index, true); - for(auto i = 0; i < 32; ++i) { - if(!checked_flags[i]) { - flags.set(i, true); - } - } - } - } else { - return false; - } - } - - return true; -} diff --git a/staging/adblock/options.h b/staging/adblock/options.h deleted file mode 100644 index efc47a6..0000000 --- a/staging/adblock/options.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_ADBLOCK_OPTIONS_H -#define SMOLBOTE_ADBLOCK_OPTIONS_H - -#include -#include -#include -#include - -namespace AdblockPlus -{ - -struct Options { - // request handling options - bool exception = false; - bool redirect = false; - - // pattern options - bool matchcase = false; - - // request type options - bool firstparty = true; - bool thirdparty = true; - - // request types - bool matchesType(QWebEngineUrlRequestInfo::ResourceType type) - { - return flags.test(type); - } - bool parseAbp(const QStringRef &options); - - // TODO private: - std::bitset<32> flags; -}; - -} // namespace AdblockPlus - -#endif // SMOLBOTE_ADBLOCK_OPTIONS_H diff --git a/staging/adblock/plugin/AdblockPlusPlugin.json b/staging/adblock/plugin/AdblockPlusPlugin.json deleted file mode 100644 index 053826a..0000000 --- a/staging/adblock/plugin/AdblockPlusPlugin.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "AdblockPlus Filter Plugin", - "author": "Aqua " -} diff --git a/staging/adblock/plugin/plugin.cpp b/staging/adblock/plugin/plugin.cpp deleted file mode 100644 index 028c83f..0000000 --- a/staging/adblock/plugin/plugin.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "plugin.h" -#include "../filterlist.h" -#include - -using namespace AdblockPlus; - -Filter* AdblockPlusFilterPlugin::load(QIODevice* from) const -{ - if(!from->isOpen()) - return nullptr; - - - QTextStream stream(from); - - if(stream.readLine().trimmed() != QLatin1String("[Adblock Plus 2.0]")) { - return nullptr; - } - - auto *list = new FilterList; - QString line; - int total, comments, rules, unsupported, failed; - - while(stream.readLineInto(&line)) { - if(!line.isEmpty()) { - ++total; - - switch(list->parse(line)) - { - case FilterList::Comment: - ++comments; - break; - case FilterList::Rule: - ++rules; - break; - case FilterList::Unsupported: - ++unsupported; - break; - case FilterList::Failed: - break; - } - } - } - - return list; -} - diff --git a/staging/adblock/plugin/plugin.h b/staging/adblock/plugin/plugin.h deleted file mode 100644 index db419bd..0000000 --- a/staging/adblock/plugin/plugin.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef ADBLOCKPLUSFILTER_PLUGIN_H -#define ADBLOCKPLUSFILTER_PLUGIN_H - -#include - -class AdblockPlusFilterPlugin : public QObject, public FilterPlugin -{ - Q_OBJECT - Q_PLUGIN_METADATA(IID FilterPluginIid FILE "AdblockPlusPlugin.json") - Q_INTERFACES(FilterPlugin) - -public: - Filter* load(QIODevice* from) const override; -}; - -#endif // ADBLOCKPLUSFILTER_PLUGIN_H - diff --git a/staging/adblock/rule.h b/staging/adblock/rule.h deleted file mode 100644 index aaab49a..0000000 --- a/staging/adblock/rule.h +++ /dev/null @@ -1,153 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_ADBLOCK_RULE_H -#define SMOLBOTE_ADBLOCK_RULE_H - -#include "options.h" -#include -#include -#include -#include - -namespace AdblockPlus -{ -class Rule -{ -public: - virtual ~Rule() = default; - /** - * requestUrl: requested URL - * initiatorUrl: URL of document that initiated navigation - * firstPartyUrl: URL of the page that issued the request - */ - virtual bool hasMatch(const QStringRef &requestUrl, - const QStringRef &initiatorUrl, - const QStringRef &firstPartyUrl, - QWebEngineUrlRequestInfo::ResourceType resourceType = QWebEngineUrlRequestInfo::ResourceTypeMainFrame) const = 0; - - bool shouldRedirect() const - { - return options.redirect; - } - bool shouldBlock() const - { - return !options.exception; - } - -protected: - Rule(const Options &opt) - : options(opt) - { - } - const Options options; -}; - -// The separator character can be anything but -// a letter, a digit, or one of the following: _, -, ., %. -// The end of the address is also accepted as a separator. -inline bool isSeparator(const QChar &c) -{ - return !c.isLetter() && !c.isDigit() && c != '_' && c != '-' && c != '.' && c != '%'; -} - -class MatcherRule : public Rule -{ -public: - enum MatchPosition { - DomainMatch, - UrlStartsWith, - UrlEndsWith, - UrlContains, - UrlEquals - }; - - MatcherRule(const QString &pattern, const Options &opt, const MatchPosition pos = UrlContains) - : Rule(opt) - , position(pos) - , matcher(pattern, opt.matchcase ? Qt::CaseSensitive : Qt::CaseInsensitive) - , patternLength(pattern.length()) - { - } - explicit MatcherRule(const MatcherRule &) = delete; - MatcherRule &operator=(const MatcherRule &) = delete; - - explicit MatcherRule(MatcherRule &&) = delete; - MatcherRule &operator=(MatcherRule &&) = delete; - - ~MatcherRule() = default; - bool hasMatch(const QStringRef &url, - const QStringRef &initiatorUrl, - const QStringRef &firstPartyUrl, - QWebEngineUrlRequestInfo::ResourceType resourceType = QWebEngineUrlRequestInfo::ResourceTypeMainFrame) const override - { - const auto index = matcher.indexIn(url); - if(index == -1) { - return false; - } - - switch(position) { - case DomainMatch: - // match if - // there is only one : left of the index - // and - // character after pattern is separator or end of string - return (url.left(index).count(':') <= 1 && (index + patternLength == url.length() || isSeparator(url[index + patternLength]))); - case UrlStartsWith: - return (index == 0); - case UrlEndsWith: - return (index == url.length() - patternLength); - case UrlContains: - return (index != -1); - case UrlEquals: - return (index == 0 && patternLength == url.length()); - } - - return false; - } - -private: - const MatchPosition position; - const QStringMatcher matcher; - const int patternLength; -}; - -class RegexRule : public Rule -{ -public: - RegexRule(const QString &rule, const Options &opt) - : Rule(opt) - , regex(rule) - { - if(!opt.matchcase) { - regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption); - } - } - explicit RegexRule(const RegexRule &) = delete; - RegexRule &operator=(const RegexRule &) = delete; - - explicit RegexRule(RegexRule &&) = delete; - RegexRule &operator=(RegexRule &&) = delete; - - ~RegexRule() = default; - bool hasMatch(const QStringRef &url, - const QStringRef &initiatorUrl, - const QStringRef &firstPartyUrl, - QWebEngineUrlRequestInfo::ResourceType resourceType = QWebEngineUrlRequestInfo::ResourceTypeMainFrame) const override - { - const auto match = regex.match(url); - return match.hasMatch(); - } - -private: - QRegularExpression regex; -}; - -} // namespace AdblockPlus - -#endif // SMOLBOTE_ADBLOCK_RULE_H diff --git a/staging/adblock/test/filterlist.cpp b/staging/adblock/test/filterlist.cpp deleted file mode 100644 index ca122ac..0000000 --- a/staging/adblock/test/filterlist.cpp +++ /dev/null @@ -1,118 +0,0 @@ -#define CATCH_CONFIG_MAIN -#include "filterlist.h" -#include -#include - -using namespace AdblockPlus; - -QByteArray sampleList = - R"(! comment on line -! Last modified: 1 Jan 2000 00:00 UTC -! Expires: 4 days (update frequency) -)"; - -TEST_CASE("placeholder") -{ - QBuffer buffer(&sampleList); - buffer.open(QIODevice::ReadOnly | QIODevice::Text); - - AdblockPlus::FilterList list(buffer); - REQUIRE(!list.isUpToDate()); -} - -TEST_CASE("domain match") -{ - const QString defaultUrl = ""; - - const QString block1 = "http://ads.example.com/foo.gif"; - const QString block2 = "http://server1.ads.example.com/foo.gif"; - const QString block3 = "https://ads.example.com:8000/"; - const QString block4 = "https://ads.example.com"; - - const QString allow1 = "http://ads.example.com.ua/foo.gif"; - const QString allow2 = "http://example.com/redirect/http://ads.example.com/"; - - auto *rule = FilterList::parseRule("||ads.example.com^"); - REQUIRE(rule->shouldBlock()); - REQUIRE(!rule->shouldRedirect()); - - REQUIRE(rule->hasMatch(&block1, &defaultUrl, &defaultUrl)); - REQUIRE(rule->hasMatch(&block2, &defaultUrl, &defaultUrl)); - REQUIRE(rule->hasMatch(&block3, &defaultUrl, &defaultUrl)); - REQUIRE(rule->hasMatch(&block4, &defaultUrl, &defaultUrl)); - - REQUIRE(!rule->hasMatch(&allow1, &defaultUrl, &defaultUrl)); - REQUIRE(!rule->hasMatch(&allow2, &defaultUrl, &defaultUrl)); - - delete rule; -} - -TEST_CASE("string equals") -{ - const QString defaultUrl = ""; - - const QString block = "http://example.com/"; - - const QString allow1 = "http://example.com/foo.gif"; - const QString allow2 = "http://example.info/redirect/http://example.com/"; - - auto *rule = FilterList::parseRule("|http://example.com/|"); - REQUIRE(rule->shouldBlock()); - REQUIRE(!rule->shouldRedirect()); - - REQUIRE(rule->hasMatch(&block, &defaultUrl, &defaultUrl)); - - REQUIRE(!rule->hasMatch(&allow1, &defaultUrl, &defaultUrl)); - REQUIRE(!rule->hasMatch(&allow2, &defaultUrl, &defaultUrl)); - - delete rule; -} - -TEST_CASE("string starts with") -{ - const QString defaultUrl = ""; - - auto *rule = FilterList::parseRule("|http://baddomain.example/"); - REQUIRE(rule->shouldBlock()); - REQUIRE(!rule->shouldRedirect()); - - const QString blocks = "http://baddomain.example/banner.gif"; - const QString allows = "http://gooddomain.example/analyze?http://baddomain.example"; - - REQUIRE(rule->hasMatch(&blocks, &defaultUrl, &defaultUrl)); - REQUIRE(!rule->hasMatch(&allows, &defaultUrl, &defaultUrl)); - delete rule; -} - -TEST_CASE("string ends with") -{ - const QString defaultUrl = ""; - - auto *rule = FilterList::parseRule("swf|"); - REQUIRE(rule->shouldBlock()); - REQUIRE(!rule->shouldRedirect()); - - const QString blocks = "http://example.com/annoyingflash.swf"; - const QString allows = "http://example.com/swf/index.html"; - - REQUIRE(rule->hasMatch(&blocks, &defaultUrl, &defaultUrl)); - REQUIRE(!rule->hasMatch(&allows, &defaultUrl, &defaultUrl)); - delete rule; -} - -TEST_CASE("regular expressions") -{ - const QString defaultUrl = ""; - - auto *rule = FilterList::parseRule("/banner\\d+/"); - const QString matches1 = "banner123"; - const QString matches2 = "banner321"; - - const QString ignores = "banners"; - - REQUIRE(rule->hasMatch(&matches1, &defaultUrl, &defaultUrl)); - REQUIRE(rule->hasMatch(&matches2, &defaultUrl, &defaultUrl)); - REQUIRE(!rule->hasMatch(&ignores, &defaultUrl, &defaultUrl)); - delete rule; -} - diff --git a/staging/adblock/test/options.cpp b/staging/adblock/test/options.cpp deleted file mode 100644 index 67dc143..0000000 --- a/staging/adblock/test/options.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#define CATCH_CONFIG_MAIN -#include "options.h" -#include - -using namespace AdblockPlus; - -SCENARIO("parsing adblock options") -{ - Options opt; - - GIVEN("an unknown option") - { - const QString unknown = "unknown"; - THEN("the option is not parsed") - { - QStringRef unknown_ref(&unknown); - REQUIRE(!opt.parseAbp(unknown_ref)); - } - } - - GIVEN("match-case,document,~subdocument") - { - const QString options = "match-case,document,~subdocument"; - REQUIRE(opt.parseAbp(&options)); - - WHEN("match-case") - { - REQUIRE(opt.matchcase); - } - - WHEN("testing set/unset options") - { - REQUIRE(opt.matchesType(QWebEngineUrlRequestInfo::ResourceTypeMainFrame)); - REQUIRE(!opt.matchesType(QWebEngineUrlRequestInfo::ResourceTypeSubFrame)); - } - - WHEN("testing other options") - { - REQUIRE(opt.matchesType(QWebEngineUrlRequestInfo::ResourceTypeStylesheet)); - } - } -} diff --git a/staging/adblock/test/rule.cpp b/staging/adblock/test/rule.cpp deleted file mode 100644 index 07186b9..0000000 --- a/staging/adblock/test/rule.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#define CATCH_CONFIG_MAIN -#include "rule.h" -#include - -using namespace AdblockPlus; - -SCENARIO("MatcherRule") -{ - GIVEN("options with case sensitive pattern") - { - const QString defaultUrl = ""; - - const Options opt { .matchcase=true }; - const QString patternContains("this string contains the pattern in it"); - const QString patternBegins("pattern starts this string"); - const QString patternEnds("this string ends with pattern"); - const QString patternMissing("and this one does not"); - - WHEN("contains") - { - MatcherRule rule("pattern", opt); - REQUIRE(rule.shouldBlock()); - - THEN("pattern is matched anywhere in the URL") - { - REQUIRE(rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl)); - REQUIRE(rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl)); - REQUIRE(rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl)); - REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl)); - } - } - - WHEN("startsWith") - { - MatcherRule rule("pattern", opt, MatcherRule::UrlStartsWith); - REQUIRE(rule.shouldBlock()); - - THEN("pattern is matched if at the start of the URL") - { - REQUIRE(!rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl)); - REQUIRE(rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl)); - REQUIRE(!rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl)); - REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl)); - } - } - - WHEN("endsWith") - { - MatcherRule rule("pattern", opt, MatcherRule::UrlEndsWith); - REQUIRE(rule.shouldBlock()); - - THEN("pattern is matched if at the end of the URL") - { - REQUIRE(!rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl)); - REQUIRE(!rule.hasMatch(&patternBegins, &defaultUrl, &defaultUrl)); - REQUIRE(rule.hasMatch(&patternEnds, &defaultUrl, &defaultUrl)); - REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl)); - } - } - } -} - -SCENARIO("RegexRule") -{ - GIVEN("options with case sensitive pattern") - { - const QString defaultUrl; - - const Options opt { .matchcase=true }; - const QString patternContains("this string contains the pattern in it"); - const QString patternMissing("and this one does not"); - - WHEN("contains") - { - RegexRule rule("pattern", opt); - REQUIRE(rule.shouldBlock()); - - THEN("pattern is matched anywhere in the URL") - { - REQUIRE(rule.hasMatch(&patternContains, &defaultUrl, &defaultUrl)); - REQUIRE(!rule.hasMatch(&patternMissing, &defaultUrl, &defaultUrl)); - } - } - } -} - diff --git a/staging/smolblok/README.md b/staging/smolblok/README.md deleted file mode 100644 index 1793009..0000000 --- a/staging/smolblok/README.md +++ /dev/null @@ -1,8 +0,0 @@ -## smolblok - -### What is this -This is a C++ library for URL filtering for Qt applications using QtWebEngine. - -### Supported formats -- AdblockPlus without element hiding rules - diff --git a/staging/smolblok/filtermanager.hpp b/staging/smolblok/filtermanager.hpp deleted file mode 100644 index 6ee4d3f..0000000 --- a/staging/smolblok/filtermanager.hpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#pragma once - -#include -#include - -class FilterManager : public QWebEngineUrlRequestInterceptor -{ -public: - FilterManager(QObject *parent = nullptr) - : QWebEngineUrlRequestInterceptor(parent) - { - } - ~FilterManager() - { - qDeleteAll(filters); - } - - void addFilterList(FilterList *list) { - filters.append(list); - } - - void interceptRequest(QWebEngineUrlRequestInfo &info) override - { - for(const auto *filter : qAsConst(filters)) { - if(filter->filter(info)) { - return; - } - } - } - -private: - QList filters; -}; diff --git a/staging/smolblok/meson.build b/staging/smolblok/meson.build deleted file mode 100644 index 6105179..0000000 --- a/staging/smolblok/meson.build +++ /dev/null @@ -1,17 +0,0 @@ -dep_smolblok = declare_dependency( - include_directories: [ '.', smolbote_interfaces ], - link_with: library('smolblok', - [ 'smolblok.cpp' ], - include_directories: smolbote_interfaces, - dependencies: dep_qt5 - ) -) - -smolblok_load = executable('smolblok-load', - dependencies: [ dep_qt5, dep_spdlog, dep_smolblok ], - sources: [ 'test/loader.cpp' ] -) - -test('load', smolblok_load, suite: 'smolblok', should_fail: true) -test('load', smolblok_load, suite: 'smolblok', args: files('meson.build'), should_fail: true) - diff --git a/staging/smolblok/smolblok.cpp b/staging/smolblok/smolblok.cpp deleted file mode 100644 index 465c348..0000000 --- a/staging/smolblok/smolblok.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "smolblok.hpp" -#include -#include - -bool smolblok::addSubscriptions(const QString &filename) -{ - if(filename.isEmpty()) { - return false; - } - - QSettings listconf(filename, QSettings::IniFormat); - - for(auto &group : listconf.childGroups()) { - listconf.beginGroup(group); - const auto *loader = m_formats.value(listconf.value("Format").toString()).instance; - if(loader != nullptr) { - QFile f(listconf.value("File").toString()); - if(!f.exists()) { - continue; - } - - auto *list = loader->load(f); - f.seek(0); - if(loader->parse(list, f)) { - m_subscriptions.addFilterList(list); - } - } - listconf.endGroup(); - } - return false; -} - diff --git a/staging/smolblok/smolblok.hpp b/staging/smolblok/smolblok.hpp deleted file mode 100644 index e547d67..0000000 --- a/staging/smolblok/smolblok.hpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_SMOLBLOK_HPP -#define SMOLBOTE_SMOLBLOK_HPP - -#include "filtermanager.hpp" -#include -#include -#include - -class smolblok -{ -public: - smolblok() = default; - ~smolblok() - { - for(auto &plugin : m_formats) { - delete plugin.loader; - } - } - - auto registerFormatPlugin(const QString &format, const QString &filename) - { - struct { - bool loaded = false; - QString error; - } ret; - - if(format.isEmpty() || filename.isEmpty()) { - ret.error = "Format or filename is empty"; - return ret; - } - - auto *plugin = new QPluginLoader(filename); - if(!plugin->load()) { - ret.error = plugin->errorString(); - delete plugin; - return ret; - } - - auto *instance = qobject_cast(plugin->instance()); - if(instance == nullptr) { - ret.error = "Unable to cast"; - delete plugin; - return ret; - } - - m_formats[format] = PluginInfo{ plugin, instance }; - ret.loaded = true; - return ret; - } - - const auto formats() const - { - return m_formats.keys(); - } - - bool addSubscriptions(const QString &filename); - QWebEngineUrlRequestInterceptor *interceptor() - { - return &m_subscriptions; - } - -private: - struct PluginInfo { - QPluginLoader *loader = nullptr; - FilterPlugin *instance = nullptr; - }; - - QHash m_formats; - FilterManager m_subscriptions; -}; - -#endif // SMOLBOTE_SMOLBLOK_HPP diff --git a/staging/smolblok/test/loader.cpp b/staging/smolblok/test/loader.cpp deleted file mode 100644 index 9e27a26..0000000 --- a/staging/smolblok/test/loader.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "smolblok.hpp" -#include - -int main(int argc, char** argv) -{ - if(argc != 2) { - spdlog::error("usage: {} path/to/plugin.so", argv[0]); - return -1; - } - - smolblok filter; - { - const auto r = filter.registerFormatPlugin("unused", argv[1]); - if(r.loaded) { - spdlog::info("Loaded plugin {}", argv[1]); - } else { - spdlog::error("Failed loading plugin {}", argv[1]); - spdlog::error(qUtf8Printable(r.error)); - return -1; - } - } - - return 0; -} - diff --git a/staging/smolblok/test/main.cpp b/staging/smolblok/test/main.cpp deleted file mode 100644 index 5624ee9..0000000 --- a/staging/smolblok/test/main.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#define CATCH_CONFIG_MAIN - -#include "smolblok.hpp" -#include - -SCENARIO("smolblok") -{ - smolblok s; - - GIVEN("invalid plugins") - { - REQUIRE(!s.registerFormatPlugin("", "")); - REQUIRE(!s.registerFormatPlugin("Format", "missing.dll")); - } - - GIVEN("invalid subscriptions") - { - REQUIRE(!s.addSubscriptions("")); - REQUIRE(!s.addSubscriptions("missing.txt")); - } -} - diff --git a/staging/smolblok/test/sample-filters.txt b/staging/smolblok/test/sample-filters.txt deleted file mode 100644 index 59e0e7b..0000000 --- a/staging/smolblok/test/sample-filters.txt +++ /dev/null @@ -1,10 +0,0 @@ -[easylist-noelemhide] -Format = AdblockPlus -File = easylist_noelemhide.txt -Href = https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt - -[StevenBlack] -Format = Hostlist -File = stevenblack.txt -Href = https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts - -- cgit v1.2.1