-
Notifications
You must be signed in to change notification settings - Fork 6
/
fasttld.go
434 lines (390 loc) · 13.1 KB
/
fasttld.go
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Package fasttld is a high performance effective top level domains (eTLD)
// extraction module implemented with compressed tries.
//
// This module is a port of the Python fasttld module,
// with additional modifications to support extraction
// of subcomponents from full URLs, IPv4 addresses, and IPv6 addresses.
package fasttld
import (
"errors"
"log"
"net/url"
"strconv"
"strings"
"github.com/spf13/afero"
"github.com/tidwall/hashmap"
"golang.org/x/net/idna"
)
const defaultPSLFolder string = "data"
const defaultPSLFileName string = "public_suffix_list.dat"
const largestPortNumber int = 65535
const pslMaxAgeHours float64 = 72
// FastTLD provides the Extract() function, to extract
// URLs using tldTrie generated from the
// Public Suffix List file at cacheFilePath.
type FastTLD struct {
cacheFilePath string
tldTrie *trie
includePrivateSuffix bool
}
// HostType indicates whether parsed URL
// contains a HostName, IPv4 address, IPv6 address
// or none of them
type HostType int
// None, HostName, IPv4 and IPv6 indicate whether parsed URL
// contains a HostName, IPv4 address, IPv6 address
// or none of them
const (
None HostType = iota
HostName
IPv4
IPv6
)
// ExtractResult contains components extracted from URL.
type ExtractResult struct {
Scheme, UserInfo, SubDomain, Domain, Suffix, RegisteredDomain, Port, Path string
HostType HostType
}
// SuffixListParams contains parameters for specifying path to Public Suffix List file and
// whether to extract private suffixes (e.g. blogspot.com).
type SuffixListParams struct {
CacheFilePath string
IncludePrivateSuffix bool
}
// URLParams specifies URL to extract components from.
//
// If IgnoreSubDomains = true, do not extract SubDomain.
//
// If ConvertURLToPunyCode = true, convert non-ASCII characters like 世界 to punycode.
type URLParams struct {
URL string
IgnoreSubDomains bool
ConvertURLToPunyCode bool
}
// trie is a node of the compressed trie
// used to store Public Suffix List eTLDs.
type trie struct {
matches hashmap.Map[string, *trie]
end bool
}
// nestedDict stores a slice of keys in the trie, by traversing the trie using the keys as a "path",
// creating new tries for keys that do not exist yet.
//
// If a new path overlaps an existing path, flag the previous path's trie node as end = true.
func nestedDict(dic *trie, keys []string) {
for _, key := range keys {
if _, ok := dic.matches.Get(key); !ok {
// key doesn't exist; add new node
var m hashmap.Map[string, *trie]
dic.matches.Set(key, &trie{matches: m})
}
dic, _ = dic.matches.Get(key)
}
// set last node to end = true
dic.end = true
}
// trieConstruct constructs a compressed trie to store Public Suffix List eTLDs split at "." in reverse-order.
//
// For example: "us.gov.pl" will be stored in the order {"pl", "gov", "us"}.
func trieConstruct(includePrivateSuffix bool, cacheFilePath string) (*trie, error) {
var m hashmap.Map[string, *trie]
tldTrie := &trie{matches: m}
var suffixLists suffixes
var err error
if cacheFilePath != "" {
suffixLists, err = getPublicSuffixList(cacheFilePath)
} else {
suffixLists, err = getHardcodedPublicSuffixList()
}
if err != nil {
log.Println(err)
return tldTrie, err
}
var suffixList []string
if includePrivateSuffix {
suffixList = suffixLists.allSuffixes
} else {
suffixList = suffixLists.publicSuffixes
}
for _, suffix := range suffixList {
sp := strings.Split(suffix, ".")
reverse(sp)
nestedDict(tldTrie, sp)
}
tldTrie.matches.Scan(func(key string, value *trie) bool {
if _, ok := value.matches.Get("*"); ok {
value.end = true
}
return true
})
return tldTrie, nil
}
// Extract components from a given `url`.
func (f *FastTLD) Extract(e URLParams) (ExtractResult, error) {
urlParts := ExtractResult{}
// Extract URL scheme
netloc := fastTrim(e.URL, whitespaceRuneSet, trimBoth)
if schemeEndIndex := getSchemeEndIndex(netloc); schemeEndIndex != -1 {
urlParts.Scheme = netloc[0:schemeEndIndex]
netloc = netloc[schemeEndIndex:]
}
// Extract URL userinfo
if atIdx := indexLastByteBefore(netloc, '@', invalidUserInfoCharsSet); atIdx != -1 {
urlParts.UserInfo = netloc[0:atIdx]
netloc = netloc[atIdx+1:]
}
// Find square brackets (if any) and host end index
openingSquareBracketIdx := -1
closingSquareBracketIdx := -1
hostEndIdx := -1
for i, r := range []byte(netloc) {
if r == '[' {
// Check for opening square bracket
if i > 0 {
// Reject if opening square bracket is not first character of hostname
return urlParts, errors.New("opening square bracket is not first character of hostname")
}
openingSquareBracketIdx = i
}
if r == ']' {
// Check for closing square bracket
closingSquareBracketIdx = i
}
if openingSquareBracketIdx == -1 {
if closingSquareBracketIdx != -1 {
// Reject if closing square bracket present but no opening square bracket
return urlParts, errors.New("closing square bracket present but no opening square bracket")
}
if endOfHostDelimitersSet.contains(r) {
// If no square brackets
// Check for endOfHostDelimitersSet
hostEndIdx = i
break
}
} else if closingSquareBracketIdx > openingSquareBracketIdx && endOfHostWithPortDelimitersSet.contains(r) {
// If opening + closing square bracket are present in correct order
// check for endOfHostWithPortDelimitersSet
hostEndIdx = i
break
}
if i == len(netloc)-1 && closingSquareBracketIdx < openingSquareBracketIdx {
// Reject if end of netloc reached but incomplete square bracket pair
return urlParts, errors.New("incomplete square bracket pair")
}
}
if closingSquareBracketIdx == len(netloc)-1 {
hostEndIdx = -1
} else if closingSquareBracketIdx != -1 {
hostEndIdx = closingSquareBracketIdx + 1
}
// Check for IPv6 address
if closingSquareBracketIdx > openingSquareBracketIdx {
if !isIPv6(netloc[1:closingSquareBracketIdx]) {
// Have square brackets but invalid IPv6 address => Domain is invalid
return urlParts, errors.New("invalid IPv6 address")
}
if hostEndIdx != -1 {
afterHost := netloc[hostEndIdx:]
if indexAnyASCII(afterHost, endOfHostDelimitersSet) != 0 {
// Reject IPv6 if there are invalid trailing characters after IPv6 address
return urlParts, errors.New("invalid trailing characters after IPv6 address")
}
}
// Closing square bracket in correct place and IPv6 is valid
urlParts.HostType = IPv6
urlParts.Domain = netloc[1:closingSquareBracketIdx]
urlParts.RegisteredDomain = netloc[1:closingSquareBracketIdx]
}
var afterHost string
// Separate URL host from subcomponents thereafter
if hostEndIdx != -1 {
afterHost = netloc[hostEndIdx:]
netloc = netloc[0:hostEndIdx]
}
// Extract Port and "Path" if any
if len(afterHost) != 0 {
pathStartIndex := indexAnyASCII(afterHost, endOfHostWithPortDelimitersSet)
if afterHost[0] == ':' {
var maybePort string
if pathStartIndex == -1 {
maybePort = afterHost[1:]
} else {
maybePort = afterHost[1:pathStartIndex]
}
if port, err := strconv.Atoi(maybePort); err == nil && 0 <= port && port <= largestPortNumber {
urlParts.Port = maybePort
} else {
return urlParts, errors.New("invalid port")
}
}
if pathStartIndex != -1 && pathStartIndex != len(afterHost) {
// If there is any path/query/fragment after the URL authority component...
// See https://stackoverflow.com/questions/47543432/what-do-we-call-the-combined-path-query-and-fragment-in-a-uri
// For simplicity, we shall call this the "Path".
urlParts.Path = afterHost[pathStartIndex:]
}
}
if urlParts.HostType == IPv6 {
return urlParts, nil
}
// decode all percentage encoded characters, if any
unescapedNetloc, err := url.QueryUnescape(netloc)
if err != nil {
return urlParts, err
}
if e.ConvertURLToPunyCode {
netloc = formatAsPunycode(unescapedNetloc)
} else if _, err := idna.ToUnicode(unescapedNetloc); err != nil {
// host is invalid if host cannot be converted to Unicode
//
// skip if host already converted to punycode
log.Println(strings.SplitAfterN(err.Error(), "idna: invalid label", 2)[0])
return urlParts, err
}
// Check for eTLD Suffix
node := f.tldTrie
var (
hasSuffix bool
hasLabels bool
end bool
previousSepIdx int
)
sepIdx, suffixStartIdx, suffixEndIdx := len(netloc), len(netloc), len(netloc)
for !end {
var label string
previousSepIdx = sepIdx
sepIdx = lastIndexAny(netloc[0:sepIdx], labelSeparatorsRuneSet)
if sepIdx != -1 {
label = netloc[sepIdx+sepSize(netloc[sepIdx]) : previousSepIdx]
if len(label) == 0 {
// allow consecutive label separators if suffix not found yet
if !hasLabels {
suffixEndIdx = sepIdx
continue
}
// any occurrences of consecutive label separators on left-hand side of a label are illegal.
return urlParts, errors.New("invalid consecutive label separators on left-hand side of a label")
}
hasLabels = true
} else {
label = netloc[0:previousSepIdx]
end = true
}
if _, ok := node.matches.Get("*"); ok {
// check if label falls under any wildcard exception rule
// e.g. !www.ck
if _, ok := node.matches.Get("!" + label); ok {
sepIdx = previousSepIdx
}
break
}
// check if label is part of an eTLD
label, _ = url.QueryUnescape(label)
if val, ok := node.matches.Get(label); ok {
suffixStartIdx = sepIdx
if !hasSuffix && val.end {
// index of end of suffix without trailing label separators
suffixEndIdx = previousSepIdx
hasSuffix = true
}
node = val
if val.matches.Len() == 0 {
// label is at a leaf node (no children) ; break out of loop
break
}
} else {
if previousSepIdx != len(netloc) {
sepIdx = previousSepIdx
}
break
}
}
// Check for IPv4 address
// Minimum possible length: len("0.0.0.0") -> 7
// Ensure first rune is numeric before expensive isIPv4()
if len(netloc) >= 7 && numericSet.contains(netloc[0]) && isIPv4(netloc) {
urlParts.HostType = IPv4
urlParts.Domain = netloc[0:previousSepIdx]
urlParts.RegisteredDomain = urlParts.Domain
return urlParts, nil
}
if sepIdx == -1 {
sepIdx, suffixStartIdx = len(netloc), len(netloc)
}
// Reject if invalidHostNameChars or consecutive label separators
// appears before Suffix
if hasSuffix {
if hasInvalidChars(netloc[0:suffixStartIdx]) {
return urlParts, errors.New("invalid characters in hostname")
}
} else {
if hasInvalidChars(netloc[0:previousSepIdx]) {
return urlParts, errors.New("invalid characters in hostname")
}
}
var domainStartSepIdx int
if hasSuffix {
if sepIdx < len(netloc) { // If there is a Domain
urlParts.Suffix = netloc[sepIdx+sepSize(netloc[sepIdx]) : suffixEndIdx]
domainStartSepIdx = lastIndexAny(netloc[0:sepIdx], labelSeparatorsRuneSet)
if domainStartSepIdx != -1 { // If there is a SubDomain
domainStartIdx := domainStartSepIdx + sepSize(netloc[domainStartSepIdx])
urlParts.Domain = netloc[domainStartIdx:sepIdx]
urlParts.RegisteredDomain = netloc[domainStartIdx:suffixEndIdx]
} else {
urlParts.Domain = netloc[0:sepIdx]
urlParts.RegisteredDomain = netloc[0:suffixEndIdx]
}
} else {
// Only Suffix exists
urlParts.Suffix = netloc[0:suffixEndIdx]
}
} else {
domainStartSepIdx = lastIndexAny(netloc[0:suffixEndIdx], labelSeparatorsRuneSet)
var domainStartIdx int
if domainStartSepIdx != -1 { // If there is a SubDomain
domainStartIdx = domainStartSepIdx + sepSize(netloc[domainStartSepIdx])
}
urlParts.Domain = netloc[domainStartIdx:suffixEndIdx]
}
if !e.IgnoreSubDomains && domainStartSepIdx != -1 { // If SubDomain is to be included
urlParts.SubDomain = netloc[0:domainStartSepIdx]
}
if len(urlParts.Domain) == 0 {
return urlParts, errors.New("empty domain")
}
urlParts.HostType = HostName
return urlParts, nil
}
// New creates a new *FastTLD using data from a Public Suffix List file.
func New(n SuffixListParams) (*FastTLD, error) {
extractor := &FastTLD{cacheFilePath: n.CacheFilePath, tldTrie: &trie{}, includePrivateSuffix: n.IncludePrivateSuffix}
// If cacheFilePath is unreachable, use temporary folder
if isValid, _ := checkCacheFile(extractor.cacheFilePath); !isValid {
filesystem := new(afero.OsFs)
defaultCacheFolderPath := afero.GetTempDir(filesystem, "")
defaultCacheFilePath := defaultCacheFolderPath + defaultPSLFileName
defaultCacheFolder, err := filesystem.Open(defaultCacheFolderPath)
if err != nil {
// temporary folder not accessible, fallback to hardcoded Public Suffix list
return newHardcodedPSL(err, n)
}
defer defaultCacheFolder.Close()
extractor.cacheFilePath = defaultCacheFilePath
isValid, lastModifiedHours := checkCacheFile(extractor.cacheFilePath)
if !isValid || lastModifiedHours > pslMaxAgeHours {
// update Public Suffix list cache if it is outdated
if updateErr := extractor.Update(); updateErr != nil {
// update failed, fallback to hardcoded Public Suffix list
return newHardcodedPSL(err, n)
}
return extractor, err
}
}
tldTrie, err := trieConstruct(n.IncludePrivateSuffix, extractor.cacheFilePath)
if err != nil {
return newHardcodedPSL(err, n)
}
extractor.tldTrie = tldTrie
return extractor, err
}