forked from particle-iot/softap-setup-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
316 lines (252 loc) · 6.82 KB
/
index.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
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
module.exports = SoftAPSetup;
var net = require('net');
var util = require('util');
var http = require('http');
var config = require('./config');
var defaults = require('./config/defaults');
var rsa = require('node-rsa');
var securityTable = {
"open": 0,
"none": 0,
"wep_psk": 1,
"wep_shared": 0x8001,
"wpa_tkip": 0x00200002,
"wpa_aes": 0x00200004,
"wpa2_aes": 0x00400004,
"wpa2_tkip": 0x00400002,
"wpa2_mixed": 0x00400006,
"wpa2": 0x00400006
};
// hashtag lazyJS
function is(cb) {
if (cb && typeof cb == 'function') { return true }
throw new Error('Invalid callback function provided.');
};
function SoftAPSetup(opts) {
if(opts && typeof opts == 'object') {
Object.keys(opts).forEach(function _loadOpts(key) {
config.set(key, opts[key]);
});
}
this.protocol = config.get('protocol');
this.keepAlive = config.get('keep_alive');
this.noDelay = config.get('no_delay');
this.timeout = config.get('timeout');
this.host = config.get('host');
this.port = config.get('port');
if(!this.protocol) {
this.protocol = config.get('default_protocol');
}
if(!this.port) {
this.port = defaults.available_protocols[this.protocol].port;
}
this.__publicKey = undefined;
return this;
};
SoftAPSetup.prototype.scan = function scan(cb) {
is(cb);
var sock = this.__sendCommand('scan-ap', cb);
return sock;
};
SoftAPSetup.prototype.connect = function connect(cb) {
is(cb);
var sock = this.__sendCommand({ name: 'connect-ap', body: { idx: 0 } }, cb);
return sock;
};
SoftAPSetup.prototype.deviceInfo = function deviceInfo(cb) {
is(cb);
var sock = this.__sendCommand('device-id', response.bind(this));
function response(err, dat) {
if(err) { return cb(err); }
var claimed = dat.c === '1' ? true : false;
this.__deviceID = dat.id;
cb(null, {
id : dat.id,
claimed : claimed
});
};
return sock;
};
SoftAPSetup.prototype.publicKey = function publicKey(cb) {
is(cb);
var sock = this.__sendCommand('public-key', response.bind(this));
function response(err, dat) {
if(err) { return cb(err); }
if(!dat) { return cb(new Error('No data received')); }
if(dat.r !== 0) {
return cb(new Error('Received non-zero response code'));
}
var buff = new Buffer(dat.b, 'hex');
this.__publicKey = new rsa(buff.slice(22), 'pkcs1-public-der', {
encryptionScheme: 'pkcs1'
})
cb(null, this.__publicKey.exportKey('pkcs8-public'));
};
return sock;
};
SoftAPSetup.prototype.setClaimCode = function(code, cb) {
is(cb);
if(!code || typeof code !== "string") {
throw new Error('Must provide claim code string as first parameter');
}
var claim = {
k: "cc"
, v: code
};
var sock = this.__sendCommand({ name: 'set', body: claim }, cb);
return sock;
};
SoftAPSetup.prototype.configure = function configure(opts, cb) {
is(cb);
var securePass = undefined;
if(!this.__publicKey) {
throw new Error('Must retrieve public key of device prior to AP configuration');
}
if(!opts || typeof opts !== 'object') {
throw new Error('Missing configuration options object as first parameter');
}
if(!opts.ssid) {
if(!opts.name) {
throw new Error('Configuration options contain no ssid property');
}
opts.ssid = opts.name;
}
if((opts.enc || opts.sec) && !opts.security) {
opts.security = opts.sec || opts.enc;
}
if(!opts.security) {
opts.security = "open";
opts.password = null;
}
if(opts.password || opts.pass) {
if(!opts.security) {
throw new Error('Password provided but no security type specified');
}
if(opts.pass && !opts.password) {
opts.password = opts.pass;
}
securePass = this.__publicKey.encrypt(opts.password, 'hex');
}
if(typeof opts.security === "string") {
opts.security = securityTable[opts.security];
}
var apConfig = {
idx: 0,
ssid: opts.ssid,
sec: opts.security,
ch: parseInt(opts.channel)
};
if(securePass) { apConfig.pwd = securePass; }
var sock = this.__sendCommand({ name: 'configure-ap', body: apConfig }, cb);
return sock;
};
SoftAPSetup.prototype.__getSocket = function __getSocket(connect, data, error) {
var errorMessage = undefined;
if(typeof connect !== 'function') {
errorMessage = "Invalid connect function specified.";
}
if(typeof data !== 'function') {
errorMessage = "Invalid data function specified.";
}
if(error && typeof error !== 'function') {
errorMessage = "Provided error handler is not a function.";
}
if(errorMessage) { throw new Error(errorMessage); }
var sock = net.createConnection(this.port, this.host);
sock.setTimeout(this.timeout);
sock.on('data', data);
if(error) { sock.on('error', error); }
sock.on('connect', connect);
return sock;
};
SoftAPSetup.prototype.__httpRequest = function __httpRequest(cmd, data, error) {
var sock;
var payload;
var errorMessage = undefined;
if(!cmd || typeof cmd !== "object") {
errorMessage = "Invalid command object specified.";
}
if(errorMessage) { throw new Error(errorMessage); }
var opts = {
method: 'GET',
path: '/' + cmd.name,
hostname: this.host,
port: this.port
};
if((cmd.body) && typeof cmd.body === 'object') {
payload = JSON.stringify(cmd.body);
opts.headers = { 'Content-Length': payload.length };
opts.method = 'POST';
}
sock = http.request(opts, function responseHandler(res) {
var results = '';
res.on('data', function dataHandler(chunk) {
if(chunk) { results += chunk.toString(); }
});
res.on('end', function () {
data(results);
});
});
sock.on('error', error);
payload && sock.write(payload);
sock.end();
return sock;
};
SoftAPSetup.prototype.__sendCommand = function(cmd, cb) {
var sock;
var protocol = this.protocol;
if(typeof cmd == 'string') {
cmd = { name : cmd, body : undefined };
}
else if (typeof cmd == 'object') {
if(!cmd.name) { throw new Error('Command object has no name property'); }
}
else { throw new Error('Invalid command'); }
is(cb);
if(protocol == "http") {
sock = this.__httpRequest(cmd, onData, cb);
}
else {
sock = this.__getSocket(tcpConnected, onData);
}
function tcpConnected() {
if((cmd.body) && typeof cmd.body === 'object') {
var body = JSON.stringify(cmd.body);
var length = body.length;
send = util.format("%s\n%s\n\n%s", cmd.name, length, body);
}
else {
send = util.format("%s\n0\n\n", cmd.name);
}
sock.write(send);
};
function onData(dat) {
if(dat instanceof Buffer || typeof dat === 'string') {
try {
var json = JSON.parse(dat.toString());
}
catch (e) {
return cb(new Error('Invalid JSON received from device.'));
}
}
else if(typeof dat === 'object') {
var json = dat;
}
cb(null, json);
};
return sock;
};
SoftAPSetup.prototype.version = function(cb) {
is(cb);
var sock = this.__sendCommand('version', cb);
return sock;
};
SoftAPSetup.prototype.securityLookup = function(dec) {
var match = null;
Object.keys(securityTable).forEach(function(key) {
if(parseInt(dec) == securityTable[key]) {
match = key;
}
});
return match;
};