Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/http/signerpool/signerpool.go
2073 views
1
package signerpool
2
3
import (
4
"fmt"
5
"strings"
6
"sync"
7
8
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/http/signer"
9
10
"github.com/projectdiscovery/nuclei/v3/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.Lock()
21
defer poolMutex.Unlock()
22
if clientPool != nil {
23
return nil // already initialized
24
}
25
clientPool = make(map[string]signer.Signer)
26
return nil
27
}
28
29
// Configuration contains the custom configuration options for a client
30
type Configuration struct {
31
SignerArgs signer.SignerArgs
32
}
33
34
// Hash returns the hash of the configuration to allow client pooling
35
func (c *Configuration) Hash() string {
36
builder := &strings.Builder{}
37
_, _ = fmt.Fprintf(builder, "%v", c.SignerArgs)
38
hash := builder.String()
39
return hash
40
}
41
42
// Get creates or gets a client for the protocol based on custom configuration
43
func Get(options *types.Options, configuration *Configuration) (signer.Signer, error) {
44
hash := configuration.Hash()
45
poolMutex.RLock()
46
if client, ok := clientPool[hash]; ok {
47
poolMutex.RUnlock()
48
return client, nil
49
}
50
poolMutex.RUnlock()
51
52
client, err := signer.NewSigner(configuration.SignerArgs)
53
if err != nil {
54
return nil, err
55
}
56
57
poolMutex.Lock()
58
clientPool[hash] = client
59
poolMutex.Unlock()
60
return client, nil
61
}
62
63