forked from kubernetes/kubernetes
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (124 loc) · 2.55 KB
/
main.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
// Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package main
import (
"bufio"
"errors"
"log"
"net/http"
"os"
"regexp"
"sort"
"strings"
"sync"
"text/template"
)
const path = "tlds.go"
var tldsTmpl = template.Must(template.New("tlds").Parse(`// Generated by tldsgen
package xurls
// TLDs is a sorted list of all public top-level domains.
//
// Sources:{{range $_, $url := .URLs}}
// * {{$url}}{{end}}
var TLDs = []string{
{{range $_, $tld := .TLDs}}` + "\t`" + `{{$tld}}` + "`" + `,
{{end}}}
`))
func cleanTld(tld string) string {
tld = strings.ToLower(tld)
if strings.HasPrefix(tld, "xn--") {
return ""
}
return tld
}
func fetchFromURL(url, pat string) {
defer wg.Done()
log.Printf("Fetching %s", url)
resp, err := http.Get(url)
if err == nil && resp.StatusCode >= 400 {
err = errors.New(resp.Status)
}
if err != nil {
errChan <- err
return
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
re := regexp.MustCompile(pat)
for scanner.Scan() {
line := scanner.Text()
tld := re.FindString(line)
tld = cleanTld(tld)
if tld == "" {
continue
}
tldChan <- tld
}
if err := scanner.Err(); err != nil {
errChan <- err
}
}
var (
wg sync.WaitGroup
tldChan = make(chan string)
errChan = make(chan error)
)
func tldList() ([]string, []string, error) {
var urls []string
fromURL := func(url, pat string) {
urls = append(urls, url)
wg.Add(1)
go fetchFromURL(url, pat)
}
fromURL("https://data.iana.org/TLD/tlds-alpha-by-domain.txt",
`^[^#]+$`)
fromURL("https://publicsuffix.org/list/effective_tld_names.dat",
`^[^/.]+$`)
tldSet := make(map[string]struct{})
anyError := false
go func() {
for {
select {
case tld := <-tldChan:
tldSet[tld] = struct{}{}
case err := <-errChan:
log.Printf("%v", err)
anyError = true
}
}
}()
wg.Wait()
if anyError {
return nil, nil, errors.New("there were some errors while fetching the TLDs")
}
tlds := make([]string, 0, len(tldSet))
for tld := range tldSet {
tlds = append(tlds, tld)
}
sort.Strings(tlds)
return tlds, urls, nil
}
func writeTlds(tlds, urls []string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return tldsTmpl.Execute(f, struct {
TLDs []string
URLs []string
}{
TLDs: tlds,
URLs: urls,
})
}
func main() {
tlds, urls, err := tldList()
if err != nil {
log.Fatalf("Could not get TLD list: %v", err)
}
log.Printf("Generating %s...", path)
if err := writeTlds(tlds, urls); err != nil {
log.Fatalf("Could not write path: %v", err)
}
}