-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
1668 lines (1541 loc) · 43.8 KB
/
index.js
File metadata and controls
1668 lines (1541 loc) · 43.8 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
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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const {spawn, spawnSync} = require('child_process');
const fs = require('fs/promises');
const {CookieJar} = require('tough-cookie');
/**
* Returns proxy url based on the proxy options.
*
* @param {object} proxy proxy options
* @return {string} proxy url based on the options
* @private
*/
// eslint-disable-next-line complexity
function makeProxyUrl(proxy, options) {
if (!proxy) return '';
let address = typeof proxy === 'string' ? proxy : (proxy.host || proxy.address || proxy.url);
if (!address) return '';
if (!address.includes('://')) {
let type = proxy.type || options.type || 'http';
if (type === 'socks') type = 'socks5';
address = `${type}://${address}`;
}
const auth = proxy.auth || options.auth || {};
const uri = new URL(address);
if (!uri.port) {
const port = proxy.port || options.port;
if (port) {
uri.port = port;
}
else if (uri.protocol.startsWith('socks')) {
uri.port = '1080';
}
}
if (!uri.username) {
const username = proxy.username || options.username || auth.username;
if (username) uri.username = username;
}
if (!uri.password) {
const password = proxy.password || options.password || auth.password;
if (password) uri.password = password;
}
return uri.toString();
}
/**
* Parse curl command string into arguments array
*
* @param {string} command
* @return {string[]}
*/
function parseCommand(command) {
// Clean up the command - remove line breaks and extra spaces
command = command.replace(/\\\s*\n/g, ' ').replace(/\s+/g, ' ').trim();
const args = [];
let current = '';
let inQuotes = false;
let quoteChar = '';
for (let i = 0; i < command.length; i++) {
const char = command[i];
const nextChar = command[i + 1];
if ((char === '"' || char === "'") && !inQuotes) {
inQuotes = true;
quoteChar = char;
}
else if (char === quoteChar && inQuotes) {
inQuotes = false;
quoteChar = '';
}
else if (char === ' ' && !inQuotes) {
if (current.trim()) {
args.push(current.trim());
current = '';
}
}
else if (char === '\\' && nextChar === quoteChar && inQuotes) {
current += nextChar;
i++; // Skip next character
}
else {
current += char;
}
}
if (current.trim()) {
args.push(current.trim());
}
return args;
}
class CurlResponse {
constructor({body = ''} = {}) {
this.url = '';
this.body = body;
this.headers = {};
this.statusCode = 0;
this.ip = '';
this.errorMsg = '';
}
get status() {
return this.statusCode;
}
setCurlJson(json, options = {}) {
if (!json) return;
this.curlJson = json;
const data = json.json || {};
this.statusCode = data.response_code || 0;
this.ip = data.remote_ip || '';
this.url = data.url_effective || data.url || '';
this.errorMsg = data.errormsg || '';
this.curlTimeTaken = Math.round((data.time_total || 0) * 1000);
const headers = json.headers || {};
for (const [key, value] of Object.entries(headers)) {
this.headers[key.toLowerCase()] = value[0];
}
if (options.cookieJar) {
const setCookies = headers['set-cookie'];
if (setCookies) {
setCookies.forEach((cookie) => {
options.cookieJar.setCookie(cookie, this.url);
});
}
}
}
}
/**
* Parse cookies string into object
*
* @param {string} cookieStr
* @return {object}
*/
function parseCookies(cookieStr) {
const cookies = {};
if (!cookieStr) return cookies;
const parts = cookieStr.split(';');
for (const part of parts) {
const equalIndex = part.indexOf('=');
if (equalIndex !== -1) {
const key = part.slice(0, equalIndex).trim();
const value = part.slice(equalIndex + 1).trim();
if (key && value) {
cookies[key] = value;
}
}
}
return cookies;
}
class Curl {
/**
* Creates a new Curl object.
* @constructor
*/
constructor() {
/**
* Various options (or parameters) defining the Connection
* @private
*/
this.options = {
// url to send request at
url: null,
// method of the request (GET / POST / OPTIONS / DELETE / PATCH / PUT)
method: 'GET',
// headers to set
headers: {
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
// 'accept-encoding': 'gzip, deflate, br, zstd',
'accept-language': 'en-US,en-IN;q=0.9,en;q=0.8',
// set empty cookie header if no cookies exist
// this is because some sites expect cookie header to be there
cookie: '',
// 'sec-ch-ua': '"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
// 'sec-ch-ua-arch': '"x86"',
// 'sec-ch-ua-bitness': '"64"',
// 'sec-ch-ua-full-version': '"129.0.6668.58"',
// 'sec-ch-ua-full-version-list': '"Google Chrome";v="129.0.6668.58", "Not=A?Brand";v="8.0.0.0", "Chromium";v="129.0.6668.58"',
// 'sec-ch-ua-mobile': '?0',
// 'sec-ch-ua-model': '""',
// 'sec-ch-ua-platform': '"Linux"',
// 'sec-ch-ua-platform-version': '"6.2.0"',
// 'sec-fetch-dest': 'empty',
// 'sec-fetch-mode': 'navigate',
// 'sec-fetch-site': 'same-origin',
// 'upgrade-insecure-requests': 1,
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36',
},
// whether to follow redirects
followRedirect: true,
// maximum number of redirects to follow
maxRedirects: 6,
// whether to ask for compressed response (automatically handles accept-encoding)
compress: true,
// timeout of the request
timeout: 120 * 1000,
// whether to verify ssl certificate
ignoreSSLError: true,
// body of the request (valid in case of POST / PUT / PATCH / DELETE)
body: '',
};
}
get _cookies() {
let cookies = this.options.cookies;
if (!cookies) {
cookies = {};
this.options.cookies = cookies;
}
return cookies;
}
get _fields() {
// post fields
let fields = this.options.fields;
if (!fields) {
fields = {};
this.options.fields = fields;
}
return fields;
}
get _query() {
// query params
let query = this.options.query;
if (!query) {
query = {};
this.options.query = query;
}
return query;
}
/**
* Set the url for the connection.
*
* @param {string} url
* @return {Curl} self
*/
url(url) {
this.options.url = url;
return this;
}
/**
* @static
* Creates and returns a new Curl object with the given url.
*
* @param {string} url
* @return {Curl} A new Curl object with url set to the given url
*/
static url(url) {
const curl = new this();
curl.url(url);
return curl;
}
/**
* @static
* Creates and returns a new Curl object (with get method) with the given url.
*
* @param {string} url
* @return {Curl} A new Curl object with url set to the given url
*/
static get(url) {
const curl = new this();
curl.url(url);
curl.get();
return curl;
}
/**
* @static
* Creates and returns a new Curl object (with post method) with the given url.
*
* @param {string} url
* @return {Curl} A new Curl object with url set to the given url
*/
static post(url) {
const curl = new this();
curl.url(url);
curl.post();
return curl;
}
/**
* @static
* Creates and returns a new Curl object (with put method) with the given url.
*
* @param {string} url
* @return {Curl} A new Curl object with url set to the given url
*/
static put(url) {
const curl = new this();
curl.url(url);
curl.put();
return curl;
}
/**
* @static
* Returns a new cookie jar.
* @param {Array<any>} args
* @return {CookieJar} A cookie jar
*/
static getNewCookieJar(...args) {
return new CookieJar(...args);
}
/**
* @static
* Returns the global cookie jar.
* @returns {CookieJar} global cookie jar
*/
static getGlobalCookieJar() {
if (!this._globalCookieJar) {
this._globalCookieJar = this.getNewCookieJar();
}
return this._globalCookieJar;
}
/**
* @static
* whether curl-impersonate-chrome is available or not
* @see https://github.com/lexiforest/curl-impersonate
*/
static hasCurlImpersonate() {
if (this._hasCurlImpersonate === undefined) {
this._hasCurlImpersonate = spawnSync('curl-impersonate', ['--version']).status === 0;
}
return this._hasCurlImpersonate;
}
/**
* set cli command (default: curl)
*
* @param {string} command name of curl binary
* @return {Curl} self
*/
cliCommand(command) {
this.options.cliCommand = command;
return this;
}
/**
* add curl cli options
*
* @param {string|Array<string>} options curl cli options
* @return {Curl} self
*/
cliOptions(options) {
const cliOptions = this.options.cliOptions || (this.options.cliOptions = []);
if (typeof options === 'string') {
cliOptions.push(options);
}
else {
cliOptions.push(...options);
}
return this;
}
/**
* @typedef {"chrome"|"chromeMobile"|"edge"|"safari"|"safariMobile"|"firefox"} BrowserType
*/
/**
* impersonate a browser
*
* @param {BrowserType} browser browser to impersonate
* @return {Curl} self
*/
impersonate(browser = 'chrome') {
if (browser === 'chrome') {
if (this.constructor.hasCurlImpersonate()) {
this.cliCommand('curl-impersonate');
this.cliOptions([
'--ciphers',
'TLS_AES_128_GCM_SHA256,TLS_AES_256_GCM_SHA384,TLS_CHACHA20_POLY1305_SHA256,ECDHE-ECDSA-AES128-GCM-SHA256,ECDHE-RSA-AES128-GCM-SHA256,ECDHE-ECDSA-AES256-GCM-SHA384,ECDHE-RSA-AES256-GCM-SHA384,ECDHE-ECDSA-CHACHA20-POLY1305,ECDHE-RSA-CHACHA20-POLY1305,ECDHE-RSA-AES128-SHA,ECDHE-RSA-AES256-SHA,AES128-GCM-SHA256,AES256-GCM-SHA384,AES128-SHA,AES256-SHA',
'--curves',
'X25519MLKEM768:X25519:P-256:P-384',
'--http2',
'--http2-settings',
'1:65536;2:0;4:6291456;6:262144',
'--http2-window-update',
'15663105',
'--http2-stream-weight',
'256',
'--http2-stream-exclusive',
'1',
'--compressed',
'--ech',
'grease',
'--tlsv1.2',
'--alps',
'--tls-permute-extensions',
'--cert-compression',
'brotli',
'--tls-grease',
'--tls-use-new-alps-codepoint',
'--tls-signed-cert-timestamps',
]);
}
this.headers({
'sec-ch-ua': '"Google Chrome";v="137", "Chromium";v="137", "Not/A)Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36',
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-User': '?1',
'Sec-Fetch-Dest': 'document',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Accept-Language': 'en-US,en;q=0.9',
'Priority': 'u=0, i',
});
}
else if (browser === 'chromeMobile') {
if (this.constructor.hasCurlImpersonate()) {
this.cliCommand('curl-impersonate');
this.cliOptions([
'--ciphers',
'TLS_AES_128_GCM_SHA256,TLS_AES_256_GCM_SHA384,TLS_CHACHA20_POLY1305_SHA256,ECDHE-ECDSA-AES128-GCM-SHA256,ECDHE-RSA-AES128-GCM-SHA256,ECDHE-ECDSA-AES256-GCM-SHA384,ECDHE-RSA-AES256-GCM-SHA384,ECDHE-ECDSA-CHACHA20-POLY1305,ECDHE-RSA-CHACHA20-POLY1305,ECDHE-RSA-AES128-SHA,ECDHE-RSA-AES256-SHA,AES128-GCM-SHA256,AES256-GCM-SHA384,AES128-SHA,AES256-SHA',
'--curves',
'X25519:P-256:P-384',
'--http2',
'--http2-settings',
'1:65536;2:0;4:6291456;6:262144',
'--http2-window-update',
'15663105',
'--http2-stream-weight',
'256',
'--http2-stream-exclusive',
'1',
'--compressed',
'--ech',
'grease',
'--tlsv1.2',
'--alps',
'--tls-permute-extensions',
'--cert-compression',
'brotli',
'--tls-grease',
'--tls-use-new-alps-codepoint',
'--tls-signed-cert-timestamps',
]);
}
this.headers({
'sec-ch-ua': 'Google Chrome";v="137", "Chromium";v="137", "Not_A Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Android"',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Linux; Android 12; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36',
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-User': '?1',
'Sec-Fetch-Dest': 'document',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Accept-Language': 'en-US,en;q=0.9',
'Priority': 'u=0, i',
});
}
else if (browser === 'edge') {
if (this.constructor.hasCurlImpersonate()) {
this.cliCommand('curl-impersonate');
this.cliOptions([
'--ciphers',
'TLS_AES_128_GCM_SHA256,TLS_AES_256_GCM_SHA384,TLS_CHACHA20_POLY1305_SHA256,ECDHE-ECDSA-AES128-GCM-SHA256,ECDHE-RSA-AES128-GCM-SHA256,ECDHE-ECDSA-AES256-GCM-SHA384,ECDHE-RSA-AES256-GCM-SHA384,ECDHE-ECDSA-CHACHA20-POLY1305,ECDHE-RSA-CHACHA20-POLY1305,ECDHE-RSA-AES128-SHA,ECDHE-RSA-AES256-SHA,AES128-GCM-SHA256,AES256-GCM-SHA384,AES128-SHA,AES256-SHA',
'--http2',
'--http2-settings',
'1:65536;3:1000;4:6291456;6:262144',
'--http2-window-update',
'15663105',
'--http2-stream-weight',
'256',
'--http2-stream-exclusive',
'1',
'--compressed',
'--tlsv1.2',
'--alps',
'--cert-compression',
'brotli',
'--tls-grease',
'--tls-signed-cert-timestamps',
]);
}
this.headers({
'sec-ch-ua': ' Not A;Brand";v="99", "Chromium";v="101", "Microsoft Edge";v="101"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47',
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-User': '?1',
'Sec-Fetch-Dest': 'document',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9',
});
}
else if (browser === 'safari') {
if (this.constructor.hasCurlImpersonate()) {
this.cliCommand('curl-impersonate');
this.cliOptions([
'--ciphers',
'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:TLS_RSA_WITH_AES_256_GCM_SHA384:TLS_RSA_WITH_AES_128_GCM_SHA256:TLS_RSA_WITH_AES_256_CBC_SHA:TLS_RSA_WITH_AES_128_CBC_SHA:TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:TLS_RSA_WITH_3DES_EDE_CBC_SHA',
'--curves',
'X25519:P-256:P-384:P-521',
'--signature-hashes',
'ecdsa_secp256r1_sha256,rsa_pss_rsae_sha256,rsa_pkcs1_sha256,ecdsa_secp384r1_sha384,rsa_pss_rsae_sha384,rsa_pss_rsae_sha384,rsa_pkcs1_sha384,rsa_pss_rsae_sha512,rsa_pkcs1_sha512,rsa_pkcs1_sha1',
'--http2',
'--http2-settings',
'2:0;3:100;4:2097152;9:1',
'--http2-pseudo-headers-order',
'msap',
'--http2-window-update',
'10420225',
'--http2-stream-weight',
'256',
'--http2-stream-exclusive',
'0',
'--compressed',
'--tlsv1.0',
'--no-tls-session-ticket',
'--cert-compression',
'zlib',
'--tls-grease',
'--tls-signed-cert-timestamps',
]);
}
this.headers({
'sec-fetch-dest': 'document',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Safari/605.1.15',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'sec-fetch-site': 'none',
'sec-fetch-mode': 'navigate',
'accept-language': 'en-US,en;q=0.9',
'priority': 'u=0, i',
'accept-encoding': 'gzip, deflate, br',
});
}
else if (browser === 'safariMobile') {
if (this.constructor.hasCurlImpersonate()) {
this.cliCommand('curl-impersonate');
this.cliOptions([
'--ciphers',
'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:TLS_RSA_WITH_AES_256_GCM_SHA384:TLS_RSA_WITH_AES_128_GCM_SHA256:TLS_RSA_WITH_AES_256_CBC_SHA:TLS_RSA_WITH_AES_128_CBC_SHA:TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:TLS_RSA_WITH_3DES_EDE_CBC_SHA',
'--curves',
'X25519:P-256:P-384:P-521',
'--signature-hashes',
'ecdsa_secp256r1_sha256,rsa_pss_rsae_sha256,rsa_pkcs1_sha256,ecdsa_secp384r1_sha384,rsa_pss_rsae_sha384,rsa_pss_rsae_sha384,rsa_pkcs1_sha384,rsa_pss_rsae_sha512,rsa_pkcs1_sha512,rsa_pkcs1_sha1',
'--http2',
'--http2-settings',
'2:0;3:100;4:2097152;9:1',
'--http2-pseudo-headers-order',
'msap',
'--http2-window-update',
'10420225',
'--http2-stream-weight',
'256',
'--http2-stream-exclusive',
'0',
'--compressed',
'--tlsv1.0',
'--no-tls-session-ticket',
'--cert-compression',
'zlib',
'--tls-grease',
'--tls-signed-cert-timestamps',
]);
}
this.headers({
'sec-fetch-dest': 'document',
'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Mobile/15E148 Safari/604.1',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'sec-fetch-site': 'none',
'sec-fetch-mode': 'navigate',
'accept-language': 'en-US,en;q=0.9',
'priority': 'u=0, i',
'accept-encoding': 'gzip, deflate, br',
});
}
else if (browser === 'firefox') {
if (this.constructor.hasCurlImpersonate()) {
this.cliCommand('curl-impersonate');
this.cliOptions([
'--ciphers',
'TLS_AES_128_GCM_SHA256,TLS_CHACHA20_POLY1305_SHA256,TLS_AES_256_GCM_SHA384,TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,TLS_RSA_WITH_AES_128_GCM_SHA256,TLS_RSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA',
'--curves',
'X25519MLKEM768:X25519:P-256:P-384:P-521:ffdhe2048:ffdhe3072',
'--signature-hashes',
'ecdsa_secp256r1_sha256,ecdsa_secp384r1_sha384,ecdsa_secp521r1_sha512,rsa_pss_rsae_sha256,rsa_pss_rsae_sha384,rsa_pss_rsae_sha512,rsa_pkcs1_sha256,rsa_pkcs1_sha384,rsa_pkcs1_sha512,ecdsa_sha1,rsa_pkcs1_sha1',
'--http2',
'--http2-settings',
'1:65536;2:0;4:131072;5:16384',
'--http2-pseudo-headers-order',
'msap',
'--http2-window-update',
'12517377',
'--http2-stream-weight',
'42',
'--http2-stream-exclusive',
'0',
'--compressed',
'--ech',
'grease',
'--tls-extension-order',
'0-23-65281-10-11-35-16-5-34-18-51-43-13-45-28-27-65037',
'--tls-delegated-credentials',
'ecdsa_secp256r1_sha256:ecdsa_secp384r1_sha384:ecdsa_secp521r1_sha512:ecdsa_sha1',
'--tls-record-size-limit',
'4001',
'--tls-key-shares-limit',
'3',
'--cert-compression',
'zlib,brotli,zstd',
'--tls-signed-cert-timestamps',
'--tls-use-firefox-tls13-ciphers',
]);
}
this.headers({
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:135.0) Gecko/20100101 Firefox/135.0',
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Priority': 'u=0, i',
'TE': 'trailers',
});
}
return this;
}
/**
* @static
* impersonate a browser
*
* @param {BrowserType} browser browser to impersonate
* @return {Curl} self
*/
static impersonate(browser = 'chrome') {
const curl = new this();
curl.impersonate(browser);
return curl;
}
/**
* Set or unset the followRedirect option for the connection.
*
* @param {boolean} shouldFollowRedirect boolean representing whether to follow redirect or not
* @return {Curl} self
*/
followRedirect(shouldFollowRedirect = true) {
this.options.followRedirect = shouldFollowRedirect;
return this;
}
/**
* Set the number of maximum redirects to follow
* @param {number} numRedirects max number of redirects
*/
maxRedirects(numRedirects) {
this.options.maxRedirects = numRedirects;
return this;
}
/**
* Set value of a header parameter for the connection.
*
* @param {string|object} headerName name of the header parameter whose value is to be set
* @param {string|undefined} headerValue value to be set
* @return {Curl} self
*/
header(headerName, headerValue) {
if (typeof headerName === 'string') {
this.options.headers[headerName.toLowerCase()] = headerValue;
}
else {
Object.assign(
this.options.headers,
Object.fromEntries(
Object.entries(headerName).map(([key, val]) => [key.toLowerCase(), val])
),
);
}
return this;
}
/**
* Set value of the headers for the connection.
*
* @param {object} headers object representing the headers for the connection
* @return {Curl} self
*/
headers(headers) {
this.header(headers);
return this;
}
/**
* Clear all headers
*
* @return {Curl} self
*/
clearHeaders() {
this.options.headers = {};
return this;
}
/**
* Set the content type as json
* Optionally also set the body of the request.
*
* @param {object|undefined} body value for body
* @return {Curl} self
*/
json(body) {
this.header('content-type', 'application/json');
if (body !== undefined) {
this.options.body = body;
}
return this;
}
/**
* Set the body of the connection object.
*
* @param {any} body value for body
* if body is an object, contentType will be set to application/json and body will be stringified
* @param {string} [contentType=null] string representing the content type of the body
* contentType can be null or json
* @return {Curl} self
*/
body(body, contentType = null) {
if (contentType) {
this.contentType = contentType;
}
this.options.body = body;
return this;
}
/**
* Set the 'Referer' field in the headers.
*
* @param {string} referer referer value
* @return {Curl}
*/
referer(referer) {
this.header('referer', referer);
return this;
}
/**
* Set the 'Referer' field in the headers.
*
* @param {string} referer referer value
* @return {Curl}
*/
referrer(referrer) {
this.header('referer', referrer);
return this;
}
/**
* Set the 'User-Agent' field in the headers.
*
* @param {string} userAgent name of the user-agent or its value
* @return {Curl} self
*/
userAgent(userAgent) {
this.header('user-agent', userAgent);
return this;
}
/**
* Set the 'Content-Type' field in the headers.
*
* @param {string} contentType value for content-type
* @return {Curl}
*/
contentType(contentType) {
if (contentType === 'json') {
this.header('content-type', 'application/json');
}
else if (contentType === 'form') {
this.header('content-type', 'application/x-www-form-urlencoded');
}
else {
this.header('content-type', contentType);
}
return this;
}
/**
* Returns whether the content-type is JSON or not
*
* @return {boolean} true, if content-type is JSON; false, otherwise
*/
isJSON() {
const contentType = this.options.headers['content-type'] || '';
return contentType.startsWith('application/json');
}
/**
* Returns whether the content-type is Form or not
*
* @return {boolean} true, if content-type is JSON; false, otherwise
*/
isForm() {
return this.options.headers['content-type'] === 'application/x-www-form-urlencoded';
}
/**
* Sets the value of a cookie.
* Can be used to enable global cookies, if cookieName is set to true
* and cookieValue is undefined (or is not passed as an argument).
* Can also be used to set multiple cookies by passing in an object
* representing the cookies and their values as key:value pairs.
*
* @param {string|boolean|object} cookieName represents the name of the
* cookie to be set, or the cookies object
* @param {string|undefined} [cookieValue] cookie value to be set
* @return {Curl} self
*/
cookie(cookieName, cookieValue) {
if (cookieValue === undefined) {
this.cookies(cookieName);
}
else if (typeof cookieName === 'string') {
if (typeof cookieValue === 'string') {
this._cookies[cookieName] = cookieValue;
}
else if (cookieValue === null || cookieValue === false) {
delete this._cookies[cookieName];
}
}
return this;
}
/**
* Sets multiple cookies.
* Can be used to enable global cookies, if cookies is set to true.
*
* @param {object|boolean|string} cookies object representing the cookies
* and their values as key:value pairs.
* @return {Curl} self
*/
cookies(cookies) {
if (cookies === true || cookies === false || cookies == null) {
this.globalCookies(cookies);
}
else if (cookies && typeof cookies === 'object') {
Object.assign(this._cookies, cookies);
}
else if (typeof cookies === 'string') {
const cookies = parseCookies(cookieName);
Object.assign(this._cookies, cookies);
}
return this;
}
/**
* Enable global cookies.
*
* @param {boolean|object} [options=true]
* @return {Curl} self
*/
globalCookies(options = true) {
if (options === false || options === null) {
delete this._cookieJar;
delete this._readCookieJar;
return this;
}
const jar = this.constructor.getGlobalCookieJar();
this.cookieJar(jar, options);
return this;
}
/**
* Set the value of cookie jar.
*
* @param {CookieJar} cookieJar value to be set
* @return {Curl} self
*/
cookieJar(cookieJar, options = {}) {
delete this._cookieFileFn;
delete this._cookieFileFnRes;
if (options.readOnly) {
this._readCookieJar = cookieJar;
}
else {
this._cookieJar = cookieJar;
}
return this;
}
/**
* Get the current cookie jar
*
* @return {CookieJar|undefined} the current cookie jar
*/
getCookieJar() {
return this._cookieJar || this._readCookieJar;
}
/**
* Set the value of cookie jar based on a file (cookie store).
*
* @param {string} fileName name of (or path to) the file
* @return {Curl} self
*/
cookieFile(fileName, options = {}) {
const setCookieJar = (cookieJar) => {
if (options.readOnly) {
this._readCookieJar = cookieJar;
}
else {
this._cookieJar = cookieJar;
}
};
this._cookieFileFn = async () => {
try {
const contents = await fs.readFile(fileName, {encoding: 'utf8'});
const obj = JSON.parse(contents);
const cookieJar = CookieJar.fromJSON(obj);
setCookieJar(cookieJar);
}
catch (e) {
setCookieJar(this.constructor.getNewCookieJar());
}
};
if (!options.readOnly) {
this._cookieFileFnRes = async () => {
const cookieJar = this._cookieJar;
if (!cookieJar) return;
try {
const contents = JSON.stringify(cookieJar.toJSON());
await fs.writeFile(fileName, contents, {encoding: 'utf8'});
}
catch (e) {
// ignore errors
}
};
}
return this;
}
/**
* Set request timeout.
*
* @param {number} timeout timeout value in seconds
* @return {Curl} self
*/
timeout(timeout) {
this.options.timeout = timeout * 1000;
return this;
}
/**
* Set request timeout.
*
* @param {number} timeoutInMs timeout value in milliseconds
* @return {Curl} self
*/
timeoutMs(timeoutInMs) {
this.options.timeout = timeoutInMs;
return this;
}
/**
* Set value of a field in the options.
* Can also be used to set multiple fields by passing in an object
* representing the field-names and their values as key:value pairs.
*
* @param {string|object} fieldName name of the field to be set, or the fields object
* @param {string|undefined} [fieldValue] value to be set