nuclei/v2/pkg/templates/compile.go

40 lines
742 B
Go
Raw Normal View History

2020-04-03 21:20:32 +00:00
package templates
import (
"fmt"
2020-04-03 21:20:32 +00:00
"os"
"gopkg.in/yaml.v2"
)
2020-06-29 12:13:08 +00:00
// Parse parses a yaml request template file
func Parse(file string) (*Template, error) {
2020-04-03 21:20:32 +00:00
template := &Template{}
f, err := os.Open(file)
if err != nil {
return nil, err
}
err = yaml.NewDecoder(f).Decode(template)
if err != nil {
return nil, err
}
2020-06-26 12:37:55 +00:00
defer f.Close()
2020-07-31 15:13:51 +00:00
template.path = file
2020-06-29 12:13:08 +00:00
// If no requests, and it is also not a workflow, return error.
2020-07-18 19:42:23 +00:00
if len(template.BulkRequestsHTTP)+len(template.RequestsDNS) <= 0 {
return nil, fmt.Errorf("no requests defined for %s", template.ID)
2020-06-26 12:37:55 +00:00
}
2020-04-03 21:20:32 +00:00
2020-04-22 20:45:02 +00:00
// Compile the matchers and the extractors for http requests
2020-07-18 19:42:23 +00:00
for _, request := range template.BulkRequestsHTTP {
2020-07-24 16:12:16 +00:00
request.InitGenerator()
2020-04-03 21:20:32 +00:00
}
2020-04-22 20:45:02 +00:00
2020-04-03 21:20:32 +00:00
return template, nil
}