-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwarmap.go
337 lines (306 loc) · 9.22 KB
/
warmap.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
//Rewrite of Tom Steele's node.JS version
package main
import (
"bufio"
"bytes"
"encoding/xml"
"flag"
"fmt"
"html/template"
"io"
"log"
"os"
"regexp"
"sort"
"strings"
"io/ioutil"
"golang.org/x/net/html/charset"
)
//To-Do
//Add Signal bleed based signal strength
//Type Definitions and variables
//tpl defines the HTML template
const tpl = `
<html>
<head>
<title>WarMap</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="http://hpneo.github.io/gmaps/gmaps.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<style>
#map {
display: block;
width: 100%;
height: 700;
}
</style>
</head>
<body>
<div class="container" style="padding-top: 80px">
<div class="row">
<div class="col-xs-12">
<p>Displaying {{.PathLength}} points.</p>
<p><button onclick="toggleHeatmap()">Toggle Heatmap</button><p>
<p><button onclick="toggleOverlay()">Toggle Overlay</button><p>
</div>
</div>
<div class="row">
<div class="col-xs-11">
<div id="map"></div>
</div>
<div class="col-xs-1">
<button type="button" id="editButton" class="btn btn-primary" data-toggle="button">Editable</button>
</div>
</div>
</div>
</body>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?libraries=visualization">
</script>
<script>
var heatMapData = {{.Heatmap}};
var overlayCoords = {{.Paths}};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: {lat: {{.Lat}}, lng: {{.Lng}}},
mapTypeId: 'satellite'
});
var heatmap = new google.maps.visualization.HeatmapLayer({
data: heatMapData
});
var polygon = new google.maps.Polygon({
paths: overlayCoords,
strokeColor: '#3366FF',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#3366FF',
fillOpacity: 0.35
});
function toggleHeatmap() {
heatmap.setMap(heatmap.getMap() ? null : map);
}
function toggleOverlay() {
polygon.setMap(polygon.getMap() ? null : map)
}
</script>
</html>
`
////////////////////
//Type Definitions//
///////////////////
// Points defines a []Point array
type Points []Point
//Point holds X, Y coordinates
type Point struct {
X, Y float64
Dbm int
}
//Page Holds the Values for html template
type Page struct {
Lat float64
Lng float64
Heatmap template.JS
Paths template.JS
PathLength int
}
//GPSRun defines a struct to hold the top level of the
//xml tree
type GPSRun struct {
XMLName xml.Name `xml:"gps-run"`
GPSVersion string `xml:"gps-version,attr"`
StartTime string `xml:"start-time,attr"`
GPSPoints []GPSPoint `xml:"gps-point"`
}
//GPSPoint defines a struct to hold the values
//of gps-point
type GPSPoint struct {
XMLName xml.Name `xml:"gps-point"`
Bssid string `xml:"bssid,attr"`
Lat float64 `xml:"lat,attr"`
Lon float64 `xml:"lon,attr"`
Source string `xml:"source,attr"`
TimeSec int `xml:"time-sec,attr"`
TimeUSec int `xml:"time-usec,attr"`
Spd float64 `xml:"spd,attr"`
Heading float64 `xml:"heading,attr"`
Fix int `xml:"fix,attr"`
Alt float64 `xml:"alt,attr"`
SignalDbm int `xml:"signal_dbm,attr"`
NoiseDbm int `xml:"noise_dbm,attr"`
}
/////////////
//Functions//
////////////
//checkError is a generic error check function
func checkError(err error) {
if err != nil {
panic(err)
}
}
//parseXML parses the specified XML file and returns a GPSRun struct
//containing its values
func parseXML(file string) (target GPSRun) {
xmlFile, err := os.Open(file)
if err != nil {
panic("Ensure the GPSXML file exists")
}
defer xmlFile.Close()
var xmlData io.Reader = bufio.NewReader(xmlFile)
decoder := xml.NewDecoder(xmlData)
decoder.CharsetReader = charset.NewReaderLabel
err = decoder.Decode(&target)
checkError(err)
return
}
//parseCoords parses a GPSPoint array and puts them into a Point
//struct that is then placed into a Points struct
func processCoords(gpspoints []GPSPoint) (points Points) {
for i := 0; i < len(gpspoints); i++ {
points = append(points, Point{gpspoints[i].Lon, gpspoints[i].Lat, gpspoints[i].SignalDbm})
}
return
}
//filterBSSID returns all GPSPoint structs that have a particular bssid field
func filterBSSID(gpsPoints []GPSPoint, bssid []string) (filteredPoints []GPSPoint) {
for i := 0; i < len(gpsPoints); i++ {
for n := 0; n < len(bssid); n++ {
if gpsPoints[i].Bssid == bssid[n] {
filteredPoints = append(filteredPoints, gpsPoints[i])
}
}
}
if len(filteredPoints) == 0 {
log.Fatal("Your BSSID was not found in the file")
}
return
}
//Swap swaps the location of two Point structs in a Points struct
func (points Points) Swap(i, j int) {
points[i], points[j] = points[j], points[i]
}
//Len is custom length definition for Points
func (points Points) Len() int {
return len(points)
}
//Less sorts Points by x and, if equal, by y
func (points Points) Less(i, j int) bool {
if points[i].X == points[j].X {
return points[i].Y < points[j].Y
}
return points[i].X < points[j].X
}
//crossProduct returns the modulo (and sign) of the cross product between vetors OA and OB
func crossProduct(A, B, O Point) float64 {
return (A.X-O.X)*(B.Y-O.Y) - (A.Y-O.Y)*(B.X-O.X)
}
// findConvexHull returns a slice of Point with a convex hull
// it is counterclockwise and starts and ends at the same point
func findConvexHull(points Points) Points {
n := len(points) // number of points to find convex hull
var result Points // final result
count := 0 // size of our convex hull (number of points added)
// lets sort our points by x and if equal by y
sort.Sort(points)
if n == 0 {
return result
}
// add the first element:
result = append(result, points[0])
count++
//find the lower hull
for i := 1; i < n; i++ {
// remove points which are not part of the lower hull
for count > 1 && crossProduct(result[count-2], result[count-1], points[i]) < 0.00000000000000001 {
count--
result = result[:count]
}
// add a new better point than the removed ones
result = append(result, points[i])
count++
}
count0 := count // our base counter for the upper hull
// find the upper hull
for i := n - 2; i >= 0; i-- {
// remove points which are not part of the upper hull
for count-count0 > 0 && crossProduct(result[count-2], result[count-1], points[i]) < 0.00000000000000001 {
count--
result = result[:count]
}
// add a new better point than the removed ones
result = append(result, points[i])
count++
}
return result
}
//parseBssid takes a filename or comma seperated list of BSSIDs
//and outputs an array containing the parsed BSSIDs
func parseBssid(bssids string) (tempBssidSlice []string) {
var bssidSlice []string
r, err := regexp.Compile("(([a-zA-Z0-9]{2}:)){5}[a-zA-Z0-9]{2}")
checkError(err)
if _, err := os.Stat(bssids); os.IsNotExist(err) {
bssidSlice = strings.Split(bssids, ",")
} else {
file, err := os.Open(bssids)
checkError(err)
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
bssidSlice = lines
}
for i := 0; i < len(bssidSlice); i++ {
if r.MatchString(bssidSlice[i]) {
tempBssidSlice = append(tempBssidSlice, strings.ToUpper(bssidSlice[i]))
}
}
if len(tempBssidSlice) == 0 {
log.Fatal("Looks like you didn't have any correctly formatted SSIDs")
}
return
}
//genHeatmap generates a string to insert a list of heatmap objects
//into the html template
//populateTemplate populates the html template
func populateTemplate(points Points, allPoints Points) []byte {
var page Page
var heatmap string
var tplBuffer bytes.Buffer
var pathsData string
for i := 0; i < len(points); i++ {
pathsData += fmt.Sprintf("(new google.maps.LatLng(%g, %g)), ", points[i].Y, points[i].X)
}
for n := 0; n < len(allPoints); n++ {
heatmap += fmt.Sprintf("{location: new google.maps.LatLng(%g, %g), weight: %f}, ", allPoints[n].Y, allPoints[n].X, (float64(allPoints[n].Dbm)/10.0)+9.0)
}
page.Lat = points[0].Y
page.Lng = points[0].X
page.PathLength = len(pathsData)
page.Paths = template.JS("[" + pathsData[:len(pathsData)-2] + "]")
page.Heatmap = template.JS("[" + heatmap[:len(heatmap)-2] + "]")
t, err := template.New("webpage").Parse(tpl)
checkError(err)
err = t.Execute(&tplBuffer, page)
checkError(err)
return tplBuffer.Bytes()
}
func main() {
//Parse command line arguments
var gpsxmlFile = flag.String("f", "", "Gpsxml input file")
var bssid = flag.String("b", "", "File or comma seperated list of bssids")
var outFile = flag.String("o", "", "Html Output file")
flag.Parse()
if !flag.Parsed() || !(flag.NFlag() == 3) {
fmt.Println("Usage: warmap -f <Kismet gpsxml file> -b <File or List of BSSIDs> -o <HTML output file>")
os.Exit(1)
}
xmlTree := parseXML(*gpsxmlFile)
bssidData := parseBssid(*bssid)
filteredPoints := filterBSSID(xmlTree.GPSPoints, bssidData)
parsedCoords := processCoords(filteredPoints)
templateBuffer := populateTemplate(findConvexHull(parsedCoords), parsedCoords)
ioutil.WriteFile(*outFile, templateBuffer, 0644)
}