|
@@ -9,32 +9,9 @@ import (
|
9
|
9
|
"github.com/garyburd/redigo/redis"
|
10
|
10
|
)
|
11
|
11
|
|
12
|
|
-// IP2ASNClient .
|
13
|
|
-type IP2ASNClient struct {
|
14
|
|
- conn redis.Conn
|
15
|
|
-}
|
16
|
|
-
|
17
|
|
-func (i *IP2ASNClient) parseIP(IP string) (net.IP, error) {
|
18
|
|
- I := net.ParseIP(IP)
|
19
|
|
- if I == nil {
|
20
|
|
- return nil, net.InvalidAddrError(IP)
|
21
|
|
- }
|
22
|
|
- return I, nil
|
23
|
|
-}
|
24
|
|
-
|
25
|
|
-func (i *IP2ASNClient) keys(IP net.IP) []net.IPNet {
|
26
|
|
- var keys []net.IPNet
|
27
|
|
- for _, n := range netmasks {
|
28
|
|
- ipn := net.IPNet{
|
29
|
|
- IP: IP.Mask(n),
|
30
|
|
- Mask: n,
|
31
|
|
- }
|
32
|
|
- keys = append(keys, ipn)
|
33
|
|
- }
|
34
|
|
- return keys
|
35
|
|
-}
|
36
|
|
-
|
37
|
|
-// NewIP2ASNClient .
|
|
12
|
+// NewIP2ASNClient returns a new client for IP address to AS Number queries.
|
|
13
|
+// The caller is reposible for closing the conn when the client is not used
|
|
14
|
+// anymore.
|
38
|
15
|
func NewIP2ASNClient(conn redis.Conn) *IP2ASNClient {
|
39
|
16
|
ipasn := &IP2ASNClient{
|
40
|
17
|
conn: conn,
|
|
@@ -42,6 +19,18 @@ func NewIP2ASNClient(conn redis.Conn) *IP2ASNClient {
|
42
|
19
|
return ipasn
|
43
|
20
|
}
|
44
|
21
|
|
|
22
|
+// ASNResult contains the lookup results from resolving an IP address to a ASN.
|
|
23
|
+type ASNResult struct {
|
|
24
|
+ Mask net.IPNet
|
|
25
|
+ ASN int
|
|
26
|
+ Date time.Time // Recorded time for when
|
|
27
|
+}
|
|
28
|
+
|
|
29
|
+// IP2ASNClient is the query client.
|
|
30
|
+type IP2ASNClient struct {
|
|
31
|
+ conn redis.Conn
|
|
32
|
+}
|
|
33
|
+
|
45
|
34
|
// Current returns the latest known result for an IP2ASN lookup.
|
46
|
35
|
func (i *IP2ASNClient) Current(IP string) *ASNResult {
|
47
|
36
|
ip, err := i.parseIP(IP)
|
|
@@ -80,11 +69,24 @@ func (i *IP2ASNClient) AllHistory(IP string) []ASNResult {
|
80
|
69
|
return result
|
81
|
70
|
}
|
82
|
71
|
|
83
|
|
-// ASNResult contains the lookup results from resolving an IP address to a ASN.
|
84
|
|
-type ASNResult struct {
|
85
|
|
- Mask net.IPNet
|
86
|
|
- ASN int
|
87
|
|
- Date time.Time // Recorded time for when
|
|
72
|
+func (i *IP2ASNClient) parseIP(IP string) (net.IP, error) {
|
|
73
|
+ I := net.ParseIP(IP)
|
|
74
|
+ if I == nil {
|
|
75
|
+ return nil, net.InvalidAddrError(IP)
|
|
76
|
+ }
|
|
77
|
+ return I, nil
|
|
78
|
+}
|
|
79
|
+
|
|
80
|
+func (i *IP2ASNClient) keys(IP net.IP) []net.IPNet {
|
|
81
|
+ var keys []net.IPNet
|
|
82
|
+ for _, n := range netmasks {
|
|
83
|
+ ipn := net.IPNet{
|
|
84
|
+ IP: IP.Mask(n),
|
|
85
|
+ Mask: n,
|
|
86
|
+ }
|
|
87
|
+ keys = append(keys, ipn)
|
|
88
|
+ }
|
|
89
|
+ return keys
|
88
|
90
|
}
|
89
|
91
|
|
90
|
92
|
// Stringer.
|