/
start.js
170 lines (154 loc) · 4.99 KB
/
start.js
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
"use strict";
var gpsd = require('node-gpsd');
var scanner = require('node-wifiscanner');
var fs = require('fs');
var colors = require('colors');
var location = {};
var store = {};
var config = require('./config.js');
var gpsListener = new gpsd.Listener({
port: 2947,
hostname: 'localhost'
});
function startGPS (callback) {
gpsListener.connect();
gpsListener.watch();
gpsListener.on('TPV', function (tpvData) {
if (tpvData.lat && tpvData.lon) {
let gotFix = !location.lat || !location.lon;
location.lat = tpvData.lat;
location.lon = tpvData.lon;
if (gotFix) {
callback();
}
}
});
}
function stopGPS () {
gpsListener.unwatch();
gpsListener.disconnect();
}
function startWifi() {
scanner.scan(function(err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
if (Array.isArray(data)) {
for (var i in data) {
var wifi = data[i];
if (!alreadyLogged(wifi) || hasBetterSignal(wifi)) {
store[wifi['mac']] = {
ssid: wifi['ssid'],
position: { lat: location.lat, lon: location.lon },
signal: parseInt(wifi['signal_level']),
quality: qualityAsPercentage(wifi['quality']),
mac: wifi['mac'],
channel: wifi['channel'],
'protected': (wifi['encryption_key'] === 'on'),
security: workOutSecurity(wifi)
};
addTags(wifi);
} else {
checkForMissingData(wifi);
}
}
}
saveStore();
startWifi();
});
}
function qualityAsPercentage(string) {
let parts = string.split('/');
let percentage = Math.round(parts[0] / parts[1] * 100);
return percentage;
}
function workOutSecurity(wifi) {
return (wifi['encryption_key'] === 'on') ? (wifi['encryption_type'] ? wifi['encryption_type'] : 'WEP') : '';
}
function alreadyLogged(wifi) {
let alreadyLogged = store[wifi.mac] ? true : false;
if (!alreadyLogged) {
console.log('Found new wifi: ' + wifi.ssid);
if(workOutSecurity(wifi) === 'WEP') {
console.log(('Found WEP! SSID: ' + wifi.ssid).black.bgYellow);
}
}
return alreadyLogged;
}
function hasBetterQuality(wifi) {
let oldQuality = store[wifi.mac]['quality'] || 0;
let newQuality = qualityAsPercentage(wifi['quality']);
if (newQuality > oldQuality) {
console.log('Updated wifi: ' + wifi.ssid + ' [Quality: ' + oldQuality + ' -> ' + newQuality + ']');
}
return (newQuality > oldQuality);
}
function hasBetterSignal(wifi) {
let oldSignal = parseInt(store[wifi.mac]['signal']) || -150;
let newSignal = parseInt(wifi.signal_level);
// Sometimes you get signal of 0, not sure why, ignore it
if (newSignal == 0) {
return false;
}
if (newSignal > oldSignal) {
console.log('Updated wifi: ' + wifi.ssid + ' [Signal: ' + oldSignal + ' -> ' + newSignal + ']');
return true;
}
return false;
}
function checkForMissingData(wifi) {
let isProtected = store[wifi.mac].protected;
let security = store[wifi.mac].security;
if (isProtected === true && !security) {
store[wifi.mac].security = workOutSecurity(wifi);
console.log('Updated wifi: ' + wifi.ssid + ' [Added Security Record]');
if(workOutSecurity(wifi) === 'WEP') {
console.log(('Found WEP! SSID: ' + wifi.ssid).black.bgYellow);
}
}
addTags(wifi);
}
function addTags(wifi) {
let tags = config.tags;
for (let tag in tags) {
var tagMatchers = tags[tag];
for (let i in tagMatchers) {
var tagMatcher = tagMatchers[i];
if (wifi.ssid.indexOf(tagMatcher) !== -1 && (!store[wifi.mac].tags || store[wifi.mac].tags.indexOf(tag) === -1)) {
if (!store[wifi.mac].tags) {
store[wifi.mac].tags = [];
}
store[wifi.mac].tags.push(tag);
console.log('Updated wifi: ' + wifi.ssid + ' [Added tag: ' + tag + ']');
}
}
}
}
function loadStore(callback) {
fs.readFile('store.json', (err, data) => {
if (err) {
console.log('Could not load previous data [' + err + ']');
}
if (data) {
store = JSON.parse(data);
}
callback();
});
}
function saveStore() {
fs.writeFile('store.json', JSON.stringify(store, null, 2), (err) => {
if (err) {
console.log('Save error: ' + err);
}
});
}
console.log("Loading previous data..");
loadStore(function() {
console.log("Getting GPS fix..");
startGPS(function() {
console.log('Got GPS fix: ' + location.lat + ' ' + location.lon);
console.log('Scanning for wifi..');
startWifi();
});
});