-
Notifications
You must be signed in to change notification settings - Fork 15
/
parser.go
118 lines (98 loc) · 2.49 KB
/
parser.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
package parser
import (
"index/suffixarray"
"strings"
"net/http"
"io/ioutil"
"bytes"
)
type Parser struct {
sa *suffixarray.Index
}
type Domain struct {
Subdomain string
Domain string
TLD string
}
func NewDomainParser() Parser {
data, err := ioutil.ReadFile("/tmp/.tlds")
if err != nil {
data, _ = download()
ioutil.WriteFile("/tmp/.tlds", data, 0644)
}
tlds := strings.Split(string(data), "\n")
sa := CreateTLDIndex(tlds)
return Parser{
sa: sa,
}
}
func (p *Parser) FindTldOffset(domain_parts []string) int {
counter := 2
for counter > 0 {
start_point := len(domain_parts) - counter
if start_point < 0 {
return 0
}
tld_parts := strings.Join(domain_parts[len(domain_parts)-counter:], ".")
indicies := p.sa.Lookup([]byte(tld_parts), -1)
if len(indicies) > 0 {
offset := (len(domain_parts) - (counter +1))
if offset >= 0 {
return offset
}
}
counter--
}
return 0
}
func (p *Parser) ParseDomain(domain string) Domain {
domain_parts := strings.Split(domain, ".")
offset := p.FindTldOffset(domain_parts)
return Domain{
Subdomain: strings.Join(domain_parts[:offset], "."),
Domain: domain_parts[offset],
TLD: strings.Join(domain_parts[offset + 1:], "."),
}
}
func (p *Parser) GetDomain(domain string) string{
domain_parts := strings.Split(domain, ".")
offset := p.FindTldOffset(domain_parts)
return domain_parts[offset]
}
func (p *Parser) GetSubdomain(domain string) string {
domain_parts := strings.Split(domain, ".")
offset := p.FindTldOffset(domain_parts)
return strings.Join(domain_parts[:offset], ".")
}
func (p *Parser) GetFQDN(domain string) string {
domain_parts := strings.Split(domain, ".")
offset := p.FindTldOffset(domain_parts)
return strings.Join(domain_parts[offset:], ".")
}
func (p *Parser) GetTld(domain string) string {
domain_parts := strings.Split(domain, ".")
offset := p.FindTldOffset(domain_parts)
return strings.Join(domain_parts[offset + 1:], ".")
}
func download() ([]byte, error) {
u := "https://publicsuffix.org/list/public_suffix_list.dat"
resp, err := http.Get(u)
if err != nil {
return []byte(""), err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
lines := strings.Split(string(body), "\n")
var buffer bytes.Buffer
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "// ===BEGIN PRIVATE DOMAINS") {
break
}
if line != "" && !strings.HasPrefix(line, "//") {
buffer.WriteString(line)
buffer.WriteString("\n")
}
}
return buffer.Bytes(), nil
}