line ending fix
This commit is contained in:
parent
a0e4de3d19
commit
0c347312bd
26 changed files with 1053 additions and 1046 deletions
86
ip/cache.go
86
ip/cache.go
|
|
@ -1,43 +1,43 @@
|
|||
package ip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
type CacheDefaultCallback func(name string) (net.IP, error)
|
||||
|
||||
type Cache struct {
|
||||
items map[string]net.IP
|
||||
}
|
||||
|
||||
func NewCache() *Cache {
|
||||
return &Cache{
|
||||
items: make(map[string]net.IP),
|
||||
}
|
||||
}
|
||||
|
||||
func (c Cache) Get(name string) (net.IP, error) {
|
||||
// Return cached entry.
|
||||
if cached, ok := c.items[name]; ok {
|
||||
return cached, nil
|
||||
}
|
||||
return nil, errors.New("key did not exist")
|
||||
}
|
||||
|
||||
func (c Cache) GetWithDefault(name string, callback CacheDefaultCallback) (net.IP, error) {
|
||||
// Return cached entry.
|
||||
if cached, ok := c.items[name]; ok {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
ip, err := callback(name)
|
||||
if err == nil {
|
||||
c.Set(name, ip)
|
||||
}
|
||||
return ip, err
|
||||
}
|
||||
|
||||
func (c *Cache) Set(name string, ip net.IP) {
|
||||
c.items[name] = ip
|
||||
}
|
||||
package ip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
type CacheDefaultCallback func(name string) (net.IP, error)
|
||||
|
||||
type Cache struct {
|
||||
items map[string]net.IP
|
||||
}
|
||||
|
||||
func NewCache() *Cache {
|
||||
return &Cache{
|
||||
items: make(map[string]net.IP),
|
||||
}
|
||||
}
|
||||
|
||||
func (c Cache) Get(name string) (net.IP, error) {
|
||||
// Return cached entry.
|
||||
if cached, ok := c.items[name]; ok {
|
||||
return cached, nil
|
||||
}
|
||||
return nil, errors.New("key did not exist")
|
||||
}
|
||||
|
||||
func (c Cache) GetWithDefault(name string, callback CacheDefaultCallback) (net.IP, error) {
|
||||
// Return cached entry.
|
||||
if cached, ok := c.items[name]; ok {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
ip, err := callback(name)
|
||||
if err == nil {
|
||||
c.Set(name, ip)
|
||||
}
|
||||
return ip, err
|
||||
}
|
||||
|
||||
func (c *Cache) Set(name string, ip net.IP) {
|
||||
c.items[name] = ip
|
||||
}
|
||||
|
|
|
|||
152
ip/cache_test.go
152
ip/cache_test.go
|
|
@ -1,76 +1,76 @@
|
|||
package ip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func defaultCallback(t *testing.T, expected_name string, ip net.IP, err error) CacheDefaultCallback {
|
||||
return func(name string) (net.IP, error) {
|
||||
assert.Equal(t, expected_name, name)
|
||||
return ip, err
|
||||
}
|
||||
}
|
||||
|
||||
func dontCallDefaultCallback(t *testing.T) CacheDefaultCallback {
|
||||
return func(name string) (net.IP, error) {
|
||||
t.Error("Should not have been called")
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestCache_Get(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
c *Cache
|
||||
iface string
|
||||
want net.IP
|
||||
wantErr bool
|
||||
}{
|
||||
{"Exists in cache", &Cache{items: map[string]net.IP{"eth0": net.IPv4(10, 4, 0, 1)}}, "eth0", net.IPv4(10, 4, 0, 1), false},
|
||||
{"Did not exist in cache", &Cache{items: map[string]net.IP{}}, "eth0", nil, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := tt.c.Get(tt.iface)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Cache.Get() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Cache.Get() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCache_GetWithDefault(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
c *Cache
|
||||
def CacheDefaultCallback
|
||||
iface string
|
||||
want net.IP
|
||||
wantErr bool
|
||||
}{
|
||||
{"Exists in cache", &Cache{items: map[string]net.IP{"eth0": net.IPv4(10, 4, 0, 1)}}, dontCallDefaultCallback(t), "eth0", net.IPv4(10, 4, 0, 1), false},
|
||||
{"Did not exists in cache", NewCache(), defaultCallback(t, "eth1", net.IPv4(192, 172, 44, 25), nil), "eth1", net.IPv4(192, 172, 44, 25), false},
|
||||
{"Callback returns error", NewCache(), defaultCallback(t, "eth1", nil, errors.New("some error")), "eth1", nil, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := tt.c.GetWithDefault(tt.iface, tt.def)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Cache.Get() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Cache.Get() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
package ip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func defaultCallback(t *testing.T, expected_name string, ip net.IP, err error) CacheDefaultCallback {
|
||||
return func(name string) (net.IP, error) {
|
||||
assert.Equal(t, expected_name, name)
|
||||
return ip, err
|
||||
}
|
||||
}
|
||||
|
||||
func dontCallDefaultCallback(t *testing.T) CacheDefaultCallback {
|
||||
return func(name string) (net.IP, error) {
|
||||
t.Error("Should not have been called")
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestCache_Get(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
c *Cache
|
||||
iface string
|
||||
want net.IP
|
||||
wantErr bool
|
||||
}{
|
||||
{"Exists in cache", &Cache{items: map[string]net.IP{"eth0": net.IPv4(10, 4, 0, 1)}}, "eth0", net.IPv4(10, 4, 0, 1), false},
|
||||
{"Did not exist in cache", &Cache{items: map[string]net.IP{}}, "eth0", nil, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := tt.c.Get(tt.iface)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Cache.Get() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Cache.Get() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCache_GetWithDefault(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
c *Cache
|
||||
def CacheDefaultCallback
|
||||
iface string
|
||||
want net.IP
|
||||
wantErr bool
|
||||
}{
|
||||
{"Exists in cache", &Cache{items: map[string]net.IP{"eth0": net.IPv4(10, 4, 0, 1)}}, dontCallDefaultCallback(t), "eth0", net.IPv4(10, 4, 0, 1), false},
|
||||
{"Did not exists in cache", NewCache(), defaultCallback(t, "eth1", net.IPv4(192, 172, 44, 25), nil), "eth1", net.IPv4(192, 172, 44, 25), false},
|
||||
{"Callback returns error", NewCache(), defaultCallback(t, "eth1", nil, errors.New("some error")), "eth1", nil, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := tt.c.GetWithDefault(tt.iface, tt.def)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Cache.Get() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Cache.Get() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,46 +1,46 @@
|
|||
package ip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
func GetInterfaceIP(iface_name string) (net.IP, error) {
|
||||
ip := net.IP{}
|
||||
iface, err := net.InterfaceByName(iface_name)
|
||||
if err != nil {
|
||||
return ip, err
|
||||
}
|
||||
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
return ip, err
|
||||
}
|
||||
|
||||
return GetPublicIp(addrs)
|
||||
}
|
||||
|
||||
func GetPublicIp(list []net.Addr) (net.IP, error) {
|
||||
for _, addr := range list {
|
||||
ip, err := AddrToIP(addr)
|
||||
if err == nil && !ip.IsPrivate() {
|
||||
return ip, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("no public ip found on interface")
|
||||
}
|
||||
|
||||
func AddrToIP(addr net.Addr) (net.IP, error) {
|
||||
switch v := addr.(type) {
|
||||
case *net.IPNet:
|
||||
return v.IP, nil
|
||||
case *net.IPAddr:
|
||||
return v.IP, nil
|
||||
case *net.UDPAddr:
|
||||
return v.IP, nil
|
||||
case *net.TCPAddr:
|
||||
return v.IP, nil
|
||||
}
|
||||
return nil, errors.New("could not find ip")
|
||||
}
|
||||
package ip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
func GetInterfaceIP(iface_name string) (net.IP, error) {
|
||||
ip := net.IP{}
|
||||
iface, err := net.InterfaceByName(iface_name)
|
||||
if err != nil {
|
||||
return ip, err
|
||||
}
|
||||
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
return ip, err
|
||||
}
|
||||
|
||||
return GetPublicIp(addrs)
|
||||
}
|
||||
|
||||
func GetPublicIp(list []net.Addr) (net.IP, error) {
|
||||
for _, addr := range list {
|
||||
ip, err := AddrToIP(addr)
|
||||
if err == nil && !ip.IsPrivate() {
|
||||
return ip, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("no public ip found on interface")
|
||||
}
|
||||
|
||||
func AddrToIP(addr net.Addr) (net.IP, error) {
|
||||
switch v := addr.(type) {
|
||||
case *net.IPNet:
|
||||
return v.IP, nil
|
||||
case *net.IPAddr:
|
||||
return v.IP, nil
|
||||
case *net.UDPAddr:
|
||||
return v.IP, nil
|
||||
case *net.TCPAddr:
|
||||
return v.IP, nil
|
||||
}
|
||||
return nil, errors.New("could not find ip")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,69 +1,69 @@
|
|||
package ip
|
||||
|
||||
import (
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetPublicIp(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
list []string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{"empty", []string{}, "", true},
|
||||
{"find", []string{"99.140.96.132"}, "99.140.96.132", false},
|
||||
{"findfirst", []string{"23.114.115.197", "251.78.128.148"}, "23.114.115.197", false},
|
||||
{"dontfindprivate", []string{"192.168.0.22", "88.12.32.44"}, "88.12.32.44", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
list := []net.Addr{}
|
||||
for _, item := range tt.list {
|
||||
list = append(list, &net.IPAddr{IP: net.ParseIP(item)})
|
||||
}
|
||||
|
||||
want := net.ParseIP(tt.want)
|
||||
|
||||
got, err := GetPublicIp(list)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetPublicIp() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("GetPublicIp() = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddrToIP(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
addr net.Addr
|
||||
want net.IP
|
||||
wantErr bool
|
||||
}{
|
||||
{"IPNet", &net.IPNet{IP: net.IPv4(177, 171, 44, 1)}, net.IPv4(177, 171, 44, 1), false},
|
||||
{"IPAddr", &net.IPAddr{IP: net.IPv4(240, 23, 119, 171)}, net.IPv4(240, 23, 119, 171), false},
|
||||
{"TCPAddr", &net.TCPAddr{IP: net.IPv4(139, 231, 35, 221)}, net.IPv4(139, 231, 35, 221), false},
|
||||
{"UDPAddr", &net.UDPAddr{IP: net.IPv4(167, 147, 140, 119)}, net.IPv4(167, 147, 140, 119), false},
|
||||
{"UnixAddr", &net.UnixAddr{}, nil, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := AddrToIP(tt.addr)
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("AddrToIP() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("AddrToIP() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
package ip
|
||||
|
||||
import (
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetPublicIp(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
list []string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{"empty", []string{}, "", true},
|
||||
{"find", []string{"99.140.96.132"}, "99.140.96.132", false},
|
||||
{"findfirst", []string{"23.114.115.197", "251.78.128.148"}, "23.114.115.197", false},
|
||||
{"dontfindprivate", []string{"192.168.0.22", "88.12.32.44"}, "88.12.32.44", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
list := []net.Addr{}
|
||||
for _, item := range tt.list {
|
||||
list = append(list, &net.IPAddr{IP: net.ParseIP(item)})
|
||||
}
|
||||
|
||||
want := net.ParseIP(tt.want)
|
||||
|
||||
got, err := GetPublicIp(list)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetPublicIp() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("GetPublicIp() = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddrToIP(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
addr net.Addr
|
||||
want net.IP
|
||||
wantErr bool
|
||||
}{
|
||||
{"IPNet", &net.IPNet{IP: net.IPv4(177, 171, 44, 1)}, net.IPv4(177, 171, 44, 1), false},
|
||||
{"IPAddr", &net.IPAddr{IP: net.IPv4(240, 23, 119, 171)}, net.IPv4(240, 23, 119, 171), false},
|
||||
{"TCPAddr", &net.TCPAddr{IP: net.IPv4(139, 231, 35, 221)}, net.IPv4(139, 231, 35, 221), false},
|
||||
{"UDPAddr", &net.UDPAddr{IP: net.IPv4(167, 147, 140, 119)}, net.IPv4(167, 147, 140, 119), false},
|
||||
{"UnixAddr", &net.UnixAddr{}, nil, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := AddrToIP(tt.addr)
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("AddrToIP() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("AddrToIP() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
package internal
|
||||
|
||||
import "net"
|
||||
|
||||
func ParseIP(s string) (net.IP, error) {
|
||||
var err error = nil
|
||||
ip := net.ParseIP(s)
|
||||
if ip == nil {
|
||||
err = &net.ParseError{
|
||||
Type: "IP address",
|
||||
Text: s,
|
||||
}
|
||||
}
|
||||
return ip, err
|
||||
}
|
||||
package internal
|
||||
|
||||
import "net"
|
||||
|
||||
func ParseIP(s string) (net.IP, error) {
|
||||
var err error = nil
|
||||
ip := net.ParseIP(s)
|
||||
if ip == nil {
|
||||
err = &net.ParseError{
|
||||
Type: "IP address",
|
||||
Text: s,
|
||||
}
|
||||
}
|
||||
return ip, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,35 +1,35 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseIP(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want net.IP
|
||||
wantErr bool
|
||||
}{
|
||||
{"localhost", "127.0.0.1", net.IPv4(127, 0, 0, 1), false},
|
||||
{"Private#1", "10.4.0.11", net.IPv4(10, 4, 0, 11), false},
|
||||
{"Private#2", "192.168.1.12", net.IPv4(192, 168, 1, 12), false},
|
||||
{"Public#1", "82.249.10.254", net.IPv4(82, 249, 10, 254), false},
|
||||
{"Public#2", "57.167.50.222", net.IPv4(57, 167, 50, 222), false},
|
||||
{"Invalid", "xx", nil, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := ParseIP(tt.input)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ParseIP() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("ParseIP() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
package internal
|
||||
|
||||
import (
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseIP(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want net.IP
|
||||
wantErr bool
|
||||
}{
|
||||
{"localhost", "127.0.0.1", net.IPv4(127, 0, 0, 1), false},
|
||||
{"Private#1", "10.4.0.11", net.IPv4(10, 4, 0, 11), false},
|
||||
{"Private#2", "192.168.1.12", net.IPv4(192, 168, 1, 12), false},
|
||||
{"Public#1", "82.249.10.254", net.IPv4(82, 249, 10, 254), false},
|
||||
{"Public#2", "57.167.50.222", net.IPv4(57, 167, 50, 222), false},
|
||||
{"Invalid", "xx", nil, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := ParseIP(tt.input)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ParseIP() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("ParseIP() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue