forked from xellio/whois
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
146 lines (119 loc) · 2.52 KB
/
query.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
package whois
import (
"bytes"
"fmt"
"io"
"net"
)
const (
// FormatDenic ...
FormatDenic = "denic"
// FormatIana ...
FormatIana = "iana"
// FormatRipe ...
FormatRipe = "ripe"
// FormatApnic ...
FormatApnic = "apnic"
// FormatAfrinic ...
FormatAfrinic = "afrinic"
// FormatLacnic ...
FormatLacnic = "lacnic"
// FormatArin ...
FormatArin = "arin"
// FormatIcaan ...
FormatIcaan = "icaan"
)
//
// query the given server, passing the given data
//
func query(server string, data string) ([]byte, error) {
conn, err := net.Dial("tcp", server)
if err != nil {
return nil, err
}
switch server {
case ArinServer:
data = "n + " + data
case DenicServer:
data = "-T dn,ace " + data
}
n, err := conn.Write([]byte(data + "\r\n"))
if err != nil {
return nil, err
}
if n != len(data)+2 {
return nil, fmt.Errorf("expected %d bytes sent, %d were sent", len(data)+2, n)
}
ret := []byte{}
tmp := make([]byte, 65536)
for {
n, err = conn.Read(tmp)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
ret = append(ret, tmp[0:n]...)
}
return ret, nil
}
//
// Parse the given raw data using a format specific parse function
//
func Parse(raw []byte, format string) (parsed QueryResult) {
if len(format) == 0 {
format = findFormatInRaw(raw)
}
switch format {
case FormatDenic:
parsed = parseDenicFormat(raw)
case FormatIana:
parsed = parseRipeFormat(raw)
case FormatRipe:
parsed = parseRipeFormat(raw)
case FormatApnic:
parsed = parseRipeFormat(raw)
case FormatAfrinic:
parsed = parseRipeFormat(raw)
case FormatLacnic:
parsed = parseRipeFormat(raw)
case FormatArin:
parsed = parseArinFormat(raw)
case FormatIcaan:
parsed = parseICANNData(raw)
default:
parsed = parseICANNData(raw)
}
return
}
//
// findFormatInRaw tries to find the format in the given raw []byte
//
func findFormatInRaw(raw []byte) string {
if bytes.Index(raw, []byte("//www.ripe.net")) != -1 {
return FormatRipe
}
if bytes.Index(raw, []byte("//www.icann.org")) != -1 {
return FormatIcaan
}
if bytes.Index(raw, []byte("//www.iana.org")) != -1 {
return FormatIana
}
if bytes.Index(raw, []byte("//www.denic.de")) != -1 {
return FormatDenic
}
if bytes.Index(raw, []byte("//www.arin.net")) != -1 {
return FormatArin
}
if bytes.Index(raw, []byte("//www.afrinic.net")) != -1 {
return FormatAfrinic
}
if bytes.Index(raw, []byte("//www.apnic.net")) != -1 {
return FormatApnic
}
if bytes.Index(raw, []byte("//www.lacnic.net")) != -1 {
return FormatLacnic
}
return "unknown"
}