Skip to content

Commit

Permalink
Merge pull request #226 from GermanBluefox/master
Browse files Browse the repository at this point in the history
Hide "undefined" in the logging
  • Loading branch information
Apollon77 authored Dec 25, 2023
2 parents d03f25c + bfce41b commit f61ee47
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 196 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
62 changes: 36 additions & 26 deletions alexa-http2push.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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']);
}
});
Expand All @@ -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(() => {
Expand Down Expand Up @@ -202,19 +211,19 @@ 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}`);
}
}
});

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();
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Loading

0 comments on commit f61ee47

Please sign in to comment.