update-ip/resolve.go

34 lines
460 B
Go
Raw Normal View History

2024-07-24 15:38:56 +02:00
package updateip
import (
"io"
"net"
"net/http"
)
type Resolver interface {
CurrentIP() (net.IP, error)
}
func NewResolver() Resolver {
return new(Ipify)
}
type Ipify struct{}
func (Ipify) CurrentIP() (net.IP, error) {
r, err := http.Get("https://api.ipify.org")
if err != nil {
return nil, err
}
defer func() { _ = r.Body.Close() }()
b, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
return net.ParseIP(string(b)), nil
}