utils

package
v1.0.15 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPublicIP added in v1.0.5

func GetPublicIP(ctx context.Context) (ipv4, ipv6 string, err error)

GetPublicIP fetches the current public IPv4 and IPv6 addresses. It queries multiple providers to increase reliability and returns empty strings if an address cannot be determined.

func GetPublicIPv4 added in v1.0.9

func GetPublicIPv4(ctx context.Context) (string, error)

GetPublicIPv4 fetches the current public IPv4 address. It queries multiple providers to increase reliability and returns an empty string if the address cannot be determined.

func GetPublicIPv6 added in v1.0.9

func GetPublicIPv6(ctx context.Context) (string, error)

GetPublicIPv6 fetches the current public IPv6 address. It queries multiple providers to increase reliability and returns an empty string if the address cannot be determined.

func PrintHeader

func PrintHeader(title string)

PrintHeader prints a section header with underline

func Retry added in v1.0.8

func Retry[T any](ctx context.Context, opts RetryOptions, fn func(context.Context) (value T, retry bool, err error)) (T, error)

Retry calls fn up to opts.Attempts times.

fn should return:

  • value: the latest value
  • retry: whether to try again
  • err: an error to return if retry is false, or the last error seen

If retry is true and attempts are exhausted, Retry returns opts.ExceededError.

Types

type ExternalHTTPProvider added in v1.0.10

type ExternalHTTPProvider struct{}

func NewExternalHTTPProvider added in v1.0.10

func NewExternalHTTPProvider() *ExternalHTTPProvider

func (*ExternalHTTPProvider) GetPublicIPv4 added in v1.0.10

func (p *ExternalHTTPProvider) GetPublicIPv4(ctx context.Context) (string, error)

func (*ExternalHTTPProvider) GetPublicIPv6 added in v1.0.10

func (p *ExternalHTTPProvider) GetPublicIPv6(ctx context.Context) (string, error)

func (*ExternalHTTPProvider) Name added in v1.0.10

func (p *ExternalHTTPProvider) Name() string

type FritzBoxSOAPProvider added in v1.0.10

type FritzBoxSOAPProvider struct {
	URL     string
	Timeout time.Duration
}

func NewFritzBoxSOAPProvider added in v1.0.10

func NewFritzBoxSOAPProvider() *FritzBoxSOAPProvider

func (*FritzBoxSOAPProvider) GetPublicIPv4 added in v1.0.10

func (p *FritzBoxSOAPProvider) GetPublicIPv4(ctx context.Context) (string, error)

func (*FritzBoxSOAPProvider) GetPublicIPv6 added in v1.0.10

func (p *FritzBoxSOAPProvider) GetPublicIPv6(ctx context.Context) (string, error)

func (*FritzBoxSOAPProvider) Name added in v1.0.10

func (p *FritzBoxSOAPProvider) Name() string

type PublicIPProvider added in v1.0.10

type PublicIPProvider interface {
	Name() string
}

PublicIPProvider is the minimal interface for IP providers. Providers can implement one or both of the family-specific interfaces below.

type PublicIPResolver added in v1.0.10

type PublicIPResolver struct {
	Providers []PublicIPProvider
}

PublicIPResolver tries multiple providers and returns the first non-empty value. This keeps the business logic (retry order, fallbacks) in one place.

func DefaultPublicIPResolver added in v1.0.10

func DefaultPublicIPResolver() *PublicIPResolver

func NewPublicIPResolver added in v1.0.10

func NewPublicIPResolver(providers ...PublicIPProvider) *PublicIPResolver

func PublicIPResolverFromEnv added in v1.0.10

func PublicIPResolverFromEnv() *PublicIPResolver

PublicIPResolverFromEnv builds a resolver based on environment variables.

Supported variables:

  • NAS_MANAGER_PUBLIC_IP_PROVIDERS: comma-separated provider names in order. Supported: fritzbox-soap, external-http Default: fritzbox-soap,external-http
  • NAS_MANAGER_FRITZBOX_WANIPCONN_URL: overrides the FritzBox WANIPConn1 control URL. Default: http://fritz.box:49000/igdupnp/control/WANIPConn1
  • NAS_MANAGER_FRITZBOX_TIMEOUT: FritzBox SOAP timeout (Go duration, e.g. 2s, 500ms). Default: 3s

func (*PublicIPResolver) GetPublicIPv4 added in v1.0.10

func (r *PublicIPResolver) GetPublicIPv4(ctx context.Context) (string, error)

func (*PublicIPResolver) GetPublicIPv6 added in v1.0.10

func (r *PublicIPResolver) GetPublicIPv6(ctx context.Context) (string, error)

type PublicIPv4Provider added in v1.0.10

type PublicIPv4Provider interface {
	PublicIPProvider
	GetPublicIPv4(ctx context.Context) (string, error)
}

type PublicIPv6Provider added in v1.0.10

type PublicIPv6Provider interface {
	PublicIPProvider
	GetPublicIPv6(ctx context.Context) (string, error)
}

type RetryOptions added in v1.0.8

type RetryOptions struct {
	Attempts  int
	BaseDelay time.Duration
	MaxDelay  time.Duration

	// Sleep defaults to time.Sleep. Useful for tests.
	Sleep func(time.Duration)

	// OnRetry is called before sleeping between attempts.
	// attempt is 1-based (i.e., the first retry is attempt=1).
	OnRetry func(attempt int, nextDelay time.Duration, lastErr error)

	// ExceededError allows customizing the error when retries are exhausted.
	ExceededError func(attempts int, lastErr error) error
}

Jump to

Keyboard shortcuts

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