39 lines
550 B
Go
39 lines
550 B
Go
package ifconfigme
|
|
|
|
import (
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type Service struct {
|
|
url string
|
|
}
|
|
|
|
func New() *Service {
|
|
return &Service{
|
|
url: "https://ifconfig.me/ip",
|
|
}
|
|
}
|
|
|
|
func (s Service) Name() string {
|
|
return "ifconfig.me"
|
|
}
|
|
|
|
func (s Service) Lookup() (net.IP, error) {
|
|
resp, err := http.DefaultClient.Get(s.url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Trim spaces and stuff.
|
|
ip_str := strings.TrimSpace(string(body))
|
|
|
|
return net.ParseIP(ip_str), err
|
|
}
|