2014-03-28 04:31:51 +00:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
2018-12-15 21:42:32 +00:00
|
|
|
"context"
|
2016-06-08 02:42:09 +00:00
|
|
|
"time"
|
2014-03-28 04:31:51 +00:00
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
log "github.com/gophish/gophish/logger"
|
2017-12-09 21:42:07 +00:00
|
|
|
"github.com/gophish/gophish/mailer"
|
2016-01-10 17:03:17 +00:00
|
|
|
"github.com/gophish/gophish/models"
|
2018-05-04 00:07:41 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2014-03-28 04:31:51 +00:00
|
|
|
)
|
|
|
|
|
2018-12-15 21:42:32 +00:00
|
|
|
// Worker is an interface that defines the operations needed for a background worker
|
|
|
|
type Worker interface {
|
|
|
|
Start()
|
|
|
|
LaunchCampaign(c models.Campaign)
|
|
|
|
SendTestEmail(s *models.EmailRequest) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultWorker is the background worker that handles watching for new campaigns and sending emails appropriately.
|
|
|
|
type DefaultWorker struct {
|
|
|
|
mailer mailer.Mailer
|
|
|
|
}
|
2014-03-28 04:31:51 +00:00
|
|
|
|
2015-06-13 04:12:43 +00:00
|
|
|
// New creates a new worker object to handle the creation of campaigns
|
2018-12-15 21:42:32 +00:00
|
|
|
func New(options ...func(Worker) error) (Worker, error) {
|
|
|
|
defaultMailer := mailer.NewMailWorker()
|
|
|
|
w := &DefaultWorker{
|
|
|
|
mailer: defaultMailer,
|
|
|
|
}
|
|
|
|
for _, opt := range options {
|
|
|
|
if err := opt(w); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMailer sets the mailer for a given worker.
|
|
|
|
// By default, workers use a standard, default mailworker.
|
|
|
|
func WithMailer(m mailer.Mailer) func(*DefaultWorker) error {
|
|
|
|
return func(w *DefaultWorker) error {
|
|
|
|
w.mailer = m
|
|
|
|
return nil
|
|
|
|
}
|
2014-03-28 04:31:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 04:18:20 +00:00
|
|
|
// processCampaigns loads maillogs scheduled to be sent before the provided
|
|
|
|
// time and sends them to the mailer.
|
|
|
|
func (w *DefaultWorker) processCampaigns(t time.Time) error {
|
|
|
|
ms, err := models.GetQueuedMailLogs(t.UTC())
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Lock the MailLogs (they will be unlocked after processing)
|
|
|
|
err = models.LockMailLogs(ms, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
campaignCache := make(map[int64]models.Campaign)
|
|
|
|
// We'll group the maillogs by campaign ID to (roughly) group
|
|
|
|
// them by sending profile. This lets the mailer re-use the Sender
|
|
|
|
// instead of having to re-connect to the SMTP server for every
|
|
|
|
// email.
|
|
|
|
msg := make(map[int64][]mailer.Mail)
|
|
|
|
for _, m := range ms {
|
|
|
|
// We cache the campaign here to greatly reduce the time it takes to
|
|
|
|
// generate the message (ref #1726)
|
|
|
|
c, ok := campaignCache[m.CampaignId]
|
|
|
|
if !ok {
|
|
|
|
c, err = models.GetCampaign(m.CampaignId, m.UserId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
campaignCache[c.Id] = c
|
|
|
|
}
|
|
|
|
m.CacheCampaign(&c)
|
|
|
|
msg[m.CampaignId] = append(msg[m.CampaignId], m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we process each group of maillogs in parallel
|
|
|
|
for cid, msc := range msg {
|
|
|
|
go func(cid int64, msc []mailer.Mail) {
|
|
|
|
c := campaignCache[cid]
|
|
|
|
if c.Status == models.CampaignQueued {
|
|
|
|
err := c.UpdateStatus(models.CampaignInProgress)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"num_emails": len(msc),
|
|
|
|
}).Info("Sending emails to mailer for processing")
|
|
|
|
w.mailer.Queue(msc)
|
|
|
|
}(cid, msc)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-09 21:42:07 +00:00
|
|
|
// Start launches the worker to poll the database every minute for any pending maillogs
|
|
|
|
// that need to be processed.
|
2018-12-15 21:42:32 +00:00
|
|
|
func (w *DefaultWorker) Start() {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Info("Background Worker Started Successfully - Waiting for Campaigns")
|
2018-12-15 21:42:32 +00:00
|
|
|
go w.mailer.Start(context.Background())
|
2016-06-08 02:42:09 +00:00
|
|
|
for t := range time.Tick(1 * time.Minute) {
|
2020-01-28 04:18:20 +00:00
|
|
|
err := w.processCampaigns(t)
|
2016-06-08 02:42:09 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-06-08 02:42:09 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-03-28 04:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-25 02:03:53 +00:00
|
|
|
|
2017-12-09 21:42:07 +00:00
|
|
|
// LaunchCampaign starts a campaign
|
2018-12-15 21:42:32 +00:00
|
|
|
func (w *DefaultWorker) LaunchCampaign(c models.Campaign) {
|
2017-12-09 21:42:07 +00:00
|
|
|
ms, err := models.GetMailLogsByCampaign(c.Id)
|
2016-05-30 19:55:45 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2017-12-09 21:42:07 +00:00
|
|
|
return
|
2016-07-07 04:25:28 +00:00
|
|
|
}
|
2017-12-09 21:42:07 +00:00
|
|
|
models.LockMailLogs(ms, true)
|
|
|
|
// This is required since you cannot pass a slice of values
|
|
|
|
// that implements an interface as a slice of that interface.
|
|
|
|
mailEntries := []mailer.Mail{}
|
2018-09-02 16:17:52 +00:00
|
|
|
currentTime := time.Now().UTC()
|
2017-12-09 21:42:07 +00:00
|
|
|
for _, m := range ms {
|
2018-09-02 16:17:52 +00:00
|
|
|
// Only send the emails scheduled to be sent for the past minute to
|
|
|
|
// respect the campaign scheduling options
|
|
|
|
if m.SendDate.After(currentTime) {
|
|
|
|
m.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
2020-01-28 04:18:20 +00:00
|
|
|
err = m.CacheCampaign(&c)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2017-12-09 21:42:07 +00:00
|
|
|
mailEntries = append(mailEntries, m)
|
2016-01-25 02:03:53 +00:00
|
|
|
}
|
2018-12-15 21:42:32 +00:00
|
|
|
w.mailer.Queue(mailEntries)
|
2017-12-09 21:42:07 +00:00
|
|
|
}
|
2017-02-20 00:43:08 +00:00
|
|
|
|
2017-12-09 21:42:07 +00:00
|
|
|
// SendTestEmail sends a test email
|
2018-12-15 21:42:32 +00:00
|
|
|
func (w *DefaultWorker) SendTestEmail(s *models.EmailRequest) error {
|
2017-12-09 21:42:07 +00:00
|
|
|
go func() {
|
2018-09-02 16:17:52 +00:00
|
|
|
ms := []mailer.Mail{s}
|
2018-12-15 21:42:32 +00:00
|
|
|
w.mailer.Queue(ms)
|
2017-12-09 21:42:07 +00:00
|
|
|
}()
|
|
|
|
return <-s.ErrorChan
|
|
|
|
}
|
2017-02-20 00:43:08 +00:00
|
|
|
|
2017-12-09 21:42:07 +00:00
|
|
|
// errorMail is a helper to handle erroring out a slice of Mail instances
|
|
|
|
// in the case that an unrecoverable error occurs.
|
|
|
|
func errorMail(err error, ms []mailer.Mail) {
|
|
|
|
for _, m := range ms {
|
|
|
|
m.Error(err)
|
2016-01-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
}
|