2014-01-09 06:42:05 +00:00
|
|
|
package models
|
2013-12-12 06:27:43 +00:00
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"os"
|
2014-01-13 03:36:26 +00:00
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
"github.com/coopernurse/gorp"
|
2014-03-26 04:53:51 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2014-03-25 03:31:33 +00:00
|
|
|
"github.com/jordan-wright/gophish/config"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
2014-01-13 03:36:26 +00:00
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
var Conn *gorp.DbMap
|
2014-03-26 04:53:51 +00:00
|
|
|
var db gorm.DB
|
2014-03-25 03:31:33 +00:00
|
|
|
var err error
|
2014-03-26 04:53:51 +00:00
|
|
|
var ErrUsernameTaken = errors.New("username already taken")
|
|
|
|
var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile)
|
2013-12-12 07:00:22 +00:00
|
|
|
|
2014-03-28 04:31:51 +00:00
|
|
|
const (
|
|
|
|
IN_PROGRESS string = "In progress"
|
|
|
|
QUEUED string = "Queued"
|
|
|
|
COMPLETE string = "Completed"
|
|
|
|
ERROR string = "Error"
|
|
|
|
)
|
|
|
|
|
2014-03-26 19:50:16 +00:00
|
|
|
// Flash is used to hold flash information for use in templates.
|
|
|
|
type Flash struct {
|
|
|
|
Type string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2014-06-02 03:30:23 +00:00
|
|
|
type Response struct {
|
2014-06-02 04:38:21 +00:00
|
|
|
Message string `json:"message"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Data interface{} `json:"data"`
|
2014-06-02 03:30:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// Setup initializes the Conn object
|
|
|
|
// It also populates the Gophish Config object
|
2014-03-25 03:38:59 +00:00
|
|
|
func Setup() error {
|
2014-03-26 19:50:16 +00:00
|
|
|
db, err = gorm.Open("sqlite3", config.Conf.DBPath)
|
2014-03-28 04:31:51 +00:00
|
|
|
db.LogMode(false)
|
2014-03-26 19:50:16 +00:00
|
|
|
db.SetLogger(Logger)
|
2014-03-26 04:53:51 +00:00
|
|
|
if err != nil {
|
2014-03-26 19:50:16 +00:00
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
2014-03-26 04:53:51 +00:00
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
//If the file already exists, delete it and recreate it
|
|
|
|
_, err = os.Stat(config.Conf.DBPath)
|
|
|
|
if err != nil {
|
2014-03-26 19:50:16 +00:00
|
|
|
Logger.Printf("Database not found... creating db at %s\n", config.Conf.DBPath)
|
|
|
|
db.CreateTable(User{})
|
|
|
|
db.CreateTable(Target{})
|
|
|
|
db.CreateTable(Result{})
|
|
|
|
db.CreateTable(Group{})
|
|
|
|
db.CreateTable(GroupTarget{})
|
|
|
|
db.CreateTable(Template{})
|
2014-06-03 18:27:20 +00:00
|
|
|
db.CreateTable(SMTP{})
|
2014-03-26 19:50:16 +00:00
|
|
|
db.CreateTable(Campaign{})
|
2014-03-25 03:31:33 +00:00
|
|
|
//Create the default user
|
|
|
|
init_user := User{
|
|
|
|
Username: "admin",
|
|
|
|
Hash: "$2a$10$IYkPp0.QsM81lYYPrQx6W.U6oQGw7wMpozrKhKAHUBVL4mkm/EvAS", //gophish
|
2014-03-26 04:53:51 +00:00
|
|
|
ApiKey: "12345678901234567890123456789012",
|
|
|
|
}
|
|
|
|
err = db.Save(&init_user).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
2014-03-25 03:38:59 +00:00
|
|
|
return nil
|
2013-12-12 07:00:22 +00:00
|
|
|
}
|