forked from nativescript-community/https
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps.android.ts
More file actions
631 lines (593 loc) · 22.2 KB
/
https.android.ts
File metadata and controls
631 lines (593 loc) · 22.2 KB
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
import { isDefined } from "@nativescript/core/utils/types";
import { HttpResponseEncoding } from "@nativescript/core/http";
import { ImageSource } from "@nativescript/core/image-source";
import * as Https from "./https.common";
import { File } from "@nativescript/core/file-system";
interface Ipeer {
enabled: boolean;
allowInvalidCertificates: boolean;
validatesDomainName: boolean;
host?: string;
commonName?: string;
certificate?: string;
x509Certificate?: java.security.cert.Certificate;
}
let peer: Ipeer = {
enabled: false,
allowInvalidCertificates: false,
validatesDomainName: true,
};
let cache: okhttp3.Cache;
export function setCache(options?: Https.CacheOptions) {
if (options) {
cache = new okhttp3.Cache(
new java.io.File(options.diskLocation),
options.diskSize
);
} else {
cache = null;
}
if (Client) {
getClient(true);
}
}
export function clearCache() {
if (cache) {
cache.evictAll();
}
}
let _timeout = 10;
class HttpsResponse implements Https.HttpsResponseLegacy {
private callback?: com.nativescript.https.OkHttpResponse.OkHttpResponseAsyncCallback;
constructor(
private response: com.nativescript.https.OkHttpResponse,
private tag: string,
private url: string
) {}
getOrCreateCloseCallback() {
if (!notClosedResponses[this.tag]) {
// we need to store handling request to be able to cancel them
notClosedResponses[this.tag] = this.response;
this.response.closeCallback = new OkHttpResponse.OkHttpResponseCloseCallback(
{
onClose() {
delete notClosedResponses[this.tag];
},
}
);
}
}
getCallback(resolve, reject) {
return new com.nativescript.https.OkHttpResponse.OkHttpResponseAsyncCallback(
{
onBitmap(res) {
resolve(new ImageSource(res));
},
onString(res) {
resolve(res);
},
onByteArray(res) {
resolve((ArrayBuffer as any).from(res));
},
onFile(res) {
resolve(res);
},
onException(err) {
reject(err);
},
}
);
}
// cache it because asking it again wont work as the socket is closed
arrayBuffer: ArrayBuffer;
toArrayBuffer() {
try {
this.arrayBuffer =
this.arrayBuffer ||
(ArrayBuffer as any).from(this.response.toByteArray());
return this.arrayBuffer;
} catch {
return null;
}
}
toArrayBufferAsync(): Promise<ArrayBuffer> {
if (this.arrayBuffer) {
return Promise.resolve(this.arrayBuffer);
}
return new Promise((resolve, reject) => {
this.getOrCreateCloseCallback();
this.response.toByteArrayAsync(this.getCallback(resolve, reject));
}).then((r: ArrayBuffer) => {
this.arrayBuffer = r;
return this.arrayBuffer;
});
}
// cache it because asking it again wont work as the socket is closed
stringResponse: string;
toString() {
try {
// TODO: handle arraybuffer already stored
this.stringResponse =
this.stringResponse || this.response.asString();
return this.stringResponse;
} catch {
return null;
}
}
async toStringAsync(): Promise<string> {
if (this.stringResponse) {
return this.stringResponse;
}
// TODO: handle arraybuffer already stored
this.stringResponse = await new Promise<string>((resolve, reject) => {
this.getOrCreateCloseCallback();
this.response.asStringAsync(this.getCallback(resolve, reject));
});
return this.stringResponse;
}
// cache it because asking it again wont work as the socket is closed
jsonResponse: any;
toJSON(encoding?: HttpResponseEncoding) {
try {
if (this.jsonResponse) {
return this.jsonResponse;
}
// TODO: handle arraybuffer already stored
this.stringResponse =
this.stringResponse || this.response.asString();
this.jsonResponse = Https.parseJSON(this.stringResponse);
return this.jsonResponse;
} catch (err) {
console.error("HttpsResponse.toJSON", err);
return null;
}
}
async toJSONAsync() {
if (this.jsonResponse) {
return this.jsonResponse;
}
if (this.stringResponse) {
this.jsonResponse = Https.parseJSON(this.stringResponse);
return this.jsonResponse;
}
// TODO: handle arraybuffer already stored
const r = await this.toStringAsync();
this.jsonResponse = Https.parseJSON(r);
return this.jsonResponse;
}
// cache it because asking it again wont work as the socket is closed
imageSource: ImageSource;
async toImage(): Promise<ImageSource> {
if (this.imageSource) {
return this.imageSource;
}
return new Promise<ImageSource>((resolve, reject) => {
this.getOrCreateCloseCallback();
this.response
.toBitmapAsync(this.getCallback(resolve, reject))
.then((r) => {
this.imageSource = r;
return r;
});
});
}
// toFile(destinationFilePath: string): File {
// if (!destinationFilePath) {
// destinationFilePath = Https.getFilenameFromUrl(this.url);
// }
// const file = this.response.toFile(destinationFilePath);
// return File.fromPath(destinationFilePath);
// }
// cache it because asking it again wont work as the socket is closed
file: File;
toFile(destinationFilePath: string): Promise<File> {
if (this.file) {
return Promise.resolve(this.file);
}
if (!destinationFilePath) {
destinationFilePath = Https.getFilenameFromUrl(this.url);
}
return new Promise((resolve, reject) => {
this.getOrCreateCloseCallback();
this.response.toFileAsync(
destinationFilePath,
this.getCallback(resolve, reject)
);
}).then(() => {
this.file = File.fromPath(destinationFilePath);
return this.file;
});
}
}
export function enableSSLPinning(options: Https.HttpsSSLPinningOptions) {
if (!peer.host && !peer.certificate) {
let certificate: string;
let inputStream: java.io.FileInputStream;
try {
let file = new java.io.File(options.certificate);
inputStream = new java.io.FileInputStream(file);
let x509Certificate = java.security.cert.CertificateFactory.getInstance(
"X509"
).generateCertificate(inputStream);
peer.x509Certificate = x509Certificate;
certificate = okhttp3.CertificatePinner.pin(x509Certificate);
inputStream.close();
} catch (error) {
try {
if (inputStream) {
inputStream.close();
}
} catch (e) {}
console.error("nativescript-https > enableSSLPinning error", error);
return;
}
peer.host = options.host;
peer.commonName = options.commonName || options.host;
peer.certificate = certificate;
if (options.allowInvalidCertificates === true) {
peer.allowInvalidCertificates = true;
}
if (options.validatesDomainName === false) {
peer.validatesDomainName = false;
}
}
peer.enabled = true;
getClient(true);
}
export function disableSSLPinning() {
peer.enabled = false;
getClient(true);
}
let Client: okhttp3.OkHttpClient;
let cookieJar: com.nativescript.https.QuotePreservingCookieJar;
let cookieManager: java.net.CookieManager;
function getClient(
reload: boolean = false,
timeout: number = 10
): okhttp3.OkHttpClient {
if (!Client) {
// ssl error fix on KitKat. Only need to be done once.
// client will be null only onced so will run only once
const version = android.os.Build.VERSION.SDK_INT;
if (version >= 16 && version < 22) {
java.security.Security.insertProviderAt(
(org as any).conscrypt.Conscrypt.newProvider(),
1
);
}
}
// if (!Client) {
// Client = new okhttp3.OkHttpClient()
// }
// if (Client) {
// Client.connectionPool().evictAll()
// Client = null
// }
if (Client && reload === false) {
if (timeout === _timeout) {
return Client;
} else {
return Client.newBuilder()
.connectTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS)
.writeTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS)
.readTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS)
.build();
}
}
if (!cookieJar) {
cookieManager = new java.net.CookieManager();
cookieManager.setCookiePolicy(java.net.CookiePolicy.ACCEPT_ALL);
cookieJar = new com.nativescript.https.QuotePreservingCookieJar(
cookieManager
);
}
let client = new okhttp3.OkHttpClient.Builder();
if (peer.enabled === true) {
if (peer.host || peer.certificate) {
let spec = okhttp3.ConnectionSpec.MODERN_TLS;
client.connectionSpecs(java.util.Collections.singletonList(spec));
let pinner = new okhttp3.CertificatePinner.Builder();
pinner.add(peer.host, [peer.certificate]);
client.certificatePinner(pinner.build());
if (peer.allowInvalidCertificates === false) {
try {
let x509Certificate = peer.x509Certificate;
let keyStore = java.security.KeyStore.getInstance(
java.security.KeyStore.getDefaultType()
);
keyStore.load(null, null);
// keyStore.setCertificateEntry(peer.host, x509Certificate)
keyStore.setCertificateEntry("CA", x509Certificate);
// let keyManagerFactory = javax.net.ssl.KeyManagerFactory.getInstance(
// javax.net.ssl.KeyManagerFactory.getDefaultAlgorithm()
// )
let keyManagerFactory = javax.net.ssl.KeyManagerFactory.getInstance(
"X509"
);
keyManagerFactory.init(keyStore, null);
let keyManagers = keyManagerFactory.getKeyManagers();
let trustManagerFactory = javax.net.ssl.TrustManagerFactory.getInstance(
javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm()
);
trustManagerFactory.init(keyStore);
let sslContext = javax.net.ssl.SSLContext.getInstance(
"TLS"
);
sslContext.init(
keyManagers,
trustManagerFactory.getTrustManagers(),
new java.security.SecureRandom()
);
client.sslSocketFactory(sslContext.getSocketFactory());
} catch (error) {
console.error(
"nativescript-https > client.allowInvalidCertificates error",
error
);
}
}
if (peer.validatesDomainName === true) {
try {
client.hostnameVerifier(
new javax.net.ssl.HostnameVerifier({
verify: (
hostname: string,
session: javax.net.ssl.SSLSession
): boolean => {
let pp = session.getPeerPrincipal().getName();
let hv = javax.net.ssl.HttpsURLConnection.getDefaultHostnameVerifier();
if (
peer.commonName &&
peer.commonName[0] === "*"
) {
return (
hv.verify(peer.host, session) &&
hostname.indexOf(peer.host) > -1 &&
hostname.indexOf(
session.getPeerHost()
) > -1 &&
pp.indexOf(peer.commonName) !== -1
);
} else {
return (
hv.verify(peer.host, session) &&
peer.host === hostname &&
peer.host === session.getPeerHost() &&
pp.indexOf(peer.host) !== -1
);
}
},
})
);
} catch (error) {
console.error(
"nativescript-https > client.validatesDomainName error",
error
);
}
}
} else {
console.warn(
"nativescript-https > Undefined host or certificate. SSL pinning NOT working!!!"
);
}
}
_timeout = timeout;
client
.connectTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS)
.writeTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS)
.readTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS);
if (cache) {
client.cache(cache);
}
if (cookieJar) {
client.cookieJar(cookieJar);
}
Client = client.build();
return Client;
}
function cancelRequest(tag: string, client: okhttp3.OkHttpClient) {
if (notClosedResponses[tag]) {
notClosedResponses[tag].cancel();
return;
}
const dispatcher = client.dispatcher();
//When you want to cancel:
//A) go through the queued calls and cancel if the tag matches:
if (dispatcher.queuedCallsCount() > 0) {
const queuedCalls = dispatcher.queuedCalls();
for (let index = 0; index < queuedCalls.size(); index++) {
const call = queuedCalls.get(index);
if (call.request().tag() === tag) {
call.cancel();
return;
}
}
}
//B) go through the running calls and cancel if the tag matches:
if (dispatcher.runningCallsCount() > 0) {
const runningCalls = dispatcher.runningCalls();
for (let index = 0; index < runningCalls.size(); index++) {
const call = runningCalls.get(index);
if (call.request().tag() === tag) {
call.cancel();
return;
}
}
}
}
let CALL_ID = 0;
let notClosedResponses: {
[k: string]: com.nativescript.https.OkHttpResponse;
} = {};
let OkHttpResponse: typeof com.nativescript.https.OkHttpResponse;
export function createRequest(
opts: Https.HttpsRequestOptions
): Https.HttpsRequest {
let client = getClient(false);
let request = new okhttp3.Request.Builder();
request.url(opts.url);
if (opts.headers) {
Object.keys(opts.headers).forEach((key) =>
request.addHeader(key, opts.headers[key] as any)
);
}
if (opts.cachePolicy) {
let cacheControlBuilder = new okhttp3.CacheControl.Builder();
switch (opts.cachePolicy) {
case "noCache":
cacheControlBuilder = cacheControlBuilder.noStore();
break;
case "onlyCache":
cacheControlBuilder = cacheControlBuilder.onlyIfCached();
break;
case "ignoreCache":
cacheControlBuilder = cacheControlBuilder.noCache();
break;
}
request.cacheControl(cacheControlBuilder.build());
}
const methods = {
GET: "get",
HEAD: "head",
DELETE: "delete",
POST: "post",
PUT: "put",
PATCH: "patch",
};
let type;
if (
["GET", "HEAD"].indexOf(opts.method) !== -1 ||
(opts.method === "DELETE" &&
!isDefined(opts.body) &&
!isDefined(opts.content))
) {
request[methods[opts.method]]();
} else {
type =
opts.headers && opts.headers["Content-Type"]
? <string>opts.headers["Content-Type"]
: "application/json";
const MEDIA_TYPE = okhttp3.MediaType.parse(type);
let okHttpBody: okhttp3.RequestBody;
if (type.startsWith("multipart/form-data")) {
let builder = new okhttp3.MultipartBody.Builder();
builder.setType(MEDIA_TYPE);
(opts.body as Https.HttpsFormDataParam[]).forEach((param) => {
if (param.fileName && param.contentType) {
const MEDIA_TYPE = okhttp3.MediaType.parse(
param.contentType
);
builder.addFormDataPart(
param.parameterName,
param.fileName,
okhttp3.RequestBody.create(MEDIA_TYPE, param.data)
);
} else {
builder.addFormDataPart(param.parameterName, param.data);
}
});
okHttpBody = builder.build();
} else {
let body;
if (opts.body) {
try {
body = JSON.stringify(opts.body);
} catch (ignore) {}
} else if (opts.content) {
body = opts.content;
}
okHttpBody = okhttp3.RequestBody.create(
okhttp3.MediaType.parse(type),
body
);
}
request[methods[opts.method]](okHttpBody);
}
const tag = `okhttp_request_${CALL_ID++}`;
const call = client.newCall(request.tag(tag).build());
// We have to allow networking on the main thread because larger responses will crash the app with an NetworkOnMainThreadException.
// Note that it would probably be better to offload it to a Worker or (natively running) AsyncTask.
// Also note that once set, this policy remains active until the app is killed.
if (opts.useLegacy === false && opts.allowLargeResponse) {
android.os.StrictMode.setThreadPolicy(
android.os.StrictMode.ThreadPolicy.LAX
);
}
return {
nativeRequest: call,
cancel: () => cancelRequest(tag, client),
run(resolve, reject) {
call.enqueue(
new okhttp3.Callback({
onResponse(call, response) {
const responseBody = response.body();
const message = response.message();
const statusCode = response.code();
const getHeaders = function () {
const heads = response.headers();
let headers = {};
// let heads: okhttp3.Headers = resp.headers();
let i: number,
len: number = heads.size();
for (i = 0; i < len; i++) {
let key = heads.name(i);
headers[key] = heads.value(i);
}
return headers;
};
if (opts.useLegacy) {
if (!OkHttpResponse) {
OkHttpResponse =
com.nativescript.https.OkHttpResponse;
}
const nResponse = new OkHttpResponse(responseBody);
if (opts.onProgress) {
nResponse.progressCallback = new OkHttpResponse.OkHttpResponseProgressCallback(
{
onProgress: opts.onProgress,
}
);
}
resolve({
response,
content: new HttpsResponse(
nResponse,
tag,
opts.url
),
statusCode,
reason: message,
get headers() {
return getHeaders();
},
});
} else {
resolve({
response,
content: responseBody.string(),
reason: message,
statusCode,
get headers() {
return getHeaders();
},
});
}
},
onFailure(task, error) {
reject(error);
},
})
);
},
};
}
export function request(opts: Https.HttpsRequestOptions) {
return new Promise((resolve, reject) => {
try {
createRequest(opts).run(resolve, reject);
} catch (error) {
reject(error);
}
});
}