mirror of https://github.com/gophish/gophish
Updated smtp model and api to support managing SMTP objects as independent entities
parent
b7396fa149
commit
dde2312183
|
@ -146,8 +146,8 @@ func API_Groups(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// API_Groups_Id returns details about the requested campaign. If the campaign is not
|
// API_Groups_Id returns details about the requested campaign. If the group is not
|
||||||
// valid, API_Campaigns_Id returns null.
|
// valid, API_Groups_Id returns null.
|
||||||
func API_Groups_Id(w http.ResponseWriter, r *http.Request) {
|
func API_Groups_Id(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
id, _ := strconv.ParseInt(vars["id"], 0, 64)
|
id, _ := strconv.ParseInt(vars["id"], 0, 64)
|
||||||
|
@ -354,35 +354,35 @@ func API_Pages_Id(w http.ResponseWriter, r *http.Request) {
|
||||||
func API_SMTP(w http.ResponseWriter, r *http.Request) {
|
func API_SMTP(w http.ResponseWriter, r *http.Request) {
|
||||||
switch {
|
switch {
|
||||||
case r.Method == "GET":
|
case r.Method == "GET":
|
||||||
ps, err := models.GetPages(ctx.Get(r, "user_id").(int64))
|
ss, err := models.GetSMTPs(ctx.Get(r, "user_id").(int64))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
JSONResponse(w, ps, http.StatusOK)
|
JSONResponse(w, ss, http.StatusOK)
|
||||||
//POST: Create a new page and return it as JSON
|
//POST: Create a new SMTP and return it as JSON
|
||||||
case r.Method == "POST":
|
case r.Method == "POST":
|
||||||
p := models.Page{}
|
s := models.SMTP{}
|
||||||
// Put the request into a page
|
// Put the request into a page
|
||||||
err := json.NewDecoder(r.Body).Decode(&p)
|
err := json.NewDecoder(r.Body).Decode(&s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
JSONResponse(w, models.Response{Success: false, Message: "Invalid request"}, http.StatusBadRequest)
|
JSONResponse(w, models.Response{Success: false, Message: "Invalid request"}, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Check to make sure the name is unique
|
// Check to make sure the name is unique
|
||||||
_, err = models.GetPageByName(p.Name, ctx.Get(r, "user_id").(int64))
|
_, err = models.GetSMTPByName(s.Name, ctx.Get(r, "user_id").(int64))
|
||||||
if err != gorm.RecordNotFound {
|
if err != gorm.RecordNotFound {
|
||||||
JSONResponse(w, models.Response{Success: false, Message: "Page name already in use"}, http.StatusConflict)
|
JSONResponse(w, models.Response{Success: false, Message: "SMTP name already in use"}, http.StatusConflict)
|
||||||
Logger.Println(err)
|
Logger.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p.ModifiedDate = time.Now()
|
s.ModifiedDate = time.Now()
|
||||||
p.UserId = ctx.Get(r, "user_id").(int64)
|
s.UserId = ctx.Get(r, "user_id").(int64)
|
||||||
err = models.PostPage(&p)
|
err = models.PostSMTP(&s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError)
|
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
JSONResponse(w, p, http.StatusCreated)
|
JSONResponse(w, s, http.StatusCreated)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,44 +391,44 @@ func API_SMTP(w http.ResponseWriter, r *http.Request) {
|
||||||
func API_SMTP_Id(w http.ResponseWriter, r *http.Request) {
|
func API_SMTP_Id(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
id, _ := strconv.ParseInt(vars["id"], 0, 64)
|
id, _ := strconv.ParseInt(vars["id"], 0, 64)
|
||||||
p, err := models.GetPage(id, ctx.Get(r, "user_id").(int64))
|
s, err := models.GetSMTP(id, ctx.Get(r, "user_id").(int64))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
JSONResponse(w, models.Response{Success: false, Message: "Page not found"}, http.StatusNotFound)
|
JSONResponse(w, models.Response{Success: false, Message: "SMTP not found"}, http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
case r.Method == "GET":
|
case r.Method == "GET":
|
||||||
JSONResponse(w, p, http.StatusOK)
|
JSONResponse(w, s, http.StatusOK)
|
||||||
case r.Method == "DELETE":
|
case r.Method == "DELETE":
|
||||||
err = models.DeletePage(id, ctx.Get(r, "user_id").(int64))
|
err = models.DeleteSMTP(id, ctx.Get(r, "user_id").(int64))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
JSONResponse(w, models.Response{Success: false, Message: "Error deleting page"}, http.StatusInternalServerError)
|
JSONResponse(w, models.Response{Success: false, Message: "Error deleting SMTP"}, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
JSONResponse(w, models.Response{Success: true, Message: "Page Deleted Successfully"}, http.StatusOK)
|
JSONResponse(w, models.Response{Success: true, Message: "SMTP Deleted Successfully"}, http.StatusOK)
|
||||||
case r.Method == "PUT":
|
case r.Method == "PUT":
|
||||||
p = models.Page{}
|
s = models.SMTP{}
|
||||||
err = json.NewDecoder(r.Body).Decode(&p)
|
err = json.NewDecoder(r.Body).Decode(&s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Logger.Println(err)
|
Logger.Println(err)
|
||||||
}
|
}
|
||||||
if p.Id != id {
|
if s.Id != id {
|
||||||
JSONResponse(w, models.Response{Success: false, Message: "/:id and /:page_id mismatch"}, http.StatusBadRequest)
|
JSONResponse(w, models.Response{Success: false, Message: "/:id and /:smtp_id mismatch"}, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = p.Validate()
|
err = s.Validate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
JSONResponse(w, models.Response{Success: false, Message: "Invalid attributes given"}, http.StatusBadRequest)
|
JSONResponse(w, models.Response{Success: false, Message: "Invalid attributes given"}, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p.ModifiedDate = time.Now()
|
s.ModifiedDate = time.Now()
|
||||||
p.UserId = ctx.Get(r, "user_id").(int64)
|
s.UserId = ctx.Get(r, "user_id").(int64)
|
||||||
err = models.PutPage(&p)
|
err = models.PutSMTP(&s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
JSONResponse(w, models.Response{Success: false, Message: "Error updating page"}, http.StatusInternalServerError)
|
JSONResponse(w, models.Response{Success: false, Message: "Error updating page"}, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
JSONResponse(w, p, http.StatusOK)
|
JSONResponse(w, s, http.StatusOK)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,23 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
// "github.com/jinzhu/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
// SMTP contains the attributes needed to handle the sending of campaign emails
|
// SMTP contains the attributes needed to handle the sending of campaign emails
|
||||||
type SMTP struct {
|
type SMTP struct {
|
||||||
SMTPId int64 `json:"-" gorm:"column:smtp_id; primary_key:yes"`
|
Id int64 `json:"-" gorm:"column:id; primary_key:yes"`
|
||||||
CampaignId int64 `json:"-" gorm:"column:campaign_id"`
|
Name string `json:"-" gorm:"column:name"`
|
||||||
|
UserId int64 `json:"-" gorm:"column:user_id"`
|
||||||
Host string `json:"host"`
|
Host string `json:"host"`
|
||||||
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"`
|
||||||
IgnoreCertErrors bool `json:"ignore_cert_errors"`
|
IgnoreCertErrors bool `json:"ignore_cert_errors"`
|
||||||
|
ModifiedDate time.Time `json:"modified_date"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrFromAddressNotSpecified is thrown when there is no "From" address
|
// ErrFromAddressNotSpecified is thrown when there is no "From" address
|
||||||
|
@ -36,3 +43,71 @@ func (s *SMTP) Validate() error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetSMTPs returns the SMTPs owned by the given user.
|
||||||
|
func GetSMTPs(uid int64) ([]SMTP, error) {
|
||||||
|
ss := []SMTP{}
|
||||||
|
err := db.Where("user_id=?", uid).Find(&ss).Error
|
||||||
|
if err != nil {
|
||||||
|
Logger.Println(err)
|
||||||
|
return ss, err
|
||||||
|
}
|
||||||
|
return ss, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSMTP returns the SMTP, if it exists, specified by the given id and user_id.
|
||||||
|
func GetSMTP(id int64, uid int64) (SMTP, error) {
|
||||||
|
s := SMTP{}
|
||||||
|
err := db.Where("user_id=? and id=?", uid, id).Find(&s).Error
|
||||||
|
if err != nil {
|
||||||
|
Logger.Println(err)
|
||||||
|
return s, err
|
||||||
|
}
|
||||||
|
return s, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSMTPByName returns the SMTP, if it exists, specified by the given name and user_id.
|
||||||
|
func GetSMTPByName(n string, uid int64) (SMTP, error) {
|
||||||
|
s := SMTP{}
|
||||||
|
err := db.Where("user_id=? and name=?", uid, n).Find(&s).Error
|
||||||
|
if err != nil {
|
||||||
|
Logger.Println(err)
|
||||||
|
}
|
||||||
|
return s, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// PostSMTP creates a new SMTP in the database.
|
||||||
|
func PostSMTP(s *SMTP) error {
|
||||||
|
err := s.Validate()
|
||||||
|
if err != nil {
|
||||||
|
Logger.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Insert into the DB
|
||||||
|
err = db.Save(s).Error
|
||||||
|
if err != nil {
|
||||||
|
Logger.Println(err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutSMTP edits an existing SMTP in the database.
|
||||||
|
// Per the PUT Method RFC, it presumes all data for a SMTP is provided.
|
||||||
|
func PutSMTP(s *SMTP) error {
|
||||||
|
err := db.Where("id=?", s.Id).Save(s).Error
|
||||||
|
if err != nil {
|
||||||
|
Logger.Println(err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteSMTP deletes an existing SMTP in the database.
|
||||||
|
// An error is returned if a SMTP with the given user id and SMTP id is not found.
|
||||||
|
func DeleteSMTP(id int64, uid int64) error {
|
||||||
|
err = db.Where("user_id=?", uid).Delete(SMTP{Id: id}).Error
|
||||||
|
if err != nil {
|
||||||
|
Logger.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue