Skip to content

Commit 9e2207a

Browse files
authored
rf: add lookup interface to getIP (#56)
fix: only error when both ipv4 and ipv6 are empty
1 parent 6215b2c commit 9e2207a

File tree

4 files changed

+19
-47
lines changed

4 files changed

+19
-47
lines changed

checks/checks.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Checks struct {
1212
BlockList *BlockList
1313
Carbon *Carbon
1414
Headers *Headers
15-
IpAddress *Ip
15+
IpAddress *NetIp
1616
LegacyRank *LegacyRank
1717
LinkedPages *LinkedPages
1818
Rank *Rank
@@ -28,7 +28,7 @@ func NewChecks() *Checks {
2828
BlockList: NewBlockList(&ip.NetDNSLookup{}),
2929
Carbon: NewCarbon(client),
3030
Headers: NewHeaders(client),
31-
IpAddress: NewIp(NewNetIp()),
31+
IpAddress: NewNetIp(&ip.NetLookup{}),
3232
LegacyRank: NewLegacyRank(legacyrank.NewInMemoryStore()),
3333
LinkedPages: NewLinkedPages(client),
3434
Rank: NewRank(client),

checks/getIP.go

+10-31
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,30 @@ package checks
33
import (
44
"context"
55
"net"
6+
7+
"github.com/xray-web/web-check-api/checks/clients/ip"
68
)
79

810
type IpAddress struct {
911
Address net.IP `json:"ip"`
1012
Family int `json:"family"`
1113
}
1214

13-
type IpGetter interface {
14-
GetIp(ctx context.Context, host string) ([]IpAddress, error)
15+
type NetIp struct {
16+
lookup ip.Lookup
1517
}
1618

17-
type IpGetterFunc func(ctx context.Context, host string) ([]IpAddress, error)
18-
19-
func (f IpGetterFunc) GetIp(ctx context.Context, host string) ([]IpAddress, error) {
20-
return f(ctx, host)
21-
}
22-
23-
type NetIp struct{}
24-
25-
func NewNetIp() *NetIp {
26-
return &NetIp{}
19+
func NewNetIp(lookup ip.Lookup) *NetIp {
20+
return &NetIp{lookup: lookup}
2721
}
2822

2923
func (l *NetIp) GetIp(ctx context.Context, host string) ([]IpAddress, error) {
30-
resolver := &net.Resolver{
31-
PreferGo: true,
32-
}
33-
ip4, err := resolver.LookupIP(ctx, "ip4", host)
24+
ip4, err := l.lookup.LookupIP(ctx, "ip4", host)
3425
if err != nil {
35-
return nil, err
26+
// do nothing
3627
}
37-
ip6, err := resolver.LookupIP(ctx, "ip6", host)
38-
if err != nil {
28+
ip6, err := l.lookup.LookupIP(ctx, "ip6", host)
29+
if err != nil && len(ip4) == 0 && len(ip6) == 0 {
3930
return nil, err
4031
}
4132

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

5041
return ipAddresses, nil
5142
}
52-
53-
type Ip struct {
54-
getter IpGetter
55-
}
56-
57-
func NewIp(l IpGetter) *Ip {
58-
return &Ip{getter: l}
59-
}
60-
61-
func (i *Ip) Lookup(ctx context.Context, host string) ([]IpAddress, error) {
62-
return i.getter.GetIp(ctx, host)
63-
}

checks/getIP_test.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,17 @@ import (
66
"testing"
77

88
"github.com/stretchr/testify/assert"
9+
"github.com/xray-web/web-check-api/checks/clients/ip"
910
)
1011

1112
func TestLookup(t *testing.T) {
1213
t.Parallel()
1314

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

24-
assert.Equal(t, ipAddresses[0].Address, actual[0].Address)
25-
assert.Equal(t, 4, actual[0].Family)
26-
27-
assert.Equal(t, ipAddresses[1].Address, actual[1].Address)
28-
assert.Equal(t, 6, actual[1].Family)
21+
assert.Contains(t, actual, IpAddress{Address: net.ParseIP("216.58.201.110"), Family: 4})
2922
}

handlers/getIP.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
"github.com/xray-web/web-check-api/checks"
77
)
88

9-
func HandleGetIP(i *checks.Ip) http.Handler {
9+
func HandleGetIP(i *checks.NetIp) http.Handler {
1010
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1111
rawURL, err := extractURL(r)
1212
if err != nil {
1313
JSONError(w, ErrMissingURLParameter, http.StatusBadRequest)
1414
return
1515
}
1616

17-
result, err := i.Lookup(r.Context(), rawURL.Hostname())
17+
result, err := i.GetIp(r.Context(), rawURL.Hostname())
1818
if err != nil {
1919
JSONError(w, err, http.StatusInternalServerError)
2020
return

0 commit comments

Comments
 (0)