2020-12-25 12:55:46 +05:30
|
|
|
package dnsclientpool
|
2020-12-23 22:09:11 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
|
2022-08-27 19:35:17 +05:30
|
|
|
"github.com/pkg/errors"
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
2020-12-23 22:09:11 +05:30
|
|
|
"github.com/projectdiscovery/retryabledns"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
poolMutex *sync.RWMutex
|
|
|
|
|
normalClient *retryabledns.Client
|
|
|
|
|
clientPool map[string]*retryabledns.Client
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// defaultResolvers contains the list of resolvers known to be trusted.
|
|
|
|
|
var defaultResolvers = []string{
|
|
|
|
|
"1.1.1.1:53", // Cloudflare
|
|
|
|
|
"1.0.0.1:53", // Cloudflare
|
|
|
|
|
"8.8.8.8:53", // Google
|
|
|
|
|
"8.8.4.4:53", // Google
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 18:54:16 +02:00
|
|
|
// Init initializes the client pool implementation
|
2020-12-23 22:09:11 +05:30
|
|
|
func Init(options *types.Options) error {
|
2021-09-07 17:31:46 +03:00
|
|
|
// Don't create clients if already created in the past.
|
2020-12-23 22:09:11 +05:30
|
|
|
if normalClient != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
poolMutex = &sync.RWMutex{}
|
|
|
|
|
clientPool = make(map[string]*retryabledns.Client)
|
|
|
|
|
|
2021-02-08 13:06:11 +05:30
|
|
|
resolvers := defaultResolvers
|
2024-04-11 19:10:31 +02:00
|
|
|
if options.ResolversFile != "" || len(options.InternalResolversList) > 0 {
|
2021-02-08 13:06:11 +05:30
|
|
|
resolvers = options.InternalResolversList
|
|
|
|
|
}
|
2022-08-27 19:35:17 +05:30
|
|
|
var err error
|
|
|
|
|
normalClient, err = retryabledns.New(resolvers, 1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "could not create dns client")
|
|
|
|
|
}
|
2020-12-23 22:09:11 +05:30
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Configuration contains the custom configuration options for a client
|
|
|
|
|
type Configuration struct {
|
|
|
|
|
// Retries contains the retries for the dns client
|
|
|
|
|
Retries int
|
2021-10-04 15:31:14 +02:00
|
|
|
// Resolvers contains the specific per request resolvers
|
|
|
|
|
Resolvers []string
|
2020-12-23 22:09:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hash returns the hash of the configuration to allow client pooling
|
|
|
|
|
func (c *Configuration) Hash() string {
|
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
builder.WriteString("r")
|
|
|
|
|
builder.WriteString(strconv.Itoa(c.Retries))
|
2021-10-04 15:31:14 +02:00
|
|
|
builder.WriteString("l")
|
|
|
|
|
builder.WriteString(strings.Join(c.Resolvers, ""))
|
2020-12-23 22:09:11 +05:30
|
|
|
hash := builder.String()
|
|
|
|
|
return hash
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get creates or gets a client for the protocol based on custom configuration
|
|
|
|
|
func Get(options *types.Options, configuration *Configuration) (*retryabledns.Client, error) {
|
2021-10-04 15:31:14 +02:00
|
|
|
if !(configuration.Retries > 1) && len(configuration.Resolvers) == 0 {
|
2020-12-23 22:09:11 +05:30
|
|
|
return normalClient, nil
|
|
|
|
|
}
|
|
|
|
|
hash := configuration.Hash()
|
|
|
|
|
poolMutex.RLock()
|
|
|
|
|
if client, ok := clientPool[hash]; ok {
|
|
|
|
|
poolMutex.RUnlock()
|
|
|
|
|
return client, nil
|
|
|
|
|
}
|
|
|
|
|
poolMutex.RUnlock()
|
|
|
|
|
|
2021-02-27 02:23:06 +05:30
|
|
|
resolvers := defaultResolvers
|
2024-04-11 19:10:31 +02:00
|
|
|
if options.ResolversFile != "" || len(options.InternalResolversList) > 0 {
|
2021-02-27 02:23:06 +05:30
|
|
|
resolvers = options.InternalResolversList
|
2021-10-04 15:31:14 +02:00
|
|
|
} else if len(configuration.Resolvers) > 0 {
|
|
|
|
|
resolvers = configuration.Resolvers
|
2021-02-27 02:23:06 +05:30
|
|
|
}
|
2022-08-27 19:35:17 +05:30
|
|
|
client, err := retryabledns.New(resolvers, configuration.Retries)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "could not create dns client")
|
|
|
|
|
}
|
2020-12-23 22:09:11 +05:30
|
|
|
|
|
|
|
|
poolMutex.Lock()
|
|
|
|
|
clientPool[hash] = client
|
|
|
|
|
poolMutex.Unlock()
|
|
|
|
|
return client, nil
|
|
|
|
|
}
|