-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-new-psl.php
185 lines (148 loc) · 4.19 KB
/
parse-new-psl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
require_once "deps/vendor/autoload.php";
// import Punycode
use TrueBV\Punycode;
$puny = new Punycode();
// acquire data
$PSL = fopen("running/public_suffix_list.dat", "r");
// punycoding and rule trimming
$endICANN = false;
$ICANN = [];
$countICANN = 0;
$endPrivate = false;
$Private = [];
$countPrivate = 0;
$parseErrors = 0;
while($endICANN == false || $endPrivate == false) {
// get a line
$holster = fgets($PSL, 65535);
// verify line is not the terminus of ICANN suffixes
if(strcmp($holster, "// ===END ICANN DOMAINS===\n") === 0) {
$endICANN = true;
} elseif(strcmp($holster, "// ===END PRIVATE DOMAINS===\n") === 0) {
$endPrivate = true;
} else {
// sanitize
$holster = trim($holster, " \t\n\r\0\x0B");
// must not be a comment, or blank
if(strcmp(substr($holster, 0, 2), "//") !== 0 && strlen($holster) > 0) {
// remove exemption rules
if(strpos($holster, "!") !== false) {
$holster = explode(".", $holster);
unset($holster[0]);
$holster = implode(".", $holster);
}
// remove supercookie rulesets and errata which are less important for our use case
for($i = 0; $i < 255; $i++) {
$holster = ltrim($holster, "*");
$holster = trim($holster, ".");
}
// punycode it
$small = $puny->encode($holster);
// echo $holster . " <-> " . $small . " <-> " . $puny->decode($small) . "\n"; // testing
if(!strcmp($holster, $puny->decode($small)) === 0) {
$parseErrors++;
}
if($endICANN == false) {
$ICANN[] = $small;
$countICANN++;
} else {
$Private[] = $small;
$countPrivate++;
}
}
}
}
// status checking before split operation
$ICANN = array_unique($ICANN);
$recountICANN = count($ICANN);
$Private = array_unique($Private);
$recountPrivate = count($Private);
echo "ICANN suffixes found: " . $countICANN . ", unique: " . $recountICANN . PHP_EOL;
echo "Private suffixes found: " . $countPrivate . ", unique: " . $recountPrivate . PHP_EOL;
echo "Discrepancy: " . (($countICANN + $countPrivate) - ($recountICANN + $recountPrivate)) . PHP_EOL;
echo "Obvious punycode errors: " . $parseErrors . PHP_EOL . PHP_EOL;
// splitting
$ICANNex = [];
$lvlMaxICANN = 0;
$Privateex = [];
$lvlMaxPrivate = 0;
foreach($ICANN as $SFX) {
$SFXex = explode(".", $SFX);
$tempCtr = count($SFXex);
if($tempCtr > $lvlMaxICANN) {
$lvlMaxICANN = $tempCtr;
}
$SFXex = array_reverse($SFXex);
ksort($SFXex);
$SFXex[] = "%";
$ICANNex[] = $SFXex;
}
foreach($Private as $SFX) {
$SFXex = explode(".", $SFX);
$tempCtr = count($SFXex);
if($tempCtr > $lvlMaxPrivate) {
$lvlMaxPrivate = $tempCtr;
}
$SFXex = array_reverse($SFXex);
ksort($SFXex);
$SFXex[] = "%";
$Privateex[] = $SFXex;
}
// debug
// var_dump($ICANNex);
// var_dump($Privateex);
echo "Max ICANN suffix depth: " . $lvlMaxICANN . PHP_EOL;
echo "Max Private suffix depth: " . $lvlMaxPrivate . PHP_EOL . PHP_EOL;
// consolidation
$ICANNTLDs = [];
$PrivateTLDs = [];
foreach($ICANNex as $SFXex) {
$walk = [];
$recon = [];
$pos = $ICANNTLDs;
for($idx = 0; $idx < count($SFXex); $idx++) {
if(!array_key_exists($SFXex[$idx], $pos)) {
$pos[$SFXex[$idx]] = [];
}
$walk[$idx] = $pos;
$recon[$idx] = $SFXex[$idx];
$pos = $pos[$SFXex[$idx]];
}
for($idx = count($walk) - 1; $idx > 0; $idx--) {
$addTo = $walk[$idx - 1];
$addTo[$recon[$idx - 1]] = $walk[$idx];
$walk[$idx - 1] = $addTo;
}
$ICANNTLDs = $walk[0];
}
foreach($Privateex as $SFXex) {
$walk = [];
$recon = [];
$pos = $PrivateTLDs;
for($idx = 0; $idx < count($SFXex); $idx++) {
if(!array_key_exists($SFXex[$idx], $pos)) {
$pos[$SFXex[$idx]] = [];
}
$walk[$idx] = $pos;
$recon[$idx] = $SFXex[$idx];
$pos = $pos[$SFXex[$idx]];
}
for($idx = count($walk) - 1; $idx > 0; $idx--) {
$addTo = $walk[$idx - 1];
$addTo[$recon[$idx - 1]] = $walk[$idx];
$walk[$idx - 1] = $addTo;
}
$PrivateTLDs = $walk[0];
}
// debug
// var_dump($ICANNTLDs);
// var_dump($PrivateTLDs);
echo "ICANN TLDs: " . count($ICANNTLDs) . PHP_EOL;
echo "Private TLDs: " . count($PrivateTLDs) . PHP_EOL;
// encode
$ICANNJSON = json_encode($ICANNTLDs);
$PrivateJSON = json_encode($PrivateTLDs);
// write
file_put_contents("running/icann.json", $ICANNJSON);
file_put_contents("running/private.json", $PrivateJSON);