Files
net/dynamicip/ifconfig_resolver.go
zeekay 304946e144 refactor: rename ips package to endpoints
Move IP and endpoint types from ips/ to endpoints/ for clearer naming.
This makes the package purpose more obvious and aligns with usage patterns.
2026-02-04 18:25:56 -08:00

45 lines
988 B
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package dynamicip
import (
"context"
"fmt"
"io"
"net/http"
"net/netip"
"strings"
"github.com/luxfi/net/endpoints"
)
var _ Resolver = (*ifConfigResolver)(nil)
// ifConfigResolver resolves our public IP using ifconfig's format.
type ifConfigResolver struct {
url string
}
func (r *ifConfigResolver) Resolve(ctx context.Context) (netip.Addr, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.url, nil)
if err != nil {
return netip.Addr{}, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return netip.Addr{}, err
}
defer resp.Body.Close()
ipBytes, err := io.ReadAll(resp.Body)
if err != nil {
// Drop any error to report the original error
return netip.Addr{}, fmt.Errorf("failed to read response from %q: %w", r.url, err)
}
ipStr := strings.TrimSpace(string(ipBytes))
return endpoints.ParseAddr(ipStr)
}