Refined models

Added *basic* worker functionality - emails get sent now! woo hoo!
pull/24/head
Jordan 2014-06-04 23:54:46 -05:00
parent db24496fb0
commit a1b6218473
6 changed files with 38 additions and 22 deletions

View File

@ -17,9 +17,10 @@ type Campaign struct {
TemplateId int64 `json:"-"` TemplateId int64 `json:"-"`
Template Template `json:"template"` //This may change Template Template `json:"template"` //This may change
Status string `json:"status"` Status string `json:"status"`
EmailsSent string `json:"emails_sent"`
Results []Result `json:"results,omitempty"` Results []Result `json:"results,omitempty"`
Groups []Group `json:"groups,omitempty"` Groups []Group `json:"groups,omitempty"`
SMTP SMTP `json:"options,omitempty"` SMTP SMTP `json:"smtp"`
} }
func (c *Campaign) Validate() (string, bool) { func (c *Campaign) Validate() (string, bool) {
@ -90,8 +91,8 @@ func PostCampaign(c *Campaign, uid int64) error {
Logger.Println(err) Logger.Println(err)
return err return err
} }
c.Template = t
c.TemplateId = t.Id c.TemplateId = t.Id
// Insert into the DB // Insert into the DB
err = db.Save(c).Error err = db.Save(c).Error
if err != nil { if err != nil {

View File

@ -3,9 +3,7 @@ package models
type SMTP struct { type SMTP struct {
SMTPId int64 `json:"-"` SMTPId int64 `json:"-"`
CampaignId int64 `json:"-"` CampaignId int64 `json:"-"`
Hostname string `json:"hostname"` Host string `json:"host"`
Port int `json:"port"`
UseAuth bool `json:"use_auth"`
Username string `json:"username,omitempty"` Username string `json:"username,omitempty"`
Password string `json:"password,omitempty" sql:"-"` Password string `json:"password,omitempty" sql:"-"`
FromAddress string `json:"from_address"` FromAddress string `json:"from_address"`
@ -13,14 +11,10 @@ type SMTP struct {
func (s *SMTP) Validate() (string, bool) { func (s *SMTP) Validate() (string, bool) {
switch { switch {
case s.UseAuth == false && (s.Username == "" && s.Password == ""):
return "Auth requested, but username or password blank", false
case s.FromAddress == "": case s.FromAddress == "":
return "No from address specified", false return "No from address specified", false
case s.Hostname == "": case s.Host == "":
return "No hostname specified", false return "No hostname specified", false
case s.Port == 0:
return "No port specified", false
} }
return "", true return "", true
} }

View File

@ -6,8 +6,9 @@ type Template struct {
Id int64 `json:"id"` Id int64 `json:"id"`
UserId int64 `json:"-"` UserId int64 `json:"-"`
Name string `json:"name"` Name string `json:"name"`
Subject string `json:"subject"`
Text string `json:"text"` Text string `json:"text"`
Html string `json:"html"` HTML string `json:"html"`
ModifiedDate time.Time `json:"modified_date"` ModifiedDate time.Time `json:"modified_date"`
} }
@ -15,7 +16,7 @@ func (t *Template) Validate() (string, bool) {
switch { switch {
case t.Name == "": case t.Name == "":
return "Template Name not specified", false return "Template Name not specified", false
case t.Text == "" && t.Html == "": case t.Text == "" && t.HTML == "":
return "Need to specify at least plaintext or HTML format", false return "Need to specify at least plaintext or HTML format", false
} }
return "", true return "", true

View File

@ -17,13 +17,10 @@
SMTP Options <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': isOpen, 'glyphicon-chevron-right': !isOpen}"></i> SMTP Options <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': isOpen, 'glyphicon-chevron-right': !isOpen}"></i>
</accordion-heading> </accordion-heading>
<label class="control-label" for="from">From:</label> <label class="control-label" for="from">From:</label>
<input type="text" class="form-control" placeholder="First Last <test@example.com>" id="form"> <input type="text" class="form-control" ng-model="campaign.smtp.from_address" placeholder="First Last <test@example.com>" id="form">
<br /> <br />
<label class="control-label" for="smtp_server">Hostname:</label> <label class="control-label" for="smtp_server">Host:</label>
<input type="text" class="form-control" ng-model="campaign.smtp.hostname" placeholder="host:port" id="smtp_server"> <input type="text" class="form-control" ng-model="campaign.smtp.host" placeholder="smtp.example.com:25" id="smtp_server">
<br />
<label class="control-label" for="smtp_server">Port:</label>
<input type="text" class="form-control" ng-model="campaign.smtp.port" placeholder="host:port" id="smtp_server">
<br /> <br />
<label class="control-label" for="smtp_server">Username:</label> <label class="control-label" for="smtp_server">Username:</label>
<input type="text" class="form-control" ng-model="campaign.smtp.username" placeholder="host:port" id="smtp_server"> <input type="text" class="form-control" ng-model="campaign.smtp.username" placeholder="host:port" id="smtp_server">

View File

@ -9,6 +9,10 @@
<div class="form-group"> <div class="form-group">
<input type="text" class="form-control" ng-model="template.name" placeholder="Template name" id="name" autofocus/> <input type="text" class="form-control" ng-model="template.name" placeholder="Template name" id="name" autofocus/>
</div> </div>
<label class="control-label" for="subject">Subject:</label>
<div class="form-group">
<input type="text" class="form-control" ng-model="template.subject" placeholder="Email Subject" id="subject"/>
</div>
<fieldset disabled> <fieldset disabled>
<div class="form-group"> <div class="form-group">
<button class="btn btn-danger btn-disabled"><i class="fa fa-envelope"></i> Import Email (Coming Soon!)</button> <button class="btn btn-danger btn-disabled"><i class="fa fa-envelope"></i> Import Email (Coming Soon!)</button>

View File

@ -2,8 +2,11 @@ package worker
import ( import (
"log" "log"
"net/smtp"
"os" "os"
"strings"
"github.com/jordan-wright/email"
"github.com/jordan-wright/gophish/models" "github.com/jordan-wright/gophish/models"
) )
@ -34,12 +37,28 @@ func processCampaign(c *models.Campaign) {
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
} }
e := email.Email{
Subject: c.Template.Subject,
From: c.SMTP.FromAddress,
Text: []byte(c.Template.Text),
HTML: []byte(c.Template.HTML),
}
Logger.Println(c.SMTP.Username)
Logger.Println(c.SMTP.Password)
Logger.Println(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])
}
for _, t := range c.Results { for _, t := range c.Results {
Logger.Println("Creating email using template") Logger.Println("Creating email using template")
/*e := email.Email{ e.To = []string{t.Email}
Text: []byte(c.Template.Text), Logger.Println(e.To)
HTML: []byte(c.Template.Html), Logger.Println(e.From)
}*/ err := e.Send(c.SMTP.Host, auth)
if err != nil {
Logger.Println(err)
}
Logger.Printf("Sending Email to %s\n", t.Email) Logger.Printf("Sending Email to %s\n", t.Email)
} }
} }