diff --git a/.gitignore b/.gitignore index 28dd025..611df49 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,7 @@ out *.swp .vscode + +# These files are either fetched at build time, or generated from the build +etld/data/public_suffix_list.h +etld/data/public_suffix_list.dat \ No newline at end of file diff --git a/Makefile b/Makefile index 8950ac6..b4da9d4 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ build: curl -s https://publicsuffix.org/list/public_suffix_list.dat -o etld/data/public_suffix_list.dat + ./etld/data/build.js ./node_modules/.bin/node-gyp configure && ./node_modules/.bin/node-gyp build test: @@ -28,3 +29,5 @@ perf: clean: rm -Rf build + rm etld/data/public_suffix_list.dat + rm etld/data/public_suffix_list.h diff --git a/binding.gyp b/binding.gyp index 15a5739..ef6f8cf 100644 --- a/binding.gyp +++ b/binding.gyp @@ -19,11 +19,10 @@ "no_fingerprint_domain.h", "protocol.cc", "protocol.h", + "etld/data/public_suffix_list.h", "etld/types.h", "etld/domain.cc", "etld/domain.h", - "etld/parser.cc", - "etld/parser.h", "etld/public_suffix_rule.cc", "etld/public_suffix_rule.h", "etld/public_suffix_rule_set.cc", diff --git a/brave/BUILD.gn b/brave/BUILD.gn index 30855ef..71284e3 100644 --- a/brave/BUILD.gn +++ b/brave/BUILD.gn @@ -29,12 +29,11 @@ source_set("ad-block") { "../etld/types.h", "../etld/domain.cc", "../etld/domain.h", - "../etld/parser.cc", - "../etld/parser.h", "../etld/public_suffix_rule.cc", "../etld/public_suffix_rule.h", "../etld/public_suffix_rule_set.cc", "../etld/public_suffix_rule_set.h", + "../etld/data/public_suffix_list.h", "../etld/matcher.cc", "../etld/matcher.h", "../etld/shared_matcher.h", diff --git a/etld/data/build.js b/etld/data/build.js new file mode 100755 index 0000000..570b8a2 --- /dev/null +++ b/etld/data/build.js @@ -0,0 +1,73 @@ +#!/usr/bin/env node + +/* Copyright (c) 2019 The Brave Software Team. Distributed under the MPL2 + * license. This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * This code parses the public suffix rule list into a header file of + * rules that gets build into the ad-block library. + */ + +const fsLib = require("fs"); +const pathLib = require("path"); + +const ruleTextPath = pathLib.join(__dirname, "public_suffix_list.dat"); +const ruleText = fsLib.readFileSync(ruleTextPath, "utf8"); + +const templatePath = pathLib.join(__dirname, "public_suffix_list.h.template"); +const templateText = fsLib.readFileSync(templatePath, "utf8"); +const rules = []; + +const toPublicSuffixRuleSerializedString = (isWildcard, isException, labels) => { + const constructorArgs = []; + constructorArgs.push(isWildcard ? "true" : "false"); + constructorArgs.push(isException ? "true" : "false"); + + const wrappedLabels = labels.map(JSON.stringify); + constructorArgs.push("{" + wrappedLabels.join(", ") + "}"); + return "{" + constructorArgs.join(", ") + "}"; +} + +for (const line of ruleText.split("\n")) { + let isWildcard = false; + let isException = false; + let trimmedLine = line.trim(); + if (trimmedLine.length === 0) { + continue; + } + + // Check to see if this is a comment line. If so, process no further. + if (trimmedLine.indexOf("//") == 0) { + continue; + } + + const firstChar = trimmedLine[0]; + switch (firstChar) { + case "!": + trimmedLine = trimmedLine.slice(1); + isException = true; + break; + + case "*": + isWildcard = true; + break; + + default: + break; + } + + const lineUntilWhiteSpace = trimmedLine.split(" ")[0]; + const ruleLabels = lineUntilWhiteSpace.split("."); + + const ruleAsString = toPublicSuffixRuleSerializedString(isWildcard, isException, ruleLabels); + rules.push(ruleAsString); +} + +const serializedString = rules.join(",\n"); +const replaceArg = "{" + serializedString + "}"; +const finalCodeText = templateText.replace("{contents}", replaceArg); + +const generatedFilePath = pathLib.join(__dirname, "public_suffix_list.h"); +fsLib.writeFileSync(generatedFilePath, finalCodeText, "utf8"); diff --git a/etld/data/.keep b/etld/data/public_suffix_list.cc similarity index 100% rename from etld/data/.keep rename to etld/data/public_suffix_list.cc diff --git a/etld/data/public_suffix_list.h.template b/etld/data/public_suffix_list.h.template new file mode 100644 index 0000000..f0926a1 --- /dev/null +++ b/etld/data/public_suffix_list.h.template @@ -0,0 +1,16 @@ +/* Copyright (c) 2019 The Brave Software Team. Distributed under the MPL2 + * license. This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef ETLD_DATA_PUBLIC_SUFFIX_LIST_H_ +#define ETLD_DATA_PUBLIC_SUFFIX_LIST_H_ + +#include +#include "etld/public_suffix_rule.h" + +using brave_etld::PublicSuffixRuleSerialized; + +const std::vector SHARED_RULES = {contents}; + +#endif // ETLD_DATA_PUBLIC_SUFFIX_LIST_H_ diff --git a/etld/domain.cc b/etld/domain.cc index d7ca6a9..d6db2e9 100644 --- a/etld/domain.cc +++ b/etld/domain.cc @@ -7,8 +7,7 @@ #include #include "etld/domain.h" -namespace Brave { -namespace eTLD { +namespace brave_etld { Domain::Domain(const std::string &string) { std::size_t current, previous = 0; @@ -47,5 +46,4 @@ std::string Domain::ToString() const { return as_string.str(); } -} // namespace eTLD -} // namespace Brave +} // namespace brave_etld diff --git a/etld/domain.h b/etld/domain.h index 958c342..2ad576d 100644 --- a/etld/domain.h +++ b/etld/domain.h @@ -10,8 +10,7 @@ #include #include "etld/types.h" -namespace Brave { -namespace eTLD { +namespace brave_etld { class Domain { public: @@ -32,7 +31,6 @@ class Domain { std::vector