ipify

package
v1.130.4 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ipify provides a small client to resolve the current instance public IP address using the ipify service (https://www.ipify.org/).

Problem

Services running behind NAT, cloud load balancers, or dynamic outbound egress often need to discover their externally visible IP address at runtime (for allow-list updates, diagnostics, or registration workflows). Implementing this from scratch requires HTTP setup, timeout handling, status validation, and error fallback logic.

Solution

This package wraps those concerns in a focused Client API:

  • New creates a configurable ipify client.
  • Client.GetPublicIP performs the request and returns the resolved IP.

The default configuration uses:

Use WithURL, WithTimeout, WithHTTPClient, and WithErrorIP to adapt the client to custom endpoints, transport stacks, and fallback policies.

Error Fallback Behavior

When request creation, transport, status-code validation, or body reading fails, Client.GetPublicIP returns the configured error-IP value together with the error. By default the fallback string is empty, but it can be set (for example to "0.0.0.0") via WithErrorIP.

IPv6 Note

The default endpoint resolves standard ipify behavior. To force IPv6-capable resolution, point the client to `https://api64.ipify.org` via WithURL.

Benefits

ipify gives applications a minimal, testable, and timeout-safe way to discover public IP information without duplicating HTTP boilerplate.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client resolves public IP addresses through an ipify-compatible endpoint.

func New

func New(opts ...Option) (*Client, error)

New constructs an ipify client with validated configuration.

It applies options, initializes a default HTTP client when needed, and validates the configured API URL.

func (*Client) GetPublicIP

func (c *Client) GetPublicIP(ctx context.Context) (ip string, err error)

GetPublicIP resolves the instance public IP through the configured ipify endpoint.

On any request or response failure, it returns the configured fallback errorIP together with the error.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is the minimal HTTP transport contract used by Client.

type Option

type Option func(c *Client)

Option is the interface that allows to set client options.

func WithErrorIP

func WithErrorIP(s string) Option

WithErrorIP sets the fallback IP string returned on failures.

func WithHTTPClient

func WithHTTPClient(hc HTTPClient) Option

WithHTTPClient injects a custom HTTP client implementation.

This is useful for testing, tracing, proxies, or custom transports.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the request timeout used by GetPublicIP.

func WithURL

func WithURL(addr string) Option

WithURL sets the ipify service endpoint URL.

For IPv6-aware responses, use https://api64.ipify.org.

Jump to

Keyboard shortcuts

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