ipify

package
v1.153.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 8 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/).

The Client API:

  • New creates a configurable ipify client.
  • Client.GetPublicIP performs the request and returns the resolved IP.
  • Client.HealthCheck probes endpoint reachability for parity with the other nurago HTTP clients.

The default configuration uses:

Use WithURL, WithTimeout, WithHTTPClient, and WithErrorIP to adapt the client to custom endpoints, transport stacks, and fallback policies. A non-positive timeout is clamped to the default; an API URL that is missing, unparseable, or not http/https with a host is rejected by New.

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. The response body is whitespace-trimmed and an empty body is treated as a failure.

Errors

Configuration problems are reported at construction time with errors matching the exported sentinel ErrInvalidOptions. A nil or empty response body from the endpoint surfaces as ErrInvalidResponse. Match the sentinels with errors.Is.

IPv6 Note

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

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidOptions is returned by New when the configured API URL is
	// missing, unparseable, or does not use an http/https scheme with a host.
	ErrInvalidOptions = errors.New("ipify: missing or invalid client options")

	// ErrInvalidResponse is returned by GetPublicIP when the endpoint returns a
	// nil response body or an empty (whitespace-only) payload.
	ErrInvalidResponse = errors.New("ipify: invalid response")
)

Sentinel errors returned by the package. Match them with errors.Is so callers can distinguish configuration problems from response failures.

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. A non-positive timeout is silently clamped to the default rather than rejected, so a misconfigured timeout cannot make every request fail with an already-expired context. An API URL that is missing, unparseable, or that does not use an http/https scheme with a host is rejected with an error matching ErrInvalidOptions.

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.

func (*Client) HealthCheck

func (c *Client) HealthCheck(ctx context.Context) error

HealthCheck verifies that the configured ipify endpoint is reachable and returns a usable public IP.

It performs a GetPublicIP call, discards the resolved address, and returns only the error. It exists for parity with the other nurago HTTP clients that expose a HealthCheck probe.

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