forked from dzmitry-duboyski/2captcha-ts
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path2captcha.ts
More file actions
2335 lines (2119 loc) · 81.6 KB
/
2captcha.ts
File metadata and controls
2335 lines (2119 loc) · 81.6 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
import fetch from "../utils/fetch"
import { APIError } from "./2captchaError"
import * as utils from "../utils/generic"
import getProviderData from "./providers/providers"
import { softId } from "./constants/constants"
import checkCaptchaParams from "../utils/checkCaptchaParams"
import renameParams from "../utils/renameParams"
const provider = getProviderData ()
export interface paramsRecaptcha {
pageurl: string,
googlekey: string,
invisible?: 0 | 1,
datas?: string,
domain?: string,
cookies?: string,
userAgent?: string,
header_acao?: boolean,
pingback?: string,
soft_id?: number,
proxy?: string,
proxytype?: string,
action?: string,
enterprise?: 0 | 1,
min_score?: number,
version?: string
}
export interface paramsHCaptcha {
sitekey: string,
pageurl: string,
header_acao?: boolean,
pingback?: string,
proxy?: string,
proxytype?: string,
invisible?: 0 | 1,
data?: string,
userAgent?: string,
soft_id?: number,
domain?: string
}
export interface paramsFunCaptcha {
publickey: string,
pageurl: string,
surl?: string,
header_acao?: boolean,
pingback?: string,
proxy?: string,
proxytype?: string,
userAgent?: string,
data?: string
}
export interface paramsImageCaptcha {
body: string,
phrase?: 0 | 1,
regsense?: 0 | 1,
numeric?: 0 | 1 | 2 | 3 | 4,
calc?: 0 | 1,
min_len?: 0 | string | number, // 1..20
max_len?: 0 | string | number, // 1..20
language?: 0 | 1 | 2,
lang?: string,
pingback?: string,
textinstructions?: string
}
export interface paramsGeetest {
gt: string,
challenge: string,
pageurl: string,
api_server?: string,
offline?: number | boolean,
new_captcha?: number | boolean,
pingback?: string,
soft_id?: number,
proxy?: string,
proxytype?: string,
userAgent?: string
}
export interface yandexSmart {
pageurl: string,
sitekey: string,
pingback?: string,
proxy?: string,
proxytype?: string,
userAgent?: string
}
export interface paramsGeeTestV4 {
pageurl: string,
captcha_id: string,
risk_type?: string,
pingback?: string,
proxy?: string,
proxytype?: string,
userAgent?: string
}
export interface paramsLemin {
pageurl: string,
captcha_id: string,
div_id: string,
api_server?: string,
pingback?: string,
proxy?: string,
proxytype?: string
}
export interface paramsAmazonWAF {
pageurl: string,
sitekey: string,
iv: string
context: string,
challenge_script?: string,
captcha_script?: string,
header_acao?: boolean,
pingback?: string,
soft_id?: number,
proxy?: string,
proxytype?: string,
}
export interface paramsTurnstile {
pageurl: string,
sitekey: string,
action?: string,
data?: string,
header_acao?: boolean,
pingback?: string,
soft_id?: number,
proxy?: string,
proxytype?: string,
}
export interface paramsCapyPuzzle {
pageurl: string,
captchakey: string,
api_server?: string,
version?: string,
pingback?: string,
proxy?: string,
proxytype?: string,
}
export interface paramsCoordinates {
body: string,
language?: 0 | 1 | 2,
lang?: string,
pingback?: string,
textinstructions?: string,
imginstructions?: string
}
export interface paramsDataDome {
pageurl: string,
captcha_url: string,
userAgent: string,
pingback?: string,
proxy: string,
proxytype: string,
}
export interface paramsCyberSiARA {
pageurl: string,
master_url_id: string,
userAgent: string,
pingback?: string,
proxy?: string,
proxytype?: string,
}
export interface paramsMTCaptcha {
pageurl: string,
sitekey: string,
userAgent?: string,
pingback?: string,
proxy?: string,
proxytype?: string,
}
export interface paramsCutcaptcha {
pageurl: string,
miseryKey: string,
apiKey: string,
pingback?: string,
proxy?: string,
proxytype?: string
}
export interface friendlyCaptcha {
pageurl: string,
sitekey: string,
pingback?: string,
proxy?: string,
proxytype?: string,
}
export interface paramsBoundingBox {
image: string,
textinstructions?: string,
imginstructions?: string,
}
export interface paramsGrid {
body: string,
recaptcha: number,
canvas?: number,
rows?: number,
cols?: number,
minClicks?: number,
maxClicks?: number,
previousId?: string,
imgType?: string,
textinstructions?: string,
imginstructions?: string,
canSkip?: number,
lang?: string,
pingback?: string,
}
export interface paramsTextcaptcha {
textcaptcha: string,
lang?: string,
pingback?: string,
}
export interface paramsRotateCaptcha {
body: string,
angle?: number,
pingback?: string,
lang?: string,
textinstructions?: string,
imginstructions?: string
}
export interface paramsKeyCaptcha {
pageurl: string,
userId: string,
sessionId: string,
webServerSign: string,
webServerSign2: string,
pingback?: string,
proxy?: string,
proxytype?: string
}
export interface paramsTencent {
pageurl: string,
appId: string,
pingback?: string,
proxy?: string,
proxytype?: string
}
export interface paramsAtbCaptcha{
pageurl: string,
appId: string,
apiServer: string,
pingback?: string,
proxy?: string,
proxytype?: string
}
export interface paramsProsopo {
pageurl: string,
sitekey: string,
proxy?: string,
proxytype?: string,
}
export interface paramsCaptchaFox {
pageurl: string,
sitekey: string,
userAgent: string,
proxy: string,
proxytype: string,
api_server?: string,
}
export interface paramsVkImage {
body: string,
steps: string,
}
export interface paramsVkCaptcha {
redirect_uri: string,
userAgent: string,
proxy: string,
proxytype: string,
}
export interface paramsTemu {
body: string,
part1: string,
part2: string,
part3: string,
}
export interface paramsAltcha {
pageurl: string,
challengeUrl?: string,
challengeJson?: string,
proxy?: string,
proxytype?: string,
}
export interface paramsAudioCaptcha {
body: string,
lang: string,
pingback?: string,
}
/**
* An object containing properties of the captcha solution.
* @typedef {Object} CaptchaAnswer
* @param {string} data The solution to the captcha
* @param {string} id The captcha ID
*/
interface CaptchaAnswer {
/** The solution to the captcha */
data: string,
/** The ID of the captcha solve */
id: string
}
/**
* The main 2captcha class, housing all API calls and api interactions.
*
*/
export class Solver {
public _apikey: string
public _pollingFrequency: number
public _headerACAO: number;
/**
* The constructor for the 2captcha Solver class.
*
* @param {string} apikey The API key to use
* @param {number} pollingFrequency The frequency to poll for requests
*
*/
constructor(apikey: string, pollingFrequency: number = 5000, enableACAO: boolean = true) {
this._apikey = apikey
this._pollingFrequency = pollingFrequency
this._headerACAO = enableACAO ? 1 : 0
}
/** The API key this instance is using */
public get apikey() { return this._apikey }
/** Frequency the instance polls for updates */
public get pollingFrequency() { return this._pollingFrequency }
/** Set the API key for this instance */
public set apikey(update: string) { this._apikey = update }
private get in() { return provider.in }
private get res() { return provider.res}
private get defaultPayload() { return { key: this.apikey, json: 1, header_acao: this._headerACAO, soft_id: softId } }
/**
* Returns the remaining account balance.
*
* @return {Promise<Number>} Remaining balance
* @throws APIError
* @example
* solver.balance()
* .then((res) => {
* console.log(res)
* })
*/
public async balance(): Promise<number> {
const res = await fetch(this.res + utils.objectToURI({
...this.defaultPayload,
action: "getbalance"
}))
const result = await res.text()
try {
const data = JSON.parse(result)
if (data.status == 1) {
return parseFloat(data.request)
}
throw new APIError(data.request)
} catch {
throw new APIError(result)
}
}
/**
* @private
*
* Polls for a captcha, finding out if it's been completed
* @param {string} id Captcha ID
*
* @returns {Promise<CaptchaAnswer>}
* @throws APIError
*/
private async pollResponse(id: string): Promise<CaptchaAnswer> {
const payload = {
...this.defaultPayload,
action: "get",
id: id
}
await utils.sleep(this.pollingFrequency)
const res = await fetch(this.res + utils.objectToURI(payload))
const result = await res.text()
let data;
try {
data = JSON.parse(result)
if (data.status == 1) {
let dataJSON = { ...data, data: data.request, id: id}
delete dataJSON.request
return dataJSON
}
} catch {
throw new APIError(result)
}
switch (data.request) {
case "CAPCHA_NOT_READY":
return this.pollResponse(id);
default: {
throw new APIError(data.request)
}
}
}
/**
* ### Solves a google reCAPTCHA V2 | V3.
*
* [Read more about other reCAPTCHA parameters](https://2captcha.com/2captcha-api#solving_recaptchav2_new).
*
* @param {{pageurl, googlekey, cookies, proxy, proxytype, userAgent, invisible, datas, pingback, action, enterprise, min_score, version, domain}} params Object
* @param {string} params.pageurl The URL the captcha appears on.
* @param {string} params.googlekey Value of `k` or `data-sitekey` parameter you found on page.
* @param {string} params.cookies Your cookies that will be passed to our worker who solve the captha.
* @param {string} params.proxy Format: `login:password@123.123.123.123:3128`. You can find more info about proxies [here](https://2captcha.com/2captcha-api#proxies).
* @param {string} params.proxytype Type of your proxy: `HTTP`, `HTTPS`, `SOCKS4`, `SOCKS5`.
* @param {string} params.userAgent Your `userAgent` that will be passed to our worker and used to solve the captcha.
* @param {number} params.invisible `1` - means that reCAPTCHA is invisible. `0` - normal reCAPTCHA.
* @param {string} params.datas Value of `data-s` parameter you found on page. Curenttly applicable for Google Search and other Google services.
* @param {string} params.pingback URL for pingback (callback) response that will be sent when captcha is solved. URL should be registered on the server. [More info here](https://2captcha.com/2captcha-api#pingback).
* @param {string} params.action Value of `action` parameter you found on page.
* @param {number} params.enterprise `1` - defines that you're sending reCAPTCHA Enterpise.
* @param {number} params.min_score The score needed for resolution reCAPTCHA V3. Currently it's almost impossible to get token with score higher than `0.3`
* @param {string} params.version `v2` — defines that you're sending a reCAPTCHA V2. `v3` — defines that you're sending a reCAPTCHA V3.
* @param {string} params.domain Domain used to load the captcha: `google.com` or `recaptcha.net`
*
* @returns {Promise<CaptchaAnswer>} The result from the solve.
* @throws APIError
* @example
* solver.recaptcha({
* pageurl: 'https://2captcha.com/demo/recaptcha-v2',
* googlekey: '6LfD3PIbAAAAAJs_eEHvoOl75_83eXSqpPSRFJ_u'
* })
* .then((res) => {
* console.log(res);
* })
* .catch((err) => {
* console.log(err);
* })
*/
public async recaptcha(params: paramsRecaptcha): Promise<CaptchaAnswer> {
checkCaptchaParams(params, "userrecaptcha")
const payload = {
...this.defaultPayload,
...params,
method: "userrecaptcha"
}
const response = await fetch(this.in + utils.objectToURI(payload))
const result = await response.text()
let data;
try {
data = JSON.parse(result)
} catch {
throw new APIError(result)
}
if (data.status == 1) {
return this.pollResponse(data.request)
} else {
throw new APIError(data.request)
}
}
/**
* ### Solves a hCaptcha
*
* [Read more about other hCaptcha parameters](https://2captcha.com/2captcha-api#solving_hcaptcha).
*
* @param {{sitekey, pageurl, data, userAgent, invisible, pingback, proxy, proxytype, domain}} params Object
* @param {string} params.sitekey The hcaptcha site key. Value of `k` or `data-sitekey` parameter you found on page.
* @param {string} params.pageurl The URL the captcha appears on.
* @param {string} params.data Custom `data` that is used in some implementations of hCaptcha, mostly with `invisible=1`. In most cases you see it as `rqdata` inside network requests. IMPORTANT: you MUST provide `userAgent` if you submit captcha with `data` paramater. The value should match the User-Agent you use when interacting with the target website.
* @param {string} params.userAgent Your userAgent that will be passed to our worker and used to solve the captcha. Required for hCaptcha with `data` parameter.
* @param {number} params.invisible Use `1` for invisible version of hcaptcha. Currently it is a very rare case.
* @param {string} params.pingback URL for pingback (callback) response that will be sent when captcha is solved. URL should be registered on the server. More info [here](https://2captcha.com/2captcha-api#pingback).
* @param {string} params.proxy Format: `login:password@123.123.123.123:3128` You can find more info about proxies [here](https://2captcha.com/2captcha-api#proxies).
* @param {string} params.proxytype Type of your proxy: `HTTP`, `HTTPS`, `SOCKS4`, `SOCKS5`.
* @param {string} params.domain Domain used to load the captcha: `hcaptcha.com` or `js.hcaptcha.com`
*
* @returns {Promise<CaptchaAnswer>} The result from the solve
* @throws APIError
* @example
* solver.hcaptcha({
* pageurl: "https://2captcha.com/demo/hcaptcha",
* sitekey: "b76cd927-d266-4cfb-a328-3b03ae07ded6"
* .then((res) => {
* console.log(res);
* })
* .catch((err) => {
* console.log(err);
* })
*/
public async hcaptcha(params: paramsHCaptcha): Promise<CaptchaAnswer> {
checkCaptchaParams(params, "hcaptcha")
const payload = {
...this.defaultPayload,
...params,
method: "hcaptcha"
}
const response = await fetch(this.in + utils.objectToURI(payload))
const result = await response.text()
let data;
try {
data = JSON.parse(result)
} catch {
throw new APIError(result)
}
if (data.status == 1) {
return this.pollResponse(data.request)
} else {
throw new APIError(data.request)
}
}
/**
* ### Solves a GeeTest Captcha.
*
* [Read more about parameters and solving for Geetest captcha](https://2captcha.com/2captcha-api#solving_geetest).
*
* @param {{ gt, challenge, api_server, offline, new_captcha,
* pageurl, pingback, proxy, proxytype, userAgent }} params
* @param {string} params.gt Value of gt parameter found on site
* @param {string} params.challenge Value of challenge parameter found on site
* @param {string} params.pageurl The URL the captcha appears on
* @param {string} params.api_server The URL of the api_server (recommended)
* @param {number} params.offline In rare cases `initGeetest` can be called with `offline` parameter on the target page. If the call uses offline: true, set the value to `1`.
* @param {number} params.new_captcha In rare cases `initGeetest` can be called with `new_captcha` parameter. If the call uses `new_captcha: true`, set the value to `1`. Mostly used with offline parameter.
* @param {string} params.pingback URL for `pingback` (callback) response that will be sent when captcha is solved. [More info here](https://2captcha.com/2captcha-api#pingback).
* @param {string} params.proxy Format: `login:password@123.123.123.123:3128`. You can find more info about proxies [here](https://2captcha.com/2captcha-api#proxies).
* @param {string} params.proxytype Type of your proxy: `HTTP`, `HTTPS`, `SOCKS4`, `SOCKS5`.
* @param {string} params.userAgent Your `userAgent` that will be passed to our worker and used to solve the captcha.
*
* @returns {Promise<CaptchaAnswer>} The result from the solve.
* @throws APIError
* @example
* ;(async () => {
*
* // Warning: Attention, the `challenge` value is not static but dynamic.
* // You need to find the queries that makes the captcha on the page to API.
* // Then you need to make request to this API and get new `challenge`.
*
* // For page https://rucaptcha.com/demo/geetest, api address is https://rucaptcha.com/api/v1/captcha-demo/gee-test/init-params?t=${t}
* // Also note that when make request to API, the request uses the dynamic parameter `t`
*
* // You can read more about sending GeeTest here https://2captcha.com/2captcha-api#solving_geetest, or here https://2captcha.com/p/geetest
* // In this example I solve GeeTest from page https://2captcha.com/demo/geetest
*
* const t = new Date().getTime()
* // below i make a request to get a new `challenge`.
* const response = await fetch(`https://2captcha.com/api/v1/captcha-demo/gee-test/init-params?t=${t}`)
* const data = await response.json()
*
* const params = {
* pageurl: 'https://rucaptcha.com/demo/geetest',
* gt: data.gt,
* challenge: data.challenge
* }
*
* const res = await solver.geetest(params)
* try {
* console.log(res)
* } catch (error) {
* console.error(error);
* }
* })()
*/
public async geetest(params: paramsGeetest): Promise<CaptchaAnswer> {
checkCaptchaParams(params, "geetest")
const payload = {
...this.defaultPayload,
...params,
method: "geetest"
}
const response = await fetch(this.in + utils.objectToURI(payload))
const result = await response.text()
let data;
try {
data = JSON.parse(result)
} catch {
throw new APIError(result)
}
if (data.status == 1) {
return this.pollResponse(data.request)
} else {
throw new APIError(data.request)
}
}
/**
* ### Solves a GeeTest V4 Captcha.
*
* This method accepts an object with the following fields: `pageurl`, `captcha_id`, `pingback`, `proxy`, `proxytype`, `userAgent`.
* The `pageurl` and `captcha_id` fields are required.
*
* @param {{pageurl, captcha_id, risk_type, pingback, proxy, proxytype, userAgent}} params The method geetestV4 takes arguments as an object.
* @param {string} params.pageurl Full URL of the page where you see Geetest V4 captcha.
* @param {string} params.captcha_id Required parameter. Value of `captcha_id` parameter you found on target website.
* @param {string} params.risk_type An optional param. Value of `risk_type` parameter is contained in the captcha loading request.
* @param {string} params.pingback An optional param. [More info here](https://2captcha.com/2captcha-api#pingback).
* @param {string} params.proxy An optional param. Format: `login:password@123.123.123.123:3128`. You can find more info about proxies [here](https://2captcha.com/2captcha-api#proxies).
* @param {string} params.proxytype An optional param. Type of your proxy: `HTTP`, `HTTPS`, `SOCKS4`, `SOCKS5`.
* @param {string} params.userAgent An optional param. Your `userAgent` that will be passed to our worker and used to solve the captcha.
*
* @returns {Promise<CaptchaAnswer>} The result from the solve.
* @throws APIError
* @example
* solver.geetestV4({
* pageurl: 'https://2captcha.com/demo/geetest-v4',
* captcha_id: 'e392e1d7fd421dc63325744d5a2b9c73'
* })
* .then((res) => {
* console.log(res)
* })
* .catch((err) => {
* console.log(err);
* })
*/
public async geetestV4(params: paramsGeeTestV4): Promise<CaptchaAnswer> {
checkCaptchaParams(params, "geetest_v4")
const payload = {
...params,
method: "geetest_v4",
...this.defaultPayload
}
const response = await fetch(this.in + utils.objectToURI(payload))
const result = await response.text()
let data;
try {
data = JSON.parse(result)
} catch {
throw new APIError(result)
}
if (data.status == 1) {
return this.pollResponse(data.request)
} else {
throw new APIError(data.request)
}
}
/**
* ### Method for sending Yandex Smart Captcha.
*
* This method accepts an object with the following fields: `pageurl`, `sitekey`, `pingback`, `proxy`, `proxytype`.
* The `pageurl` and `sitekey` fields are required.
*
* @param {{pageurl, sitekey, pingback, proxy, proxytype, userAgent}} params The method takes arguments as an object.
* @param {string} params.pageurl Required parameter. URL of the page where the captcha is located.
* @param {string} params.sitekey Required parameter. The `sitekey` value you found on the captcha page.
* @param {string} params.pingback An optional param.
* @param {string} params.proxy An optional param. Format: `login:password@123.123.123.123:3128`.
* @param {string} params.proxytype An optional param. Type of your proxy: `HTTP`, `HTTPS`, `SOCKS4`, `SOCKS5`.
* @param {string} params.userAgent An optional param. Your `userAgent` that will be passed to our worker and used to solve the captcha.
*
* @returns {Promise<CaptchaAnswer>} The result from the solve.
* @throws APIError
* @example
* solver.yandexSmart({
* pageurl: "https://captcha-api.yandex.ru/demo",
* sitekey: "FEXfAbHQsToo97VidNVk3j4dC74nGW1DgdxjtNB9"
* })
* .then((res) => {
* console.log(res)
* })
* .catch((err) => {
* console.log(err);
* })
*/
public async yandexSmart(params: yandexSmart): Promise<CaptchaAnswer> {
checkCaptchaParams(params, "yandex")
const payload = {
...params,
method: "yandex",
...this.defaultPayload
}
const response = await fetch(this.in + utils.objectToURI(payload))
const result = await response.text()
let data;
try {
data = JSON.parse(result)
} catch {
throw new APIError(result)
}
if (data.status == 1) {
return this.pollResponse(data.request)
} else {
throw new APIError(data.request)
}
}
/**
* ### Solves a image-based captcha.
*
* [Read more about parameters for image captcha](https://2captcha.com/2captcha-api#solving_normal_captcha).
*
* @param {{ body,
* phrase,
* regsense,
* numeric,
* calc,
* min_len,
* max_len,
* language,
* lang,
* textinstructions,
* pingback }} params Extra properties to pass to 2captcha.
* @param {string} params.body Base64 image data for the captcha.
* @param {number} params.phrase Captcha contains two or more words? `1` - Yes. `0` - No.
* @param {number} params.regsense Captcha is case sensitive? `1` - Yes. `0` - No.
* @param {number} params.numeric `0` - not specified. `1` - captcha contains only numbers. `2` - captcha contains only letters. `3` - captcha contains only numbers OR only letters. `4` - captcha MUST contain both numbers AND letters.
* @param {number} params.calc Does captcha require calculations? (e.g. type the result 4 + 8 = ) `1` - Yes. `0` - No.
* @param {number} params.min_len `1..20` - minimal number of symbols in captcha. `0` - not specified.
* @param {number} params.max_len `1..20` - maximal number of symbols in captcha. `0` - not specified.
* @param {number} params.language `0` - not specified. `1` - Cyrillic captcha. `2` - Latin captcha
* @param {string} params.lang Language code. [See the list of supported languages](https://2captcha.com/2captcha-api#language).
* @param {string} params.textinstructions Text will be shown to worker to help him to solve the captcha correctly. For example: type red symbols only.
* @param {string} params.pingback URL for `pingback` (callback) response that will be sent when captcha is solved. [More info here](https://2captcha.com/2captcha-api#pingback).
*
* @returns {Promise<CaptchaAnswer>} The result from the solve
* @throws APIError
* @example
* const imageBase64 = fs.readFileSync("./tests/media/imageCaptcha_6e584.png", "base64")
*
* solver.imageCaptcha({
* body: imageBase64,
* numeric: 4,
* min_len: 5,
* max_len: 5
* })
* .then((res) => {
* console.log(res);
* })
* .catch((err) => {
* console.log(err);
* })
*/
public async imageCaptcha( params: paramsImageCaptcha ): Promise<CaptchaAnswer> {
checkCaptchaParams(params, "base64")
const payload = {
...this.defaultPayload,
...params,
method: "base64"
}
const URL = this.in
const response = await fetch(URL, {
body: JSON.stringify( payload ),
method: "post",
headers: {'Content-Type': 'application/json'}
})
const result = await response.text()
let data;
try {
data = JSON.parse(result)
} catch {
throw new APIError(result)
}
if (data.status == 1) {
return this.pollResponse(data.request)
} else {
throw new APIError(data.request)
}
}
/**
* ### Solves Arkose Labs FunCaptcha.
*
* [Read more](https://2captcha.com/2captcha-api#solving_funcaptcha_new) about other solving and other parameters for Arkose Labs FunCaptcha.
*
* @param {{pageurl, publicKey, surl, data, pingback, proxy, proxytype, userAgent}} params Object
* @param {string} params.publicKey The FunCaptcha Public Key
* @param {string} params.pageurl The URL to the website the captcha is seen on
* @param {string} params.surl The FunCaptcha Service URL (recommended)
* @param {string} params.data Custom data to pass to FunCaptcha. For example: `'data': '{"blob": "foo"}'`.
* @param {string} params.pingback URL for pingback (callback) response that will be sent when captcha is solved. URL should be registered on the server. [More info here](https://2captcha.com/2captcha-api#pingback).
* @param {string} params.proxy Format: `login:password@123.123.123.123:3128` You can find more info about proxies [here](https://2captcha.com/2captcha-api#proxies).
* @param {string} params.proxytype Type of your proxy: `HTTP`, `HTTPS`, `SOCKS4`, `SOCKS5`.
* @param {string} params.userAgent Your `userAgent` that will be passed to our worker and used to solve the captcha.
*
* @returns {Promise<CaptchaAnswer>} The result from the solve
* @throws APIError
*
* @example
* solver.funCaptcha({
* pageurl: "https://funcaptcha.com/tile-game-lite-mode/fc/api/nojs/?pkey=804380F4-6844-FFA1-ED4E-5877CA1F1EA4&lang=en",
* publickey: "804380F4-6844-FFA1-ED4E-5877CA1F1EA4"
* })
* .then((res) => {
* console.log(res);
* })
* .catch((err) => {
* console.log(err);
* })
*/
public async funCaptcha(params: paramsFunCaptcha): Promise<CaptchaAnswer> {
checkCaptchaParams(params, "funcaptcha")
const payload = {
...this.defaultPayload,
...params,
method: "funcaptcha",
}
const response = await fetch(this.in + utils.objectToURI(payload))
const result = await response.text()
let data;
try {
data = JSON.parse(result)
} catch {
throw new APIError(result)
}
if (data.status == 1) {
return this.pollResponse(data.request)
} else {
throw new APIError(data.request)
}
}
/**
*
* ### Solves a Lemin captcha
*
* [Read more about other Lemin captcha parameters](https://2captcha.com/2captcha-api#lemin).
*
* @param {{ pageurl, captcha_id, div_id, api_server, pingback, proxy, proxytype}} params Object
* @param {string} params.pageurl The URL the captcha appears on.
* @param {string} params.captcha_id Value of `captcha_id` parameter you found on page.
* @param {string} params.div_id Value `id` of captcha pareent `<div></div>` element.
* @param {string} params.api_server The domain part of script URL you found on page. Default value: `https://api.leminnow.com/`
* @param {string} params.pingback URL for pingback (callback) response that will be sent when captcha is solved. URL should be registered on the server. [More info here](https://2captcha.com/2captcha-api#pingback).
* @param {string} params.proxy Format: `login:password@123.123.123.123:3128` You can find more info about proxies [here](https://2captcha.com/2captcha-api#proxies).
* @param {string} params.proxytype Type of your proxy: `HTTP`, `HTTPS`, `SOCKS4`, `SOCKS5`.
*
* @example
* solver.lemin({
* pageurl:'https://2captcha.com/demo/lemin',
* captcha_id: 'CROPPED_3dfdd5c_d1872b526b794d83ba3b365eb15a200b',
* div_id: 'lemin-cropped-captcha',
* api_server: 'api.leminnow.com'
* })
* .then((res) => {
* console.log(res);
* })
* .catch((err) => {
* console.log(err);
* })
*/
public async lemin(params: paramsLemin): Promise<CaptchaAnswer> {
checkCaptchaParams(params, "lemin")
const payload = {
...this.defaultPayload,
...params,
method: "lemin",
}
const response = await fetch(this.in + utils.objectToURI(payload))
const result = await response.text()
let data;
try {
data = JSON.parse(result)
} catch {
throw new APIError(result)
}
if (data.status == 1) {
return this.pollResponse(data.request)
} else {
throw new APIError(data.request)
}
}
/**
*
* ### Solves Amazon WAF captcha
*
* [Read more about "Amazon WAF" captcha](https://2captcha.com/2captcha-api#amazon-waf).
*
* @param {{ pageurl, sitekey, iv, context, challenge_script, captcha_script, pingback, proxy, proxytype}} params The `amazonWaf` method takes arguments as an object. Thus, the `pageurl`, `sitekey`, `iv`, `context` fields in the passed object are mandatory.
* @param {string} params.pageurl Is the full `URL` of page where you were challenged by the captcha.
* @param {string} params.sitekey Is a value of `key` parameter in the page source.
* @param {string} params.iv Is a value of `iv` parameter in the page source.
* @param {string} params.context Is a value of `context` parameter in the page source.
* @param {string} params.challenge_script The source URL of `challenge.js` script on the page.
* @param {string} params.captcha_script The source URL of `captcha.js` script on the page.
* @param {string} params.pingback URL for pingback (callback) response that will be sent when captcha is solved. URL should be registered on the server. [More info here](https://2captcha.com/2captcha-api#pingback).
* @param {string} params.proxy Format: `login:password@123.123.123.123:3128` You can find more info about proxies [here](https://2captcha.com/2captcha-api#proxies).
* @param {string} params.proxytype Type of your proxy: `HTTP`, `HTTPS`, `SOCKS4`, `SOCKS5`.
*
* @example
* solver.amazonWaf({
* pageurl: "https://non-existent-example.execute-api.us-east-1.amazonaws.com/latest",
* sitekey: "AQIDAHjcYu/GjX+QlghicBgQ/7bFaQZ+m5FKCMDnO+vTbNg96AHMDLodoefdvyOnsHMRtEKQAAAAfjB8BgkqhkiG9w0BBwagbzBtAgEAMGgGCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQMUX+ZqwwuANRnZujSAgEQgDvHSxUQmVBuyUtumoW2n4ccTG7xQN1r3X/zz41qmQaYv9SSSvQrjIoDXKaUQ23tVb4ii8+uljuRdz/HPA==",
* context: "9BUgmlm48F92WUoqv97a49ZuEJJ50TCk9MVr3C7WMtQ0X6flVbufM4n8mjFLmbLVAPgaQ1Jydeaja94iAS49ljb+sUNLoukWedAQZKrlY4RdbOOzvcFqmD/ZepQFS9N5w15Exr4VwnVq+HIxTsDJwRviElWCdzKDebN/mk8/eX2n7qJi5G3Riq0tdQw9+C4diFZU5E97RSeahejOAAJTDqduqW6uLw9NsjJBkDRBlRjxjn5CaMMo5pYOxYbGrM8Un1JH5DMOLeXbq1xWbC17YSEoM1cRFfTgOoc+VpCe36Ai9Kc=",
* iv: "CgAHbCe2GgAAAAAj",
* })
* .then((res) => {
* console.log(res);
* })
* .catch((err) => {
* console.log(err);
* })
*/
public async amazonWaf(params: paramsAmazonWAF): Promise<CaptchaAnswer> {
checkCaptchaParams(params, "amazon_waf")
const payload = {
...this.defaultPayload,
...params,
method: "amazon_waf",
}
const response = await fetch(this.in + utils.objectToURI(payload))
const result = await response.text()
let data;
try {
data = JSON.parse(result)
} catch {
throw new APIError(result)
}
if (data.status == 1) {
return this.pollResponse(data.request)
} else {
throw new APIError(data.request)
}
}
/**
*
* ### Solves Cloudflare Turnstile captcha
*
* [Read more about Cloudflare Turnstile captcha](https://2captcha.com/2captcha-api#turnstile).
*
* @param {{ pageurl, sitekey, action, data, pingback, proxy, proxytype}} params The `cloudflareTurnstile` method takes arguments as an object. Thus, the `pageurl`, `sitekey` fields in the passed object are mandatory.
* @param {string} params.pageurl Full `URL` of the page where you see the captcha.
* @param {string} params.sitekey Is a value of `sitekey` parameter in the page source.
* @param {string} params.action Value of optional `action` parameter you found on page.
* @param {string} params.data Value of optional `data` parameter you found on page.
* @param {string} params.pingback URL for pingback (callback) response that will be sent when captcha is solved. URL should be registered on the server. [More info here](https://2captcha.com/2captcha-api#pingback).
* @param {string} params.proxy Format: `login:password@123.123.123.123:3128` You can find more info about proxies [here](https://2captcha.com/2captcha-api#proxies).
* @param {string} params.proxytype Type of your proxy: `HTTP`, `HTTPS`, `SOCKS4`, `SOCKS5`.
*
* @example
* solver.cloudflareTurnstile({
* pageurl: "https://app.nodecraft.com/login",
* sitekey: "0x4AAAAAAAAkg0s3VIOD10y4"
* })
* .then((res) => {
* console.log(res);
* })
* .catch((err) => {