1
0
Fork 0

line ending fix

This commit is contained in:
Henrik Hautakoski 2025-10-12 23:52:30 +02:00
parent a0e4de3d19
commit 0c347312bd
26 changed files with 1053 additions and 1046 deletions

View file

@ -1,46 +1,46 @@
package http
import (
"context"
"io"
"net"
"net/http"
"strings"
httputils "dnsupdater/http"
"dnsupdater/ip/internal"
)
type Decoder func(io.Reader) ([]byte, 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 = io.ReadAll
}
body, err := s.Decoder(resp.Body)
if err != nil {
return nil, err
}
// Trim spaces and stuff.
ip_str := strings.TrimSpace(string(body))
return internal.ParseIP(ip_str)
}
package http
import (
"context"
"io"
"net"
"net/http"
"strings"
httputils "dnsupdater/http"
"dnsupdater/ip/internal"
)
type Decoder func(io.Reader) ([]byte, 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 = io.ReadAll
}
body, err := s.Decoder(resp.Body)
if err != nil {
return nil, err
}
// Trim spaces and stuff.
ip_str := strings.TrimSpace(string(body))
return internal.ParseIP(ip_str)
}

View file

@ -1,85 +1,85 @@
package http
import (
"context"
"net"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestService_Name(t *testing.T) {
s := Service{ServiceName: "my_service"}
assert.Equal(t, "my_service", s.Name())
}
func TestService_Lookup(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("255.240.85.2"))
assert.NoError(t, err)
}))
defer server.Close()
s := Service{Url: server.URL}
ip, err := s.Lookup(context.Background())
assert.NoError(t, err)
assert.Equal(t, net.IPv4(255, 240, 85, 2), ip)
}
func TestService_Lookup_WithHeaders(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
_, err := w.Write([]byte("125.74.233.13"))
assert.NoError(t, err)
}))
defer server.Close()
s := Service{
Url: server.URL,
Headers: http.Header{
"Content-Type": []string{"application/json"},
},
}
ip, err := s.Lookup(context.Background())
assert.NoError(t, err)
assert.Equal(t, net.IPv4(125, 74, 233, 13), ip)
}
func TestService_Lookup_HTTPError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
}))
defer server.Close()
s := Service{
Url: server.URL,
}
ip, err := s.Lookup(context.Background())
assert.EqualError(t, err, "HTTP Response: 404 Not Found")
assert.Nil(t, ip)
}
func TestService_Lookup_ParseError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("random_string"))
assert.NoError(t, err)
}))
defer server.Close()
s := Service{
Url: server.URL,
}
ip, err := s.Lookup(context.Background())
assert.EqualError(t, err, "invalid IP address: random_string")
assert.Nil(t, ip)
}
package http
import (
"context"
"net"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestService_Name(t *testing.T) {
s := Service{ServiceName: "my_service"}
assert.Equal(t, "my_service", s.Name())
}
func TestService_Lookup(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("255.240.85.2"))
assert.NoError(t, err)
}))
defer server.Close()
s := Service{Url: server.URL}
ip, err := s.Lookup(context.Background())
assert.NoError(t, err)
assert.Equal(t, net.IPv4(255, 240, 85, 2), ip)
}
func TestService_Lookup_WithHeaders(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
_, err := w.Write([]byte("125.74.233.13"))
assert.NoError(t, err)
}))
defer server.Close()
s := Service{
Url: server.URL,
Headers: http.Header{
"Content-Type": []string{"application/json"},
},
}
ip, err := s.Lookup(context.Background())
assert.NoError(t, err)
assert.Equal(t, net.IPv4(125, 74, 233, 13), ip)
}
func TestService_Lookup_HTTPError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
}))
defer server.Close()
s := Service{
Url: server.URL,
}
ip, err := s.Lookup(context.Background())
assert.EqualError(t, err, "HTTP Response: 404 Not Found")
assert.Nil(t, ip)
}
func TestService_Lookup_ParseError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("random_string"))
assert.NoError(t, err)
}))
defer server.Close()
s := Service{
Url: server.URL,
}
ip, err := s.Lookup(context.Background())
assert.EqualError(t, err, "invalid IP address: random_string")
assert.Nil(t, ip)
}

View file

@ -1,20 +1,20 @@
package resolver
import (
"encoding/json"
"io"
)
func JsonipDecoder(r io.Reader) ([]byte, 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
}
package resolver
import (
"encoding/json"
"io"
)
func JsonipDecoder(r io.Reader) ([]byte, 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
}

View file

@ -1,53 +1,53 @@
package resolver
import (
"net/http"
httpres "dnsupdater/ip/resolver/http"
)
var services []Service
func Provide(service Service) {
services = append(services, service)
}
func Get(name string) Service {
for _, service := range services {
if service.Name() == name {
return service
}
}
return nil
}
func init() {
Provide(&httpres.Service{
ServiceName: "jsonip",
Url: "https://jsonip.com",
Decoder: JsonipDecoder,
})
Provide(&httpres.Service{
ServiceName: "ifconfig.me",
Url: "https://ifconfig.me/ip",
})
Provide(&httpres.Service{
ServiceName: "ip.me",
Url: "https://ip.me",
Headers: http.Header{
"User-Agent": []string{"curl"},
},
})
Provide(&httpres.Service{
ServiceName: "ipecho",
Url: "http://ipecho.net/plain",
})
Provide(&httpres.Service{
ServiceName: "icanhazip",
Url: "https://icanhazip.com",
})
}
package resolver
import (
"net/http"
httpres "dnsupdater/ip/resolver/http"
)
var services []Service
func Provide(service Service) {
services = append(services, service)
}
func Get(name string) Service {
for _, service := range services {
if service.Name() == name {
return service
}
}
return nil
}
func init() {
Provide(&httpres.Service{
ServiceName: "jsonip",
Url: "https://jsonip.com",
Decoder: JsonipDecoder,
})
Provide(&httpres.Service{
ServiceName: "ifconfig.me",
Url: "https://ifconfig.me/ip",
})
Provide(&httpres.Service{
ServiceName: "ip.me",
Url: "https://ip.me",
Headers: http.Header{
"User-Agent": []string{"curl"},
},
})
Provide(&httpres.Service{
ServiceName: "ipecho",
Url: "http://ipecho.net/plain",
})
Provide(&httpres.Service{
ServiceName: "icanhazip",
Url: "https://icanhazip.com",
})
}

View file

@ -1,19 +1,19 @@
package mock
import (
"context"
"net"
)
type Service struct {
IP net.IP
Error error
}
func (s Service) Name() string {
return "mock"
}
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
return s.IP, s.Error
}
package mock
import (
"context"
"net"
)
type Service struct {
IP net.IP
Error error
}
func (s Service) Name() string {
return "mock"
}
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
return s.IP, s.Error
}

View file

@ -1,15 +1,15 @@
package resolver
import (
"context"
"net"
)
// Interface that IP Lookup Services must implement.
type Service interface {
// Get the name of the serivce
Name() string
// Lookup the public ip.
Lookup(ctx context.Context) (net.IP, error)
}
package resolver
import (
"context"
"net"
)
// Interface that IP Lookup Services must implement.
type Service interface {
// Get the name of the serivce
Name() string
// Lookup the public ip.
Lookup(ctx context.Context) (net.IP, error)
}