-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
27 changed files
with
1,660 additions
and
1 deletion.
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,101 @@ | ||
// Copyright 2023 The Wait4X Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package a | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"wait4x.dev/v2/checker" | ||
) | ||
|
||
// Option configures an DNS A records | ||
type Option func(d *A) | ||
|
||
// A represents DNS A data structure | ||
type A struct { | ||
nameserver string | ||
address string | ||
expectedIPs []string | ||
resolver *net.Resolver | ||
} | ||
|
||
// New creates the DNS A checker | ||
func New(address string, opts ...Option) checker.Checker { | ||
d := &A{ | ||
address: address, | ||
resolver: net.DefaultResolver, | ||
} | ||
|
||
// apply the list of options to A | ||
for _, opt := range opts { | ||
opt(d) | ||
} | ||
|
||
// Nameserver settings. | ||
if d.nameserver != "" { | ||
d.resolver = &net.Resolver{ | ||
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { | ||
dialer := net.Dialer{} | ||
return dialer.DialContext(ctx, network, d.nameserver) | ||
}, | ||
} | ||
} | ||
|
||
return d | ||
} | ||
|
||
// WithNameServer overrides the default nameserver | ||
func WithNameServer(nameserver string) Option { | ||
return func(d *A) { | ||
d.nameserver = nameserver | ||
} | ||
} | ||
|
||
// WithExpectedIPV4s sets expected IPv4s | ||
func WithExpectedIPV4s(ips []string) Option { | ||
return func(d *A) { | ||
d.expectedIPs = ips | ||
} | ||
} | ||
|
||
// Identity returns the identity of the checker | ||
func (d *A) Identity() (string, error) { | ||
return d.address, nil | ||
} | ||
|
||
// Check checks A DNS records | ||
func (d *A) Check(ctx context.Context) (err error) { | ||
ips, err := d.resolver.LookupIP(ctx, "ip4", d.address) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, ip := range ips { | ||
if len(d.expectedIPs) == 0 { | ||
return nil | ||
} | ||
for _, expectedIP := range d.expectedIPs { | ||
if expectedIP == ip.String() { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return checker.NewExpectedError( | ||
"the A record value doesn't expect", nil, | ||
"actual", ips, "expect", d.expectedIPs, | ||
) | ||
} |
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,32 @@ | ||
package a | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"wait4x.dev/v2/checker" | ||
) | ||
|
||
const server = "wait4x.dev" | ||
|
||
func TestCheckExistenceA(t *testing.T) { | ||
d := New(server) | ||
assert.Nil(t, d.Check(context.Background())) | ||
} | ||
|
||
func TestCorrectA(t *testing.T) { | ||
d := New(server, WithExpectedIPV4s([]string{"172.67.154.180", "127.0.0.1"})) | ||
assert.Nil(t, d.Check(context.Background())) | ||
} | ||
|
||
func TestIncorrectA(t *testing.T) { | ||
var expectedError *checker.ExpectedError | ||
d := New(server, WithExpectedIPV4s([]string{"127.0.0.1"})) | ||
assert.ErrorAs(t, d.Check(context.Background()), &expectedError) | ||
} | ||
|
||
func TestCustomNSCorrectA(t *testing.T) { | ||
d := New(server, WithNameServer("8.8.8.8:53"), WithExpectedIPV4s([]string{"172.67.154.180"})) | ||
assert.Nil(t, d.Check(context.Background())) | ||
} |
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,101 @@ | ||
// Copyright 2023 The Wait4X Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package aaaa | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"wait4x.dev/v2/checker" | ||
) | ||
|
||
// Option configures an DNS AAAA records | ||
type Option func(d *AAAA) | ||
|
||
// AAAA represents DNS AAAA data structure | ||
type AAAA struct { | ||
nameserver string | ||
address string | ||
expectedIPs []string | ||
resolver *net.Resolver | ||
} | ||
|
||
// New creates the DNS AAAA checker | ||
func New(address string, opts ...Option) checker.Checker { | ||
d := &AAAA{ | ||
address: address, | ||
resolver: net.DefaultResolver, | ||
} | ||
|
||
// apply the list of options to AAAA | ||
for _, opt := range opts { | ||
opt(d) | ||
} | ||
|
||
// Nameserver settings. | ||
if d.nameserver != "" { | ||
d.resolver = &net.Resolver{ | ||
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { | ||
dialer := net.Dialer{} | ||
return dialer.DialContext(ctx, network, d.nameserver) | ||
}, | ||
} | ||
} | ||
|
||
return d | ||
} | ||
|
||
// WithNameServer overrides the default nameserver | ||
func WithNameServer(nameserver string) Option { | ||
return func(d *AAAA) { | ||
d.nameserver = nameserver | ||
} | ||
} | ||
|
||
// WithExpectedIPV6s sets expected IPv6s | ||
func WithExpectedIPV6s(ips []string) Option { | ||
return func(d *AAAA) { | ||
d.expectedIPs = ips | ||
} | ||
} | ||
|
||
// Identity returns the identity of the checker | ||
func (d *AAAA) Identity() (string, error) { | ||
return d.address, nil | ||
} | ||
|
||
// Check checks DNS records | ||
func (d *AAAA) Check(ctx context.Context) (err error) { | ||
values, err := d.resolver.LookupIP(ctx, "ip6", d.address) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, ip := range values { | ||
if len(d.expectedIPs) == 0 { | ||
return nil | ||
} | ||
for _, expectedIP := range d.expectedIPs { | ||
if expectedIP == ip.String() { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return checker.NewExpectedError( | ||
"the AAAA record value doesn't expect", nil, | ||
"actual", values, "expect", d.expectedIPs, | ||
) | ||
} |
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,32 @@ | ||
package aaaa | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"wait4x.dev/v2/checker" | ||
) | ||
|
||
const server = "wait4x.dev" | ||
|
||
func TestCheckExistenceAAAA(t *testing.T) { | ||
d := New(server) | ||
assert.Nil(t, d.Check(context.Background())) | ||
} | ||
|
||
func TestCorrectAAAA(t *testing.T) { | ||
d := New(server, WithExpectedIPV6s([]string{"2606:4700:3034::6815:591"})) | ||
assert.Nil(t, d.Check(context.Background())) | ||
} | ||
|
||
func TestIncorrectAAAA(t *testing.T) { | ||
var expectedError *checker.ExpectedError | ||
d := New(server, WithExpectedIPV6s([]string{"127.0.0.1"})) | ||
assert.ErrorAs(t, d.Check(context.Background()), &expectedError) | ||
} | ||
|
||
func TestCustomNSCorrectAAAA(t *testing.T) { | ||
d := New(server, WithNameServer("8.8.8.8:53"), WithExpectedIPV6s([]string{"2606:4700:3034::6815:591"})) | ||
assert.Nil(t, d.Check(context.Background())) | ||
} |
Oops, something went wrong.