Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
real terror here
Browse files Browse the repository at this point in the history
  • Loading branch information
pes10k committed Feb 9, 2019
1 parent f348297 commit be4c62e
Show file tree
Hide file tree
Showing 24 changed files with 239 additions and 689 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -28,3 +29,5 @@ perf:

clean:
rm -Rf build
rm etld/data/public_suffix_list.dat
rm etld/data/public_suffix_list.h
3 changes: 1 addition & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions brave/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
73 changes: 73 additions & 0 deletions etld/data/build.js
Original file line number Diff line number Diff line change
@@ -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");
File renamed without changes.
16 changes: 16 additions & 0 deletions etld/data/public_suffix_list.h.template
Original file line number Diff line number Diff line change
@@ -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 <vector>
#include "etld/public_suffix_rule.h"

using brave_etld::PublicSuffixRuleSerialized;

const std::vector<PublicSuffixRuleSerialized> SHARED_RULES = {contents};

#endif // ETLD_DATA_PUBLIC_SUFFIX_LIST_H_
6 changes: 2 additions & 4 deletions etld/domain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#include <sstream>
#include "etld/domain.h"

namespace Brave {
namespace eTLD {
namespace brave_etld {

Domain::Domain(const std::string &string) {
std::size_t current, previous = 0;
Expand Down Expand Up @@ -47,5 +46,4 @@ std::string Domain::ToString() const {
return as_string.str();
}

} // namespace eTLD
} // namespace Brave
} // namespace brave_etld
6 changes: 2 additions & 4 deletions etld/domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#include <vector>
#include "etld/types.h"

namespace Brave {
namespace eTLD {
namespace brave_etld {

class Domain {
public:
Expand All @@ -32,7 +31,6 @@ class Domain {
std::vector<Label> labels_;
};

} // namespace eTLD
} // namespace Brave
} // namespace brave_etld

#endif // ETLD_DOMAIN_H_
28 changes: 12 additions & 16 deletions etld/matcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@
#include <fstream>

#include "etld/types.h"
#include "etld/parser.h"
#include "etld/public_suffix_rule.h"
#include "etld/matcher.h"

namespace Brave {
namespace eTLD {
namespace brave_etld {

Matcher::Matcher(std::ifstream &rule_file) {
PublicSuffixParseResult rules = parse_rule_file(rule_file);
Matcher::Matcher(const std::vector<PublicSuffixRule> &rules) {
ConsumeRules(rules);
}

Matcher::Matcher(const std::string &rule_text) {
PublicSuffixParseResult rules = parse_rule_text(rule_text);
ConsumeRules(rules);
}

Matcher::Matcher(const PublicSuffixParseResult &rules) {
Matcher::Matcher(const std::vector<PublicSuffixRuleSerialized> &serialized_rules) {
std::vector<PublicSuffixRule> rules;
for (auto &elm : serialized_rules) {
rules.push_back(PublicSuffixRule(elm));
}
ConsumeRules(rules);
}

Expand All @@ -39,16 +36,16 @@ DomainInfo Matcher::Match(const Domain &domain) const {
return BuildDomainInfo(rule_match.rule, domain);
}

return BuildDomainInfo(PublicSuffixRule("*"), domain);
return BuildDomainInfo(PublicSuffixRule({"*"}), domain);
}

DomainInfo Matcher::BuildDomainInfo(const PublicSuffixRule &rule,
const Domain &domain) const {
return rule.Apply(domain);
}

void Matcher::ConsumeRules(const PublicSuffixParseResult &rules) {
for (auto &elm : rules.Rules()) {
void Matcher::ConsumeRules(const std::vector<PublicSuffixRule> &rules) {
for (auto &elm : rules) {
if (elm.IsException()) {
exception_rules_.AddRule(elm);
} else {
Expand All @@ -57,5 +54,4 @@ void Matcher::ConsumeRules(const PublicSuffixParseResult &rules) {
}
}

} // namespace eTLD
} // namespace Brave
} // namespace brave_etld
21 changes: 7 additions & 14 deletions etld/matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,30 @@

#include <string>
#include <fstream>
#include <vector>
#include "etld/domain.h"
#include "etld/types.h"
#include "etld/parser.h"
#include "etld/public_suffix_rule.h"
#include "etld/public_suffix_rule_set.h"

namespace Brave {
namespace eTLD {
namespace brave_etld {

class Matcher {
public:
static Matcher FromFilePath(const std::string &file_path) {
std::ifstream rule_file;
rule_file.open(file_path, std::ifstream::in);
return Matcher(rule_file);
}
explicit Matcher(std::ifstream &rule_file);
explicit Matcher(const std::string &rule_text);
explicit Matcher(const PublicSuffixParseResult &rules);
explicit Matcher(const std::vector<PublicSuffixRule> &rules);
explicit Matcher(
const std::vector<PublicSuffixRuleSerialized> &serialized_rules);

DomainInfo Match(const Domain &domain) const;

private:
DomainInfo BuildDomainInfo(const PublicSuffixRule &rule,
const Domain &domain) const;
void ConsumeRules(const PublicSuffixParseResult &rules);
void ConsumeRules(const std::vector<PublicSuffixRule> &rules);
PublicSuffixRuleSet exception_rules_;
PublicSuffixRuleSet rules_;
};

} // namespace eTLD
} // namespace Brave
} // namespace brave_etld

#endif // ETLD_MATCHER_H_
94 changes: 0 additions & 94 deletions etld/parser.cc

This file was deleted.

Loading

0 comments on commit be4c62e

Please sign in to comment.