Ensure callback is called
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Tyler 2021-03-07 00:18:28 -05:00
parent 78c3e3158a
commit 6218a8ca50

34
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) {
json.NewEncoder(w).Encode(m)
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)
}