1
0
Fork 0
mirror of https://gitlab.com/pnx-tools/dns-updater.git synced 2026-06-16 05:54:56 +02:00
dns-updater/ip/resolver/http/service.go

50 lines
959 B
Go

package http
import (
"bufio"
"context"
"io"
"net"
"net/http"
"strings"
httputils "dnsupdater/http"
"dnsupdater/ip/internal"
)
type Decoder func(io.Reader) (string, error)
type Service struct {
ServiceName string
Url string
Headers http.Header
Decoder Decoder
}
func (s Service) Name() string {
return s.ServiceName
}
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
resp, err := httputils.Get(ctx, s.Url, s.Headers)
if err != nil {
return nil, err
}
if s.Decoder == nil {
s.Decoder = func(r io.Reader) (string, error) {
line, err := bufio.NewReader(r).ReadString('\n')
if err == nil || err == io.EOF {
line = strings.TrimRight(line, "\r\n")
line = strings.TrimSpace(line)
}
return line, err
}
}
body, err := s.Decoder(resp.Body)
if err != nil && err != io.EOF {
return nil, err
}
return internal.ParseIP(string(body))
}