-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes.go
More file actions
94 lines (78 loc) · 2.26 KB
/
Copy pathtypes.go
File metadata and controls
94 lines (78 loc) · 2.26 KB
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
// Package cloudip provides fast cloud provider IP detection.
//
// It can determine if an IP address belongs to major cloud providers
// (AWS, GCP, Azure, Cloudflare, DigitalOcean, Oracle) with sub-microsecond
// lookup times using a Patricia trie data structure.
package cloudip
import (
"net"
"net/netip"
"github.com/yl2chen/cidranger"
)
// Provider represents a cloud provider.
type Provider string
// Cloud provider constants.
const (
ProviderUnknown Provider = ""
ProviderAWS Provider = "aws"
ProviderGCP Provider = "gcp"
ProviderAzure Provider = "azure"
ProviderCloudflare Provider = "cloudflare"
ProviderDigitalOcean Provider = "digitalocean"
ProviderOracle Provider = "oracle"
)
// String returns the provider name.
func (p Provider) String() string {
if p == ProviderUnknown {
return "unknown"
}
return string(p)
}
// LookupResult contains information about an IP address lookup.
type LookupResult struct {
// Found indicates whether the IP was found in any cloud provider range.
Found bool
// Provider is the cloud provider that owns this IP range.
Provider Provider
// Region is the geographic region (e.g., "us-east-1", "europe-west1").
// May be empty if not available.
Region string
// Service is the cloud service (e.g., "EC2", "S3", "CLOUDFRONT").
// May be empty if not available.
Service string
// CIDR is the IP range that matched.
CIDR string
}
// rangeEntry implements cidranger.RangerEntry for storing IP range metadata.
type rangeEntry struct {
network net.IPNet
provider Provider
region string
service string
cidr string
}
// Network returns the IP network for this entry.
func (e *rangeEntry) Network() net.IPNet {
return e.network
}
// networkFromCIDR converts a CIDR string to net.IPNet.
func networkFromCIDR(cidr string) (net.IPNet, error) {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return net.IPNet{}, err
}
return *ipNet, nil
}
// netIPToNetIP converts netip.Addr to net.IP.
func netIPToNetIP(addr netip.Addr) net.IP {
return addr.AsSlice()
}
// detectorState holds the immutable state for lookups.
// This is swapped atomically for lock-free reads.
type detectorState struct {
ranger cidranger.Ranger
version string
buildTime int64
providers []string
rangeCount int
}