forked from dizel-by/ripego
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhois.go
124 lines (108 loc) · 2.81 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
package ripego
import (
"errors"
"net"
"strings"
)
//go:generate go run gen.go
type lookupFunc func(string, string) (*WhoisInfo, error)
var lookupFunctions = map[string]lookupFunc{
"whois.afrinic.net4": AfrinicCheck,
"whois.apnic.net4": ApnicCheck,
"whois.arin.net4": ArinCheck,
"whois.lacnic.net4": LacnicCheck,
"whois.ripe.net4": RipeCheck4,
"whois.afrinic.net6": AfrinicCheck,
"whois.apnic.net6": ApnicCheck6,
"whois.arin.net6": ArinCheck,
"whois.lacnic.net6": LacnicCheck,
"whois.ripe.net6": RipeCheck6,
}
func init() {
initIPv6()
}
// IPLookup function that returns IP information at provider and returns information.
func IPLookup(ipaddr string) (*WhoisInfo, error) {
var ipVersion string
var whoisServer string
ip := net.ParseIP(ipaddr)
if ip4 := ip.To4(); ip4 != nil {
ipVersion = "4"
whoisServer = getIPv4Server(ip4)
} else if ip6 := ip.To16(); ip6 != nil {
ipVersion = "6"
whoisServer = getIPv6Server(ip6)
} else {
return nil, errors.New("Invalid IP address: " + ipaddr)
}
if whoisServer == "" {
return nil, errors.New("unable to find WhoIs-Server for: " + ipaddr)
}
lf := lookupFunctions[whoisServer+ipVersion]
if lf == nil {
return nil, errors.New("Unable to find whois function for: " + whoisServer)
}
response, error := lf(ipaddr, whoisServer)
if error == nil && strings.HasPrefix(response.Netname, "APNIC-ERX") {
lf = ApnicCheck
whoisServer = "whois.apnic.net"
response, error = lf(ipaddr, whoisServer)
}
return response, error
}
// getIPv4Server returns the whois server fot the given IPv4 address
func getIPv4Server(ip net.IP) string {
return ipv4prefixes[ip[0]]
}
// getIPv6Server returns the whois server fot the given IPv6 address
func getIPv6Server(ip net.IP) string {
for i := range ipv6prefixes {
entry := &ipv6prefixes[i]
if entry.net.Contains(ip) {
return entry.whois
}
}
return ""
}
// WhoisInfo struct with information on IP address range.
type WhoisInfo struct {
Noc string
Inetnum string
Netname string
Descr string
Country string
Organization string
AdminC string
TechC string
MntLower string
Status string
MntBy string
Created string
LastModified string
Source string
MntRoutes string
Person WhoisPerson
Route WhoisRoute
}
// WhoisPerson struct for Person information from provider.
type WhoisPerson struct {
Name string
Address string
Phone string
AbuseMailbox string
NicHdl string
MntBy string
Created string
LastModified string
Source string
}
// WhoisRoute struct for Route and Network information from provider.
type WhoisRoute struct {
Route string
Descr string
Origin string
MntBy string
Created string
LastModified string
Source string
}