-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranger.go
53 lines (44 loc) · 1.03 KB
/
ranger.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
package cloudranger
import (
"net"
"github.com/infobloxopen/go-trees/iptree"
)
type CloudRanger struct {
tree *iptree.Tree
}
// IPInfo contains information about an IP address.
type IPInfo struct {
cloud string
region string
}
// New returns a new cloudRanger.
func New() *CloudRanger {
tree := iptree.NewTree()
for _, r := range cloudRanges {
tree.InplaceInsertNet(r.net, r.info)
}
return &CloudRanger{
tree: tree,
}
}
// GetIP returns the IPInfo for the given IP address. If the IP address is not
// found in any of the known cloud providers the second return value is false.
func (cr *CloudRanger) GetIP(ip string) (IPInfo, bool) {
addr := net.ParseIP(ip)
if addr == nil {
return IPInfo{}, false
}
n, found := cr.tree.GetByIP(addr)
if !found {
return IPInfo{}, false
}
return n.(IPInfo), true
}
// Cloud returns the cloud provider for the IP address.
func (i IPInfo) Cloud() string {
return i.cloud
}
// Region returns the cloud provider's region for the IP address.
func (i IPInfo) Region() string {
return i.region
}