github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/http/signerpool/signerpool.go (about) 1 package signerpool 2 3 import ( 4 "fmt" 5 "strings" 6 "sync" 7 8 "github.com/projectdiscovery/nuclei/v2/pkg/protocols/http/signer" 9 10 "github.com/projectdiscovery/nuclei/v2/pkg/types" 11 ) 12 13 var ( 14 poolMutex *sync.RWMutex 15 clientPool map[string]signer.Signer 16 ) 17 18 // Init initializes the clientpool implementation 19 func Init(options *types.Options) error { 20 poolMutex = &sync.RWMutex{} 21 clientPool = make(map[string]signer.Signer) 22 return nil 23 } 24 25 // Configuration contains the custom configuration options for a client 26 type Configuration struct { 27 SignerArgs signer.SignerArgs 28 } 29 30 // Hash returns the hash of the configuration to allow client pooling 31 func (c *Configuration) Hash() string { 32 builder := &strings.Builder{} 33 builder.WriteString(fmt.Sprintf("%v", c.SignerArgs)) 34 hash := builder.String() 35 return hash 36 } 37 38 // Get creates or gets a client for the protocol based on custom configuration 39 func Get(options *types.Options, configuration *Configuration) (signer.Signer, error) { 40 hash := configuration.Hash() 41 poolMutex.RLock() 42 if client, ok := clientPool[hash]; ok { 43 poolMutex.RUnlock() 44 return client, nil 45 } 46 poolMutex.RUnlock() 47 48 client, err := signer.NewSigner(configuration.SignerArgs) 49 if err != nil { 50 return nil, err 51 } 52 53 poolMutex.Lock() 54 clientPool[hash] = client 55 poolMutex.Unlock() 56 return client, nil 57 }