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-26 04:53:51 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2014-03-25 03:31:33 +00:00
|
|
|
"github.com/jordan-wright/gophish/config"
|
2015-02-07 02:24:10 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3" // Blank import needed to import sqlite3
|
2014-03-25 03:31:33 +00:00
|
|
|
)
|
2014-01-13 03:36:26 +00:00
|
|
|
|
2014-03-26 04:53:51 +00:00
|
|
|
var db gorm.DB
|
2014-03-25 03:31:33 +00:00
|
|
|
var err error
|
2015-02-07 02:24:10 +00:00
|
|
|
|
|
|
|
// ErrUsernameTaken is thrown when a user attempts to register a username that is taken.
|
2014-03-26 04:53:51 +00:00
|
|
|
var ErrUsernameTaken = errors.New("username already taken")
|
2015-02-07 02:24:10 +00:00
|
|
|
|
|
|
|
// Logger is a global logger used to show informational, warning, and error messages
|
2014-03-26 04:53:51 +00:00
|
|
|
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 (
|
2014-07-02 01:32:34 +00:00
|
|
|
CAMPAIGN_IN_PROGRESS string = "In progress"
|
|
|
|
CAMPAIGN_QUEUED string = "Queued"
|
|
|
|
CAMPAIGN_COMPLETE string = "Completed"
|
2014-07-07 02:34:02 +00:00
|
|
|
EVENT_SENT string = "Email Sent"
|
|
|
|
EVENT_OPENED string = "Email Opened"
|
|
|
|
EVENT_CLICKED string = "Clicked Link"
|
|
|
|
STATUS_SUCCESS string = "Success"
|
|
|
|
STATUS_UNKNOWN string = "Unknown"
|
2014-07-02 01:32:34 +00:00
|
|
|
ERROR string = "Error"
|
2014-03-28 04:31:51 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-07 02:24:10 +00:00
|
|
|
// Response contains the attributes found in an API response
|
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-07-12 18:46:38 +00:00
|
|
|
db.CreateTable(Attachment{})
|
2015-02-07 02:24:10 +00:00
|
|
|
db.CreateTable(Page{})
|
2014-06-03 18:27:20 +00:00
|
|
|
db.CreateTable(SMTP{})
|
2014-06-26 02:01:01 +00:00
|
|
|
db.CreateTable(Event{})
|
2014-03-26 19:50:16 +00:00
|
|
|
db.CreateTable(Campaign{})
|
2014-03-25 03:31:33 +00:00
|
|
|
//Create the default user
|
2015-02-07 02:24:10 +00:00
|
|
|
initUser := User{
|
2014-03-25 03:31:33 +00:00
|
|
|
Username: "admin",
|
|
|
|
Hash: "$2a$10$IYkPp0.QsM81lYYPrQx6W.U6oQGw7wMpozrKhKAHUBVL4mkm/EvAS", //gophish
|
2014-03-26 04:53:51 +00:00
|
|
|
ApiKey: "12345678901234567890123456789012",
|
|
|
|
}
|
2015-02-07 02:24:10 +00:00
|
|
|
err = db.Save(&initUser).Error
|
2014-03-26 04:53:51 +00:00
|
|
|
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
|
|
|
}
|