-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
executable file
·183 lines (167 loc) · 4.66 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
// Copyright 2015-2016 Zack Scholl. All rights reserved.
// Use of this source code is governed by a AGPL
// license that can be found in the LICENSE file.
// db.go contains generic functions for parsing data from the database.
// This file is not exhaustive of all database functions, if they pertain to a
// specific property (fingerprinting/priors/parameters), it will instead be in respective file.
package main
import (
"log"
"os"
"path"
"strings"
"time"
"github.com/boltdb/bolt"
)
func groupExists(group string) bool {
if _, err := os.Stat(path.Join(RuntimeArgs.SourcePath, strings.ToLower(group)+".db")); os.IsNotExist(err) {
return false
}
return true
}
func renameNetwork(group string, oldName string, newName string) {
Debug.Println("Opening parameters")
ps, _ := openParameters(group)
Debug.Println("Opening persistent parameters")
persistentPs, _ := openPersistentParameters(group)
Debug.Println("Looping network macs")
for n := range ps.NetworkMacs {
if n == oldName {
macs := []string{}
Debug.Println("Looping macs for ", n)
for mac := range ps.NetworkMacs[n] {
macs = append(macs, mac)
}
Debug.Println("Adding to persistentPs")
persistentPs.NetworkRenamed[newName] = macs
delete(persistentPs.NetworkRenamed, oldName)
break
}
}
Debug.Println("Saving persistentPs")
savePersistentParameters(group, persistentPs)
}
func getUsers(group string) []string {
val, ok := getUserCache(group)
if ok {
return val
}
uniqueUsers := []string{}
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("fingerprints-track"))
if b == nil {
return nil
}
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
v2 := loadFingerprint(v)
if !stringInSlice(v2.Username, uniqueUsers) {
uniqueUsers = append(uniqueUsers, v2.Username)
}
}
return nil
})
go setUserCache(group, uniqueUsers)
return uniqueUsers
}
func getUniqueMacs(group string) []string {
defer timeTrack(time.Now(), "getUniqueMacs")
uniqueMacs := []string{}
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("fingerprints"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
v2 := loadFingerprint(v)
for _, router := range v2.WifiFingerprint {
if !stringInSlice(router.Mac, uniqueMacs) {
uniqueMacs = append(uniqueMacs, router.Mac)
}
}
}
return nil
})
return uniqueMacs
}
func getUniqueLocations(group string) (uniqueLocs []string) {
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("fingerprints"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
v2 := loadFingerprint(v)
if !stringInSlice(v2.Location, uniqueLocs) {
uniqueLocs = append(uniqueLocs, v2.Location)
}
}
return nil
})
return uniqueLocs
}
func getMacCount(group string) (macCount map[string]int) {
macCount = make(map[string]int)
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("fingerprints"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
v2 := loadFingerprint(v)
for _, router := range v2.WifiFingerprint {
if _, ok := macCount[router.Mac]; !ok {
macCount[router.Mac] = 0
}
macCount[router.Mac]++
}
}
return nil
})
return
}
func getMacCountByLoc(group string) (macCountByLoc map[string]map[string]int) {
macCountByLoc = make(map[string]map[string]int)
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("fingerprints"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
v2 := loadFingerprint(v)
if _, ok := macCountByLoc[v2.Location]; !ok {
macCountByLoc[v2.Location] = make(map[string]int)
}
for _, router := range v2.WifiFingerprint {
if _, ok := macCountByLoc[v2.Location][router.Mac]; !ok {
macCountByLoc[v2.Location][router.Mac] = 0
}
macCountByLoc[v2.Location][router.Mac]++
}
}
return nil
})
return
}