mirror of https://github.com/gophish/gophish
gofmt'ing
parent
9c7adb9941
commit
7bf2c00356
|
@ -352,84 +352,84 @@ func API_Pages_Id(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// API_SMTP handles requests for the /api/smtp/ endpoint
|
||||
func API_SMTP(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.Method == "GET":
|
||||
ss, err := models.GetSMTPs(ctx.Get(r, "user_id").(int64))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
JSONResponse(w, ss, http.StatusOK)
|
||||
//POST: Create a new SMTP and return it as JSON
|
||||
case r.Method == "POST":
|
||||
s := models.SMTP{}
|
||||
// Put the request into a page
|
||||
err := json.NewDecoder(r.Body).Decode(&s)
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "Invalid request"}, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// Check to make sure the name is unique
|
||||
_, err = models.GetSMTPByName(s.Name, ctx.Get(r, "user_id").(int64))
|
||||
if err != gorm.RecordNotFound {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "SMTP name already in use"}, http.StatusConflict)
|
||||
Logger.Println(err)
|
||||
return
|
||||
}
|
||||
s.ModifiedDate = time.Now()
|
||||
s.UserId = ctx.Get(r, "user_id").(int64)
|
||||
err = models.PostSMTP(&s)
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
JSONResponse(w, s, http.StatusCreated)
|
||||
}
|
||||
switch {
|
||||
case r.Method == "GET":
|
||||
ss, err := models.GetSMTPs(ctx.Get(r, "user_id").(int64))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
JSONResponse(w, ss, http.StatusOK)
|
||||
//POST: Create a new SMTP and return it as JSON
|
||||
case r.Method == "POST":
|
||||
s := models.SMTP{}
|
||||
// Put the request into a page
|
||||
err := json.NewDecoder(r.Body).Decode(&s)
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "Invalid request"}, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// Check to make sure the name is unique
|
||||
_, err = models.GetSMTPByName(s.Name, ctx.Get(r, "user_id").(int64))
|
||||
if err != gorm.RecordNotFound {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "SMTP name already in use"}, http.StatusConflict)
|
||||
Logger.Println(err)
|
||||
return
|
||||
}
|
||||
s.ModifiedDate = time.Now()
|
||||
s.UserId = ctx.Get(r, "user_id").(int64)
|
||||
err = models.PostSMTP(&s)
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
JSONResponse(w, s, http.StatusCreated)
|
||||
}
|
||||
}
|
||||
|
||||
// API_SMTP_Id contains functions to handle the GET'ing, DELETE'ing, and PUT'ing
|
||||
// of a SMTP object
|
||||
func API_SMTP_Id(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id, _ := strconv.ParseInt(vars["id"], 0, 64)
|
||||
s, err := models.GetSMTP(id, ctx.Get(r, "user_id").(int64))
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "SMTP not found"}, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case r.Method == "GET":
|
||||
JSONResponse(w, s, http.StatusOK)
|
||||
case r.Method == "DELETE":
|
||||
err = models.DeleteSMTP(id, ctx.Get(r, "user_id").(int64))
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "Error deleting SMTP"}, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
JSONResponse(w, models.Response{Success: true, Message: "SMTP Deleted Successfully"}, http.StatusOK)
|
||||
case r.Method == "PUT":
|
||||
s = models.SMTP{}
|
||||
err = json.NewDecoder(r.Body).Decode(&s)
|
||||
if err != nil {
|
||||
Logger.Println(err)
|
||||
}
|
||||
if s.Id != id {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "/:id and /:smtp_id mismatch"}, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
err = s.Validate()
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "Invalid attributes given"}, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
s.ModifiedDate = time.Now()
|
||||
s.UserId = ctx.Get(r, "user_id").(int64)
|
||||
err = models.PutSMTP(&s)
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "Error updating page"}, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
JSONResponse(w, s, http.StatusOK)
|
||||
}
|
||||
vars := mux.Vars(r)
|
||||
id, _ := strconv.ParseInt(vars["id"], 0, 64)
|
||||
s, err := models.GetSMTP(id, ctx.Get(r, "user_id").(int64))
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "SMTP not found"}, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case r.Method == "GET":
|
||||
JSONResponse(w, s, http.StatusOK)
|
||||
case r.Method == "DELETE":
|
||||
err = models.DeleteSMTP(id, ctx.Get(r, "user_id").(int64))
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "Error deleting SMTP"}, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
JSONResponse(w, models.Response{Success: true, Message: "SMTP Deleted Successfully"}, http.StatusOK)
|
||||
case r.Method == "PUT":
|
||||
s = models.SMTP{}
|
||||
err = json.NewDecoder(r.Body).Decode(&s)
|
||||
if err != nil {
|
||||
Logger.Println(err)
|
||||
}
|
||||
if s.Id != id {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "/:id and /:smtp_id mismatch"}, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
err = s.Validate()
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "Invalid attributes given"}, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
s.ModifiedDate = time.Now()
|
||||
s.UserId = ctx.Get(r, "user_id").(int64)
|
||||
err = models.PutSMTP(&s)
|
||||
if err != nil {
|
||||
JSONResponse(w, models.Response{Success: false, Message: "Error updating page"}, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
JSONResponse(w, s, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
// API_Import_Group imports a CSV of group members
|
||||
|
@ -536,15 +536,15 @@ func API_Send_Test_Email(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
// Get the sending profile requested by name
|
||||
s.SMTP, err = models.GetSMTPByName(s.SMTP.Name, ctx.Get(r, "user_id").(int64))
|
||||
if err == gorm.RecordNotFound {
|
||||
Logger.Printf("Error - Sending profile %s does not exist", s.SMTP.Name)
|
||||
JSONResponse(w, models.Response{Success: false, Message: models.ErrSMTPNotFound.Error()}, http.StatusBadRequest)
|
||||
} else if err != nil {
|
||||
Logger.Println(err)
|
||||
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
s.SMTP, err = models.GetSMTPByName(s.SMTP.Name, ctx.Get(r, "user_id").(int64))
|
||||
if err == gorm.RecordNotFound {
|
||||
Logger.Printf("Error - Sending profile %s does not exist", s.SMTP.Name)
|
||||
JSONResponse(w, models.Response{Success: false, Message: models.ErrSMTPNotFound.Error()}, http.StatusBadRequest)
|
||||
} else if err != nil {
|
||||
Logger.Println(err)
|
||||
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// Send the test email
|
||||
err = worker.SendTestEmail(s)
|
||||
if err != nil {
|
||||
|
|
|
@ -316,14 +316,14 @@ func LandingPages(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// SendingProfiles handles the default path and template execution
|
||||
func SendingProfiles(w http.ResponseWriter, r *http.Request) {
|
||||
// Example of using session - will be removed.
|
||||
params := struct {
|
||||
User models.User
|
||||
Title string
|
||||
Flashes []interface{}
|
||||
Token string
|
||||
}{Title: "Sending Profiles", User: ctx.Get(r, "user").(models.User), Token: nosurf.Token(r)}
|
||||
getTemplate(w, "sending_profiles").ExecuteTemplate(w, "base", params)
|
||||
// Example of using session - will be removed.
|
||||
params := struct {
|
||||
User models.User
|
||||
Title string
|
||||
Flashes []interface{}
|
||||
Token string
|
||||
}{Title: "Sending Profiles", User: ctx.Get(r, "user").(models.User), Token: nosurf.Token(r)}
|
||||
getTemplate(w, "sending_profiles").ExecuteTemplate(w, "base", params)
|
||||
}
|
||||
|
||||
// Settings handles the changing of settings
|
||||
|
|
|
@ -10,22 +10,22 @@ import (
|
|||
|
||||
//Campaign is a struct representing a created campaign
|
||||
type Campaign struct {
|
||||
Id int64 `json:"id"`
|
||||
UserId int64 `json:"-"`
|
||||
Name string `json:"name" sql:"not null"`
|
||||
CreatedDate time.Time `json:"created_date"`
|
||||
CompletedDate time.Time `json:"completed_date"`
|
||||
TemplateId int64 `json:"-"`
|
||||
Template Template `json:"template"`
|
||||
PageId int64 `json:"-"`
|
||||
Page Page `json:"page"`
|
||||
Status string `json:"status"`
|
||||
Results []Result `json:"results,omitempty"`
|
||||
Groups []Group `json:"groups,omitempty"`
|
||||
Events []Event `json:"timeline,omitemtpy"`
|
||||
SMTPId int64 `json:"-"`
|
||||
SMTP SMTP `json:"smtp"`
|
||||
URL string `json:"url"`
|
||||
Id int64 `json:"id"`
|
||||
UserId int64 `json:"-"`
|
||||
Name string `json:"name" sql:"not null"`
|
||||
CreatedDate time.Time `json:"created_date"`
|
||||
CompletedDate time.Time `json:"completed_date"`
|
||||
TemplateId int64 `json:"-"`
|
||||
Template Template `json:"template"`
|
||||
PageId int64 `json:"-"`
|
||||
Page Page `json:"page"`
|
||||
Status string `json:"status"`
|
||||
Results []Result `json:"results,omitempty"`
|
||||
Groups []Group `json:"groups,omitempty"`
|
||||
Events []Event `json:"timeline,omitemtpy"`
|
||||
SMTPId int64 `json:"-"`
|
||||
SMTP SMTP `json:"smtp"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// ErrCampaignNameNotSpecified indicates there was no template given by the user
|
||||
|
@ -95,7 +95,7 @@ func (s *SendTestEmailRequest) Validate() error {
|
|||
return ErrEmailNotSpecified
|
||||
case s.SMTP.Name == "":
|
||||
return ErrSMTPNotSpecified
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -179,9 +179,9 @@ func GetCampaign(id int64, uid int64) (Campaign, error) {
|
|||
Logger.Printf("%s: page not found for campaign\n", err)
|
||||
}
|
||||
err = db.Table("SMTP").Where("id=?", c.SMTPId).Find(&c.SMTP).Error
|
||||
if err != nil {
|
||||
Logger.Printf("%s: sending profile not found for campaign\n", err)
|
||||
}
|
||||
if err != nil {
|
||||
Logger.Printf("%s: sending profile not found for campaign\n", err)
|
||||
}
|
||||
return c, err
|
||||
}
|
||||
|
||||
|
@ -229,16 +229,16 @@ func PostCampaign(c *Campaign, uid int64) error {
|
|||
c.Page = p
|
||||
c.PageId = p.Id
|
||||
// Check to make sure the sending profile exists
|
||||
s, err := GetSMTPByName(c.SMTP.Name, uid)
|
||||
if err == gorm.RecordNotFound {
|
||||
Logger.Printf("Error - Sending profile %s does not exist", s.Name)
|
||||
return ErrPageNotFound
|
||||
} else if err != nil {
|
||||
Logger.Println(err)
|
||||
return err
|
||||
}
|
||||
c.SMTP = s
|
||||
c.SMTPId = s.Id
|
||||
s, err := GetSMTPByName(c.SMTP.Name, uid)
|
||||
if err == gorm.RecordNotFound {
|
||||
Logger.Printf("Error - Sending profile %s does not exist", s.Name)
|
||||
return ErrPageNotFound
|
||||
} else if err != nil {
|
||||
Logger.Println(err)
|
||||
return err
|
||||
}
|
||||
c.SMTP = s
|
||||
c.SMTPId = s.Id
|
||||
// Insert into the DB
|
||||
err = db.Save(c).Error
|
||||
if err != nil {
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SMTP contains the attributes needed to handle the sending of campaign emails
|
||||
type SMTP struct {
|
||||
Id int64 `json:"id" gorm:"column:id; primary_key:yes"`
|
||||
UserId int64 `json:"-" gorm:"column:user_id"`
|
||||
Interface string `json:"interface_type" gorm:"column:interface_type"`
|
||||
Name string `json:"name"`
|
||||
Host string `json:"host"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
FromAddress string `json:"from_address"`
|
||||
IgnoreCertErrors bool `json:"ignore_cert_errors"`
|
||||
ModifiedDate time.Time `json:"modified_date"`
|
||||
Id int64 `json:"id" gorm:"column:id; primary_key:yes"`
|
||||
UserId int64 `json:"-" gorm:"column:user_id"`
|
||||
Interface string `json:"interface_type" gorm:"column:interface_type"`
|
||||
Name string `json:"name"`
|
||||
Host string `json:"host"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
FromAddress string `json:"from_address"`
|
||||
IgnoreCertErrors bool `json:"ignore_cert_errors"`
|
||||
ModifiedDate time.Time `json:"modified_date"`
|
||||
}
|
||||
|
||||
// ErrFromAddressNotSpecified is thrown when there is no "From" address
|
||||
|
|
Loading…
Reference in New Issue