-
Notifications
You must be signed in to change notification settings - Fork 6
/
psl.go
213 lines (195 loc) · 6.52 KB
/
psl.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
package fasttld
import (
"bytes"
"errors"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/spf13/afero"
"golang.org/x/net/idna"
)
var publicSuffixListSources = []string{
"https://publicsuffix.org/list/public_suffix_list.dat",
"https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat",
}
type suffixes struct {
publicSuffixes []string
privateSuffixes []string
allSuffixes []string
}
func processLine(rawLine string, psl suffixes, isPrivateSuffix bool) (suffixes, bool) {
line := strings.TrimSpace(rawLine)
if "// ===BEGIN PRIVATE DOMAINS===" == line {
isPrivateSuffix = true
}
if len(line) == 0 || strings.HasPrefix(line, "//") {
return psl, isPrivateSuffix
}
suffix, err := idna.ToASCII(line)
if err != nil {
// skip line if unable to convert to ascii
log.Println(line, '|', err)
return psl, isPrivateSuffix
}
if isPrivateSuffix {
psl.privateSuffixes = append(psl.privateSuffixes, suffix)
if suffix != line {
// add non-punycode version if it is different from punycode version
psl.privateSuffixes = append(psl.privateSuffixes, line)
}
} else {
psl.publicSuffixes = append(psl.publicSuffixes, suffix)
if suffix != line {
// add non-punycode version if it is different from punycode version
psl.publicSuffixes = append(psl.publicSuffixes, line)
}
}
psl.allSuffixes = append(psl.allSuffixes, suffix)
if suffix != line {
// add non-punycode version if it is different from punycode version
psl.allSuffixes = append(psl.allSuffixes, line)
}
return psl, isPrivateSuffix
}
// getPublicSuffixList retrieves Public Suffixes and Private Suffixes from Public Suffix list located at cacheFilePath.
//
// publicSuffixes: ICANN domains. Example: com, net, org etc.
//
// privateSuffixes: PRIVATE domains. Example: blogspot.co.uk, appspot.com etc.
//
// allSuffixes: Both ICANN and PRIVATE domains.
func getPublicSuffixList(cacheFilePath string) (suffixes, error) {
var psl suffixes
b, err := os.ReadFile(cacheFilePath)
if err != nil {
log.Println(err)
return psl, err
}
var isPrivateSuffix bool
for _, line := range strings.Split(string(b), "\n") {
psl, isPrivateSuffix = processLine(line, psl, isPrivateSuffix)
}
return psl, nil
}
// getHardcodedPublicSuffixList retrieves Public Suffixes and Private Suffixes from hardcoded Public Suffix list.
//
// publicSuffixes: ICANN domains. Example: com, net, org etc.
//
// privateSuffixes: PRIVATE domains. Example: blogspot.co.uk, appspot.com etc.
//
// allSuffixes: Both ICANN and PRIVATE domains.
func getHardcodedPublicSuffixList() (suffixes, error) {
var psl suffixes
var isPrivateSuffix bool
for _, line := range strings.Split(hardcodedPSL, "\n") {
psl, isPrivateSuffix = processLine(line, psl, isPrivateSuffix)
}
return psl, nil
}
// newHardcodedPSL creates a new *FastTLD using data from a hardcoded Public Suffix List file.
func newHardcodedPSL(err error, n SuffixListParams) (*FastTLD, error) {
log.Println(err, "Fallback to hardcoded Public Suffix List")
tldTrie, err := trieConstruct(n.IncludePrivateSuffix, "")
return &FastTLD{cacheFilePath: "", tldTrie: tldTrie, includePrivateSuffix: n.IncludePrivateSuffix}, err
}
// downloadFile downloads file from url as byte slice
func downloadFile(url string) ([]byte, error) {
// Make HTTP GET request
var bodyBytes []byte
resp, err := http.Get(url)
if err != nil {
return bodyBytes, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err = afero.ReadAll(resp.Body)
} else {
err = errors.New("Download failed, HTTP status code : " + fmt.Sprint(resp.StatusCode))
}
return bodyBytes, err
}
// getCurrentFilePath returns path to current module file
//
// Similar to os.path.dirname(os.path.realpath(__file__)) in Python
//
// Credits: https://andrewbrookins.com/tech/golang-get-directory-of-the-current-file
func getCurrentFilePath() (string, bool) {
_, file, _, ok := runtime.Caller(0)
return filepath.Dir(file), ok
}
// Number of hours elapsed since last modified time of fileinfo.
func fileLastModifiedHours(fileinfo os.FileInfo) float64 {
return time.Now().Sub(fileinfo.ModTime()).Hours()
}
// update updates the local cache of Public Suffix List
func update(file afero.File,
publicSuffixListSources []string) error {
for _, publicSuffixListSource := range publicSuffixListSources {
// Write GET request body to local file
if bodyBytes, err := downloadFile(publicSuffixListSource); err != nil {
log.Println(err)
} else {
if !validPSLDelimiters(bodyBytes) {
continue
}
if _, err := file.Seek(0, 0); err != nil {
log.Println(err)
continue
}
if _, err := file.Write(bodyBytes); err != nil {
log.Println(err)
continue
}
log.Println("Public Suffix List updated.")
return nil
}
}
return errors.New("failed to fetch any Public Suffix List from all mirrors")
}
func validPSLDelimiters(contents []byte) bool {
return bytes.Contains(contents, []byte("// ===BEGIN ICANN DOMAINS===")) &&
bytes.Contains(contents, []byte("// ===END ICANN DOMAINS===")) &&
bytes.Contains(contents, []byte("// ===BEGIN PRIVATE DOMAINS===")) &&
bytes.Contains(contents, []byte("// ===END PRIVATE DOMAINS==="))
}
func checkCacheFile(cacheFilePath string) (bool, float64) {
cacheFilePath, pathValidErr := filepath.Abs(strings.TrimSpace(cacheFilePath))
stat, fileinfoErr := os.Stat(cacheFilePath)
var lastModifiedHours float64
if fileinfoErr == nil {
lastModifiedHours = fileLastModifiedHours(stat)
}
var validDelimiters bool
if contents, err := os.ReadFile(cacheFilePath); err == nil {
validDelimiters = validPSLDelimiters(contents)
}
return pathValidErr == nil && fileinfoErr == nil && !stat.IsDir() && validDelimiters, lastModifiedHours
}
// Update updates the default Public Suffix list file and updates its suffix trie using the updated file.
// If cache file path is not the same as the default cache file path, this will be a no-op.
func (f *FastTLD) Update() error {
filesystem := new(afero.OsFs)
defaultCacheFilePath := afero.GetTempDir(filesystem, "") + defaultPSLFileName
if f.cacheFilePath != defaultCacheFilePath {
return errors.New("No-op. Only default Public Suffix list file can be updated")
}
file, err := os.OpenFile(defaultCacheFilePath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
if updateErr := update(file, publicSuffixListSources); updateErr != nil {
return updateErr
}
tldTrie, err := trieConstruct(f.includePrivateSuffix, defaultCacheFilePath)
if err == nil {
f.tldTrie = tldTrie
f.cacheFilePath = defaultCacheFilePath
}
return err
}