-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudip.go
More file actions
154 lines (125 loc) · 4.53 KB
/
Copy pathcloudip.go
File metadata and controls
154 lines (125 loc) · 4.53 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
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
147
148
149
150
151
152
153
154
package cloudip
import (
"context"
"net/netip"
"sync"
"github.com/rezmoss/go-cloudip/internal/source"
)
// VersionInfo contains metadata about the data version.
// This is a re-export of the internal type for public use.
type VersionInfo = source.VersionInfo
var (
// Global default detector
defaultDetector *Detector
defaultDetectorOnce sync.Once
defaultDetectorErr error
)
// getDefaultDetector returns the global default detector, initializing it if needed.
func getDefaultDetector() (*Detector, error) {
defaultDetectorOnce.Do(func() {
defaultDetector, defaultDetectorErr = NewDetector()
})
return defaultDetector, defaultDetectorErr
}
// mustGetDefaultDetector returns the default detector or panics if initialization failed.
func mustGetDefaultDetector() *Detector {
d, err := getDefaultDetector()
if err != nil {
// Return a non-nil detector that returns empty results
// rather than panicking
return &Detector{}
}
return d
}
// Lookup returns information about the given IP address using the default detector.
func Lookup(ip string) LookupResult {
return mustGetDefaultDetector().Lookup(ip)
}
// LookupAddr returns information about the given IP address using the default detector.
func LookupAddr(addr netip.Addr) LookupResult {
return mustGetDefaultDetector().LookupAddr(addr)
}
// GetProvider returns the provider for the given IP, or empty string if not found.
func GetProvider(ip string) Provider {
return mustGetDefaultDetector().GetProvider(ip)
}
// GetProviderAddr returns the provider for the given IP address.
func GetProviderAddr(addr netip.Addr) Provider {
return mustGetDefaultDetector().GetProviderAddr(addr)
}
// IsCloudProvider returns true if the IP belongs to any cloud provider.
func IsCloudProvider(ip string) bool {
return mustGetDefaultDetector().IsCloudProvider(ip)
}
// IsCloudProviderAddr returns true if the IP belongs to any cloud provider.
func IsCloudProviderAddr(addr netip.Addr) bool {
return mustGetDefaultDetector().IsCloudProviderAddr(addr)
}
// IsAWS returns true if the IP belongs to AWS.
func IsAWS(ip string) bool {
return mustGetDefaultDetector().IsAWS(ip)
}
// IsAWSAddr returns true if the IP belongs to AWS.
func IsAWSAddr(addr netip.Addr) bool {
return mustGetDefaultDetector().IsAWSAddr(addr)
}
// IsGCP returns true if the IP belongs to Google Cloud.
func IsGCP(ip string) bool {
return mustGetDefaultDetector().IsGCP(ip)
}
// IsGCPAddr returns true if the IP belongs to Google Cloud.
func IsGCPAddr(addr netip.Addr) bool {
return mustGetDefaultDetector().IsGCPAddr(addr)
}
// IsAzure returns true if the IP belongs to Microsoft Azure.
func IsAzure(ip string) bool {
return mustGetDefaultDetector().IsAzure(ip)
}
// IsAzureAddr returns true if the IP belongs to Microsoft Azure.
func IsAzureAddr(addr netip.Addr) bool {
return mustGetDefaultDetector().IsAzureAddr(addr)
}
// IsCloudflare returns true if the IP belongs to Cloudflare.
func IsCloudflare(ip string) bool {
return mustGetDefaultDetector().IsCloudflare(ip)
}
// IsCloudflareAddr returns true if the IP belongs to Cloudflare.
func IsCloudflareAddr(addr netip.Addr) bool {
return mustGetDefaultDetector().IsCloudflareAddr(addr)
}
// IsDigitalOcean returns true if the IP belongs to DigitalOcean.
func IsDigitalOcean(ip string) bool {
return mustGetDefaultDetector().IsDigitalOcean(ip)
}
// IsDigitalOceanAddr returns true if the IP belongs to DigitalOcean.
func IsDigitalOceanAddr(addr netip.Addr) bool {
return mustGetDefaultDetector().IsDigitalOceanAddr(addr)
}
// IsOracle returns true if the IP belongs to Oracle Cloud.
func IsOracle(ip string) bool {
return mustGetDefaultDetector().IsOracle(ip)
}
// IsOracleAddr returns true if the IP belongs to Oracle Cloud.
func IsOracleAddr(addr netip.Addr) bool {
return mustGetDefaultDetector().IsOracleAddr(addr)
}
// Version returns the data version string from the default detector.
func Version() string {
return mustGetDefaultDetector().Version()
}
// RangeCount returns the number of IP ranges loaded in the default detector.
func RangeCount() int {
return mustGetDefaultDetector().RangeCount()
}
// Providers returns the list of providers from the default detector.
func Providers() []string {
return mustGetDefaultDetector().Providers()
}
// Update fetches the latest data using the default detector.
func Update(ctx context.Context) error {
return mustGetDefaultDetector().Update(ctx)
}
// CheckUpdate checks if a new version is available using the default detector.
func CheckUpdate(ctx context.Context) (bool, *VersionInfo, error) {
return mustGetDefaultDetector().CheckUpdate(ctx)
}