From cb2123f6d9b70b8bf8008af8ede01985ad70f4e8 Mon Sep 17 00:00:00 2001 From: GermanBluefox Date: Thu, 21 Dec 2023 16:38:20 +0100 Subject: [PATCH] Hide "undefined" in the logging --- README.md | 12 ++++----- alexa-http2push.js | 62 +++++++++++++++++++++++++++------------------- package.json | 8 +++--- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 4202e12..9ad61f1 100644 --- a/README.md +++ b/README.md @@ -13,18 +13,18 @@ Library to remote control an Alexa (Amazon Echo) device via LAN/WLAN. ## Troubleshooting ### Issues when getting the cookie and tokens initially -If you still use the E-Mail or SMS based 2FA flow then this might not work. Please update the 2FA/OTP method in the amazon settings to the current process. +If you still use the E-Mail or SMS based 2FA flow, then this might not work. Please update the 2FA/OTP method in the amazon settings to the current process. -If you open the Proxy URL from a mobile device where also the Alexa App is installed on it might be that it do not work because Amazon might open the Alexa App. So please use a device or PC where the Alexa App is not installed +If you open the Proxy URL from a mobile device where also the Alexa App is installed on, it might be that it does not work because Amazon might open the Alexa App. So please use a device or PC where the Alexa App is not installed -If you see a page that tells you that "alexa.amazon.xx is deprecated" and you should use the alexa app and with a QR code on it when you enter the Proxy URL" then this means that you call the proxy URL ith a different IP/Domainname then you entered in the "proxy own IP" settings or you adjusted the IP shown in the Adapter configuration. The "proxy own IP" setting **needs to** match the IP/Domainname you use to call the proxy URL! +If you see a page that tells you that "alexa.amazon.xx is deprecated" and you should use the alexa app and with a QR code on it when you enter the Proxy URL" then this means that you call the proxy URL ith a different IP/Domainname then you entered in the "proxy own IP" settings, or you adjusted the IP shown in the Adapter configuration. The "proxy own IP" setting **needs to** match the IP/Domainname you use to call the proxy URL! ### Push Connections do not connect Sometimes it could happen that because of too many connection tries aAmazon blocks the push connection endpoint for a specific IP and "device". -If the Push connection is never established then you can try to use the following: -* delete all cookie, formerRegistrationData and macDms from the settings -* lokale the location of the alexa-cookie2 library in your npm tree +If the Push connection is never established, then you can try to use the following: +* delete all cookies, formerRegistrationData and macDms from the settings +* locale the location of the alexa-cookie2 library in your npm tree * check if there is a file like .../alexa-cookie2/lib/formerDataStore.json - if existing please delete them * get new cookie via proxy diff --git a/alexa-http2push.js b/alexa-http2push.js index de75c53..6de3252 100755 --- a/alexa-http2push.js +++ b/alexa-http2push.js @@ -45,7 +45,7 @@ class AlexaHttp2Push extends EventEmitter { this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Use host ${host}`); - const http2_options = { + const http2Options = { ':method': 'GET', ':path': '/v20160207/directives', ':authority': host, @@ -74,7 +74,7 @@ class AlexaHttp2Push extends EventEmitter { this.client = null; this.stream = null; this.connectionActive = false; - this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Close: ' + code + ': ' + reason); + this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Close: ${code}: ${reason}`); if (this.initTimeout) { clearTimeout(this.initTimeout); this.initTimeout = null; @@ -87,17 +87,23 @@ class AlexaHttp2Push extends EventEmitter { clearTimeout(this.pongTimeout); this.pongTimeout = null; } - if (this.stop) return; + if (this.stop) { + return; + } if (this.errorRetryCounter > 100) { this.emit('disconnect', false, 'Too many failed retries. Check cookie and data'); return; - } else { - this.errorRetryCounter++; } + this.errorRetryCounter++; + const retryDelay = (immediateReconnect || this.errorRetryCounter === 1) ? 1 : Math.min(60, this.errorRetryCounter * 5); - this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Retry Connection in ' + retryDelay + 's'); - this.emit('disconnect', true, `Retry Connection in ${retryDelay}s (${code}: ${reason})`); + this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Retry Connection in ${retryDelay}s`); + if (code !== undefined || reason !== undefined) { + this.emit('disconnect', true, `Retry Connection in ${retryDelay}s (${code}: ${reason})`); + } else { + this.emit('disconnect', true, `Retry Connection in ${retryDelay}s`); + } this.reconnectTimeout && clearTimeout(this.reconnectTimeout); this.reconnectTimeout = setTimeout(() => { this.reconnectTimeout = null; @@ -123,12 +129,14 @@ class AlexaHttp2Push extends EventEmitter { }; try { - this.client = http2.connect(`https://${http2_options[':authority']}`, () => { - if (!this.client) return; + this.client = http2.connect(`https://${http2Options[':authority']}`, () => { + if (!this.client) { + return; + } try { - this.stream = this.client.request(http2_options); + this.stream = this.client.request(http2Options); } catch (error) { - this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Error on Request ' + error.message); + this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Error on Request ${error.message}`); this.emit('error', error); return; } @@ -142,8 +150,7 @@ class AlexaHttp2Push extends EventEmitter { } onHttp2Close(headers[':status'], undefined, this.errorRetryCounter < 3); }); - } - else if (headers[':status'] !== 200) { + } else if (headers[':status'] !== 200) { onHttp2Close(headers[':status']); } }); @@ -159,13 +166,15 @@ class AlexaHttp2Push extends EventEmitter { this.client.ping(() => onPingResponse(false)); this.pingPongInterval = setInterval(() => { - if (!this.stream || !this.client) return; + if (!this.stream || !this.client) { + return; + } this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Send Ping'); - //console.log('SEND: ' + msg.toString('hex')); + // console.log('SEND: ' + msg.toString('hex')); try { this.client.ping(() => onPingResponse(true)); } catch (error) { - this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Error on Ping ' + error.message); + this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Error on Ping ${error.message}`); } this.pongTimeout = setTimeout(() => { @@ -202,11 +211,11 @@ class AlexaHttp2Push extends EventEmitter { const command = dataContent.command; const payload = JSON.parse(dataContent.payload); - this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Command ' + command + ': ' + JSON.stringify(payload, null, 4)); + this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Command ${command}: ${JSON.stringify(payload, null, 4)}`); this.emit('command', command, payload); }); } catch (err) { - this.emit('unexpected-response', `Could not parse json: ${message} : ${err.message}`); + this.emit('unexpected-response', `Could not parse json: ${message}: ${err.message}`); } } }); @@ -214,7 +223,7 @@ class AlexaHttp2Push extends EventEmitter { this.stream.on('close', onHttp2Close); this.stream.on('error', (error) => { - this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Stream-Error: ' + error); + this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Stream-Error: ${error}`); this.emit('error', error); this.stream && this.stream.end(); this.client && this.client.close(); @@ -224,14 +233,14 @@ class AlexaHttp2Push extends EventEmitter { this.client.on('close', onHttp2Close); this.client.on('error', (error) => { - this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Client-Error: ' + error); + this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Client-Error: ${error}`); this.emit('error', error); this.stream && this.stream.end(); this.client && this.client.close(); }); } catch (err) { - this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Error on Init ' + err.message); + this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Error on Init ${err.message}`); this._options.logger && this._options.logger(err.stack); this.emit('error', err); return; @@ -244,9 +253,9 @@ class AlexaHttp2Push extends EventEmitter { this.stream && this.stream.end(); this.client && this.client.close(); } catch (err) { - //just make sure + // just make sure } - if (this.stream || !this.reconnectTimeout) { // seems no close was emitted so far?! + if (this.stream || !this.reconnectTimeout) { // it seems no close was emitted so far?! onHttp2Close(); } }, 30000); @@ -267,15 +276,16 @@ class AlexaHttp2Push extends EventEmitter { this.initTimeout = null; } this.stop = true; - if (!this.client && !this.stream) return; + if (!this.client && !this.stream) { + return; + } try { this.stream && this.stream.end(); this.client && this.client.close(); } catch (e) { - this.connectionActive && this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Disconnect error: ' + e.message); + this.connectionActive && this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Disconnect error: ${e.message}`); } } } - module.exports = AlexaHttp2Push; diff --git a/package.json b/package.json index 8e35eec..630ee11 100644 --- a/package.json +++ b/package.json @@ -32,14 +32,14 @@ "alexa-cookie2": "^5.0.2", "https": "^1.0.0", "querystring": "^0.2.1", - "ws": "^8.14.2", + "ws": "^8.15.1", "extend": "^3.0.2", "uuid": "^9.0.1" }, "devDependencies": { - "@alcalzone/release-script": "^3.6.0", - "@alcalzone/release-script-plugin-license": "^3.5.9", - "eslint": "^8.54.0" + "@alcalzone/release-script": "^3.7.0", + "@alcalzone/release-script-plugin-license": "^3.7.0", + "eslint": "^8.56.0" }, "scripts": { "test": "node node_modules/mocha/bin/mocha",