2021-10-27 11:20:36 +00:00
|
|
|
package core
|
2021-10-27 10:23:04 +00:00
|
|
|
|
|
|
|
import (
|
2021-10-28 11:50:07 +00:00
|
|
|
"github.com/remeh/sizedwaitgroup"
|
2021-11-25 15:09:20 +00:00
|
|
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/templates/types"
|
2021-10-27 10:23:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// WorkPool implements an execution pool for executing different
|
2021-11-25 16:54:16 +00:00
|
|
|
// types of task with different concurrency requirements.
|
2021-10-27 10:23:04 +00:00
|
|
|
//
|
|
|
|
// It also allows Configuration of such requirements. This is used
|
|
|
|
// for per-module like separate headless concurrency etc.
|
|
|
|
type WorkPool struct {
|
2021-10-28 11:50:07 +00:00
|
|
|
Headless *sizedwaitgroup.SizedWaitGroup
|
|
|
|
Default *sizedwaitgroup.SizedWaitGroup
|
|
|
|
config WorkPoolConfig
|
2021-10-27 10:23:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 16:54:16 +00:00
|
|
|
// WorkPoolConfig is the configuration for work pool
|
2021-10-27 10:23:04 +00:00
|
|
|
type WorkPoolConfig struct {
|
|
|
|
// InputConcurrency is the concurrency for inputs values.
|
|
|
|
InputConcurrency int
|
|
|
|
// TypeConcurrency is the concurrency for the request type templates.
|
|
|
|
TypeConcurrency int
|
|
|
|
// HeadlessInputConcurrency is the concurrency for headless inputs values.
|
|
|
|
HeadlessInputConcurrency int
|
|
|
|
// TypeConcurrency is the concurrency for the headless request type templates.
|
|
|
|
HeadlessTypeConcurrency int
|
|
|
|
}
|
|
|
|
|
2021-10-27 11:20:36 +00:00
|
|
|
// NewWorkPool returns a new WorkPool instance
|
|
|
|
func NewWorkPool(config WorkPoolConfig) *WorkPool {
|
2021-10-28 11:50:07 +00:00
|
|
|
headlessWg := sizedwaitgroup.New(config.HeadlessTypeConcurrency)
|
|
|
|
defaultWg := sizedwaitgroup.New(config.TypeConcurrency)
|
|
|
|
|
|
|
|
return &WorkPool{
|
|
|
|
config: config,
|
|
|
|
Headless: &headlessWg,
|
|
|
|
Default: &defaultWg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 16:54:16 +00:00
|
|
|
// Wait waits for all the work pool wait groups to finish
|
2021-10-28 11:50:07 +00:00
|
|
|
func (w *WorkPool) Wait() {
|
|
|
|
w.Default.Wait()
|
|
|
|
w.Headless.Wait()
|
2021-10-27 10:23:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 16:54:16 +00:00
|
|
|
// InputWorkPool is a work pool per-input
|
2021-10-28 11:50:07 +00:00
|
|
|
type InputWorkPool struct {
|
2021-11-25 16:54:16 +00:00
|
|
|
WaitGroup *sizedwaitgroup.SizedWaitGroup
|
2021-10-28 11:50:07 +00:00
|
|
|
}
|
2021-10-27 10:23:04 +00:00
|
|
|
|
2021-11-25 16:54:16 +00:00
|
|
|
// InputPool returns a work pool for an input type
|
2021-11-03 13:28:00 +00:00
|
|
|
func (w *WorkPool) InputPool(templateType types.ProtocolType) *InputWorkPool {
|
2021-10-28 11:50:07 +00:00
|
|
|
var count int
|
2021-11-03 13:28:00 +00:00
|
|
|
if templateType == types.HeadlessProtocol {
|
2021-10-28 11:50:07 +00:00
|
|
|
count = w.config.HeadlessInputConcurrency
|
|
|
|
} else {
|
|
|
|
count = w.config.InputConcurrency
|
|
|
|
}
|
|
|
|
swg := sizedwaitgroup.New(count)
|
2021-11-25 16:54:16 +00:00
|
|
|
return &InputWorkPool{WaitGroup: &swg}
|
2021-10-27 10:23:04 +00:00
|
|
|
}
|