mirror of https://github.com/gophish/gophish
Fixed bug in POSTing campaign
Removed gorp initialization code from models.gopull/24/head
parent
6e5361b210
commit
5740ee3273
|
@ -80,6 +80,7 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) {
|
||||||
c.CreatedDate = time.Now()
|
c.CreatedDate = time.Now()
|
||||||
c.CompletedDate = time.Time{}
|
c.CompletedDate = time.Time{}
|
||||||
c.Status = IN_PROGRESS
|
c.Status = IN_PROGRESS
|
||||||
|
c.UserId = ctx.Get(r, "user_id").(int64)
|
||||||
err = models.PostCampaign(&c, ctx.Get(r, "user_id").(int64))
|
err = models.PostCampaign(&c, ctx.Get(r, "user_id").(int64))
|
||||||
if checkError(err, w, "Cannot insert campaign into database", http.StatusInternalServerError) {
|
if checkError(err, w, "Cannot insert campaign into database", http.StatusInternalServerError) {
|
||||||
return
|
return
|
||||||
|
|
|
@ -74,7 +74,7 @@ func PostCampaign(c *Campaign, uid int64) error {
|
||||||
}
|
}
|
||||||
// Insert into the DB
|
// Insert into the DB
|
||||||
/*err = Conn.Insert(c)*/
|
/*err = Conn.Insert(c)*/
|
||||||
err = db.Debug().Save(&c).Error
|
err = db.Debug().Save(c).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Logger.Println(err)
|
Logger.Println(err)
|
||||||
return err
|
return err
|
||||||
|
|
109
models/models.go
109
models/models.go
|
@ -3,10 +3,8 @@ package models
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/coopernurse/gorp"
|
"github.com/coopernurse/gorp"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
@ -21,55 +19,33 @@ var err error
|
||||||
var ErrUsernameTaken = errors.New("username already taken")
|
var ErrUsernameTaken = errors.New("username already taken")
|
||||||
var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile)
|
var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
|
||||||
|
// Flash is used to hold flash information for use in templates.
|
||||||
|
type Flash struct {
|
||||||
|
Type string
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
// Setup initializes the Conn object
|
// Setup initializes the Conn object
|
||||||
// It also populates the Gophish Config object
|
// It also populates the Gophish Config object
|
||||||
func Setup() error {
|
func Setup() error {
|
||||||
DB, err := sql.Open("sqlite3", config.Conf.DBPath)
|
db, err = gorm.Open("sqlite3", config.Conf.DBPath)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("%v", err)
|
|
||||||
}
|
|
||||||
db, err = gorm.Open("sqlite3", "gophish_gorm.db")
|
|
||||||
db.SetLogger(Logger)
|
db.SetLogger(Logger)
|
||||||
db.CreateTable(User{})
|
if err != nil {
|
||||||
db.CreateTable(Target{})
|
Logger.Println(err)
|
||||||
db.CreateTable(Result{})
|
return err
|
||||||
db.CreateTable(Group{})
|
}
|
||||||
db.CreateTable(GroupTarget{})
|
|
||||||
db.CreateTable(UserGroup{})
|
|
||||||
db.CreateTable(Template{})
|
|
||||||
db.CreateTable(Campaign{})
|
|
||||||
Conn = &gorp.DbMap{Db: DB, Dialect: gorp.SqliteDialect{}}
|
|
||||||
//If the file already exists, delete it and recreate it
|
//If the file already exists, delete it and recreate it
|
||||||
_, err = os.Stat(config.Conf.DBPath)
|
_, err = os.Stat(config.Conf.DBPath)
|
||||||
Conn.AddTableWithName(User{}, "users").SetKeys(true, "Id")
|
|
||||||
Conn.AddTableWithName(Campaign{}, "campaigns").SetKeys(true, "Id")
|
|
||||||
Conn.AddTableWithName(Group{}, "groups").SetKeys(true, "Id")
|
|
||||||
Conn.AddTableWithName(Template{}, "templates").SetKeys(true, "Id")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Logger.Println("Database not found, recreating...")
|
Logger.Printf("Database not found... creating db at %s\n", config.Conf.DBPath)
|
||||||
createTablesSQL := []string{
|
db.CreateTable(User{})
|
||||||
//Create tables
|
db.CreateTable(Target{})
|
||||||
`CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, hash VARCHAR(60) NOT NULL, api_key VARCHAR(32), UNIQUE(username), UNIQUE(api_key));`,
|
db.CreateTable(Result{})
|
||||||
`CREATE TABLE campaigns (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, created_date TIMESTAMP NOT NULL, completed_date TIMESTAMP, template TEXT, status TEXT NOT NULL);`,
|
db.CreateTable(Group{})
|
||||||
`CREATE TABLE targets (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, UNIQUE(email));`,
|
db.CreateTable(GroupTarget{})
|
||||||
`CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL);`,
|
db.CreateTable(UserGroup{})
|
||||||
`CREATE TABLE campaign_results (cid INTEGER NOT NULL, email TEXT NOT NULL, status TEXT NOT NULL, FOREIGN KEY (cid) REFERENCES campaigns(id), UNIQUE(cid, email, status))`,
|
db.CreateTable(Template{})
|
||||||
`CREATE TABLE templates (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL, html TEXT NOT NULL, text TEXT NOT NULL);`,
|
db.CreateTable(Campaign{})
|
||||||
`CREATE TABLE files (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, path TEXT NOT NULL);`,
|
|
||||||
`CREATE TABLE user_campaigns (uid INTEGER NOT NULL, cid INTEGER NOT NULL, FOREIGN KEY (uid) REFERENCES users(id), FOREIGN KEY (cid) REFERENCES campaigns(id), UNIQUE(uid, cid))`,
|
|
||||||
`CREATE TABLE user_groups (uid INTEGER NOT NULL, gid INTEGER NOT NULL, FOREIGN KEY (uid) REFERENCES users(id), FOREIGN KEY (gid) REFERENCES groups(id), UNIQUE(uid, gid))`,
|
|
||||||
`CREATE TABLE group_targets (gid INTEGER NOT NULL, tid INTEGER NOT NULL, FOREIGN KEY (gid) REFERENCES groups(id), FOREIGN KEY (tid) REFERENCES targets(id), UNIQUE(gid, tid));`,
|
|
||||||
`CREATE TABLE user_templates (uid INTEGER NOT NULL, tid INTEGER NOT NULL, FOREIGN KEY (uid) REFERENCES users(id), FOREIGN KEY (tid) REFERENCES templates(id), UNIQUE(uid, tid));`,
|
|
||||||
`CREATE TABLE template_files (tid INTEGER NOT NULL, fid INTEGER NOT NULL, FOREIGN KEY (tid) REFERENCES templates(id), FOREIGN KEY(fid) REFERENCES files(id), UNIQUE(tid, fid));`,
|
|
||||||
}
|
|
||||||
Logger.Printf("Creating db at %s\n", config.Conf.DBPath)
|
|
||||||
//Create the tables needed
|
|
||||||
for _, stmt := range createTablesSQL {
|
|
||||||
_, err = DB.Exec(stmt)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//Create the default user
|
//Create the default user
|
||||||
init_user := User{
|
init_user := User{
|
||||||
Username: "admin",
|
Username: "admin",
|
||||||
|
@ -80,51 +56,6 @@ func Setup() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Logger.Println(err)
|
Logger.Println(err)
|
||||||
}
|
}
|
||||||
init_campaign := Campaign{
|
|
||||||
UserId: 1,
|
|
||||||
Name: "First Campaign",
|
|
||||||
CreatedDate: time.Now(), //gophish
|
|
||||||
CompletedDate: time.Now(),
|
|
||||||
Template: "",
|
|
||||||
Status: "In Progress",
|
|
||||||
Results: []Result{},
|
|
||||||
Groups: []Group{},
|
|
||||||
}
|
|
||||||
err = db.Debug().Save(&init_campaign).Error
|
|
||||||
if err != nil {
|
|
||||||
Logger.Println(err)
|
|
||||||
}
|
|
||||||
init_result := Result{
|
|
||||||
CampaignId: 1,
|
|
||||||
Email: "test@example.com",
|
|
||||||
Status: "Unsuccessful",
|
|
||||||
}
|
|
||||||
err = db.Debug().Save(&init_result).Error
|
|
||||||
if err != nil {
|
|
||||||
Logger.Println(err)
|
|
||||||
}
|
|
||||||
init_group := Group{
|
|
||||||
Name: "New Group",
|
|
||||||
ModifiedDate: time.Now(),
|
|
||||||
Targets: []Target{Target{
|
|
||||||
Email: "test@example.com"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
err = PostGroup(&init_group, 1)
|
|
||||||
if err != nil {
|
|
||||||
Logger.Println(err)
|
|
||||||
}
|
|
||||||
init_group.Targets = []Target{Target{Email: "test2@example.com"}}
|
|
||||||
err = PutGroup(&init_group, 1)
|
|
||||||
if err != nil {
|
|
||||||
Logger.Println(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flash is used to hold flash information for use in templates.
|
|
||||||
type Flash struct {
|
|
||||||
Type string
|
|
||||||
Message string
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue