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

Commit be4c62e

Browse files
committed
real terror here
1 parent f348297 commit be4c62e

24 files changed

+239
-689
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ out
5050
*.swp
5151

5252
.vscode
53+
54+
# These files are either fetched at build time, or generated from the build
55+
etld/data/public_suffix_list.h
56+
etld/data/public_suffix_list.dat

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
build:
88
curl -s https://publicsuffix.org/list/public_suffix_list.dat -o etld/data/public_suffix_list.dat
9+
./etld/data/build.js
910
./node_modules/.bin/node-gyp configure && ./node_modules/.bin/node-gyp build
1011

1112
test:
@@ -28,3 +29,5 @@ perf:
2829

2930
clean:
3031
rm -Rf build
32+
rm etld/data/public_suffix_list.dat
33+
rm etld/data/public_suffix_list.h

binding.gyp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
"no_fingerprint_domain.h",
2020
"protocol.cc",
2121
"protocol.h",
22+
"etld/data/public_suffix_list.h",
2223
"etld/types.h",
2324
"etld/domain.cc",
2425
"etld/domain.h",
25-
"etld/parser.cc",
26-
"etld/parser.h",
2726
"etld/public_suffix_rule.cc",
2827
"etld/public_suffix_rule.h",
2928
"etld/public_suffix_rule_set.cc",

brave/BUILD.gn

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ source_set("ad-block") {
2929
"../etld/types.h",
3030
"../etld/domain.cc",
3131
"../etld/domain.h",
32-
"../etld/parser.cc",
33-
"../etld/parser.h",
3432
"../etld/public_suffix_rule.cc",
3533
"../etld/public_suffix_rule.h",
3634
"../etld/public_suffix_rule_set.cc",
3735
"../etld/public_suffix_rule_set.h",
36+
"../etld/data/public_suffix_list.h",
3837
"../etld/matcher.cc",
3938
"../etld/matcher.h",
4039
"../etld/shared_matcher.h",

etld/data/build.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env node
2+
3+
/* Copyright (c) 2019 The Brave Software Team. Distributed under the MPL2
4+
* license. This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7+
8+
/**
9+
* This code parses the public suffix rule list into a header file of
10+
* rules that gets build into the ad-block library.
11+
*/
12+
13+
const fsLib = require("fs");
14+
const pathLib = require("path");
15+
16+
const ruleTextPath = pathLib.join(__dirname, "public_suffix_list.dat");
17+
const ruleText = fsLib.readFileSync(ruleTextPath, "utf8");
18+
19+
const templatePath = pathLib.join(__dirname, "public_suffix_list.h.template");
20+
const templateText = fsLib.readFileSync(templatePath, "utf8");
21+
const rules = [];
22+
23+
const toPublicSuffixRuleSerializedString = (isWildcard, isException, labels) => {
24+
const constructorArgs = [];
25+
constructorArgs.push(isWildcard ? "true" : "false");
26+
constructorArgs.push(isException ? "true" : "false");
27+
28+
const wrappedLabels = labels.map(JSON.stringify);
29+
constructorArgs.push("{" + wrappedLabels.join(", ") + "}");
30+
return "{" + constructorArgs.join(", ") + "}";
31+
}
32+
33+
for (const line of ruleText.split("\n")) {
34+
let isWildcard = false;
35+
let isException = false;
36+
let trimmedLine = line.trim();
37+
if (trimmedLine.length === 0) {
38+
continue;
39+
}
40+
41+
// Check to see if this is a comment line. If so, process no further.
42+
if (trimmedLine.indexOf("//") == 0) {
43+
continue;
44+
}
45+
46+
const firstChar = trimmedLine[0];
47+
switch (firstChar) {
48+
case "!":
49+
trimmedLine = trimmedLine.slice(1);
50+
isException = true;
51+
break;
52+
53+
case "*":
54+
isWildcard = true;
55+
break;
56+
57+
default:
58+
break;
59+
}
60+
61+
const lineUntilWhiteSpace = trimmedLine.split(" ")[0];
62+
const ruleLabels = lineUntilWhiteSpace.split(".");
63+
64+
const ruleAsString = toPublicSuffixRuleSerializedString(isWildcard, isException, ruleLabels);
65+
rules.push(ruleAsString);
66+
}
67+
68+
const serializedString = rules.join(",\n");
69+
const replaceArg = "{" + serializedString + "}";
70+
const finalCodeText = templateText.replace("{contents}", replaceArg);
71+
72+
const generatedFilePath = pathLib.join(__dirname, "public_suffix_list.h");
73+
fsLib.writeFileSync(generatedFilePath, finalCodeText, "utf8");
File renamed without changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Copyright (c) 2019 The Brave Software Team. Distributed under the MPL2
2+
* license. This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#ifndef ETLD_DATA_PUBLIC_SUFFIX_LIST_H_
7+
#define ETLD_DATA_PUBLIC_SUFFIX_LIST_H_
8+
9+
#include <vector>
10+
#include "etld/public_suffix_rule.h"
11+
12+
using brave_etld::PublicSuffixRuleSerialized;
13+
14+
const std::vector<PublicSuffixRuleSerialized> SHARED_RULES = {contents};
15+
16+
#endif // ETLD_DATA_PUBLIC_SUFFIX_LIST_H_

etld/domain.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
#include <sstream>
88
#include "etld/domain.h"
99

10-
namespace Brave {
11-
namespace eTLD {
10+
namespace brave_etld {
1211

1312
Domain::Domain(const std::string &string) {
1413
std::size_t current, previous = 0;
@@ -47,5 +46,4 @@ std::string Domain::ToString() const {
4746
return as_string.str();
4847
}
4948

50-
} // namespace eTLD
51-
} // namespace Brave
49+
} // namespace brave_etld

etld/domain.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#include <vector>
1111
#include "etld/types.h"
1212

13-
namespace Brave {
14-
namespace eTLD {
13+
namespace brave_etld {
1514

1615
class Domain {
1716
public:
@@ -32,7 +31,6 @@ class Domain {
3231
std::vector<Label> labels_;
3332
};
3433

35-
} // namespace eTLD
36-
} // namespace Brave
34+
} // namespace brave_etld
3735

3836
#endif // ETLD_DOMAIN_H_

etld/matcher.cc

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,20 @@
66
#include <fstream>
77

88
#include "etld/types.h"
9-
#include "etld/parser.h"
9+
#include "etld/public_suffix_rule.h"
1010
#include "etld/matcher.h"
1111

12-
namespace Brave {
13-
namespace eTLD {
12+
namespace brave_etld {
1413

15-
Matcher::Matcher(std::ifstream &rule_file) {
16-
PublicSuffixParseResult rules = parse_rule_file(rule_file);
14+
Matcher::Matcher(const std::vector<PublicSuffixRule> &rules) {
1715
ConsumeRules(rules);
1816
}
1917

20-
Matcher::Matcher(const std::string &rule_text) {
21-
PublicSuffixParseResult rules = parse_rule_text(rule_text);
22-
ConsumeRules(rules);
23-
}
24-
25-
Matcher::Matcher(const PublicSuffixParseResult &rules) {
18+
Matcher::Matcher(const std::vector<PublicSuffixRuleSerialized> &serialized_rules) {
19+
std::vector<PublicSuffixRule> rules;
20+
for (auto &elm : serialized_rules) {
21+
rules.push_back(PublicSuffixRule(elm));
22+
}
2623
ConsumeRules(rules);
2724
}
2825

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

42-
return BuildDomainInfo(PublicSuffixRule("*"), domain);
39+
return BuildDomainInfo(PublicSuffixRule({"*"}), domain);
4340
}
4441

4542
DomainInfo Matcher::BuildDomainInfo(const PublicSuffixRule &rule,
4643
const Domain &domain) const {
4744
return rule.Apply(domain);
4845
}
4946

50-
void Matcher::ConsumeRules(const PublicSuffixParseResult &rules) {
51-
for (auto &elm : rules.Rules()) {
47+
void Matcher::ConsumeRules(const std::vector<PublicSuffixRule> &rules) {
48+
for (auto &elm : rules) {
5249
if (elm.IsException()) {
5350
exception_rules_.AddRule(elm);
5451
} else {
@@ -57,5 +54,4 @@ void Matcher::ConsumeRules(const PublicSuffixParseResult &rules) {
5754
}
5855
}
5956

60-
} // namespace eTLD
61-
} // namespace Brave
57+
} // namespace brave_etld

0 commit comments

Comments
 (0)