2021-11-26 13:49:12 +01:00
|
|
|
package signerpool
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/http/signer"
|
2021-11-26 13:49:12 +01:00
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
2021-11-26 13:49:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2025-07-09 14:47:26 -05:00
|
|
|
poolMutex sync.RWMutex
|
2021-11-26 13:49:12 +01:00
|
|
|
clientPool map[string]signer.Signer
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Init initializes the clientpool implementation
|
|
|
|
|
func Init(options *types.Options) error {
|
2025-07-09 14:47:26 -05:00
|
|
|
poolMutex.Lock()
|
|
|
|
|
defer poolMutex.Unlock()
|
|
|
|
|
if clientPool != nil {
|
|
|
|
|
return nil // already initialized
|
|
|
|
|
}
|
2021-11-26 13:49:12 +01:00
|
|
|
clientPool = make(map[string]signer.Signer)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Configuration contains the custom configuration options for a client
|
|
|
|
|
type Configuration struct {
|
|
|
|
|
SignerArgs signer.SignerArgs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hash returns the hash of the configuration to allow client pooling
|
|
|
|
|
func (c *Configuration) Hash() string {
|
|
|
|
|
builder := &strings.Builder{}
|
2025-07-01 00:40:44 +07:00
|
|
|
_, _ = fmt.Fprintf(builder, "%v", c.SignerArgs)
|
2021-11-26 13:49:12 +01:00
|
|
|
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) (signer.Signer, error) {
|
|
|
|
|
hash := configuration.Hash()
|
|
|
|
|
poolMutex.RLock()
|
|
|
|
|
if client, ok := clientPool[hash]; ok {
|
|
|
|
|
poolMutex.RUnlock()
|
|
|
|
|
return client, nil
|
|
|
|
|
}
|
|
|
|
|
poolMutex.RUnlock()
|
|
|
|
|
|
|
|
|
|
client, err := signer.NewSigner(configuration.SignerArgs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
poolMutex.Lock()
|
|
|
|
|
clientPool[hash] = client
|
|
|
|
|
poolMutex.Unlock()
|
|
|
|
|
return client, nil
|
|
|
|
|
}
|