/
whois.go
185 lines (177 loc) · 4.67 KB
/
whois.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
package cmd
import (
"encoding/json"
"fmt"
"github.com/juzeon/lip/util"
"github.com/likexian/whois"
whoisparser "github.com/likexian/whois-parser"
"log"
"os"
"strings"
"time"
"github.com/spf13/cobra"
)
var whoisCmd = &cobra.Command{
Use: "whois <domain>",
Short: "WHOIS lookup for a domain",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
dialer, err := util.GetProxyDialer(persistentFlags.Proxy,
time.Duration(whoisFlags.Timeout)*time.Second)
if err != nil {
log.Fatalln("cannot get dialer: " + err.Error())
}
client := whois.NewClient().SetDialer(dialer).SetTimeout(time.Duration(whoisFlags.Timeout) * time.Second)
host, _ := util.RemoveProtocol(args[0])
rawResult, err := client.Whois(host)
if err != nil {
log.Fatalln("cannot do whois lookup: " + err.Error())
}
info, err := whoisparser.Parse(rawResult)
if err != nil {
log.Println("cannot parse whois result. Use raw result instead: " + err.Error())
fmt.Println(rawResult)
} else {
renderWhoisResult(info)
}
},
}
func renderWhoisResult(info whoisparser.WhoisInfo) {
if whoisFlags.JSON {
var target any
if whoisFlags.Domain {
if info.Domain == nil {
log.Println("no information for domain")
return
}
target = info.Domain
} else {
target = info
}
v, err := json.MarshalIndent(target, "", " ")
if err != nil {
log.Fatalln("cannot marshal whois json: " + err.Error())
}
fmt.Println(string(v))
return
}
if info.Domain != nil {
fmt.Println("Domain:")
var data [][]string
data = append(data, removeWhoisRedacted([]string{"" +
whoisRedact("ID"),
"Domain",
"Punycode",
whoisRedact("Name"),
whoisRedact("Extension"),
"WhoisServer",
"Status",
"NameServers",
"DNSSec",
"CreatedDate",
"UpdatedDate",
"ExpirationDate",
}))
data = append(data, removeWhoisRedacted([]string{
whoisRedact(info.Domain.ID),
info.Domain.Domain,
info.Domain.Punycode,
whoisRedact(info.Domain.Name),
whoisRedact(info.Domain.Extension),
info.Domain.WhoisServer,
strings.Join(info.Domain.Status, ", "),
strings.Join(info.Domain.NameServers, ", "),
util.Ternary(info.Domain.DNSSec, "true", "false"),
info.Domain.CreatedDate,
info.Domain.UpdatedDate,
info.Domain.ExpirationDate,
}))
util.WriteTable(data, os.Stdout, whoisFlags.Reverse)
}
if whoisFlags.Domain {
return
}
type ContactWithName struct {
Name string
Contact *whoisparser.Contact
}
var data [][]string
data = append(data, removeWhoisRedacted([]string{
"Source",
whoisRedact("ID"),
"Name",
"Organization",
"Street",
"City",
"Province",
"PostalCode",
"Country",
"Phone",
whoisRedact("PhoneExt"),
whoisRedact("Fax"),
whoisRedact("FaxExt"),
"Email",
"ReferralURL",
}))
for _, contact := range []ContactWithName{
{Contact: info.Registrar, Name: "Registrar"}, {Contact: info.Registrant, Name: "Registrant"},
{Contact: info.Administrative, Name: "Administrative"}, {Contact: info.Technical, Name: "Technical"},
{Contact: info.Billing, Name: "Billing"}} {
if contact.Contact == nil {
continue
}
data = append(data, removeWhoisRedacted([]string{
contact.Name,
whoisRedact(contact.Contact.ID),
contact.Contact.Name,
contact.Contact.Organization,
contact.Contact.Street,
contact.Contact.City,
contact.Contact.Province,
contact.Contact.PostalCode,
contact.Contact.Country,
contact.Contact.Phone,
whoisRedact(contact.Contact.PhoneExt),
whoisRedact(contact.Contact.Fax),
whoisRedact(contact.Contact.FaxExt),
contact.Contact.Email,
contact.Contact.ReferralURL,
}))
}
if len(data) > 1 {
if info.Domain != nil {
fmt.Println()
}
fmt.Println("Contacts:")
}
util.WriteTable(data, os.Stdout, whoisFlags.Reverse)
}
func whoisRedact(str string) string {
return util.Ternary(whoisFlags.Full, str, "^")
}
func removeWhoisRedacted(arr []string) []string {
return util.FilterKeep(arr, func(value string) bool {
return value != "^"
})
}
type whoisFlagStruct struct {
Reverse bool
Timeout int
JSON bool
Full bool
Domain bool
}
var whoisFlags = whoisFlagStruct{}
func init() {
rootCmd.AddCommand(whoisCmd)
whoisCmd.Flags().BoolVarP(&whoisFlags.Reverse, "reverse", "r", false,
"reverse the output table")
whoisCmd.Flags().IntVarP(&whoisFlags.Timeout, "timeout", "t", 10,
"n seconds timeout for whois lookup")
whoisCmd.Flags().BoolVarP(&whoisFlags.JSON, "json", "j", false,
"use JSON output format instead of ASCII table")
whoisCmd.Flags().BoolVarP(&whoisFlags.Full, "full", "f", false,
"show full information columns of whois lookup")
whoisCmd.Flags().BoolVarP(&whoisFlags.Domain, "domain", "d", false,
"show information of the domain only, hiding contacts")
}