34 lines
460 B
Go
34 lines
460 B
Go
|
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
|
||
|
}
|