-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdb.go
403 lines (347 loc) · 9.17 KB
/
db.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package main
import (
"database/sql"
"fmt"
"net"
"time"
"github.com/mattn/go-sqlite3"
)
// Global variables
var db *sql.DB
func dbSetup() error {
var err error
db, err = sql.Open("sqlite3", fmt.Sprintf("lxd-demo.sqlite3?_busy_timeout=5000&_txlock=exclusive"))
if err != nil {
return err
}
err = dbCreateTables()
if err != nil {
return err
}
return nil
}
func dbCreateTables() error {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
uuid VARCHAR(36) NOT NULL,
status INTEGER NOT NULL,
container_name VARCHAR(64) NOT NULL,
container_ip VARCHAR(39) NOT NULL,
container_username VARCHAR(10) NOT NULL,
container_password VARCHAR(10) NOT NULL,
container_expiry INT NOT NULL,
request_date INT NOT NULL,
request_ip VARCHAR(39) NOT NULL,
request_terms VARCHAR(64) NOT NULL
);
CREATE TABLE IF NOT EXISTS feedback (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
session_id INTEGER NOT NULL,
rating INTEGER,
email VARCHAR(255),
email_use INTEGER,
feedback TEXT,
FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
);
`)
if err != nil {
return err
}
return nil
}
func dbGetStats(period string, unique bool, network *net.IPNet) (int64, error) {
var count int64
// Deal with unique filter
what := "request_ip"
if unique {
what = "distinct request_ip"
}
// Deal with period filter
where := ""
if period == "current" {
where = "WHERE status=0"
} else if period == "hour" {
creation := time.Now().Add(-time.Hour).Unix()
where = fmt.Sprintf("WHERE request_date > %d", creation)
} else if period == "day" {
creation := time.Now().Add(-time.Hour * 24).Unix()
where = fmt.Sprintf("WHERE request_date > %d", creation)
} else if period == "week" {
creation := time.Now().Add(-time.Hour * 24 * 7).Unix()
where = fmt.Sprintf("WHERE request_date > %d", creation)
} else if period == "month" {
creation := time.Now().Add(-time.Hour * time.Duration(24*30.5)).Unix()
where = fmt.Sprintf("WHERE request_date > %d", creation)
} else if period == "year" {
creation := time.Now().Add(-time.Hour * time.Duration(24*365.25)).Unix()
where = fmt.Sprintf("WHERE request_date > %d", creation)
}
if network == nil {
err := db.QueryRow(fmt.Sprintf("SELECT count(%s) FROM sessions %s;", what, where)).Scan(&count)
if err != nil {
return -1, err
}
} else {
outfmt := []interface{}{""}
q := fmt.Sprintf("SELECT %s FROM sessions %s;", what, where)
result, err := dbQueryScan(db, q, nil, outfmt)
if err != nil {
return -1, err
}
for _, ip := range result {
netIp := net.ParseIP(ip[0].(string))
if netIp == nil {
continue
}
if !network.Contains(netIp) {
continue
}
count += 1
}
}
return count, nil
}
func dbActive() ([][]interface{}, error) {
q := fmt.Sprintf("SELECT id, container_name, container_expiry FROM sessions WHERE status=0;")
var containerID int
var containerName string
var containerExpiry int
outfmt := []interface{}{containerID, containerName, containerExpiry}
result, err := dbQueryScan(db, q, nil, outfmt)
if err != nil {
return nil, err
}
return result, nil
}
func dbGetContainer(id string, active bool) (int64, string, string, string, string, int64, error) {
var sessionId int64
var containerName string
var containerIP string
var containerUsername string
var containerPassword string
var containerExpiry int64
var err error
var rows *sql.Rows
sessionId = -1
if active {
rows, err = dbQuery(db, "SELECT id, container_name, container_ip, container_username, container_password, container_expiry FROM sessions WHERE status=0 AND uuid=?;", id)
} else {
rows, err = dbQuery(db, "SELECT id, container_name, container_ip, container_username, container_password, container_expiry FROM sessions WHERE uuid=?;", id)
}
if err != nil {
return -1, "", "", "", "", 0, err
}
defer rows.Close()
for rows.Next() {
rows.Scan(&sessionId, &containerName, &containerIP, &containerUsername, &containerPassword, &containerExpiry)
}
return sessionId, containerName, containerIP, containerUsername, containerPassword, containerExpiry, nil
}
func dbGetFeedback(id int64) (int64, int64, string, int64, string, error) {
var feedbackId int64
var rating int64
var email string
var emailUse int64
var feedback string
feedbackId = -1
rating = -1
emailUse = -1
rows, err := dbQuery(db, "SELECT id, rating, email, email_use, feedback FROM feedback WHERE session_id=?;", id)
if err != nil {
return -1, -1, "", -1, "", err
}
defer rows.Close()
for rows.Next() {
rows.Scan(&feedbackId, &rating, &email, &emailUse, &feedback)
}
return feedbackId, rating, email, emailUse, feedback, nil
}
func dbNew(id string, containerName string, containerIP string, containerUsername string, containerPassword string, containerExpiry int64, requestDate int64, requestIP string, requestTerms string) (int64, error) {
res, err := db.Exec(`
INSERT INTO sessions (
status,
uuid,
container_name,
container_ip,
container_username,
container_password,
container_expiry,
request_date,
request_ip,
request_terms) VALUES (0, ?, ?, ?, ?, ?, ?, ?, ?, ?);
`, id, containerName, containerIP, containerUsername, containerPassword, containerExpiry, requestDate, requestIP, requestTerms)
if err != nil {
return 0, err
}
containerID, err := res.LastInsertId()
if err != nil {
return 0, err
}
return containerID, nil
}
func dbRecordFeedback(id int64, feedback Feedback) error {
// Get the feedback
feedbackId, _, _, _, _, err := dbGetFeedback(id)
if err != nil {
return err
}
if feedbackId == -1 {
// Record new feedback
_, err := db.Exec(`
INSERT INTO feedback (
session_id,
rating,
email,
email_use,
feedback) VALUES (?, ?, ?, ?, ?);
`, id, feedback.Rating, feedback.Email, feedback.EmailUse, feedback.Message)
if err != nil {
return err
}
return nil
}
// Update existing feedback
_, err = db.Exec(`
UPDATE feedback SET rating=?, email=?, email_use=?, feedback=? WHERE session_id=?;
`, feedback.Rating, feedback.Email, feedback.EmailUse, feedback.Message, id)
if err != nil {
return err
}
return nil
}
func dbExpire(id int64) error {
_, err := db.Exec("UPDATE sessions SET status=1 WHERE id=?;", id)
return err
}
func dbActiveCount() (int, error) {
var count int
statement := `SELECT count(*) FROM sessions WHERE status=0;`
err := db.QueryRow(statement).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func dbActiveCountForIP(ip string) (int, error) {
var count int
statement := `SELECT count(*) FROM sessions WHERE status=0 AND request_ip=?;`
err := db.QueryRow(statement, ip).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func dbNextExpire() (int, error) {
var expire int
statement := `SELECT MIN(container_expiry) FROM sessions WHERE status=0;`
err := db.QueryRow(statement).Scan(&expire)
if err != nil {
return 0, err
}
return expire, nil
}
func dbIsLockedError(err error) bool {
if err == nil {
return false
}
if err == sqlite3.ErrLocked || err == sqlite3.ErrBusy {
return true
}
if err.Error() == "database is locked" {
return true
}
return false
}
func dbIsNoMatchError(err error) bool {
if err == nil {
return false
}
if err.Error() == "sql: no rows in result set" {
return true
}
return false
}
func dbQueryRowScan(db *sql.DB, q string, args []interface{}, outargs []interface{}) error {
for {
err := db.QueryRow(q, args...).Scan(outargs...)
if err == nil {
return nil
}
if dbIsNoMatchError(err) {
return err
}
if !dbIsLockedError(err) {
return err
}
time.Sleep(1 * time.Second)
}
}
func dbQuery(db *sql.DB, q string, args ...interface{}) (*sql.Rows, error) {
for {
result, err := db.Query(q, args...)
if err == nil {
return result, nil
}
if !dbIsLockedError(err) {
return nil, err
}
time.Sleep(1 * time.Second)
}
}
func dbDoQueryScan(db *sql.DB, q string, args []interface{}, outargs []interface{}) ([][]interface{}, error) {
rows, err := db.Query(q, args...)
if err != nil {
return [][]interface{}{}, err
}
defer rows.Close()
result := [][]interface{}{}
for rows.Next() {
ptrargs := make([]interface{}, len(outargs))
for i := range outargs {
switch t := outargs[i].(type) {
case string:
str := ""
ptrargs[i] = &str
case int:
integer := 0
ptrargs[i] = &integer
default:
return [][]interface{}{}, fmt.Errorf("Bad interface type: %s\n", t)
}
}
err = rows.Scan(ptrargs...)
if err != nil {
return [][]interface{}{}, err
}
newargs := make([]interface{}, len(outargs))
for i := range ptrargs {
switch t := outargs[i].(type) {
case string:
newargs[i] = *ptrargs[i].(*string)
case int:
newargs[i] = *ptrargs[i].(*int)
default:
return [][]interface{}{}, fmt.Errorf("Bad interface type: %s\n", t)
}
}
result = append(result, newargs)
}
err = rows.Err()
if err != nil {
return [][]interface{}{}, err
}
return result, nil
}
func dbQueryScan(db *sql.DB, q string, inargs []interface{}, outfmt []interface{}) ([][]interface{}, error) {
for {
result, err := dbDoQueryScan(db, q, inargs, outfmt)
if err == nil {
return result, nil
}
if !dbIsLockedError(err) {
return nil, err
}
time.Sleep(1 * time.Second)
}
}