forked from tonesto7/echo-speaks-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
893 lines (857 loc) · 38.7 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
const packageFile = require("./package.json"),
appVer = packageFile.version,
alexaCookie = require("./libs/alexa-cookie/alexa-cookie"),
reqPromise = require("request-promise"),
logger = require("./logger"),
express = require("express"),
bodyParser = require("body-parser"),
childProcess = require("child_process"),
compareVersions = require("compare-versions"),
os = require("os"),
editJsonFile = require("edit-json-file", {
autosave: true,
}),
dataFolder = os.homedir(),
configFile = editJsonFile(dataFolder + "/es_config.json"),
sessionFile = editJsonFile(dataFolder + "/session.json"),
fs = require("fs"),
webApp = express(),
urlencodedParser = bodyParser.urlencoded({
extended: false,
});
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
sessionFile.save();
// These the config variables
let configData = {};
let sessionData = sessionFile.get() || {};
let runTimeData = {};
let forceHeroku = false;
runTimeData.savedConfig = {};
runTimeData.scheduledUpdatesActive = false;
runTimeData.alexaUrl = "https://alexa.amazon.com";
runTimeData.loginProxyActive = false;
runTimeData.loginComplete = false;
runTimeData.ignoredDevices = {};
runTimeData.authenticated = false;
runTimeData.serviceDebug = false;
runTimeData.serviceTrace = false;
runTimeData.serviceStartTime = Date.now(); //Returns time in millis
runTimeData.eventCount = 0;
runTimeData.echoDevices = {};
runTimeData.guardData = {};
function initConfig() {
return new Promise((resolve) => {
// logger.debug('dataFolder: ' + dataFolder);
// Create the log directory if it does not exist
if (!fs.existsSync(dataFolder)) {
fs.mkdirSync(dataFolder);
}
if (!fs.existsSync(dataFolder + "/logs")) {
fs.mkdirSync(dataFolder + "/logs");
}
resolve(loadConfig());
});
}
function loadConfig() {
configData = configFile.get() || {};
// console.log(configData);
if (!configData.settings) {
configData.settings = {};
}
if (process.env.hostUrl) {
configFile.set("settings.hostUrl", process.env.hostUrl);
}
configFile.set("settings.useHeroku", forceHeroku || process.env.useHeroku === true || process.env.useHeroku === "true");
configFile.set("settings.amazonDomain", process.env.amazonDomain || configData.settings.amazonDomain || "amazon.com");
configFile.set("settings.hubPlatform", "Hubitat");
configFile.set("settings.ipAddress", process.env.ipAddress || getIPAddress());
configFile.set("settings.appCallbackUrl", process.env.appCallbackUrl || configData.settings.appCallbackUrl || (process.env.smartThingsUrl !== null ? process.env.smartThingsUrl : configData.settings.smartThingsUrl));
if (process.env.serviceDebug === true || process.env.serviceDebug === "true") console.log("** SERVICE DEBUG IS ACTIVE **");
configFile.set("settings.serviceDebug", process.env.serviceDebug === true || process.env.serviceDebug === "true");
configFile.set("settings.serviceTrace", process.env.serviceTrace === true || process.env.serviceTrace === "true");
configFile.set("settings.regionLocale", process.env.regionLocale || configData.settings.regionLocale || "en-US"),
// configFile.set('settings.serviceDebug', true);
// configFile.set('settings.serviceTrace', true);
configFile.set("settings.serverPort", process.env.PORT || configData.settings.serverPort || 8091);
if (!configData.state) {
configData.state = {};
}
configFile.set("state.scriptVersion", appVer);
configFile.save();
configData = configFile.get();
// console.log("configData: ", configData);
return true;
}
const getLocalHost = (noPort = false) => {
return `${configData.settings.ipAddress}${noPort || configData.settings.useHeroku ? "" : `:${configData.settings.serverPort}`}`;
};
const getProtoPrefix = () => {
return `${configData.settings.useHeroku ? "https" : "http"}`;
};
function startWebConfig() {
return new Promise((resolve, reject) => {
try {
webApp.listen(configData.settings.serverPort, () => {
logger.info(`** Echo Speaks Config Service (v${appVer}) is Running at (IP: ${configData.settings.ipAddress} | Port: ${configData.settings.serverPort}) | ProcessId: ${process.pid} **`);
// logger.info(`** To Signin to Amazon please open your browser to: (${getProtoPrefix()}://${getLocalHost()}) **`);
logger.info(`** On Heroku: (${configData.settings.useHeroku}) **`);
checkVersion();
});
// }
webApp.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
webApp.get("/", (req, res) => {
if (req.hostname) {
if (configData.settings.hostUrl === undefined || configData.settings.hostUrl !== req.hostname) {
logger.debug(`set host url: ${req.hostname}`);
configFile.set("settings.hostUrl", req.hostname);
configFile.save();
configData.settings.hostUrl = req.hostname;
}
}
if (!configData.state.loginComplete) {
startWebServer();
}
logger.debug("config(/) page requested");
res.sendFile(__dirname + "/public/index.html");
});
webApp.get("/config", (req, res) => {
if (req.hostname) {
if (configData.settings.hostUrl === undefined || configData.settings.hostUrl !== req.hostname) {
logger.debug(`set host url: ${req.hostname}`);
configFile.set("settings.hostUrl", req.hostname);
configFile.save();
configData.settings.hostUrl = req.hostname;
}
}
if (!configData.state.loginComplete) {
startWebServer();
}
logger.debug("/config page requested");
res.sendFile(__dirname + "/public/index.html");
});
webApp.get("/manualCookie", (req, res) => {
logger.debug("/manualCookie page requested");
res.sendFile(__dirname + "/public/manual_cookie.html");
});
webApp.get("/cookieData", (req, res) => {
// console.log(configData)
res.send(JSON.stringify(sessionFile.get() || {}));
});
webApp.post("/wakeup", (req, res) => {
// console.log('req: ', req.headers);
logger.info(`Server Wakeup Received | Reason: (${req.headers.wakesrc})`);
res.send("OK");
});
webApp.get("/checkVersion", (req, res) => {
// console.log(configData)
res.send(JSON.stringify(checkVersion()));
});
webApp.get("/agsData", async (req, res) => {
logger.info("Requesting Guard Support Data...");
let resp = await getGuardDataSupport();
res.send(
JSON.stringify({
guardData: resp || null,
}),
);
});
webApp.post("/cookieData", (req, res) => {
let saveFile = false;
if (req.headers.cookiedata) {
let cData = JSON.parse(req.headers.cookiedata);
// console.log(cData);
sessionFile.set("cookieData", {
localCookie: cData.cookie,
csrf: cData.csrf,
});
saveFile = true;
}
if (saveFile) {
sessionFile.save();
sessionData = sessionFile.get();
logger.debug("** Cookie Settings File Updated via Manual Entry **");
sendCookiesToEndpoint(configData.settings.appCallbackUrl ? String(configData.settings.appCallbackUrl).replace("/receiveData?", "/cookie?") : null, sessionData.cookieData).then((sendResp) => {
if (sendResp) {
res.send("done");
} else {
res.send("failed");
}
});
} else {
res.send("failed");
}
});
webApp.get("/clearAuth", urlencodedParser, (req, res) => {
logger.verbose("got request for to clear authentication");
clearAuth().then(() => {
res.send({
result: "Clear Complete",
});
});
});
webApp.get("/refreshCookie", urlencodedParser, (req, res) => {
logger.verbose("refreshCookie request received");
logger.debug(`cookieData: ${runTimeData.savedConfig || null}`);
alexaCookie.refreshAlexaCookie(
{
formerRegistrationData: runTimeData.savedConfig.cookieData,
},
(err, result) => {
if (result && Object.keys(result).length >= 2) {
isCookieValid(result).then((valid) => {
if (valid) {
sendCookiesToEndpoint(configData.settings.appCallbackUrl ? String(configData.settings.appCallbackUrl).replace("/receiveData?", "/cookie?") : null, result);
runTimeData.savedConfig.cookieData = result;
// console.log('RESULT: ' + err + ' / ' + JSON.stringify(result));
logger.info("Successfully Refreshed Alexa Cookie...");
res.send({
result: JSON.stringify(result),
});
} else {
logger.error(`** ERROR: Unsuccessfully refreshed Alexa Cookie it was found to be invalid/expired... **`);
logger.error("RESULT: " + err + " / " + JSON.stringify(result));
logger.warn(`** WARNING: We are clearing the Cookie from ${configData.settings.hubPlatform} to prevent further requests and server load... **`);
sendClearAuthToHub();
}
});
} else {
logger.error(`** ERROR: Unsuccessfully refreshed Alexa Cookie it was found to be invalid/expired... **`);
logger.error("RESULT: " + err + " / " + JSON.stringify(result));
logger.warn(`** WARNING: We are clearing the Cookie from ${configData.settings.hubPlatform} to prevent further requests and server load... **`);
sendClearAuthToHub();
}
setTimeout(() => {
logger.warn("Restarting after cookie refresh attempt");
process.exit(1);
}, 25 * 1000);
},
);
});
webApp.get("/configData", (req, res) => {
// console.log(configData)
res.send(JSON.stringify(configData));
});
webApp.post("/configData", (req, res) => {
let saveFile = false;
if (req.headers.user) {
configFile.set("settings.user", req.headers.user);
saveFile = true;
}
if (req.headers.password) {
configFile.set("settings.password", req.headers.password);
saveFile = true;
}
if (req.headers.appcallbackurl) {
configFile.set("settings.appCallbackUrl", req.headers.appcallbackurl);
saveFile = true;
}
if (req.headers.hubplatform) {
console.log("hubPlatform: " + req.headers.hubplatform);
configFile.set("settings.hubPlatform", req.headers.hubplatform);
saveFile = true;
}
if (req.headers.serverport) {
configFile.set("settings.serverPort", req.headers.serverport);
saveFile = true;
}
if (saveFile) {
configFile.save();
loadConfig();
res.send("done");
configCheckOk().then((res) => {
if (res) {
// console.log('configData(set): ', configData);
logger.debug("** Settings File Updated via Web Config **");
if (!runTimeData.scheduledUpdatesActive || !runTimeData.loginProxyActive) {
startWebServer();
}
}
});
} else {
res.send("failed");
}
});
webApp.get("/cookie-success", (req, res) => {
const host = req.get("host");
res.send(loginSuccessHtml(host));
});
resolve(true);
} catch (ex) {
reject(ex);
}
});
}
let clearAuth = () => {
return new Promise((resolve) => {
logger.verbose("got request for to clear authentication");
let clearUrl = configData.settings.appCallbackUrl ? String(configData.settings.appCallbackUrl).replace("/receiveData?", "/cookie?") : null;
clearSession(clearUrl, configData.settings.useHeroku);
configFile.set("state.loginProxyActive", true);
configData.state.loginProxyActive = true;
runTimeData.savedConfig.cookieData = undefined;
runTimeData.authenticated = false;
configFile.set("state.loginComplete", false);
configData.state.loginComplete = false;
configFile.unset("user");
configFile.unset("password");
configFile.save();
startWebServer();
resolve(true);
});
};
function startWebServer(checkForCookie = false) {
const isHeroku = configData.settings.useHeroku === true || configData.settings.useHeroku === "true";
const alexaOptions = {
debug: configData.settings.serviceDebug === true,
trace: configData.settings.serviceTrace === true,
checkForCookie: checkForCookie,
serverPort: configData.settings.serverPort,
amazonPage: configData.settings.amazonDomain === "amazon.co.jp" ? configData.settings.amazonDomain : undefined,
logger: console.log,
baseAmazonPage: configData.settings.amazonDomain === "amazon.co.jp" ? configData.settings.amazonDomain : undefined,
// alexaServiceHost: ((configData.settings.amazonDomain === 'amazon.de' || configData.settings.amazonDomain === 'amazon.co.uk') ? 'layla.' : 'pitangui.') + configData.settings.amazonDomain,
setupProxy: true,
proxyOwnIp: configData.settings.ipAddress || getIPAddress(),
proxyListenBind: "0.0.0.0",
// proxyLogLevel: 'info', // optional: Loglevel of Proxy, default 'warn'
protocolPrefix: getProtoPrefix(),
regDataAppName: "Echo Speaks",
useHeroku: isHeroku,
proxyHost: configData.settings.hostUrl,
proxyPort: configData.settings.serverPort,
proxyRootPath: isHeroku ? "/proxy" : "/proxy",
acceptLanguage: configData.settings.regionLocale,
formerRegistrationData: runTimeData.savedConfig.cookieData,
expressInstance: webApp,
callbackEndpoint: configData.settings.appCallbackUrl ? String(configData.settings.appCallbackUrl).replace("/receiveData?", "/cookie?") : null,
};
configFile.set("state.loginProxyActive", true);
configFile.set("state.loginComplete", false);
configFile.save();
configData = configFile.get();
runTimeData.loginProxyActive = true;
alexaLogin(undefined, undefined, alexaOptions, async (error, response, config) => {
runTimeData.alexaUrl = `https://alexa.${configData.settings.amazonDomain}`;
if (config) {
runTimeData.savedConfig = config;
}
// console.log('error:', error);
if (response !== undefined && response !== "") {
logger.debug("Alexa Login Status: " + response);
}
sendServerDataToHub();
// console.log('response: ', response);
if (response.startsWith("Login Successful") && config.devicesArray) {
configFile.set("state.loginProxyActive", false);
configData.state.loginProxyActive = false;
configFile.set("state.loginComplete", true);
configData.state.loginComplete = true;
configFile.save();
logger.silly("Echo Speaks Alexa API is Actively Running at (IP: " + configData.settings.ipAddress + " | Port: " + configData.settings.serverPort + ") | ProcessId: " + process.pid);
runTimeData.guardData = await getGuardDataSupport();
}
});
}
function sendServerDataToHub() {
let url = configData.settings.appCallbackUrl;
return new Promise((resolve) => {
if (url) {
const options = {
method: "POST",
uri: url,
body: {
version: appVer,
onHeroku: configData.settings.useHeroku === true,
isLocal: configData.settings.useHeroku !== true,
serverUrl: configData.settings.useHeroku === true ? null : `http://${getLocalHost()}`,
},
json: true,
};
// console.log("Sending Server Data to Hub: ", options);
reqPromise(options)
.then((resp) => {
// console.log("resp:", resp);
if (resp) {
logger.info(`** ServerVersion Sent to ${configData.settings.hubPlatform} Endpoint Successfully! **`);
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
logger.error(`ERROR: Unable to send Server Version to ${configData.settings.hubPlatform}: ` + err.message);
resolve(false);
});
}
});
}
function sendClearAuthToHub() {
let url = configData.settings.appCallbackUrl ? String(configData.settings.appCallbackUrl).replace("/receiveData?", "/cookie?") : null;
return new Promise((resolve) => {
if (url) {
let options = {
method: "DELETE",
uri: url,
json: true,
};
reqPromise(options)
.then((resp) => {
// console.log('resp:', resp);
if (resp) {
logger.info(`** Sent Request to ${configData.settings.hubPlatform} Cloud Endpoint to Remove All Auth Data Successfully! **`);
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
logger.error(`ERROR: Unable to send Auth Data Reset to ${configData.settings.hubPlatform}: ` + err.message);
resolve(false);
});
}
});
}
function getGuardDataSupport() {
return new Promise((resolve) => {
if (runTimeData.guardData && Object.keys(runTimeData.guardData)) {
resolve(runTimeData.guardData);
}
// console.log('alexaUrl: ', runTimeData.alexaUrl);
// console.log('cookieData: ', runTimeData.savedConfig.cookieData);
if (runTimeData.alexaUrl && runTimeData.savedConfig.cookieData) {
reqPromise({
method: "GET",
uri: `${runTimeData.alexaUrl}/api/phoenix`,
query: {
cached: true,
_: new Date().getTime(),
},
headers: {
cookie: runTimeData.savedConfig.cookieData.localCookie,
csrf: runTimeData.savedConfig.cookieData.csrf,
},
json: true,
})
.then((resp) => {
// console.log('guardresp:', resp);
if (resp && resp.networkDetail) {
let details = JSON.parse(resp.networkDetail);
let locDetails = details.locationDetails.locationDetails.Default_Location.amazonBridgeDetails.amazonBridgeDetails["LambdaBridge_AAA/OnGuardSmartHomeBridgeService"] || undefined;
if (locDetails && locDetails.applianceDetails && locDetails.applianceDetails.applianceDetails) {
let applKey = Object.keys(locDetails.applianceDetails.applianceDetails).filter((i) => {
return i.includes("AAA_OnGuardSmartHomeBridgeService_");
});
if (Object.keys(applKey).length >= 1) {
let guardData = locDetails.applianceDetails.applianceDetails[applKey[0]];
// console.log('guardData: ', guardData);
if (guardData.modelName === "REDROCK_GUARD_PANEL") {
let gData = {
entityId: guardData.entityId,
applianceId: guardData.applianceId,
friendlyName: guardData.friendlyName,
supported: true,
};
// console.log(JSON.stringify(gData));
runTimeData.guardData = gData;
resolve(gData);
} else {
logger.error("AlexaGuardDataSupport | No Alexa Guard Appliance Data found...");
runTimeData.guardData = undefined;
resolve(undefined);
}
} else {
logger.error("AlexaGuardDataSupport | No Alexa Guard Appliance Data found...");
runTimeData.guardData = undefined;
resolve(undefined);
}
} else {
logger.error("AlexaGuardDataSupport | No Alexa Guard Appliance Data found...");
runTimeData.guardData = undefined;
resolve(undefined);
}
} else {
logger.error("AlexaGuardDataSupport | No Alexa Guard Appliance Data found...");
runTimeData.guardData = undefined;
resolve(undefined);
}
})
.catch((err) => {
logger.error(`ERROR: Unable to send Alexa Guard Data to ${configData.settings.hubPlatform}: ` + err.message);
resolve(undefined);
});
} else {
runTimeData.guardData = undefined;
resolve(undefined);
}
});
}
function configCheckOk() {
return new Promise((resolve) => {
let res = ((configData.settings.useHeroku === true && !configData.settings.appCallbackUrl) || !configData.settings.amazonDomain || (!configData.settings.useHeroku && !configData.settings.appCallbackUrl)) !== true;
resolve(res);
});
}
initConfig()
.then((res) => {
if (res) {
startWebConfig()
.then(() => {
configCheckOk().then((res) => {
if (res === true) {
if (configData.state.loginComplete === true || (configData.settings.hostUrl && configData.settings.appCallbackUrl)) {
logger.info("-- Echo Speaks Web Service Starting Up! Takes about 10 seconds before it's available... --");
startWebServer(configData.settings.useHeroku === true && configData.settings.appCallbackUrl !== undefined);
} else {
logger.info(`** Echo Speaks Web Service is Waiting for Amazon Login to Start... loginComplete: ${configData.state.loginComplete || undefined} | hostUrl: ${configData.settings.hostUrl || undefined} | appCallbackUrl: ${configData.settings.appCallbackUrl} **`);
}
} else {
logger.error("Config Check Did Not Pass");
}
});
})
.catch((err) => {
logger.error("## Start Web Config Error: " + err.message);
});
}
})
.catch((err) => {
logger.error("## InitConfig Error: " + err.message);
});
function alexaLogin(username, password, alexaOptions, callback) {
let config = {};
config.devicesArray = [];
config.alexaURL = alexaOptions.amazonPage;
getRemoteCookie(alexaOptions).then((remoteCookies) => {
runTimeData.serviceDebug && logger.debug(`remoteCookies: ${JSON.stringify(remoteCookies) || undefined} | keys: ${Object.keys(remoteCookies) || {}}`);
if (remoteCookies !== undefined && Object.keys(remoteCookies).length > 0 && remoteCookies.cookieData && remoteCookies.cookieData.localCookie && remoteCookies.cookieData.csrf) {
updSessionItem("cookieData", remoteCookies.cookieData);
config.cookieData = remoteCookies.cookieData;
callback(null, `Login Successful (Retreived from ${configData.settings.hubPlatform})`, config);
} else if (sessionData && sessionData.cookieData && Object.keys(sessionData.cookieData) >= 2) {
config.cookieData = sessionData.cookieData || {};
callback(null, "Login Successful (Stored Session)", config);
} else {
alexaCookie.generateAlexaCookie(username, password, alexaOptions, (err, result) => {
// console.log('generateAlexaCookie error: ', err);
// console.log('generateAlexaCookie result: ', result);
if (err && (err.message.startsWith("Login unsuccessful") || err.message.startsWith("Amazon-Login-Error:") || err.message.startsWith(" You can try to get the cookie manually by opening"))) {
logger.debug("Please complete Amazon login by going here: (http://" + alexaOptions.proxyHost + ":" + alexaOptions.proxyPort + "/config)");
} else if (err && !result) {
logger.error("generateAlexaCookie: " + err.message);
callback(err, "There was an error", null);
} else if (result) {
runTimeData.alexaUrl = "https://alexa." + alexaOptions.amazonPage;
// IMPORTANT: can be called multiple times!! As soon as a new cookie is fetched or an error happened. Consider that!
runTimeData.serviceDebug && logger.debug("cookie: " + result.localCookie || undefined);
runTimeData.serviceDebug && logger.debug("csrf: " + result.csrf || undefined);
if (result && result.csrf && (result.cookie || result.localCookie)) {
logger.debug(`result: ${result}`);
updSessionItem("cookieData", result);
config.cookieData = result;
sendCookiesToEndpoint(alexaOptions.callbackEndpoint, result);
alexaCookie.stopProxyServer();
callback(null, "Login Successful", config);
} else {
callback(true, "There was an error getting authentication", null);
if (alexaOptions.callbackEndpoint) {
clearSession(alexaOptions.callbackEndpoint);
}
}
}
});
}
});
}
let updSessionItem = (key, value) => {
if (sessionData[key] === undefined || sessionData[key] !== value) {
sessionFile.set(key, value);
sessionFile.save();
}
sessionData = sessionFile.get();
};
let remSessionItem = (key) => {
sessionFile.unset(key);
sessionFile.save();
sessionData = sessionFile.get();
};
var clearSession = () => {
remSessionItem("csrf");
remSessionItem("cookie");
remSessionItem("cookieData");
if (runTimeData.savedConfig.cookieData) delete runTimeData.savedConfig.cookieData;
sendClearAuthToHub();
};
function getRemoteCookie(alexaOptions) {
console.log("getRemoteCookie...");
return new Promise((resolve) => {
// if (alexaOptions.checkForCookie === false) {
// resolve(undefined);
// }
let config = {};
if (alexaOptions.callbackEndpoint) {
getCookiesFromEndpoint(alexaOptions.callbackEndpoint).then((data) => {
if (data) {
updSessionItem("cookieData", data);
config.cookieData = data;
resolve(config);
}
resolve(config);
});
} else {
resolve(config);
}
});
}
function sendCookiesToEndpoint(url, cookieData) {
return new Promise((resolve) => {
if (url && cookieData && Object.keys(cookieData).length >= 2) {
let options = {
method: "POST",
uri: url,
body: {
cookieData: cookieData,
version: appVer,
onHeroku: configData.settings.useHeroku === true,
isLocal: configData.settings.useHeroku !== true,
serverUrl: configData.settings.useHeroku === true ? null : `http://${getLocalHost()}`,
},
json: true,
};
// console.log("sendCookiesToEndpoint: ", options);
reqPromise(options)
.then((resp) => {
// console.log('resp:', resp);
if (resp) {
logger.info(`** Alexa Cookie Data sent to ${configData.settings.hubPlatform} Cloud Endpoint Successfully! **`);
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
logger.error(`ERROR: Unable to send Alexa Cookie Data to ${configData.settings.hubPlatform}: ` + err.message);
resolve(false);
});
}
});
}
function isCookieValid(cookieData) {
return new Promise((resolve) => {
if (!(cookieData && cookieData.loginCookie && cookieData.csrf)) {
logger.error(`isCookieValid ERROR | Cookie or CSRF value not received!!!`);
resolve(false);
}
reqPromise({
method: "GET",
uri: `https://alexa.${configData.settings.amazonDomain}/api/bootstrap`,
query: {
version: 0,
},
headers: {
Cookie: cookieData.loginCookie,
csrf: cookieData.csrf,
},
json: true,
})
.then((resp) => {
if (resp && resp.authentication) {
let valid = resp.authentication.authenticated !== false;
// logger.info(`** Alexa Cookie Valid (${valid}) **`);
resolve(valid);
}
resolve(true);
})
.catch((err) => {
logger.error(`ERROR: Unable to validate Alexa Cookie Data: ` + err.message);
resolve(true);
});
});
}
function getCookiesFromEndpoint(url) {
return new Promise((resolve) => {
reqPromise({
method: "GET",
uri: url,
headers: {
serverVersion: appVer,
onHeroku: configData.settings.useHeroku === true,
isLocal: configData.settings.useHeroku !== true,
},
json: true,
})
.then((resp) => {
// console.log('getCookiesFromEndpoint resp: ', resp);
if (resp && Object.keys(resp).length >= 2) {
logger.info(`** Retrieved Alexa Cookie Data from ${configData.settings.hubPlatform} Cloud Endpoint Successfully! **`);
isCookieValid(resp).then((valid) => {
if (valid) {
logger.info(`** Alexa Cookie Data Received from ${configData.settings.hubPlatform} Cloud Endpoint has been Confirmed to be Valid! **`);
resolve(resp);
} else {
logger.error(`** ERROR: In an attempt to validate the Alexa Cookie from ${configData.settings.hubPlatform} it was found to be invalid/expired... **`);
logger.warn(`** WARNING: We are clearing the Cookie from ${configData.settings.hubPlatform} to prevent further requests and server load... **`);
sendClearAuthToHub().then(() => {
clearAuth().then(() => {
resolve(undefined);
});
});
}
});
} else {
resolve(false);
}
})
.catch((err) => {
logger.error(`ERROR: Unable to retrieve Alexa Cookie Data from ${configData.settings.hubPlatform}: ` + err.message);
resolve({});
});
});
}
/*******************************************************************************
SYSTEM INFO FUNCTIONS
********************************************************************************/
function getIPAddress() {
const interfaces = os.networkInterfaces();
let addr = "0.0.0.0";
Object.keys(interfaces).forEach((dev) => {
interfaces[dev].filter((details) => {
// console.log(details);
const cidr = details.cidr.split("/");
const cidr24 = cidr.indexOf("24");
if (details.family === "IPv4" && details.internal === false && cidr24 !== -1) {
console.log("IP Address: ", details.address);
addr = details.address;
}
});
});
return addr;
}
const loginSuccessHtml = (host) => {
let html = "";
let redirUrl = configData.settings.useHeroku ? "https://" + host + "/config" : "http://" + host + "/config";
html += "<!DOCTYPE html>";
html += "<html>";
html += " <head>";
html += ' <meta name="viewport" content="width=640">';
html += " <title>Echo Speaks Amazon Authentication</title>";
html += ' <style type="text/css">';
html += " body { background-color: slategray; text-align: center; }";
html += " .container {";
html += " width: 90%;";
html += " padding: 4%;";
html += " text-align: center;";
html += " color: white;";
html += " }";
html += " p {";
html += " font-size: 2.2em;";
html += " text-align: center;";
html += " padding: 0 40px;";
html += " margin-bottom: 0;";
html += " }";
html += " </style>";
html += " </head>";
html += " <body>";
html += ' <div class="container">';
html += " <h3>Amazon Alexa Cookie Retrieved Successfully</h3>";
html += " <h5>You will be redirected back to the config page in 5 seconds.</h5>";
html += " </div>";
html += " <script>setTimeout( function(){ window.location.href = '" + redirUrl + "'; }, 5000 );</script>";
html += " </body>";
html += "</html>";
return html;
};
function checkVersion() {
logger.info("Checking for Server Version Updates...");
try {
childProcess.exec(`npm view ${packageFile.name} version`, (error, stdout) => {
const newVer = stdout && stdout.trim();
if (newVer && compareVersions(stdout.trim(), packageFile.version) > 0) {
logger.warn(`---------------------------------------------------------------`);
logger.warn(`NOTICE: New version of ${packageFile.name} available: ${newVer}`);
logger.warn(`---------------------------------------------------------------`);
return {
update: true,
version: newVer,
};
} else {
logger.info(`Server Version is Up-to-Date.`);
return {
update: false,
version: undefined,
};
}
});
} catch (e) {
return {
update: false,
version: undefined,
};
}
}
/*******************************************************************************
PROCESS EXIT FUNCTIONS
********************************************************************************/
//so the program will not close instantly
process.stdin.resume();
//do something when app is closing
process.on(
"exit",
exitHandler.bind(null, {
cleanup: true,
}),
);
//catches ctrl+c event
process.on(
"SIGINT",
exitHandler.bind(null, {
exit: true,
}),
);
process.on(
"SIGUSR2",
exitHandler.bind(null, {
exit: true,
}),
);
process.on(
"SIGHUP",
exitHandler.bind(null, {
exit: true,
}),
);
process.on(
"SIGTERM",
exitHandler.bind(null, {
exit: true,
}),
);
process.on(
"uncaughtException",
exitHandler.bind(null, {
exit: true,
}),
);
function exitHandler(options, exitCode) {
alexaCookie.stopProxyServer();
if (runTimeData.scheduledUpdatesActive) {
// stopScheduledDataUpdates();
}
if (options.cleanup) {
console.log("clean");
}
if (exitCode || exitCode === 0) {
console.log(exitCode);
}
if (options.exit) {
process.exit();
}
console.log("graceful setting timeout for PID: " + process.pid);
setTimeout(() => {
console.error("Could not close connections in time, forcefully shutting down");
process.exit(1);
}, 2 * 1000);
}