forked from ShivaGuntuku/find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameters.go
executable file
·413 lines (363 loc) · 11.7 KB
/
parameters.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
404
405
406
407
408
409
410
411
412
413
// 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.
// parameters.go contains structures and functions for setting and getting Naive-Bayes parameters.
package main
import (
"encoding/json"
"fmt"
"log"
"path"
"strconv"
"strings"
"github.com/boltdb/bolt"
)
// PersistentParameters are not reloaded each time
type PersistentParameters struct {
NetworkRenamed map[string][]string
}
// PriorParameters contains the network-specific bayesian priors and Mac frequency, as well as special variables
type PriorParameters struct {
P map[string]map[string][]float32 // standard P
NP map[string]map[string][]float32 // standard nP
MacFreq map[string]map[string]float32 // Frequency of a mac in a certain location
NMacFreq map[string]map[string]float32 // Frequency of a mac, in everywhere BUT a certain location
Special map[string]float64
}
// ResultsParameters contains the information about the accuracy from crossValidation
type ResultsParameters struct {
Accuracy map[string]int // accuracy measurement for a given location
TotalLocations map[string]int // number of locations
CorrectLocations map[string]int // number of times guessed correctly
Guess map[string]map[string]int // correct -> guess -> times
}
// FullParameters is the full parameter set for a given group
type FullParameters struct {
NetworkMacs map[string]map[string]bool // map of networks and then the associated macs in each
NetworkLocs map[string]map[string]bool // map of the networks, and then the associated locations in each
MacVariability map[string]float32 // variability of macs
MacCount map[string]int // number of each mac
MacCountByLoc map[string]map[string]int // number of each mac, by location
UniqueLocs []string
UniqueMacs []string
Priors map[string]PriorParameters // generate priors for each network
Results map[string]ResultsParameters // generate priors for each network
Loaded bool // flag to determine if parameters have been loaded
}
// NewFullParameters generates a blank FullParameters
func NewFullParameters() *FullParameters {
return &FullParameters{
NetworkMacs: make(map[string]map[string]bool),
NetworkLocs: make(map[string]map[string]bool),
MacCount: make(map[string]int),
MacCountByLoc: make(map[string]map[string]int),
UniqueMacs: []string{},
UniqueLocs: []string{},
Priors: make(map[string]PriorParameters),
MacVariability: make(map[string]float32),
Results: make(map[string]ResultsParameters),
Loaded: false,
}
}
// NewPriorParameters generates a blank PriorParameters
func NewPriorParameters() *PriorParameters {
return &PriorParameters{
P: make(map[string]map[string][]float32),
NP: make(map[string]map[string][]float32),
MacFreq: make(map[string]map[string]float32),
NMacFreq: make(map[string]map[string]float32),
Special: make(map[string]float64),
}
}
// NewResultsParameters generates a blank ResultsParameters
func NewResultsParameters() *ResultsParameters {
return &ResultsParameters{
Accuracy: make(map[string]int),
TotalLocations: make(map[string]int),
CorrectLocations: make(map[string]int),
Guess: make(map[string]map[string]int),
}
}
// NewPersistentParameters returns the peristent parameters initialization
func NewPersistentParameters() *PersistentParameters {
return &PersistentParameters{
NetworkRenamed: make(map[string][]string),
}
}
func dumpParameters(res FullParameters) []byte {
jsonByte, _ := res.MarshalJSON()
return compressByte(jsonByte)
}
func loadParameters(jsonByte []byte) FullParameters {
var res2 FullParameters
res2.UnmarshalJSON(decompressByte(jsonByte))
return res2
}
func saveParameters(group string, res FullParameters) error {
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
if err != nil {
Error.Println(err)
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
bucket, err2 := tx.CreateBucketIfNotExists([]byte("resources"))
if err2 != nil {
return fmt.Errorf("create bucket: %s", err2)
}
err2 = bucket.Put([]byte("fullParameters"), dumpParameters(res))
if err2 != nil {
return fmt.Errorf("could add to bucket: %s", err2)
}
return err2
})
return err
}
func openParameters(group string) (FullParameters, error) {
psCached, ok := getPsCache(group)
if ok {
return psCached, nil
}
var ps = *NewFullParameters()
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
if err != nil {
Error.Println(err)
}
defer db.Close()
err = db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("resources"))
if b == nil {
return fmt.Errorf("Resources dont exist")
}
v := b.Get([]byte("fullParameters"))
ps = loadParameters(v)
return nil
})
go setPsCache(group, ps)
return ps, err
}
func openPersistentParameters(group string) (PersistentParameters, error) {
var persistentPs = *NewPersistentParameters()
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
if err != nil {
Error.Println(err)
}
defer db.Close()
err = db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("resources"))
if b == nil {
return fmt.Errorf("Resources dont exist")
}
v := b.Get([]byte("persistentParameters"))
json.Unmarshal(v, &persistentPs)
return nil
})
return persistentPs, err
}
func savePersistentParameters(group string, res PersistentParameters) error {
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
if err != nil {
Error.Println(err)
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
bucket, err2 := tx.CreateBucketIfNotExists([]byte("resources"))
if err2 != nil {
return fmt.Errorf("create bucket: %s", err)
}
jsonByte, _ := json.Marshal(res)
err2 = bucket.Put([]byte("persistentParameters"), jsonByte)
if err2 != nil {
return fmt.Errorf("could add to bucket: %s", err)
}
return err2
})
Debug.Println("Saved")
return err
}
func getParameters(group string, ps *FullParameters, fingerprintsInMemory map[string]Fingerprint, fingerprintsOrdering []string) {
persistentPs, err := openPersistentParameters(group)
ps.NetworkMacs = make(map[string]map[string]bool)
ps.NetworkLocs = make(map[string]map[string]bool)
ps.UniqueMacs = []string{}
ps.UniqueLocs = []string{}
ps.MacCount = make(map[string]int)
ps.MacCountByLoc = make(map[string]map[string]int)
ps.Loaded = true
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Get all parameters that don't need a network graph
for _, v1 := range fingerprintsOrdering {
v2 := fingerprintsInMemory[v1]
// unique locs
if !stringInSlice(v2.Location, ps.UniqueLocs) {
ps.UniqueLocs = append(ps.UniqueLocs, v2.Location)
}
// mac by location count
if _, ok := ps.MacCountByLoc[v2.Location]; !ok {
ps.MacCountByLoc[v2.Location] = make(map[string]int)
}
// building network
macs := []string{}
for _, router := range v2.WifiFingerprint {
// building network
macs = append(macs, router.Mac)
// unique macs
if !stringInSlice(router.Mac, ps.UniqueMacs) {
ps.UniqueMacs = append(ps.UniqueMacs, router.Mac)
}
// mac count
if _, ok := ps.MacCount[router.Mac]; !ok {
ps.MacCount[router.Mac] = 0
}
ps.MacCount[router.Mac]++
// mac by location count
if _, ok := ps.MacCountByLoc[v2.Location][router.Mac]; !ok {
ps.MacCountByLoc[v2.Location][router.Mac] = 0
}
ps.MacCountByLoc[v2.Location][router.Mac]++
}
// building network
ps.NetworkMacs = buildNetwork(ps.NetworkMacs, macs)
}
ps.NetworkMacs = mergeNetwork(ps.NetworkMacs)
// Rename the NetworkMacs
if len(persistentPs.NetworkRenamed) > 0 {
newNames := []string{}
for k := range persistentPs.NetworkRenamed {
newNames = append(newNames, k)
}
for n := range ps.NetworkMacs {
renamed := false
for mac := range ps.NetworkMacs[n] {
for renamedN := range persistentPs.NetworkRenamed {
if stringInSlice(mac, persistentPs.NetworkRenamed[renamedN]) && !stringInSlice(n, newNames) {
ps.NetworkMacs[renamedN] = make(map[string]bool)
for k, v := range ps.NetworkMacs[n] {
ps.NetworkMacs[renamedN][k] = v
}
delete(ps.NetworkMacs, n)
renamed = true
}
if renamed {
break
}
}
if renamed {
break
}
}
}
}
// Get the locations for each graph (Has to have network built first)
for _, v1 := range fingerprintsOrdering {
v2 := fingerprintsInMemory[v1]
macs := []string{}
for _, router := range v2.WifiFingerprint {
macs = append(macs, router.Mac)
}
networkName, inNetwork := hasNetwork(ps.NetworkMacs, macs)
if inNetwork {
if _, ok := ps.NetworkLocs[networkName]; !ok {
ps.NetworkLocs[networkName] = make(map[string]bool)
}
if _, ok := ps.NetworkLocs[networkName][v2.Location]; !ok {
ps.NetworkLocs[networkName][v2.Location] = true
}
}
}
}
func getMixinOverride(group string) (float64, error) {
group = strings.ToLower(group)
override := float64(-1)
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
defer db.Close()
if err != nil {
Error.Println(err)
}
err = db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("resources"))
if b == nil {
return fmt.Errorf("Resources dont exist")
}
v := b.Get([]byte("mixinOverride"))
if len(v) == 0 {
return fmt.Errorf("No mixin override")
}
override, err = strconv.ParseFloat(string(v), 64)
return err
})
return override, err
}
func getCutoffOverride(group string) (float64, error) {
group = strings.ToLower(group)
override := float64(-1)
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
defer db.Close()
if err != nil {
Error.Println(err)
}
err = db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("resources"))
if b == nil {
return fmt.Errorf("Resources dont exist")
}
v := b.Get([]byte("cutoffOverride"))
if len(v) == 0 {
return fmt.Errorf("No mixin override")
}
override, err = strconv.ParseFloat(string(v), 64)
return err
})
return override, err
}
func setMixinOverride(group string, mixin float64) error {
if (mixin < 0 || mixin > 1) && mixin != -1 {
return fmt.Errorf("mixin must be between 0 and 1")
}
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
defer db.Close()
if err != nil {
Error.Println(err)
}
err = db.Update(func(tx *bolt.Tx) error {
bucket, err2 := tx.CreateBucketIfNotExists([]byte("resources"))
if err2 != nil {
return fmt.Errorf("create bucket: %s", err2)
}
err2 = bucket.Put([]byte("mixinOverride"), []byte(strconv.FormatFloat(mixin, 'E', -1, 64)))
if err2 != nil {
return fmt.Errorf("could add to bucket: %s", err2)
}
return err2
})
return err
}
func setCutoffOverride(group string, cutoff float64) error {
if (cutoff < 0 || cutoff > 1) && cutoff != -1 {
return fmt.Errorf("cutoff must be between 0 and 1")
}
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
defer db.Close()
if err != nil {
Error.Println(err)
}
err = db.Update(func(tx *bolt.Tx) error {
bucket, err2 := tx.CreateBucketIfNotExists([]byte("resources"))
if err2 != nil {
return fmt.Errorf("create bucket: %s", err2)
}
err2 = bucket.Put([]byte("cutoffOverride"), []byte(strconv.FormatFloat(cutoff, 'E', -1, 64)))
if err2 != nil {
return fmt.Errorf("could add to bucket: %s", err2)
}
return err2
})
return err
}