Documentation
¶
Overview ¶
Package cloudip provides fast cloud provider IP detection.
It determines whether an IP address belongs to AWS, GCP, Azure, Cloudflare, DigitalOcean, or Oracle Cloud with sub-microsecond lookup times using a Patricia trie data structure.
Quick Start ¶
Use package-level functions for simple lookups:
if cloudip.IsAWS("52.94.76.1") {
fmt.Println("This is an AWS IP")
}
provider := cloudip.GetProvider("34.64.0.1")
fmt.Println("Provider:", provider) // "gcp"
result := cloudip.Lookup("52.94.76.1")
if result.Found {
fmt.Printf("Provider: %s, Region: %s\n", result.Provider, result.Region)
}
Custom Detector ¶
For more control, create a custom Detector:
detector, err := cloudip.NewDetector(
cloudip.WithDataDir("./cache"),
cloudip.WithAutoUpdate(24 * time.Hour),
)
if err != nil {
log.Fatal(err)
}
defer detector.Close()
result := detector.Lookup("52.94.76.1")
High Performance ¶
For best performance, use netip.Addr directly:
addr, _ := netip.ParseAddr("52.94.76.1")
result := cloudip.LookupAddr(addr)
Data Source ¶
IP ranges are sourced from official cloud provider publications and compiled into an embedded database that can be updated at runtime.
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.
Index ¶
- func IsAWS(ip string) bool
- func IsAWSAddr(addr netip.Addr) bool
- func IsAzure(ip string) bool
- func IsAzureAddr(addr netip.Addr) bool
- func IsCloudProvider(ip string) bool
- func IsCloudProviderAddr(addr netip.Addr) bool
- func IsCloudflare(ip string) bool
- func IsCloudflareAddr(addr netip.Addr) bool
- func IsDigitalOcean(ip string) bool
- func IsDigitalOceanAddr(addr netip.Addr) bool
- func IsGCP(ip string) bool
- func IsGCPAddr(addr netip.Addr) bool
- func IsOracle(ip string) bool
- func IsOracleAddr(addr netip.Addr) bool
- func Providers() []string
- func RangeCount() int
- func Update(ctx context.Context) error
- func Version() string
- type Detector
- func (d *Detector) CheckUpdate(ctx context.Context) (bool, *source.VersionInfo, error)
- func (d *Detector) Close() error
- func (d *Detector) GetProvider(ip string) Provider
- func (d *Detector) GetProviderAddr(addr netip.Addr) Provider
- func (d *Detector) IsAWS(ip string) bool
- func (d *Detector) IsAWSAddr(addr netip.Addr) bool
- func (d *Detector) IsAzure(ip string) bool
- func (d *Detector) IsAzureAddr(addr netip.Addr) bool
- func (d *Detector) IsCloudProvider(ip string) bool
- func (d *Detector) IsCloudProviderAddr(addr netip.Addr) bool
- func (d *Detector) IsCloudflare(ip string) bool
- func (d *Detector) IsCloudflareAddr(addr netip.Addr) bool
- func (d *Detector) IsDigitalOcean(ip string) bool
- func (d *Detector) IsDigitalOceanAddr(addr netip.Addr) bool
- func (d *Detector) IsGCP(ip string) bool
- func (d *Detector) IsGCPAddr(addr netip.Addr) bool
- func (d *Detector) IsOracle(ip string) bool
- func (d *Detector) IsOracleAddr(addr netip.Addr) bool
- func (d *Detector) Lookup(ip string) LookupResult
- func (d *Detector) LookupAddr(addr netip.Addr) LookupResult
- func (d *Detector) Providers() []string
- func (d *Detector) RangeCount() int
- func (d *Detector) Update(ctx context.Context) error
- func (d *Detector) Version() string
- type LookupResult
- type Option
- type Provider
- type VersionInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAzureAddr ¶
IsAzureAddr returns true if the IP belongs to Microsoft Azure.
func IsCloudProvider ¶
IsCloudProvider returns true if the IP belongs to any cloud provider.
func IsCloudProviderAddr ¶
IsCloudProviderAddr returns true if the IP belongs to any cloud provider.
func IsCloudflare ¶
IsCloudflare returns true if the IP belongs to Cloudflare.
func IsCloudflareAddr ¶
IsCloudflareAddr returns true if the IP belongs to Cloudflare.
func IsDigitalOcean ¶
IsDigitalOcean returns true if the IP belongs to DigitalOcean.
func IsDigitalOceanAddr ¶
IsDigitalOceanAddr returns true if the IP belongs to DigitalOcean.
func IsOracleAddr ¶
IsOracleAddr returns true if the IP belongs to Oracle Cloud.
func Providers ¶
func Providers() []string
Providers returns the list of providers from the default detector.
func RangeCount ¶
func RangeCount() int
RangeCount returns the number of IP ranges loaded in the default detector.
Types ¶
type Detector ¶
type Detector struct {
// contains filtered or unexported fields
}
Detector is the main type for cloud IP detection. It is safe for concurrent use.
func NewDetector ¶
NewDetector creates a new Detector with the given options. By default, it loads embedded data and can fetch updates from GitHub.
func (*Detector) CheckUpdate ¶
CheckUpdate checks if a new version is available. Returns true if the remote version is newer than the local version.
func (*Detector) GetProvider ¶
GetProvider returns the provider for the given IP, or empty string if not found.
func (*Detector) GetProviderAddr ¶
GetProviderAddr returns the provider for the given IP address.
func (*Detector) IsAzureAddr ¶
IsAzureAddr returns true if the IP belongs to Microsoft Azure.
func (*Detector) IsCloudProvider ¶
IsCloudProvider returns true if the IP belongs to any cloud provider.
func (*Detector) IsCloudProviderAddr ¶
IsCloudProviderAddr returns true if the IP belongs to any cloud provider.
func (*Detector) IsCloudflare ¶
IsCloudflare returns true if the IP belongs to Cloudflare.
func (*Detector) IsCloudflareAddr ¶
IsCloudflareAddr returns true if the IP belongs to Cloudflare.
func (*Detector) IsDigitalOcean ¶
IsDigitalOcean returns true if the IP belongs to DigitalOcean.
func (*Detector) IsDigitalOceanAddr ¶
IsDigitalOceanAddr returns true if the IP belongs to DigitalOcean.
func (*Detector) IsOracleAddr ¶
IsOracleAddr returns true if the IP belongs to Oracle Cloud.
func (*Detector) Lookup ¶
func (d *Detector) Lookup(ip string) LookupResult
Lookup returns information about the given IP address.
func (*Detector) LookupAddr ¶
func (d *Detector) LookupAddr(addr netip.Addr) LookupResult
LookupAddr returns information about the given IP address.
func (*Detector) RangeCount ¶
RangeCount returns the number of IP ranges loaded.
type LookupResult ¶
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
}
LookupResult contains information about an IP address lookup.
func Lookup ¶
func Lookup(ip string) LookupResult
Lookup returns information about the given IP address using the default detector.
func LookupAddr ¶
func LookupAddr(addr netip.Addr) LookupResult
LookupAddr returns information about the given IP address using the default detector.
type Option ¶
type Option func(*options)
Option is a functional option for configuring the Detector.
func WithAutoUpdate ¶
WithAutoUpdate enables automatic background updates at the given interval. Minimum interval is 1 hour. Use zero to disable (default).
func WithDataDir ¶
WithDataDir sets the directory for caching data. Set to empty string to disable caching.
func WithDataURL ¶
WithDataURL overrides the default data URL. Use this if you're hosting your own cloudip-db mirror.
func WithHTTPClient ¶
WithHTTPClient sets a custom HTTP client for network requests.
func WithOffline ¶
func WithOffline() Option
WithOffline disables all network access. The detector will only use embedded data.
func WithVersionURL ¶
WithVersionURL overrides the default version URL.
type Provider ¶
type Provider string
Provider represents a cloud provider.
const ( ProviderUnknown Provider = "" ProviderAWS Provider = "aws" ProviderGCP Provider = "gcp" ProviderAzure Provider = "azure" ProviderCloudflare Provider = "cloudflare" ProviderDigitalOcean Provider = "digitalocean" ProviderOracle Provider = "oracle" )
Cloud provider constants.
func GetProvider ¶
GetProvider returns the provider for the given IP, or empty string if not found.
func GetProviderAddr ¶
GetProviderAddr returns the provider for the given IP address.
type VersionInfo ¶
type VersionInfo = source.VersionInfo
VersionInfo contains metadata about the data version. This is a re-export of the internal type for public use.
func CheckUpdate ¶
func CheckUpdate(ctx context.Context) (bool, *VersionInfo, error)
CheckUpdate checks if a new version is available using the default detector.