2022-10-03 12:12:20 +02:00
|
|
|
package contextargs
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http/cookiejar"
|
|
|
|
|
|
2023-06-09 19:52:56 +05:30
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
|
maputils "github.com/projectdiscovery/utils/maps"
|
2022-10-03 12:12:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Context implements a shared context struct to share information across multiple templates within a workflow
|
|
|
|
|
type Context struct {
|
2022-11-09 14:18:56 +01:00
|
|
|
// Meta is the target for the executor
|
|
|
|
|
MetaInput *MetaInput
|
|
|
|
|
|
2022-10-03 12:12:20 +02:00
|
|
|
// CookieJar shared within workflow's http templates
|
|
|
|
|
CookieJar *cookiejar.Jar
|
|
|
|
|
// Args is a workflow shared key-value store
|
2023-06-09 19:52:56 +05:30
|
|
|
args maputils.SyncLockMap[string, interface{}]
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new contextargs instance
|
|
|
|
|
func New() *Context {
|
2023-06-09 19:52:56 +05:30
|
|
|
return NewWithInput("")
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new contextargs instance with input string
|
|
|
|
|
func NewWithInput(input string) *Context {
|
2023-06-09 19:52:56 +05:30
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Error().Msgf("Could not create cookie jar: %s\n", err)
|
|
|
|
|
}
|
|
|
|
|
return &Context{MetaInput: &MetaInput{Input: input}, CookieJar: jar, args: maputils.SyncLockMap[string, interface{}]{
|
|
|
|
|
Map: make(map[string]interface{}),
|
|
|
|
|
}}
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the specific key-value pair
|
|
|
|
|
func (ctx *Context) Set(key string, value interface{}) {
|
2023-06-09 19:52:56 +05:30
|
|
|
if err := ctx.args.Set(key, value); err != nil {
|
|
|
|
|
gologger.Error().Msgf("contextargs: could not set key: %s\n", err)
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the value with specific key if exists
|
|
|
|
|
func (ctx *Context) Get(key string) (interface{}, bool) {
|
2023-06-09 19:52:56 +05:30
|
|
|
return ctx.args.Get(key)
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-09 19:52:56 +05:30
|
|
|
func (ctx *Context) GetAll() maputils.Map[string, interface{}] {
|
|
|
|
|
return ctx.args.GetAll()
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-09 19:52:56 +05:30
|
|
|
func (ctx *Context) ForEach(f func(string, interface{}) error) {
|
|
|
|
|
if err := ctx.args.Iterate(f); err != nil {
|
|
|
|
|
gologger.Error().Msgf("contextargs: could not iterate: %s\n", err)
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-09 19:52:56 +05:30
|
|
|
// Merge merges the map into the contextargs
|
|
|
|
|
func (ctx *Context) Merge(m map[string]interface{}) {
|
|
|
|
|
if err := ctx.args.Merge(m); err != nil {
|
|
|
|
|
gologger.Error().Msgf("contextargs: could not merge: %s\n", err)
|
|
|
|
|
}
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Has check if the key exists
|
|
|
|
|
func (ctx *Context) Has(key string) bool {
|
2023-06-09 19:52:56 +05:30
|
|
|
return ctx.args.Has(key)
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ctx *Context) HasArgs() bool {
|
2023-06-09 19:52:56 +05:30
|
|
|
return !ctx.args.IsEmpty()
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
2022-11-09 14:18:56 +01:00
|
|
|
|
|
|
|
|
func (ctx *Context) Clone() *Context {
|
|
|
|
|
newCtx := &Context{
|
|
|
|
|
MetaInput: ctx.MetaInput.Clone(),
|
2023-06-09 19:52:56 +05:30
|
|
|
args: *ctx.args.Clone(),
|
2022-11-09 14:18:56 +01:00
|
|
|
CookieJar: ctx.CookieJar,
|
|
|
|
|
}
|
|
|
|
|
return newCtx
|
|
|
|
|
}
|