-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiwhois.go
153 lines (118 loc) · 2.76 KB
/
multiwhois.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
package multiwhois
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/domainr/whois"
"gopkg.in/yaml.v2"
)
type TLDConfig struct {
Expiration string `yaml:"expiration"`
ExpirationFormat string `yaml:"expiration_format"`
Availability string `yaml:"availability"`
}
type DomainInfo struct {
Domain string
Expiration time.Time
IsAvailable bool
FullWhois string
}
var tldConfigs map[string]TLDConfig
func init() {
var err error
tldConfigs, err = loadTLDConfigs()
if err != nil {
panic(err)
}
}
func QueryDomainInfo(domain string) (*DomainInfo, error) {
req, err := whois.NewRequest(domain)
if err != nil {
return nil, err
}
res, err := whois.DefaultClient.Fetch(req)
if err != nil {
return nil, err
}
body := string(res.Body)
info := &DomainInfo{}
suffix := strings.TrimPrefix(filepath.Ext(domain), ".")
tld, ok := tldConfigs[strings.ToLower(suffix)]
if !ok {
err = fmt.Errorf("unsupported TLD")
return nil, err
}
info, err = parseTLDInfo(body, tld)
if err != nil {
return nil, err
}
info.Domain = domain
return info, nil
}
func loadTLDConfigs() (map[string]TLDConfig, error) {
tldConfigs := make(map[string]TLDConfig)
// Read the YAML files in the tlds directory
dirContents, err := filepath.Glob("tlds/*.yml")
if err != nil {
return nil, err
}
// Load the TLD configuration from each YAML file
for _, filename := range dirContents {
configFile, err := readFile(filename)
if err != nil {
return nil, err
}
var tldConfig TLDConfig
err = yaml.Unmarshal(configFile, &tldConfig)
if err != nil {
return nil, err
}
suffix := strings.TrimSuffix(filepath.Base(filename), ".yml")
tldConfigs[suffix] = tldConfig
}
return tldConfigs, nil
}
func readFile(filename string) ([]byte, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return nil, err
}
data := make([]byte, fileInfo.Size())
_, err = file.Read(data)
if err != nil {
return nil, err
}
return data, nil
}
func parseTLDInfo(body string, config TLDConfig) (*DomainInfo, error) {
info := &DomainInfo{
FullWhois: body,
}
statusRegex := regexp.MustCompile(config.Availability)
statusMatch := statusRegex.FindString(body)
if statusMatch != "" {
info.IsAvailable = true
return info, nil
}
expirationRegex := regexp.MustCompile(config.Expiration)
expirationMatch := expirationRegex.FindStringSubmatch(body)
if len(expirationMatch) > 1 {
expiration := expirationMatch[1]
expirationTime, err := time.Parse(config.ExpirationFormat, expiration)
if err != nil {
fmt.Println("error parsing datetime:", err)
return info, nil
}
info.Expiration = expirationTime
return info, nil
}
return info, nil
}