Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RF: add lookup interface to getIP #56

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions checks/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
BlockList *BlockList
Carbon *Carbon
Headers *Headers
IpAddress *Ip
IpAddress *NetIp
LegacyRank *LegacyRank
LinkedPages *LinkedPages
Rank *Rank
Expand All @@ -28,7 +28,7 @@
BlockList: NewBlockList(&ip.NetDNSLookup{}),
Carbon: NewCarbon(client),
Headers: NewHeaders(client),
IpAddress: NewIp(NewNetIp()),
IpAddress: NewNetIp(&ip.NetLookup{}),

Check warning on line 31 in checks/checks.go

View check run for this annotation

Codecov / codecov/patch

checks/checks.go#L31

Added line #L31 was not covered by tests
LegacyRank: NewLegacyRank(legacyrank.NewInMemoryStore()),
LinkedPages: NewLinkedPages(client),
Rank: NewRank(client),
Expand Down
41 changes: 10 additions & 31 deletions checks/getIP.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,30 @@ package checks
import (
"context"
"net"

"github.com/xray-web/web-check-api/checks/clients/ip"
)

type IpAddress struct {
Address net.IP `json:"ip"`
Family int `json:"family"`
}

type IpGetter interface {
GetIp(ctx context.Context, host string) ([]IpAddress, error)
type NetIp struct {
lookup ip.Lookup
}

type IpGetterFunc func(ctx context.Context, host string) ([]IpAddress, error)

func (f IpGetterFunc) GetIp(ctx context.Context, host string) ([]IpAddress, error) {
return f(ctx, host)
}

type NetIp struct{}

func NewNetIp() *NetIp {
return &NetIp{}
func NewNetIp(lookup ip.Lookup) *NetIp {
return &NetIp{lookup: lookup}
}

func (l *NetIp) GetIp(ctx context.Context, host string) ([]IpAddress, error) {
resolver := &net.Resolver{
PreferGo: true,
}
ip4, err := resolver.LookupIP(ctx, "ip4", host)
ip4, err := l.lookup.LookupIP(ctx, "ip4", host)
if err != nil {
return nil, err
// do nothing
}
ip6, err := resolver.LookupIP(ctx, "ip6", host)
if err != nil {
ip6, err := l.lookup.LookupIP(ctx, "ip6", host)
if err != nil && len(ip4) == 0 && len(ip6) == 0 {
return nil, err
}

Expand All @@ -49,15 +40,3 @@ func (l *NetIp) GetIp(ctx context.Context, host string) ([]IpAddress, error) {

return ipAddresses, nil
}

type Ip struct {
getter IpGetter
}

func NewIp(l IpGetter) *Ip {
return &Ip{getter: l}
}

func (i *Ip) Lookup(ctx context.Context, host string) ([]IpAddress, error) {
return i.getter.GetIp(ctx, host)
}
17 changes: 5 additions & 12 deletions checks/getIP_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,17 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/xray-web/web-check-api/checks/clients/ip"
)

func TestLookup(t *testing.T) {
t.Parallel()

ipAddresses := []IpAddress{
{net.ParseIP("216.58.201.110"), 4},
{net.ParseIP("2a00:1450:4009:826::200e"), 6},
}
i := NewIp(IpGetterFunc(func(ctx context.Context, host string) ([]IpAddress, error) {
return ipAddresses, nil
n := NewNetIp(ip.LookupFunc(func(ctx context.Context, network string, host string) ([]net.IP, error) {
return []net.IP{net.ParseIP("216.58.201.110")}, nil
}))
actual, err := i.Lookup(context.Background(), "google.com")
actual, err := n.GetIp(context.Background(), "google.com")
assert.NoError(t, err)

assert.Equal(t, ipAddresses[0].Address, actual[0].Address)
assert.Equal(t, 4, actual[0].Family)

assert.Equal(t, ipAddresses[1].Address, actual[1].Address)
assert.Equal(t, 6, actual[1].Family)
assert.Contains(t, actual, IpAddress{Address: net.ParseIP("216.58.201.110"), Family: 4})
}
4 changes: 2 additions & 2 deletions handlers/getIP.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"github.com/xray-web/web-check-api/checks"
)

func HandleGetIP(i *checks.Ip) http.Handler {
func HandleGetIP(i *checks.NetIp) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rawURL, err := extractURL(r)
if err != nil {
JSONError(w, ErrMissingURLParameter, http.StatusBadRequest)
return
}

result, err := i.Lookup(r.Context(), rawURL.Hostname())
result, err := i.GetIp(r.Context(), rawURL.Hostname())

Check warning on line 17 in handlers/getIP.go

View check run for this annotation

Codecov / codecov/patch

handlers/getIP.go#L17

Added line #L17 was not covered by tests
if err != nil {
JSONError(w, err, http.StatusInternalServerError)
return
Expand Down