-
Notifications
You must be signed in to change notification settings - Fork 6
/
sqlite.go
355 lines (306 loc) · 8.26 KB
/
sqlite.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
package sqlite
import (
"database/sql"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
_ "github.com/jamesog/scan/internal/migrations"
"github.com/jamesog/scan/pkg/scan"
_ "github.com/mattn/go-sqlite3"
"github.com/pressly/goose"
)
// DefaultDBFile is the default SQLite database file name.
const DefaultDBFile = "scan.db"
// DB is the database.
type DB struct {
*sql.DB
}
func toNullInt64(i *int64) sql.NullInt64 {
var ni sql.NullInt64
if i != nil {
ni = sql.NullInt64{Int64: *i, Valid: true}
}
return ni
}
// Open creates a new SQLite database object.
func Open(dsn string) (*DB, error) {
var err error
db, err := sql.Open("sqlite3", dsn)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
log.Fatal(err)
}
// Run migrations
goose.SetDialect("sqlite3")
// Use a temporary directory for goose.Up() - we don't have any .sql files
// to run, it's all embedded in the binary
tmpdir, err := ioutil.TempDir(filepath.Dir(dsn), "")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpdir)
// FIXME(jamesog): The verbose flag isn't accessible here anymore
// if verbose {
// log.Println("Checking database migration status")
// goose.Status(db, tmpdir)
// } else {
// Discard Goose's log output
goose.SetLogger(log.New(ioutil.Discard, "", 0))
// }
err = goose.Up(db, tmpdir)
if err != nil {
log.Fatalf("Error running database migrations: %v\n", err)
}
return &DB{DB: db}, nil
}
// SQLFilter is for constructing data filters ("WHERE" clauses) in a SQL statement
type SQLFilter struct {
Where []string
Values []interface{}
}
// String constructs a SQL WHERE clause.
func (f SQLFilter) String() string {
if len(f.Where) > 0 {
return "WHERE " + strings.Join(f.Where, " AND ")
}
return ""
}
// LoadData loads all data for displaying in the browser.
func (db *DB) LoadData(filter SQLFilter) ([]scan.IPInfo, error) {
qry := fmt.Sprintf(`SELECT ip, port, proto, firstseen, lastseen FROM scan %s ORDER BY port, proto, ip, lastseen`, filter)
rows, err := db.Query(qry, filter.Values...)
if err != nil {
return []scan.IPInfo{}, err
}
defer rows.Close()
var data []scan.IPInfo
var ip, proto string
var firstseen, lastseen time.Time
var port int
var latest time.Time
tracerouteIPs, err := db.LoadTracerouteIPs()
if err != nil {
return []scan.IPInfo{}, err
}
submission, err := db.LoadSubmission(SQLFilter{Where: []string{"job_id IS NULL"}})
if err == nil {
latest = submission.Time.Time
}
for rows.Next() {
err := rows.Scan(&ip, &port, &proto, &firstseen, &lastseen)
if err != nil {
log.Println("loadData: error scanning table:", err)
return []scan.IPInfo{}, err
}
if lastseen.After(latest) {
latest = lastseen
}
var hasTraceroute bool
if _, ok := tracerouteIPs[ip]; ok {
hasTraceroute = true
}
data = append(data, scan.IPInfo{
IP: ip,
Port: port,
Proto: proto,
FirstSeen: scan.Time{Time: firstseen},
LastSeen: scan.Time{Time: lastseen},
New: firstseen.Equal(lastseen) && lastseen == latest,
Gone: lastseen.Before(latest),
HasTraceroute: hasTraceroute})
}
return data, nil
}
// ResultData retrieves stored results. Each argument is optional and allows
// searching by IP address, first seen and last seen.
func (db *DB) ResultData(ip, fs, ls string) (scan.Data, error) {
var filter SQLFilter
if ip != "" {
filter.Where = append(filter.Where, `ip LIKE ?`)
filter.Values = append(filter.Values, fmt.Sprintf("%%%s%%", ip))
}
if fs != "" {
i, err := strconv.ParseInt(fs, 10, 0)
if err != nil {
log.Printf("couldn't parse firstseen value %q: %v", ls, err)
} else {
t := time.Unix(i, 0).UTC()
filter.Where = append(filter.Where, `firstseen=?`)
filter.Values = append(filter.Values, t)
}
}
if ls != "" {
i, err := strconv.ParseInt(ls, 10, 0)
if err != nil {
log.Printf("couldn't parse lastseen value %q: %v", ls, err)
} else {
t := time.Unix(i, 0).UTC()
filter.Where = append(filter.Where, `lastseen=?`)
filter.Values = append(filter.Values, t)
}
}
results, err := db.LoadData(filter)
if err != nil {
return scan.Data{}, err
}
data := scan.Data{
Results: results,
Total: len(results),
}
// Find all the latest results and store the number in the struct
// Set latest to Unix(0, 0) rather than the default zero value of the type
// to allow tests to receive an actual 0 value rather than a negative int
latest := time.Unix(0, 0)
for _, r := range results {
last := r.LastSeen.Time
if last.After(latest) {
latest = last
}
}
for _, r := range results {
if !r.Gone {
data.Latest++
}
if r.New {
data.New++
}
}
data.LastSeen = latest.Unix()
return data, nil
}
// SaveData saves the results posted.
func (db *DB) SaveData(results []scan.Result, now time.Time) (int64, error) {
txn, err := db.Begin()
if err != nil {
return 0, err
}
insert, err := txn.Prepare(`INSERT INTO scan (ip, port, proto, firstseen, lastseen) VALUES (?, ?, ?, ?, ?)`)
if err != nil {
txn.Rollback()
return 0, err
}
qry, err := txn.Prepare(`SELECT 1 FROM scan WHERE ip=? AND port=? AND proto=?`)
if err != nil {
txn.Rollback()
return 0, err
}
update, err := txn.Prepare(`UPDATE scan SET lastseen=? WHERE ip=? AND port=? AND proto=?`)
if err != nil {
txn.Rollback()
return 0, err
}
var count int64
for _, r := range results {
// Although it's an array, only one port is in each
port := r.Ports[0]
// Skip results which are (usually) banner-only
// While it would be nice to store banners, we need to restructure a
// bit to accommodate this and it just inserts duplicate data for now
if port.Status == "" || port.Service.Name != "" {
continue
}
// Search for the IP/port/proto combo
// If it exists, update `lastseen`, else insert a new record
// Because we have to scan into something
var x int
err := qry.QueryRow(r.IP, port.Port, port.Proto).Scan(&x)
switch {
case err == sql.ErrNoRows:
_, err = insert.Exec(r.IP, port.Port, port.Proto, now, now)
if err != nil {
txn.Rollback()
return 0, err
}
count++
continue
case err != nil:
txn.Rollback()
return 0, err
}
_, err = update.Exec(now, r.IP, port.Port, port.Proto)
if err != nil {
txn.Rollback()
return 0, err
}
count++
}
txn.Commit()
return count, nil
}
// LoadSubmission retrieves the stored submissions.
func (db *DB) LoadSubmission(filter SQLFilter) (scan.Submission, error) {
var host string
var job sql.NullInt64
var subTime sql.NullTime
qry := fmt.Sprintf(`SELECT host, job_id, submission_time FROM submission %s ORDER BY rowid DESC LIMIT 1`, filter)
err := db.QueryRow(qry, filter.Values...).Scan(&host, &job, &subTime)
if err != nil && err != sql.ErrNoRows {
log.Println("loadSubmission: error scanning table:", err)
return scan.Submission{}, err
}
return scan.Submission{Host: host, Job: job.Int64, Time: scan.Time{Time: subTime.Time.UTC()}}, nil
}
// SaveSubmission stores when and which host just submitted data.
func (db *DB) SaveSubmission(host string, job *int64, now time.Time) error {
txn, err := db.Begin()
if err != nil {
return err
}
qry := `INSERT INTO submission (host, job_id, submission_time) VALUES (?, ?, ?)`
_, err = txn.Exec(qry, host, toNullInt64(job), now)
if err != nil {
txn.Rollback()
return err
}
err = txn.Commit()
if err != nil {
return err
}
return nil
}
// LoadTracerouteIPs retrieves the stored traceroutes.
func (db *DB) LoadTracerouteIPs() (map[string]struct{}, error) {
ips := make(map[string]struct{})
rows, err := db.Query(`SELECT dest FROM traceroute`)
if err != nil {
return nil, err
}
defer rows.Close()
var ip string
for rows.Next() {
err := rows.Scan(&ip)
if err != nil {
return nil, err
}
if _, ok := ips[ip]; !ok {
ips[ip] = struct{}{}
}
}
return ips, nil
}
// LoadTraceroute retrieves a traceroute.
func (db *DB) LoadTraceroute(dest string) (string, error) {
var path string
err := db.QueryRow(`SELECT path FROM traceroute WHERE dest = ?`, dest).Scan(&path)
return path, err
}
func (db *DB) SaveTraceroute(dest, trace string) error {
txn, err := db.Begin()
if err != nil {
return err
}
_, err = txn.Exec(`INSERT OR REPLACE INTO traceroute (dest, path) VALUES (?, ?)`, dest, trace)
if err != nil {
txn.Rollback()
return err
}
return txn.Commit()
}