2014-03-28 04:31:51 +00:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
2014-06-27 00:55:56 +00:00
|
|
|
"bytes"
|
2014-03-28 04:31:51 +00:00
|
|
|
"log"
|
2014-06-05 04:54:46 +00:00
|
|
|
"net/smtp"
|
2014-03-28 04:31:51 +00:00
|
|
|
"os"
|
2014-06-05 04:54:46 +00:00
|
|
|
"strings"
|
2014-06-27 00:55:56 +00:00
|
|
|
"text/template"
|
2014-03-28 04:31:51 +00:00
|
|
|
|
2014-06-05 04:54:46 +00:00
|
|
|
"github.com/jordan-wright/email"
|
2014-03-28 04:31:51 +00:00
|
|
|
"github.com/jordan-wright/gophish/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile)
|
|
|
|
|
|
|
|
type Worker struct {
|
|
|
|
Queue chan *models.Campaign
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Worker {
|
|
|
|
return &Worker{
|
|
|
|
Queue: make(chan *models.Campaign),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start launches the worker to monitor the database for any jobs.
|
|
|
|
// If a job is found, it launches the job
|
|
|
|
func (w *Worker) Start() {
|
|
|
|
Logger.Println("Background Worker Started Successfully - Waiting for Campaigns")
|
|
|
|
for {
|
|
|
|
processCampaign(<-w.Queue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processCampaign(c *models.Campaign) {
|
|
|
|
Logger.Printf("Worker received: %s", c.Name)
|
2014-07-02 01:32:34 +00:00
|
|
|
err := c.UpdateStatus(models.CAMPAIGN_IN_PROGRESS)
|
2014-03-28 04:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2014-06-05 04:54:46 +00:00
|
|
|
e := email.Email{
|
|
|
|
Subject: c.Template.Subject,
|
|
|
|
From: c.SMTP.FromAddress,
|
|
|
|
}
|
|
|
|
var auth smtp.Auth
|
|
|
|
if c.SMTP.Username != "" && c.SMTP.Password != "" {
|
|
|
|
auth = smtp.PlainAuth("", c.SMTP.Username, c.SMTP.Password, strings.Split(c.SMTP.Host, ":")[0])
|
|
|
|
}
|
2014-03-28 04:31:51 +00:00
|
|
|
for _, t := range c.Results {
|
2014-06-27 00:55:56 +00:00
|
|
|
// Parse the templates
|
|
|
|
var buff bytes.Buffer
|
|
|
|
tmpl, err := template.New("html_template").Parse(c.Template.HTML)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(&buff, t)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
e.HTML = buff.Bytes()
|
|
|
|
buff.Reset()
|
|
|
|
tmpl, err = template.New("text_template").Parse(c.Template.Text)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(&buff, t)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
e.Text = buff.Bytes()
|
|
|
|
buff.Reset()
|
2014-03-28 04:31:51 +00:00
|
|
|
Logger.Println("Creating email using template")
|
2014-06-05 04:54:46 +00:00
|
|
|
e.To = []string{t.Email}
|
2014-06-27 00:55:56 +00:00
|
|
|
err = e.Send(c.SMTP.Host, auth)
|
2014-06-05 04:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
2014-06-26 02:01:01 +00:00
|
|
|
err = t.UpdateStatus("Error")
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
} else {
|
2014-07-07 02:34:02 +00:00
|
|
|
err = t.UpdateStatus(models.EVENT_SENT)
|
2014-06-26 02:01:01 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2014-06-05 04:54:46 +00:00
|
|
|
}
|
2014-03-28 04:31:51 +00:00
|
|
|
Logger.Printf("Sending Email to %s\n", t.Email)
|
|
|
|
}
|
|
|
|
}
|