-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
108 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"net" | ||
) | ||
|
||
type IpAddress struct { | ||
Address net.IP `json:"ip"` | ||
Family int `json:"family"` | ||
} | ||
|
||
type IpGetter interface { | ||
GetIp(ctx context.Context, host string) ([]IpAddress, error) | ||
} | ||
|
||
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 (l *NetIp) GetIp(ctx context.Context, host string) ([]IpAddress, error) { | ||
resolver := &net.Resolver{ | ||
PreferGo: true, | ||
} | ||
ip4, err := resolver.LookupIP(ctx, "ip4", host) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ip6, err := resolver.LookupIP(ctx, "ip6", host) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var ipAddresses []IpAddress | ||
for _, ip := range ip4 { | ||
ipAddresses = append(ipAddresses, IpAddress{Address: ip, Family: 4}) | ||
} | ||
for _, ip := range ip6 { | ||
ipAddresses = append(ipAddresses, IpAddress{Address: ip, Family: 6}) | ||
} | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
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 | ||
})) | ||
actual, err := i.Lookup(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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters