ip/resolver: use ip.ParseIP() helper function
This commit is contained in:
parent
2204be7777
commit
acf8976f53
3 changed files with 8 additions and 13 deletions
|
|
@ -7,6 +7,8 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"dnsupdater/ip"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
|
|
@ -44,9 +46,5 @@ func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
||||||
// Trim spaces and stuff.
|
// Trim spaces and stuff.
|
||||||
ip_str := strings.TrimSpace(string(body))
|
ip_str := strings.TrimSpace(string(body))
|
||||||
|
|
||||||
ip := net.ParseIP(ip_str)
|
return ip.ParseIP(ip_str)
|
||||||
if ip == nil {
|
|
||||||
err = fmt.Errorf("Failed to parse ip: %s", ip_str)
|
|
||||||
}
|
|
||||||
return ip, err
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,6 @@ func TestService_Lookup_ParseError(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := s.Lookup(context.Background())
|
ip, err := s.Lookup(context.Background())
|
||||||
assert.EqualError(t, err, "Failed to parse ip: random_string")
|
assert.EqualError(t, err, "invalid IP address: random_string")
|
||||||
assert.Nil(t, ip)
|
assert.Nil(t, ip)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"dnsupdater/ip"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
|
|
@ -22,12 +24,7 @@ func (s Service) Name() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
||||||
req, err := http.NewRequestWithContext(ctx, "GET", s.url, nil)
|
resp, err := httphelper.Get(ctx, s.url, nil)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -41,5 +38,5 @@ func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return net.ParseIP(v.Ip), err
|
return ip.ParseIP(v.Ip)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue