mirror of
https://github.com/luxfi/net.git
synced 2026-10-30 15:24:11 +00:00
Move IP and endpoint types from ips/ to endpoints/ for clearer naming. This makes the package purpose more obvious and aligns with usage patterns.
45 lines
988 B
Go
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)
|
|
}
|