Ensure callback is called
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Tyler 2021-03-07 00:18:28 -05:00
parent 78c3e3158a
commit 6218a8ca50
1 changed files with 22 additions and 12 deletions

32
main.go
View File

@ -18,7 +18,7 @@ import (
"time"
)
type callbackFunc func(rules yara.MatchRules)
type callbackFunc func(*yara.MatchRules, error)
type Job struct {
Data io.ReadCloser
@ -106,11 +106,16 @@ func scanHandler(w http.ResponseWriter, r *http.Request) {
wg := &sync.WaitGroup{}
results := make([]yara.MatchRules, 0)
results := make([]*yara.MatchRules, 0)
jobCallback := func(m yara.MatchRules) {
results = append(results, m)
jobCallback := func(m *yara.MatchRules, err error) {
wg.Done()
if err != nil {
return
}
results = append(results, m)
}
log.WithField("contentType", contentType).Debug("Adding files from multipart form as jobs")
@ -143,12 +148,20 @@ func scanHandler(w http.ResponseWriter, r *http.Request) {
wg.Wait()
log.WithField("matches", w).Debug("Matched rules")
json.NewEncoder(w).Encode(results)
default:
job := &Job{
Data: r.Body,
Callback: func(m yara.MatchRules) {
Callback: func(m *yara.MatchRules, err error) {
log.WithField("match", m).Debug("Matched rules")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
} else {
json.NewEncoder(w).Encode(m)
}
},
}
@ -235,19 +248,16 @@ func processJob(s *yara.Scanner, job *Job) {
b, err := io.ReadAll(job.Data)
if err != nil {
job.Callback(nil, err)
return
}
err = s.SetCallback(&m).ScanMem(b)
if err != nil {
job.Callback(nil, err)
return
}
// Respond with job
if len(m) < 1 {
return
}
job.Callback(m)
job.Callback(&m, nil)
}