-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
213 lines (187 loc) · 4.18 KB
/
main.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
package main
import (
"errors"
"os"
"os/user"
"runtime"
"strings"
"time"
log "github.com/cihub/seelog"
"github.com/evrrnv/your-map/server/main/src/models"
"github.com/urfave/cli"
)
var (
wifiInterface string
version string
commit string
date string
macSring string
macList []string
server string
family, device, location string
scanSeconds int
minimumThreshold int
doBluetooth bool
doWifi bool
doReverse bool
doDebug bool
runForever bool
currentChannel string
)
func main() {
defer log.Flush()
app := cli.NewApp()
app.Name = "cli-scanner"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "bluetooth",
Usage: "scan bluetooth",
},
cli.BoolFlag{
Name: "wifi",
Usage: "scan wifi",
},
cli.StringFlag{
Name: "server",
Value: "http://localhost:8005/",
Usage: "server for submitting fingerprints",
},
cli.StringFlag{
Name: "interface,i",
Value: "wlan0",
Usage: "wifi interface for scanning",
},
cli.StringFlag{
Name: "location,l",
Value: "",
Usage: "location name",
},
cli.StringFlag{
Name: "device,d",
Value: "",
Usage: "device name",
},
cli.StringFlag{
Name: "sublocation,s",
Value: "",
Usage: "sublocation name (automatically toggles learning)",
},
cli.BoolFlag{
Name: "debug",
Usage: "enable debug mode",
},
cli.BoolFlag{
Name: "forever",
Usage: "run until Ctl+C signal",
},
cli.IntFlag{
Name: "min-rssi",
Value: -100,
Usage: "minimum RSSI to use",
},
cli.IntFlag{
Name: "scantime,t",
Value: 40,
Usage: "number of seconds to scan",
},
cli.StringFlag{
Name: "macs,m",
Usage: "access points mac addresses",
FilePath: "./mac-list",
},
}
app.Action = func(c *cli.Context) (err error) {
server = c.GlobalString("server")
family = strings.ToLower(c.GlobalString("location"))
device = c.GlobalString("device")
wifiInterface = c.GlobalString("interface")
location = c.GlobalString("sublocation")
macSring = c.GlobalString("macs")
doBluetooth = c.GlobalBool("bluetooth")
doWifi = c.GlobalBool("wifi")
doDebug = c.GlobalBool("debug")
runForever = c.GlobalBool("forever")
scanSeconds = c.GlobalInt("scantime")
minimumThreshold = c.GlobalInt("min-rssi")
if doDebug {
setLogLevel("debug")
} else {
setLogLevel("info")
}
macList = strings.Split(macSring, ",")
if runtime.GOOS == "linux" && os.Getenv("SUDO_USER") == "" {
user, usererr := user.Current()
if usererr == nil && user.Name != "root" {
err = errors.New("need to run with sudo")
return
}
}
if !doBluetooth && !doWifi {
doWifi = true
}
if device == "" {
return errors.New("device cannot be blank (set with -d)")
} else if family == "" {
return errors.New("family cannot be blank (set with -f)")
}
for {
if doWifi {
log.Infof("scanning with %s", wifiInterface)
}
if doBluetooth {
log.Infof("scanning bluetooth")
}
if doBluetooth || doReverse {
log.Infof("scanning for %d seconds", scanSeconds)
}
if !doReverse {
err = basicCapture()
}
if !runForever {
break
} else if err != nil {
log.Warn(err)
}
}
return
}
err := app.Run(os.Args)
if err != nil {
log.Error(err)
}
}
func basicCapture() (err error) {
payload := models.SensorData{}
payload.Timestamp = time.Now().UnixNano() / int64(time.Millisecond)
payload.Family = family
payload.Device = device
payload.Location = location
payload.Sensors = make(map[string]map[string]interface{})
c := make(chan map[string]map[string]interface{})
numSensors := 0
if doWifi {
go scanWifi(c)
numSensors++
}
if doBluetooth {
go scanBluetooth(c)
numSensors++
}
for i := 0; i < numSensors; i++ {
data := <-c
for sensor := range data {
payload.Sensors[sensor] = make(map[string]interface{})
for device := range data[sensor] {
payload.Sensors[sensor][device] = data[sensor][device]
}
}
}
if len(payload.Sensors) == 0 {
err = errors.New("collected no data")
return
}
payload.GPS.Latitude = 35.2102962
payload.GPS.Longitude = -0.633085
err = postData(payload, "/data")
return
}