-
Notifications
You must be signed in to change notification settings - Fork 975
/
Copy pathi.js
936 lines (860 loc) · 34 KB
/
i.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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
const { ConnectionError, PushError, SendError, ERROR, Creds, Template } = require('../data'),
{ Base, util } = require('../std'),
FORGE = require('node-forge'),
logger = require('../../../../../api/utils/log'),
jwt = require('jsonwebtoken'),
{ threadId } = require('worker_threads'),
{ proxyAgent } = require('../../proxy'),
HTTP2 = require('http2');
/**
* Platform key
*/
const key = 'i';
/**
* Extract token & field from token_session request
*
* @param {object} qstring request params
* @returns {string[]|undefined} array of [platform, field, token] if qstring has platform-specific token data, undefined otherwise
*/
function extractor(qstring) {
if (qstring.ios_token !== undefined && qstring.test_mode in FIELDS) {
return [key, FIELDS[qstring.test_mode], qstring.ios_token, util.hash(qstring.ios_token)];
}
}
/**
* Make an estimated guess about request platform
*
* @param {string} userAgent user-agent header
* @returns {string} platform key if it looks like request made by this platform
*/
function guess(userAgent) {
return userAgent.includes('iOS') && key;
}
/**
* Token types for APN
* A number comes from SDK, we need to map it into smth like tkip/tkid/tkia
*/
const FIELDS = {
'0': 'p', // prod
'1': 'd', // debug
'2': 'a', // ad hoc
};
/**
* Token types for APN
* A number comes from SDK, we need to map it into smth like tkip/tkid/tkia
*/
const FIELDS_TITLES = {
'0': 'APN Production Token', // prod
'1': 'APN Development Token', // debug
'2': 'APN AdHoc / TestFlight Token', // ad hoc
};
/**
* APN development token reuqires different host
*/
const FIELD_DEV = 'd';
/**
* Credential types for APN
*/
const CREDS = {
'apn_universal': class CertCreds extends Creds {
/**
* Validation scheme of this class
*
* @returns {object} validateArgs scheme
*/
static get scheme() {
return Object.assign(super.scheme, {
cert: { required: true, type: 'String' },
fileType: { required: false, type: 'String' }, // multipart upload case only; when fileType is present, cert is supposed to be a file contents
secret: { required: false, type: 'String', 'min-length': 1},
title: { required: false, type: 'String' },
notBefore: { required: false, type: 'Date' },
notAfter: { required: false, type: 'Date' },
topics: { required: false, type: 'String[]' },
bundle: { required: false, type: 'String' },
hash: { required: false, type: 'String' },
});
}
/**
* Get TLS options
*
* @returns {object} with cert & key keys set to PEM-encoded certificate & private key
*/
get tls() {
let buffer = FORGE.util.decode64(this._data.cert),
asn1 = FORGE.asn1.fromDer(buffer),
p12 = FORGE.pkcs12.pkcs12FromAsn1(asn1, false, this._data.secret || null),
cert = p12.getBags({bagType: FORGE.pki.oids.certBag})[FORGE.pki.oids.certBag][0],
pk = p12.getBags({bagType: FORGE.pki.oids.pkcs8ShroudedKeyBag})[FORGE.pki.oids.pkcs8ShroudedKeyBag][0];
if (!cert || !pk || !cert.cert || !pk.key) {
throw new PushError('Failed to get tls opts from creds');
}
return {
cert: FORGE.pki.certificateToPem(cert.cert),
key: FORGE.pki.privateKeyToPem(pk.key)
};
}
/**
* Check credentials for correctness, throw PushError otherwise
*
* @throws PushError in case the check fails
* @returns {undefined}
*/
validate() {
if (this._data.fileType) {
let mime = this._data.cert.indexOf(';base64,') === -1 ? null : this._data.cert.substring(0, this._data.cert.indexOf(';base64,'));
if (mime === 'data:application/x-pkcs12' || mime === 'data:application/pkcs12' || (mime === 'data:application/octet-stream' && this._data.fileType === 'p12')) {
this._data.cert = this._data.cert.substring(this._data.cert.indexOf(',') + 1);
delete this._data.fileType;
}
else {
return ['APN certificate file must be in P12 format with private key inside'];
}
}
let res = super.validate();
if (res) {
return res;
}
try {
var buffer = FORGE.util.decode64(this._data.cert),
asn1 = FORGE.asn1.fromDer(buffer),
p12 = FORGE.pkcs12.pkcs12FromAsn1(asn1, false, this._data.secret || null),
topics = [];
p12.safeContents.forEach(safeContents => {
safeContents.safeBags.forEach(safeBag => {
if (safeBag.cert) {
var title = safeBag.cert.subject.getField({type: '2.5.4.3'});
if (title) {
this._data.title = title.value;
}
if (safeBag.cert.validity) {
if (safeBag.cert.validity.notBefore) {
this._data.notBefore = safeBag.cert.validity.notBefore;
}
if (safeBag.cert.validity.notAfter) {
this._data.notAfter = safeBag.cert.validity.notAfter;
}
}
// if (safeBag.cert.getExtension({id: '1.2.840.113635.100.6.3.1'})) {
// dev = true;
// }
// if (safeBag.cert.getExtension({id: '1.2.840.113635.100.6.3.2'})) {
// prod = true;
// }
var tpks = safeBag.cert.getExtension({id: '1.2.840.113635.100.6.3.6'});
if (tpks) {
tpks = tpks.value.replace(/0[\x00-\x1f\(\)!]/gi, '') //eslint-disable-line no-useless-escape
.replace('\f\f', '\f')
.split('\f')
.map(s => s.replace(/[^A-Za-z\-\.]/gi, '').trim()); //eslint-disable-line no-useless-escape
tpks.shift();
for (var i = 0; i < tpks.length; i++) {
for (var j = 0; j < tpks.length; j++) {
if (i !== j && tpks[j].indexOf(tpks[i]) === 0) {
if (topics.indexOf(tpks[i]) === -1 && tpks[i] && tpks[i].indexOf('.') !== -1 && tpks[i].indexOf(".") !== 0) {
topics.push(tpks[i]);
}
if (topics.indexOf(tpks[j]) === -1 && tpks[j] && tpks[j].indexOf('.') !== -1 && tpks[j].indexOf(".") !== 0) {
topics.push(tpks[j]);
}
}
}
}
}
}
});
});
if (topics.length === 0) {
return ['Not a universal (Sandbox & Production) certificate'];
}
if (!this._data.notBefore || !this._data.notAfter) {
return ['No validity dates in the certificate'];
}
if (this._data.notBefore.getTime() > Date.now()) {
return ['Certificate is not valid yet'];
}
if (this._data.notAfter.getTime() < Date.now()) {
return ['Certificate is expired'];
}
topics.sort((a, b) => a.length - b.length);
this._data.bundle = topics.length > 0 ? topics[0] : this._data.title.split(' ').pop();
this._data.topics = topics;
this._data.hash = FORGE.md.sha256.create().update(this._data.cert).digest().toHex();
logger('push:credentials:i').d('checked credentials %s topics %j, bundle %j', this.id, this._data.topics, this._data.bundle);
}
catch (e) {
if (e.message && e.message.indexOf('Invalid password') !== -1) {
return ['Invalid certificate passphrase'];
}
else {
logger('push:credentials:i').w('error while parsing certificate', e);
return ['Failed to parse certificate'];
}
}
}
/**
* Hash string of the credentials used to compare them
*/
get hash() {
return this._data.hash;
}
/**
* "View" json, that is some truncated/simplified version of credentials that is "ok" to display
*
* @returns {object} json without sensitive information
*/
get view() {
let json = {
_id: this._id,
type: this._data.type,
cert: 'APN Sandbox & Production Certificate (P12)',
bundle: this._data.bundle,
topics: this._data.topics,
notBefore: this._data.notBefore,
notAfter: this._data.notAfter,
hash: this._data.hash,
};
if (this._data.secret) {
json.secret = new Array(this._data.secret.length).fill('*').join('');
}
return json;
}
},
'apn_token': class TokenCreds extends Creds {
/**
* Validation scheme of this class
*
* @returns {object} validateArgs scheme
*/
static get scheme() {
return Object.assign(super.scheme, {
key: { required: true, type: 'String' },
keyid: { required: true, type: 'String' },
bundle: { required: true, type: 'String' },
team: { required: true, type: 'String' },
fileType: { required: false, type: 'String' },
hash: { required: false, type: 'String' },
});
}
/**
* Check credentials for correctness, throw PushError otherwise
*
* @throws PushError in case the check fails
* @returns {undefined}
*/
validate() {
if (this._data.fileType) {
let mime = this._data.key.indexOf(';base64,') === -1 ? null : this._data.key.substring(0, this._data.key.indexOf(';base64,'));
if (mime === 'data:application/x-pkcs8' || mime === 'data:application/pkcs8' || mime === 'data:' || (mime === 'data:application/octet-stream' && this._data.fileType === 'p8')) {
this._data.key = this._data.key.substring(this._data.key.indexOf(',') + 1);
delete this._data.fileType;
}
else {
return ['APN key file must be in P8 format'];
}
}
let res = super.validate();
if (res) {
return res;
}
let k = FORGE.util.decode64(this._data.key);
if (!k) {
return ['Not a base64-encoded string'];
}
if (k.indexOf('-----BEGIN PRIVATE KEY-----') === -1 || k.indexOf('-----END PRIVATE KEY-----') === -1) {
return ['Not a private key in P8 format in base64-encoded string'];
}
const message = this._data.key + this._data.bundle + this._data.keyid + this._data.team;
this._data.hash = FORGE.md.sha256.create().update(message).digest().toHex();
}
/**
* "View" json, that is some truncated/simplified version of credentials that is "ok" to display
*
* @returns {object} json without sensitive information
*/
get view() {
let json = {
_id: this._id,
type: this._data.type,
key: 'APN Key File (P8)',
bundle: this._data.bundle,
keyid: this._data.keyid,
team: this._data.team,
hash: this._data.hash,
};
return json;
}
/**
* Generate new JWT token
*/
get bearer() {
if (!this._bearer) {
let token = jwt.sign({
iss: this._data.team,
iat: Math.floor(Date.now() / 1000)
}, FORGE.util.decode64(this._data.key), {
algorithm: 'ES256',
header: {
alg: 'ES256',
kid: this._data.keyid
}
});
this._bearer = `bearer ${token}`;
}
return this._bearer;
}
}
};
/**
* Create new empty payload for the note object given
*
* @param {Note} note Note object
* @returns {object} empty payload object
*/
function empty(note) {
return {aps: {}, c: {i: note.id}};
}
/**
* Non-personalizable fields of Note which can be sent for iOS
* !NOTE! order matters!
*/
const fields = [
'sound',
'badge',
'contentAvailable',
'url',
'media',
];
/**
* Mapping of Content properties to APN payload props
*/
const map = {
/**
* Sends sound
* @param {Template} t template
* @param {string} sound sound string
*/
sound: function(t, sound) {
if (sound) {
t.result.aps.sound = sound;
}
},
/**
* Sends badge
* @param {Template} t template
* @param {number} badge badge (0..N)
*/
badge: function(t, badge) {
t.result.aps.badge = badge;
},
/**
* Sends buttons
* !NOTE! buttons & messagePerLocale are inter-dependent as buttons urls/titles are locale-specific
*
* @param {Template} t template
* @param {number} buttons buttons (1..2)
*/
buttons: function(t, buttons) {
if (buttons) {
t.result.c.b = buttons.map(b => ({t: b.title, l: b.url}));
t.result.aps['mutable-content'] = 1;
}
},
/**
* Set title string
*
* @param {Template} t template
* @param {String} title title string
*/
title: function(t, title) {
if (!t.result.aps.alert) {
t.result.aps.alert = {};
}
t.result.aps.alert.title = title;
},
/**
* Set message string
*
* @param {Template} t template
* @param {String} message message string
*/
message: function(t, message) {
if (!t.result.aps.alert) {
t.result.aps.alert = {};
}
t.result.aps.alert.body = message;
},
/**
* Send content-available. It's set automatically when there's no alert and no sound.
*
* @param {Template} template template
* @param {boolean} ca contentAvailable of the Note
*/
contentAvailable: function(template, ca) {
if (ca || (!template.result.aps.alert && !template.result.aps.sound)) {
template.result.aps['content-available'] = 1;
}
},
/**
* Send notification-tap url
*
* @param {Template} template template
* @param {string} url on-tap url
*/
url: function(template, url) {
template.result.c.l = url;
},
/**
* Send media (picture, video, gif, etc) along with the message.
* Sets mutable-content in order for iOS extension to be run.
*
* @param {Template} template template
* @param {string} media attached media url
*/
media: function(template, media) {
template.result.c.a = media;
template.result.aps['mutable-content'] = 1;
},
/**
* Sends custom data along with the message
*
* @param {Template} template template
* @param {Object} data data to be sent
*/
data: function(template, data) {
Object.assign(template.result, data);
},
/**
* Sends user props along with the message
*
* @param {Template} template template
* @param {[string]} extras extra user props to be sent
* @param {Object} data personalization
*/
extras: function(template, extras, data) {
let e = {},
any = false;
for (let i = 0; i < extras.length; i++) {
let k = extras[i];
if (data[k] !== null && data[k] !== undefined) {
e[k] = data[k];
any = true;
}
}
if (any) {
template.result.c.e = e;
}
},
/**
* Sends platform specific fields
*
* @param {Template} template template
* @param {object} specific platform specific props to be sent
*/
specific: function(template, specific) {
if (specific) {
if (specific.subtitle) {
template.result.aps.alert = template.result.aps.alert || {};
template.result.aps.alert.subtitle = specific.subtitle;
}
if (specific.setContentAvailable) {
template.result.aps["content-available"] = 1;
}
}
},
};
/**
* Connection implementation for APN
*/
class APN extends Base {
/**
* Standard constructor
* @param {string} log logger name
* @param {string} type type of connection: ap, at, id, ia, ip, ht, hp
* @param {Creds} creds credentials instance
* @param {Object[]} messages initial array of messages to send
* @param {Object} options standard stream options
* @param {number} options.pool.pushes number of notifications which can be processed concurrently
*/
constructor(log, type, creds, messages, options) {
super(log, type, creds, messages, options);
this.log = logger(log).sub(`${threadId}-i`);
if (this._options.proxy) {
this._options.proxy.http2 = true;
}
this.templates = {};
this.results = [];
this.host = this.type === 'id' ? 'api.development.push.apple.com' : 'api.push.apple.com';
this.port = 2197;
let authority = `${this.host}:${this.port}`;
this.authority = `https://${authority}`;
this.headersFirst = {
':path': '/3/device/' + Math.random(),
':method': 'POST',
':scheme': 'https',
':authority': authority,
[HTTP2.sensitiveHeaders]: ['authorization', ':path']
},
this.headersSecond = {
':method': 'POST',
':scheme': 'https',
':authority': authority,
// this is being added before the actual request depending on weather message have setContentAvailable
// "apns-priority": 5,
[HTTP2.sensitiveHeaders]: ['authorization', ':path', 'apns-id', 'apns-expiration', 'apns-collapse-id']
};
this.headersSecondWithToken = token => {
this.headersSecond[':path'] = '/3/device/' + token;
return this.headersSecond;
};
this.sessionOptions = {};
if (this.creds instanceof CREDS.apn_universal) {
Object.assign(this.sessionOptions, this.creds.tls);
}
if (this.creds instanceof CREDS.apn_token) {
this.headersFirst.authorization = this.headersSecond.authorization = this.creds.bearer;
}
this.headersFirst['apns-topic'] = this.headersSecond['apns-topic'] = this.creds._data.bundle;
}
/**
* Get template by message id, create if none exists
*
* @param {string} id message id
* @param {map} map platform mapping object
* @returns {Template} template instance
*/
template(id) {
let { PLATFORM } = require('../index');
return this.templates[id] || (this.templates[id] = new Template(this.messages[id], PLATFORM[this.type.substr(0, 1)], true));
}
/**
* Send push notifications
*
* @param {Object[]} pushesData pushes to send
* @param {integer} length number of bytes in data
* @returns {Promise} sending promise
*/
send(pushesData, length) {
if (!this.session) {
return this.connect().then(ok => {
if (ok) {
return this.send(pushesData, length);
}
else {
return ok;
}
});
}
return this.with_retries(pushesData, length, (pushes, bytes, attempt) => new Promise((resolve, reject) => {
this.log.d('%d-th attempt for %d bytes', attempt, bytes);
let self = this,
nonRecoverableError,
recoverableErrors = 0,
oks = [],
errors = {},
one = Math.ceil(bytes / pushes.length),
/**
* Get an error for given code & message, create it if it doesn't exist yet
*
* @param {number} code error code
* @param {string} message error message
* @returns {SendError} error instance
*/
error = (code, message) => {
recoverableErrors++;
let err = code + message;
if (!(err in errors)) {
errors[err] = new SendError(message, code);
}
return errors[err];
},
/**
* Called on stream completion, returns results for this batch
*/
streamDone = () => {
if (oks.length + recoverableErrors + (nonRecoverableError && nonRecoverableError.left.length || 0) === pushes.length) {
let errored = nonRecoverableError && nonRecoverableError.bytes || 0;
if (oks.length) {
this.send_results(oks, bytes - errored);
}
for (let k in errors) {
errored += errors[k].affectedBytes;
this.send_push_error(errors[k]);
}
if (nonRecoverableError) {
reject(nonRecoverableError);
}
else {
resolve();
}
}
};
this.log.d('sending %d streams', pushes.length);
pushes.forEach((p, i) => {
// this.log.d('%d: sending', i);
if (i % 200 === 0) {
this.log.d('state', this.session.state);
}
if (nonRecoverableError) {
nonRecoverableError.addLeft(p._id, one);
streamDone();
return;
}
if (!this.messages[p.m]) {
this.log.e('No message %s', p.m);
}
let content = this.template(p.m).compile(p),
reqHeaders = self.headersSecondWithToken(p.t);
// find if we need to add the priority header (check if content-available set)
delete reqHeaders["apns-priority"];
const message = self.messages[p.m];
if (message && Array.isArray(message.contents)) {
const contentItem = message.contents.find(cont => Array.isArray(cont.specific));
if (contentItem) {
const obj = contentItem.specific.find(cont => cont.setContentAvailable !== undefined);
if (obj && obj.setContentAvailable) {
reqHeaders["apns-priority"] = 5;
}
}
}
// =======0========000=================0========000=================0========0
let stream = this.session.request(reqHeaders),
status,
data = '';
stream.on('error', err => {
if (!nonRecoverableError) {
nonRecoverableError = new ConnectionError(`APN Stream Error: ${err.message}`, ERROR.CONNECTION_PROVIDER).addAffected(p._id, one);
}
else {
nonRecoverableError.addAffected(p._id, one);
}
});
stream.on('frameError', (type, code, id) => {
this.log.e('stream frameError %d, %d, %d', type, code, id);
});
stream.on('timeout', () => {
this.log.e('stream timeout');
if (!nonRecoverableError) {
nonRecoverableError = new ConnectionError(`APN Stream Error: timeout`, ERROR.CONNECTION_PROVIDER).addAffected(p._id, one);
}
else {
nonRecoverableError.addAffected(p._id, one);
}
streamDone();
});
stream.on('response', function(headers) {
status = headers[':status'];
// self.log.d('%d: status %d: %j', i, status, self.session.state);
if (status === 200) {
const apnsUniqueId = headers["apns-id"] ?? headers["apns-unique-id"];
oks.push({ p: p._id, r: apnsUniqueId });
stream.destroy();
streamDone();
}
else if (status === 410) {
stream.destroy();
error(ERROR.DATA_TOKEN_EXPIRED, 'ExpiredToken').addAffected(p._id, one);
streamDone();
}
else if (status === 500 || status === 503 || status === 404 || status === 405 || status === 413) {
stream.destroy();
this.session.destroy();
if (!nonRecoverableError) {
nonRecoverableError = new ConnectionError(`APN Server Error: ${status}`, ERROR.CONNECTION_PROVIDER).addAffected(p._id, one);
}
else {
nonRecoverableError.addAffected(p._id, one);
}
}
else if (status === 400 || status === 403 || status === 429) {
// handle in on('end') because we need response error code
}
});
stream.on('data', dt => {
data += dt;
});
stream.on('end', () => {
if (status === 400 || status === 403 || status === 429) {
try {
let json = JSON.parse(data);
if (status === 400) {
if (json.reason) {
if (json.reason === 'DeviceTokenNotForTopic' || json.reason === 'BadDeviceToken') {
error(ERROR.DATA_TOKEN_INVALID, json.reason).addAffected(p._id, one);
}
else {
error(ERROR.DATA_COUNTLY, json.reason).addAffected(p._id, one);
}
}
else {
self.log.e('provider returned %d: %j', status, json);
error(ERROR.DATA_PROVIDER, data).addAffected(p._id, one);
}
}
else if (status === 403) {
if (!nonRecoverableError) {
nonRecoverableError = new ConnectionError(`APN Unauthorized: ${status} (${json.reason})`, ERROR.INVALID_CREDENTIALS).addAffected(p._id, one);
}
else {
nonRecoverableError.addAffected(p._id, one);
}
}
else if (status === 429) {
self.log.e('provider returned %d: %j', status, json);
error(ERROR.DATA_PROVIDER, data).addAffected(p._id, one);
}
else {
throw new PushError('IMPOSSIBRU');
}
}
catch (e) {
self.log.e('provider returned %d: %s', status, data, e);
error(ERROR.DATA_PROVIDER, data).addAffected(p._id, one);
}
streamDone();
}
});
stream.setEncoding('utf-8');
stream.setTimeout(10000, () => {
self.log.w('%d: cancelling stream %d for push %s / %s', i, stream.id, p._id, p.t);
stream.close(HTTP2.constants.NGHTTP2_CANCEL);
});
stream.end(content);
});
}));
}
/**
* Connect
*
* @param {Object[]|undefined} messages messages array
*/
async connect(messages) {
if (messages) {
if (!this.session) {
let ok = await this.connect();
if (!ok) {
return ok;
}
}
if (!this.session) {
throw new Error('Failed to connect');
}
messages.forEach(m => this.message(m._id, m));
return true;
}
else {
return new Promise((resolve, reject) => {
this.log.i('connecting to %s', this.authority);
this.ensureProxy(this.host, this.port, error => {
if (error) {
this.log.e('Proxy connection error', error);
return reject(new ConnectionError('NoProxyConnection', ERROR.CONNECTION_PROXY));
}
let session = HTTP2.connect(this.authority, this.sessionOptions);
session.on('error', err => {
this.log.e('session error', err);
reject(new ConnectionError(err.message, ERROR.CONNECTION_PROVIDER));
session.destroy();
});
session.on('connect', () => {
this.log.d('connected to %s [%s]', this.authority, session.socket.remoteAddress);
let stream = session.request(this.headersFirst);
stream.on('error', err => {
this.log.e('first request error', err);
reject(new ConnectionError(err.message, ERROR.CONNECTION_PROVIDER));
session.destroy();
});
stream.on('response', headers => {
let status = headers[':status'];
this.log.d('provider returned %d', status);
if (status === 403 || status === 400) {
if (status === 400) {
this.session = session;
}
else {
session.destroy();
}
resolve(status === 400);
}
else {
reject(new ConnectionError(`APN returned status ${status}`, ERROR.CONNECTION_PROVIDER));
}
stream.destroy();
});
stream.setEncoding('utf8');
stream.end(JSON.stringify({
aps: {
alert: 'xxTESTxx'
}
}));
});
});
});
}
}
/**
* Ensure connection to proxy server
*
* @param {string} host APN hostname
* @param {int} port APN port
* @param {function} callback callback to call in the end, first param is error
*/
ensureProxy(host, port, callback) {
if (!this._options.proxy || !this._options.proxy.host || !this._options.proxy.port) {
callback();
return;
}
if (!this.agent) {
let ProxyAgent = proxyAgent('https://example.com', this._options.proxy);
this.agent = new ProxyAgent();
}
this.agent.createConnection({host, port, protocol: 'https:'}, (err, socket) => {
if (err) {
callback(err);
}
else {
this.sessionOptions.createConnection = () => socket;
callback();
}
});
}
/**
* Gracefully shutdown the worker
*
* @param {Error} error optional error
* @param {function} callback callback to call once done destroying
*/
_destroy(error, callback) {
if (error) {
this.log.e('destroy', error);
}
else {
this.log.d('destroy');
}
if (this.session) {
this.session.destroy();
this.session = undefined;
}
if (this.agent) {
this.agent.destroy();
this.agent = undefined;
}
if (callback) {
callback(error);
}
}
}
module.exports = {
key,
title: 'iOS',
extractor,
guess,
FIELDS,
FIELDS_TITLES,
FIELD_DEV,
CREDS,
empty,
fields,
map,
connection: APN
};