Skip to content

Commit

Permalink
feat: add client to allow passing custom resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
babariviere committed Nov 6, 2024
1 parent a1960f6 commit 025859c
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions dnsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@ import (

const hexDigit = "0123456789abcdef"

var DefaultClient *Client = &Client{resolver: net.DefaultResolver}

type Client struct {
resolver *net.Resolver
}

func NewClient(resolver *net.Resolver) *Client {
return &Client{
resolver: resolver,
}
}

// LookupIP looks up a single IP with the API.
// The service recommends that bulk lookups use the BulkClient out of respect
// for their server load.
func LookupIP(ctx context.Context, ip net.IP) (*Response, error) {
var r net.Resolver
func (c *Client) LookupIP(ctx context.Context, ip net.IP) (*Response, error) {
lookupName, err := formatDNSLookupName(ip)
if err != nil {
return nil, err
}
txts, err := r.LookupTXT(ctx, lookupName)
txts, err := c.resolver.LookupTXT(ctx, lookupName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -71,9 +82,8 @@ func LookupIP(ctx context.Context, ip net.IP) (*Response, error) {
return nil, fmt.Errorf("no records found")
}

func LookupASN(ctx context.Context, asn ASN) (*Response, error) {
var r net.Resolver
txts, err := r.LookupTXT(ctx, asn.String()+".asn.cymru.com")
func (c *Client) LookupASN(ctx context.Context, asn ASN) (*Response, error) {
txts, err := c.resolver.LookupTXT(ctx, asn.String()+".asn.cymru.com")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -108,6 +118,17 @@ func LookupASN(ctx context.Context, asn ASN) (*Response, error) {
return nil, fmt.Errorf("no records found")
}

// LookupIP looks up a single IP with the API.
// The service recommends that bulk lookups use the BulkClient out of respect
// for their server load.
func LookupIP(ctx context.Context, ip net.IP) (*Response, error) {
return DefaultClient.LookupIP(ctx, ip)
}

func LookupASN(ctx context.Context, asn ASN) (*Response, error) {
return DefaultClient.LookupASN(ctx, asn)
}

func formatDNSLookupName(ip net.IP) (string, error) {
switch {
case len(ip) == net.IPv4len || ip.To4() != nil:
Expand Down

0 comments on commit 025859c

Please sign in to comment.