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. --- 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 -------- 40 files changed, 1187 insertions(+), 353 deletions(-) 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 (limited to 'plugins') 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()); - } - } -} -- cgit v1.2.1