forked from soef/alexa-remote
-
Notifications
You must be signed in to change notification settings - Fork 25
/
alexa-http2push.js
executable file
·299 lines (272 loc) · 13.5 KB
/
alexa-http2push.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
const http2 = require('http2');
const EventEmitter = require('events');
class AlexaHttp2Push extends EventEmitter {
constructor(options, update_access_token) {
super();
this._options = options;
this.stop = false;
this.client = null;
this.stream = null;
this.pingPongInterval = null;
this.errorRetryCounter = 0;
this.reconnectTimeout = null;
this.pongTimeout = null;
this.initTimeout = null;
this.connectionActive = false;
this.access_token = null;
this.update_access_token = update_access_token;
this.inClosing = false;
}
isConnected() {
return this.connectionActive;
}
connect() {
this.inClosing = false;
this.update_access_token(token => {
this.access_token = token;
let host = 'bob-dispatch-prod-eu.amazon.com';
if (this._options.pushDispatchHost) {
host = this._options.pushDispatchHost;
} else if (this._options.amazonPage === 'amazon.com') {
host = 'bob-dispatch-prod-na.amazon.com';
} else if (this._options.amazonPage === 'amazon.ca') {
host = 'bob-dispatch-prod-na.amazon.com';
} else if (this._options.amazonPage === 'amazon.com.mx') {
host = 'bob-dispatch-prod-na.amazon.com';
} else if (this._options.amazonPage === 'amazon.com.br') {
host = 'bob-dispatch-prod-na.amazon.com';
} else if (this._options.amazonPage === 'amazon.co.jp') {
host = 'bob-dispatch-prod-fe.amazon.com';
} else if (this._options.amazonPage === 'amazon.com.au') {
host = 'bob-dispatch-prod-fe.amazon.com';
} else if (this._options.amazonPage === 'amazon.com.in') {
host = 'bob-dispatch-prod-fe.amazon.com';
} else if (this._options.amazonPage === 'amazon.co.nz') {
host = 'bob-dispatch-prod-fe.amazon.com';
}
this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Use host ${host}`);
const http2Options = {
':method': 'GET',
':path': '/v20160207/directives',
':authority': host,
':scheme': 'https',
'authorization': `Bearer ${this.access_token}`,
'accept-encoding': 'gzip',
'user-agent': 'okhttp/4.3.2-SNAPSHOT',
};
const onHttp2Close = (code, reason, immediateReconnect) => {
if (this.inClosing) return;
this.inClosing = true;
if (reason) {
reason = reason.toString();
}
try {
this.stream && this.stream.destroy();
} catch (err) {
// ignore
}
try {
this.client && this.client.destroy();
} catch (err) {
// ignore
}
this.client = null;
this.stream = null;
this.connectionActive = false;
this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Close: ${code}: ${reason}`);
if (this.initTimeout) {
clearTimeout(this.initTimeout);
this.initTimeout = null;
}
if (this.pingPongInterval) {
clearInterval(this.pingPongInterval);
this.pingPongInterval = null;
}
if (this.pongTimeout) {
clearTimeout(this.pongTimeout);
this.pongTimeout = null;
}
if (this.stop) {
return;
}
if (this.errorRetryCounter > 100) {
this.emit('disconnect', false, 'Too many failed retries. Check cookie and data');
return;
}
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`);
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;
this.connect();
}, retryDelay * 1000);
};
const onPingResponse = (resetErrorCount) => {
if (this.initTimeout) {
clearTimeout(this.initTimeout);
this.initTimeout = null;
this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Initialization completed');
this.emit('connect');
}
if (this.pongTimeout) {
clearTimeout(this.pongTimeout);
this.pongTimeout = null;
}
this.connectionActive = true;
if (resetErrorCount) {
this.errorRetryCounter = 0;
}
};
try {
this.client = http2.connect(`https://${http2Options[':authority']}`, () => {
if (!this.client) {
return;
}
try {
this.stream = this.client.request(http2Options);
} catch (error) {
this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Error on Request ${error.message}`);
this.emit('error', error);
return;
}
this.stream.on('response', async (headers) => {
if (headers[':status'] === 403) {
this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Error 403 .... refresh token');
this.update_access_token(token => {
if (token) {
this.access_token = token;
}
onHttp2Close(headers[':status'], undefined, this.errorRetryCounter < 3);
});
} else if (headers[':status'] !== 200) {
onHttp2Close(headers[':status']);
}
});
this.stream.on('data', (chunk) => {
if (this.stop) {
this.stream && this.stream.end();
this.client && this.client.close();
return;
}
chunk = chunk.toString();
if (chunk.startsWith('------')) {
this.client.ping(() => onPingResponse(false));
this.pingPongInterval = setInterval(() => {
if (!this.stream || !this.client) {
return;
}
this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Send Ping');
// 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.pongTimeout = setTimeout(() => {
this.pongTimeout = null;
this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: No Pong received after 30s');
this.stream && this.stream.end();
this.client && this.client.close();
this.connectionActive = false;
}, 30000);
}, 180000);
return;
}
if (chunk.startsWith('Content-Type: application/json')) {
const json_start = chunk.indexOf('{');
const json_end = chunk.lastIndexOf('}');
if (json_start === -1 || json_end === -1) {
this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Unexpected ResponseCould not find json in chunk: ${chunk}`);
return;
}
const message = chunk.substring(json_start, json_end + 1);
try {
const data = JSON.parse(message);
if (!data || !data.directive || !data.directive.payload || !Array.isArray(data.directive.payload.renderingUpdates)) {
this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Unexpected ResponseCould not find renderingUpdates in json: ${message}`);
return;
}
data.directive.payload.renderingUpdates.forEach(update => {
if (!update || !update.resourceMetadata) {
this._options.logger && this._options.logger(`Alexa-Remote HTTP2-PUSH: Unexpected ResponseCould not find resourceMetadata in renderingUpdates: ${message}`);
}
const dataContent = JSON.parse(update.resourceMetadata);
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.emit('command', command, payload);
});
} catch (err) {
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.emit('error', error);
this.stream && this.stream.end();
this.client && this.client.close();
});
});
this.client.on('close', onHttp2Close);
this.client.on('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(err.stack);
this.emit('error', err);
return;
}
this.initTimeout && clearTimeout(this.initTimeout);
this.initTimeout = setTimeout(() => {
this._options.logger && this._options.logger('Alexa-Remote HTTP2-PUSH: Initialization not done within 30s');
try {
this.stream && this.stream.end();
this.client && this.client.close();
} catch (err) {
// just make sure
}
if (this.stream || !this.reconnectTimeout) { // it seems no close was emitted so far?!
onHttp2Close();
}
}, 30000);
});
}
disconnect() {
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
}
if (this.pollingTimeout) {
clearTimeout(this.pollingTimeout);
this.pollingTimeout = null;
}
if (this.initTimeout) {
clearTimeout(this.initTimeout);
this.initTimeout = null;
}
this.stop = true;
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}`);
}
}
}
module.exports = AlexaHttp2Push;