-
Notifications
You must be signed in to change notification settings - Fork 5
/
file.go
154 lines (130 loc) · 3.93 KB
/
file.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package store
import (
"errors"
"fmt"
"log"
"os"
"path"
"time"
gologme "github.com/erasche/gologme/types"
)
// FileDataStore struct
type FileDataStore struct {
DSN string
KeyFreqs *os.File
Windows *os.File
Blog *os.File
Notes *os.File
}
// SetupDb runs any migrations needed
func (ds *FileDataStore) SetupDb() {
return
}
// LogToDb logs a set of windowLogs and keyLogs to the DB
func (ds *FileDataStore) LogToDb(uid int, windowlogs []*gologme.WindowLogs, keylogs []*gologme.KeyLogs) {
wll := len(windowlogs)
log.Printf("%d window logs %d key logs from [%d]\n", wll, len(keylogs), uid)
for _, w := range keylogs {
keyfreqText := fmt.Sprintf("%d %d", w.Time.Unix(), w.Count)
if _, err := ds.KeyFreqs.WriteString(keyfreqText); err != nil {
log.Fatal(err)
}
}
for i, w := range windowlogs {
windowText := fmt.Sprintf("%d %s", w.Time.Unix(), w.Name)
if _, err := ds.Windows.WriteString(windowText); err != nil {
log.Fatal(err)
}
// Dunno why windowLogs comes through two too big, so whatever.
if i >= wll-1 {
break
}
}
}
func (ds *FileDataStore) CreateNote(uid int, date time.Time, message string) {
noteText := fmt.Sprintf("%d %d", date.Unix(), message)
if _, err := ds.Notes.WriteString(noteText); err != nil {
log.Fatal(err)
}
}
func (ds *FileDataStore) CreateBlog(uid int, date time.Time, message string) {
noteText := fmt.Sprintf("%d %d", date.Unix(), message)
if _, err := ds.Blog.WriteString(noteText); err != nil {
log.Fatal(err)
}
}
// CheckAuth of the key, returning -1 or the user's ID
func (ds *FileDataStore) CheckAuth(key string) (int, error) {
// Pretty assuredly not safe from timing attacks.
return 0, nil
}
// Name of the DS implementatino
func (ds *FileDataStore) Name() string {
return "FileDataStore"
}
// MaxDate returns latest log entry
func (ds *FileDataStore) MaxDate() int {
// not implemented
return 0
}
// MinDate returns first log entry
func (ds *FileDataStore) MinDate() int {
// not implemented
return 0
}
// FindUserNameByID returns a username for a given ID
func (ds *FileDataStore) FindUserNameByID(id int) (string, error) {
return "admin", nil
}
func (ds *FileDataStore) ExportWindowLogsByRange(t0 int64, t1 int64) []*gologme.SEvent {
return nil
}
func (ds *FileDataStore) exportKeyLogsByRange(t0 int64, t1 int64) []*gologme.IEvent {
return nil
}
func (ds *FileDataStore) exportBlog(t0 int64, t1 int64) []*gologme.SEvent {
return nil
}
func (ds *FileDataStore) exportNotes(t0 int64, t1 int64) []*gologme.SEvent {
return nil
}
// ExportEventsByDate extracts events for a given day
func (ds *FileDataStore) ExportEventsByDate(tm time.Time) *gologme.EventLog {
return nil
}
// NewFileDataStore builds a new sqlite3 DS
func NewFileDataStore(conf map[string]string) (DataStore, error) {
var dsn string
if val, ok := conf["DATASTORE_URL"]; ok {
dsn = val
} else {
return nil, errors.New(fmt.Sprintf("%s is required for the sqlite datastore", "DATASTORE_URL"))
}
keyFilePath := path.Join(dsn, "keyfreq_date.txt")
keyFileHandle, err := os.OpenFile(keyFilePath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Panicf("Failed to connect to datastore: %s", err.Error())
}
winFilePath := path.Join(dsn, "window_date.txt")
winFileHandle, err := os.OpenFile(winFilePath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Panicf("Failed to connect to datastore: %s", err.Error())
}
blogFilePath := path.Join(dsn, "blog_date.txt")
blogFileHandle, err := os.OpenFile(blogFilePath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Panicf("Failed to connect to datastore: %s", err.Error())
}
notesFilePath := path.Join(dsn, "notes_date.txt")
notesFileHandle, err := os.OpenFile(notesFilePath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Panicf("Failed to connect to datastore: %s", err.Error())
}
return &FileDataStore{
DSN: dsn,
KeyFreqs: keyFileHandle,
Windows: winFileHandle,
Blog: blogFileHandle,
Notes: notesFileHandle,
}, nil
}