diff --git a/dnsclient.go b/dnsclient.go index 3405bba..4408235 100644 --- a/dnsclient.go +++ b/dnsclient.go @@ -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 } @@ -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 } @@ -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: