1 |
office |
1 |
#!/usr/bin/env node |
|
|
2 |
|
|
|
3 |
'use strict'; |
|
|
4 |
|
|
|
5 |
const request = require('request'); |
|
|
6 |
const tls = require('tls'); |
|
|
7 |
const discovery = require('../lib/discovery'); |
|
|
8 |
const { constants } = require('crypto'); |
|
|
9 |
|
|
|
10 |
if (!process.argv[2]) { |
|
|
11 |
console.log('Usage: npm run getpassword [firmware version]'); |
|
|
12 |
process.exit(); |
|
|
13 |
} |
|
|
14 |
|
|
|
15 |
const host = process.argv[2]; |
|
|
16 |
const fversion = process.argv[3]; |
|
|
17 |
|
|
|
18 |
var requestOptions = { |
|
|
19 |
'method': 'POST', |
|
|
20 |
'uri': 'https://' + host + ':443/umi', |
|
|
21 |
'strictSSL': false, |
|
|
22 |
'headers': { |
|
|
23 |
'Content-Type': 'application/json', |
|
|
24 |
'Connection': 'close', |
|
|
25 |
'User-Agent': 'aspen%20production/2618 CFNetwork/758.3.15 Darwin/15.4.0', |
|
|
26 |
'Content-Encoding': 'identity', |
|
|
27 |
'Accept': '*/*', |
|
|
28 |
'Accept-Language': 'en-us', |
|
|
29 |
'Host': host |
|
|
30 |
} |
|
|
31 |
}; |
|
|
32 |
|
|
|
33 |
function checkV1 (rid) { |
|
|
34 |
if (rid === 120) { |
|
|
35 |
console.log('Timeout getting password. Are you following the instructions? You already setup your robot? Its the robot IP correct?'); |
|
|
36 |
process.exit(1); |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
requestOptions.body = '{"do":"get","args":["passwd"],"id":' + rid + '}'; |
|
|
40 |
|
|
|
41 |
request(requestOptions, function (error, response, body) { |
|
|
42 |
if (error) { |
|
|
43 |
console.log('Fatal error connecting to robot. Please verify the IP address and connectivity:', error); |
|
|
44 |
process.exit(1); |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
if (response.statusCode === 401) { |
|
|
48 |
setTimeout(function () { checkV1(++rid); }, 2000); |
|
|
49 |
} else if (response.statusCode === 200) { |
|
|
50 |
console.log('========>'); |
|
|
51 |
let pass = JSON.parse(body).ok.passwd; |
|
|
52 |
console.log('Good job!'); |
|
|
53 |
console.log('Password: ' + pass); |
|
|
54 |
getBlid(++rid, pass); |
|
|
55 |
} else { |
|
|
56 |
console.log('Unespected response. Checking again...'); |
|
|
57 |
setTimeout(function () { checkV1(++rid); }, 2000); |
|
|
58 |
} |
|
|
59 |
}); |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
function getBlid (rid, pass) { |
|
|
63 |
requestOptions.body = '{"do":"get","args":["sys"],"id":' + rid + '}'; |
|
|
64 |
requestOptions.headers['Authorization'] = 'Basic ' + new Buffer('user:' + pass).toString('base64'); |
|
|
65 |
|
|
|
66 |
request(requestOptions, function (error, response, body) { |
|
|
67 |
if (error) { |
|
|
68 |
console.log('Fatal error getting username/blid:', error); |
|
|
69 |
process.exit(1); |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
if (response.statusCode === 200) { |
|
|
73 |
const blid = JSON.parse(body).ok.blid.map(function (dec) { |
|
|
74 |
return (dec + 0x10000).toString(16).substr(-2).toUpperCase(); |
|
|
75 |
}).join(''); |
|
|
76 |
|
|
|
77 |
console.log('Username/blid: ' + blid); |
|
|
78 |
console.log('Use this credentials in dorita980 lib :)'); |
|
|
79 |
} else { |
|
|
80 |
console.log('Unespected error getting username/blid'); |
|
|
81 |
} |
|
|
82 |
}); |
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
function checkV2 () { |
|
|
86 |
var sliceFrom = 13; |
|
|
87 |
discovery.getRobotPublicInfo(host, function (e, robotData) { |
|
|
88 |
console.log('Robot Data:'); |
|
|
89 |
console.log(robotData); |
|
|
90 |
}); |
|
|
91 |
const packet = 'f005efcc3b2900'; |
|
|
92 |
var client = tls.connect(8883, host, {timeout: 10000, rejectUnauthorized: false, ciphers: process.env.ROBOT_CIPHERS || 'AES128-SHA256', secureOptions: constants.SSL_OP_LEGACY_SERVER_CONNECT}, function () { |
|
|
93 |
client.write(new Buffer(packet, 'hex')); |
|
|
94 |
}); |
|
|
95 |
|
|
|
96 |
client.on('data', function (data) { |
|
|
97 |
if (data.length === 2) { |
|
|
98 |
sliceFrom = 9; |
|
|
99 |
return; |
|
|
100 |
} |
|
|
101 |
if (data.length <= 7) { |
|
|
102 |
console.log('Error getting password. Follow the instructions and try again.'); |
|
|
103 |
} else { |
|
|
104 |
console.log('Password=> ' + new Buffer(data).slice(sliceFrom).toString() + ' <= Yes, all this string.'); |
|
|
105 |
console.log('Use this credentials in dorita980 lib :)'); |
|
|
106 |
} |
|
|
107 |
client.end(); |
|
|
108 |
process.exit(0); |
|
|
109 |
}); |
|
|
110 |
|
|
|
111 |
client.setEncoding('utf-8'); |
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
console.log('Make sure your robot is on the Home Base and powered on (green lights on). Then press and hold the HOME button (or DOCK+SPOT on some models) on your robot until it plays a series of tones (about 2 seconds). Release the button and your robot will flash WIFI light.'); |
|
|
115 |
|
|
|
116 |
if (fversion === '1') { |
|
|
117 |
console.log('Then wait and look here...'); |
|
|
118 |
checkV1(1); |
|
|
119 |
} else { |
|
|
120 |
console.log('Then press any key here...'); |
|
|
121 |
process.stdin.setRawMode(true); |
|
|
122 |
process.stdin.resume(); |
|
|
123 |
process.stdin.on('data', checkV2); |
|
|
124 |
} |