Adding ip/resolver/basic_http
This commit is contained in:
parent
9f344a2ae9
commit
2ddf2dadd7
2 changed files with 95 additions and 0 deletions
42
ip/resolver/basic_http/service.go
Normal file
42
ip/resolver/basic_http/service.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package basic_http
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
ServiceName string
|
||||
Url string
|
||||
Headers http.Header
|
||||
}
|
||||
|
||||
func (s Service) Name() string {
|
||||
return s.ServiceName
|
||||
}
|
||||
|
||||
func (s Service) Lookup() (net.IP, error) {
|
||||
req, err := http.NewRequest("GET", s.Url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header = s.Headers
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Trim spaces and stuff.
|
||||
ip_str := strings.TrimSpace(string(body))
|
||||
|
||||
return net.ParseIP(ip_str), err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue