Path: blob/dev/pkg/protocols/http/signerpool/signerpool.go
2073 views
package signerpool12import (3"fmt"4"strings"5"sync"67"github.com/projectdiscovery/nuclei/v3/pkg/protocols/http/signer"89"github.com/projectdiscovery/nuclei/v3/pkg/types"10)1112var (13poolMutex sync.RWMutex14clientPool map[string]signer.Signer15)1617// Init initializes the clientpool implementation18func Init(options *types.Options) error {19poolMutex.Lock()20defer poolMutex.Unlock()21if clientPool != nil {22return nil // already initialized23}24clientPool = make(map[string]signer.Signer)25return nil26}2728// Configuration contains the custom configuration options for a client29type Configuration struct {30SignerArgs signer.SignerArgs31}3233// Hash returns the hash of the configuration to allow client pooling34func (c *Configuration) Hash() string {35builder := &strings.Builder{}36_, _ = fmt.Fprintf(builder, "%v", c.SignerArgs)37hash := builder.String()38return hash39}4041// Get creates or gets a client for the protocol based on custom configuration42func Get(options *types.Options, configuration *Configuration) (signer.Signer, error) {43hash := configuration.Hash()44poolMutex.RLock()45if client, ok := clientPool[hash]; ok {46poolMutex.RUnlock()47return client, nil48}49poolMutex.RUnlock()5051client, err := signer.NewSigner(configuration.SignerArgs)52if err != nil {53return nil, err54}5556poolMutex.Lock()57clientPool[hash] = client58poolMutex.Unlock()59return client, nil60}616263