2024-08-18 06:55:47 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DNSResult struct {
|
|
|
|
msg *dns.Msg
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2024-08-18 07:11:04 +00:00
|
|
|
type GeneralResolver struct {
|
|
|
|
client *dns.Client
|
2024-08-18 06:55:47 +00:00
|
|
|
server string
|
|
|
|
}
|
|
|
|
|
2024-08-18 07:11:04 +00:00
|
|
|
func NewGeneralClient(server string) *GeneralResolver {
|
|
|
|
return &GeneralResolver{
|
|
|
|
client: &dns.Client{},
|
2024-08-18 06:55:47 +00:00
|
|
|
server: server,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-18 07:11:04 +00:00
|
|
|
func (r *GeneralResolver) Resolve(ctx context.Context, host string, qTypes []uint16) ([]net.IPAddr, error) {
|
2024-08-18 09:51:20 +00:00
|
|
|
resultCh := lookupAllTypes(ctx, host, qTypes, r.exchange)
|
2024-08-18 06:55:47 +00:00
|
|
|
addrs, err := processResults(ctx, resultCh)
|
|
|
|
return addrs, err
|
|
|
|
}
|
|
|
|
|
2024-08-18 07:11:04 +00:00
|
|
|
func (c *GeneralResolver) String() string {
|
2024-08-18 07:33:02 +00:00
|
|
|
return fmt.Sprintf("general resolver(%s)", c.server)
|
2024-08-18 06:55:47 +00:00
|
|
|
}
|
2024-08-18 09:51:20 +00:00
|
|
|
|
|
|
|
func (r *GeneralResolver) exchange(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
|
|
|
resp, _, err := r.client.Exchange(msg, r.server)
|
|
|
|
return resp, err
|
|
|
|
}
|