ipdetect

package module
v0.0.0-...-07559d8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 11 Imported by: 0

README

ipdetect

Get your server's public IP address on any cloud provider.

The Problem

On AWS, GCP, and Azure, your public IPv4 isn't assigned to a network interface. It's NAT'd at the hypervisor level, so you can't find it by looking at your interfaces. Using external websites to check your IP is unreliable behind NAT and adds external dependencies.

This library queries the cloud provider's metadata service to get your actual public IP.

Install

go get github.com/nickgarlis/go-ipdetect

Usage

provider, err := ipdetect.GetProvider(context.Background())
if err != nil {
    log.Fatal(err)
}

fmt.Println("Provider:", provider.Name())

ip, err := provider.GetPublicIPv4(context.Background())
if err != nil {
    log.Fatal(err)
}
fmt.Println("Public IP:", ip)

What it does

  • Detects which cloud you're running on (AWS, GCP, Azure, etc.)
  • Queries the metadata service for your default public IPv4 and IPv6
  • Falls back to route inspection for bare metal or local development

Supported Providers

  • AWS
  • GCP
  • Azure
  • Oracle Cloud
  • OpenStack
  • Bare metal / VPS / Local development (via route inspection)

License

MIT License - see LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("not found")
)

Functions

This section is empty.

Types

type AWSProvider

type AWSProvider struct {
	// contains filtered or unexported fields
}

func (*AWSProvider) GetIMDSv2Token

func (p *AWSProvider) GetIMDSv2Token(ctx context.Context) (string, error)

GetIMDSv2Token gets an IMDSv2 token for secure metadata access

func (*AWSProvider) GetPrimaryIPv6

func (p *AWSProvider) GetPrimaryIPv6(ctx context.Context) (netip.Addr, error)

GetPrimaryIPv6 returns the primary IPv6 address

func (*AWSProvider) GetPublicIPv4

func (p *AWSProvider) GetPublicIPv4(ctx context.Context) (netip.Addr, error)

GetPublicIPv4 returns the public IPv4 address

func (*AWSProvider) Name

func (p *AWSProvider) Name() string

type AzureProvider

type AzureProvider struct {
	// contains filtered or unexported fields
}

func (*AzureProvider) GetPrimaryIPv6

func (p *AzureProvider) GetPrimaryIPv6(ctx context.Context) (netip.Addr, error)

func (*AzureProvider) GetPublicIPv4

func (p *AzureProvider) GetPublicIPv4(ctx context.Context) (netip.Addr, error)

func (*AzureProvider) Name

func (p *AzureProvider) Name() string

type Config

type Config struct {
	// Timeout for metadata service requests. Defaults to 500 milliseconds.
	MetadataTimeout time.Duration
	// Use this destination IP to determine the default source IP for IPv4
	// when using the LocalProvider. Defaults to 8.8.8.8.
	IPv4RouteDst netip.Addr
	// Use this destination IP to determine the default source IP for IPv6
	// when using the LocalProvider. Defaults to 2001:4860:4860::8888.
	IPv6RouteDst netip.Addr
}

type GCPProvider

type GCPProvider struct {
	// contains filtered or unexported fields
}

func (*GCPProvider) GetPrimaryIPv6

func (p *GCPProvider) GetPrimaryIPv6(ctx context.Context) (netip.Addr, error)

func (*GCPProvider) GetPublicIPv4

func (p *GCPProvider) GetPublicIPv4(ctx context.Context) (netip.Addr, error)

func (*GCPProvider) Name

func (p *GCPProvider) Name() string

type LocalProvider

type LocalProvider struct{}

func (*LocalProvider) GetPrimaryIPv6

func (p *LocalProvider) GetPrimaryIPv6(ctx context.Context) (netip.Addr, error)

func (*LocalProvider) GetPublicIPv4

func (p *LocalProvider) GetPublicIPv4(ctx context.Context) (netip.Addr, error)

func (*LocalProvider) Name

func (p *LocalProvider) Name() string

type OCIProvider

type OCIProvider struct {
	// contains filtered or unexported fields
}

func (*OCIProvider) GetPrimaryIPv6

func (p *OCIProvider) GetPrimaryIPv6(ctx context.Context) (netip.Addr, error)

func (*OCIProvider) GetPublicIPv4

func (p *OCIProvider) GetPublicIPv4(ctx context.Context) (netip.Addr, error)

func (*OCIProvider) Name

func (p *OCIProvider) Name() string

type OpenStackProvider

type OpenStackProvider struct {
	// contains filtered or unexported fields
}

func (*OpenStackProvider) GetPrimaryIPv6

func (p *OpenStackProvider) GetPrimaryIPv6(ctx context.Context) (netip.Addr, error)

func (*OpenStackProvider) GetPublicIPv4

func (p *OpenStackProvider) GetPublicIPv4(ctx context.Context) (netip.Addr, error)

func (*OpenStackProvider) Name

func (p *OpenStackProvider) Name() string

type Provider

type Provider interface {
	Name() string
	// GetPublicIPv4 returns the public IPv4 address of the instance
	GetPublicIPv4(ctx context.Context) (netip.Addr, error)
	// GetPrimaryIPv6 returns the primary IPv6 address of the instance
	GetPrimaryIPv6(ctx context.Context) (netip.Addr, error)
}

func GetProvider

func GetProvider(ctx context.Context) (Provider, error)

GetProvider detects the cloud provider and returns a Provider instance

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL