mirror of
https://gitlab.com/pnx-tools/dns-updater.git
synced 2026-06-16 05:54:56 +02:00
resolver: make decoders return string instead of byte slice
This commit is contained in:
parent
ae79cd131e
commit
c36a83c9b8
3 changed files with 16 additions and 20 deletions
|
|
@ -5,16 +5,12 @@ import (
|
|||
"io"
|
||||
)
|
||||
|
||||
func Jsonip(r io.Reader) ([]byte, error) {
|
||||
func Jsonip(r io.Reader) (string, error) {
|
||||
var v struct {
|
||||
Ip string `json:"ip"`
|
||||
Location string `json:"geo-ip"`
|
||||
Help string `json:"API Help"`
|
||||
}
|
||||
var val []byte
|
||||
err := json.NewDecoder(r).Decode(&v)
|
||||
if err == nil {
|
||||
val = []byte(v.Ip)
|
||||
}
|
||||
return val, err
|
||||
return v.Ip, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,16 +5,12 @@ import (
|
|||
"io"
|
||||
)
|
||||
|
||||
func MyIP(r io.Reader) ([]byte, error) {
|
||||
func MyIP(r io.Reader) (string, error) {
|
||||
var v struct {
|
||||
Ip string `json:"ip"`
|
||||
Country string `json:"country"`
|
||||
Cc string `json:"cc"`
|
||||
}
|
||||
var val []byte
|
||||
err := json.NewDecoder(r).Decode(&v)
|
||||
if err == nil {
|
||||
val = []byte(v.Ip)
|
||||
}
|
||||
return val, err
|
||||
return v.Ip, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
|
|
@ -11,7 +12,7 @@ import (
|
|||
"dnsupdater/ip/internal"
|
||||
)
|
||||
|
||||
type Decoder func(io.Reader) ([]byte, error)
|
||||
type Decoder func(io.Reader) (string, error)
|
||||
|
||||
type Service struct {
|
||||
ServiceName string
|
||||
|
|
@ -31,16 +32,19 @@ func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
|||
}
|
||||
|
||||
if s.Decoder == nil {
|
||||
s.Decoder = io.ReadAll
|
||||
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 {
|
||||
if err != nil && err != io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Trim spaces and stuff.
|
||||
ip_str := strings.TrimSpace(string(body))
|
||||
|
||||
return internal.ParseIP(ip_str)
|
||||
return internal.ParseIP(string(body))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue