Skip to content

Commit ba9d55d

Browse files
authored
added altcha captcha method to 2captcha.ts, implemented altcha-specific parameter validation in checkCaptchaParams including exact-one-of challenge_url or challenge_json, and added example and README description (#28)
1 parent afed028 commit ba9d55d

5 files changed

Lines changed: 128 additions & 1 deletion

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Examples of API requests for different captcha types are available on the [JavaS
4747
- [VkImage](#vkimage)
4848
- [VkCaptcha](#vkcaptcha)
4949
- [Temu](#temu)
50+
- [Altcha](#altcha)
5051
- [Audio Captcha](#audio-captcha)
5152
- [Other methods](#other-methods)
5253
- [goodReport](#goodreport)
@@ -737,6 +738,25 @@ console.log(err);
737738
})
738739
```
739740

741+
### Altcha
742+
743+
<sup>[API method description.](https://2captcha.com/2captcha-api#altchacaptcha)</sup>
744+
745+
This method can be used to solve Altcha captcha. Returns a token.
746+
747+
```js
748+
solver.altcha({
749+
pageurl: "https://mysite.com/page/with/altcha",
750+
challenge_url: "https://example/altcha",
751+
})
752+
.then((res) => {
753+
console.log(res);
754+
})
755+
.catch((err) => {
756+
console.log(err);
757+
})
758+
```
759+
740760
### Audio Captcha
741761

742762
<sup>[API method description.](https://2captcha.com/2captcha-api#audio-recognition)</sup>

examples/altcha.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const TwoCaptcha = require("../dist/index.js");
2+
require('dotenv').config();
3+
const APIKEY = process.env.APIKEY
4+
const solver = new TwoCaptcha.Solver(APIKEY);
5+
6+
solver.altcha({
7+
pageurl: "https://mysite.com/page/with/altcha",
8+
challenge_url: "https://example/altcha",
9+
// challenge_json: '{"algorithm":"SHA-256","challenge":"a4c9d8e7f1b23a6c...",..."signature":"7b3e2a9d5c8f1046e2d91c3a..."}'
10+
})
11+
.then((res) => {
12+
console.log(res);
13+
})
14+
.catch((err) => {
15+
console.log(err);
16+
})

src/structs/2captcha.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ export interface paramsTemu {
302302
part3: string,
303303
}
304304

305+
export interface paramsAltcha {
306+
pageurl: string,
307+
challengeUrl?: string,
308+
challengeJson?: string,
309+
proxy?: string,
310+
proxytype?: string,
311+
}
312+
305313
export interface paramsAudioCaptcha {
306314
body: string,
307315
lang: string,
@@ -2144,6 +2152,64 @@ public async temu(params: paramsTemu): Promise<CaptchaAnswer> {
21442152
}
21452153
}
21462154

2155+
/**
2156+
* ### Solves Altcha Captcha
2157+
*
2158+
* This method can be used to solve Altcha captcha. Returns a token.
2159+
* [Read more about Altcha Method](https://2captcha.com/2captcha-api#altchacaptcha).
2160+
*
2161+
* @param {{ pageurl, challenge_url, challenge_json, proxy, proxytype}} params Parameters Altcha as an object.
2162+
* @param {string} params.pageurl Full URL of the page where you solve the captcha.
2163+
* @param {string} params.challenge_url The value of the 'challenge_url' parameter for the 'altcha-widget' element containing the captcha on the page. You can send either challenge_url or challenge_json parameter, but not two of it simultaneously.
2164+
* @param {string} params.challenge_json The contents of the file from the 'challenge_url' parameter. You can send either challenge_url or challenge_json parameter, but not two of it simultaneously.
2165+
* @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).
2166+
* @param {string} params.proxytype Type of your proxy: `HTTP`, `HTTPS`, `SOCKS4`, `SOCKS5`.
2167+
*
2168+
* @example
2169+
* solver.altcha({
2170+
* pageurl: "https://mysite.com/page/with/altcha",
2171+
* challenge_url: "https://example/altcha",
2172+
* })
2173+
* .then((res) => {
2174+
* console.log(res);
2175+
* })
2176+
* .catch((err) => {
2177+
* console.log(err);
2178+
* })
2179+
*/
2180+
2181+
public async altcha(params: paramsAltcha): Promise<CaptchaAnswer> {
2182+
params = renameParams(params)
2183+
2184+
checkCaptchaParams(params, "altcha")
2185+
2186+
const payload = {
2187+
...this.defaultPayload,
2188+
...params,
2189+
method: "altcha",
2190+
};
2191+
2192+
const response = await fetch(this.in, {
2193+
body: JSON.stringify(payload),
2194+
method: "post",
2195+
headers: { "Content-Type": "application/json" }
2196+
})
2197+
const result = await response.text()
2198+
2199+
let data;
2200+
try {
2201+
data = JSON.parse(result)
2202+
} catch {
2203+
throw new APIError(result)
2204+
}
2205+
2206+
if (data.status == 1) {
2207+
return this.pollResponse(data.request)
2208+
} else {
2209+
throw new APIError(data.request)
2210+
}
2211+
}
2212+
21472213
/**
21482214
* ### Method for solving Audio captcha.
21492215
*

src/utils/checkCaptchaParams.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Captcha methods for which parameter checking is available
22
const supportedMethods = ["userrecaptcha", "hcaptcha", "geetest", "geetest_v4","yandex","funcaptcha","lemin","amazon_waf",
33
"turnstile", "base64", "capy","datadome", "cybersiara", "mt_captcha", "bounding_box", 'friendly_captcha', 'grid',
4-
'textcaptcha', 'canvas', 'rotatecaptcha', 'keycaptcha', 'cutcaptcha', 'tencent', 'atb_captcha', 'prosopo', 'captchafox', 'vkimage', 'vkcaptcha', 'temu', 'audio']
4+
'textcaptcha', 'canvas', 'rotatecaptcha', 'keycaptcha', 'cutcaptcha', 'tencent', 'atb_captcha', 'prosopo', 'captchafox', 'vkimage', 'vkcaptcha', 'temu', 'altcha', 'audio']
55

66
// Names of required fields that must be contained in the parameters captcha
77
const recaptchaRequiredFields = ['pageurl','googlekey']
@@ -34,6 +34,7 @@ const captchaFoxRequiredFields = ['pageurl', 'sitekey', 'userAgent', 'proxy', '
3434
const vkimageRequiredFields = ['body', 'steps']
3535
const vkcaptchaRequiredFields = ['redirect_uri', 'userAgent', 'proxy', 'proxytype']
3636
const temuRequiredFields = ['body', 'part1', 'part2', 'part3']
37+
const altchaRequiredFields = ['pageurl']
3738
const audioRequiredFields = ['body', 'lang']
3839

3940
/**
@@ -133,6 +134,9 @@ const getRequiredFildsArr = (method: string):Array<string> => {
133134
case "temu":
134135
requiredFieldsArr = temuRequiredFields
135136
break;
137+
case "altcha":
138+
requiredFieldsArr = altchaRequiredFields
139+
break;
136140
case "audio":
137141
requiredFieldsArr = audioRequiredFields
138142
break;
@@ -176,6 +180,23 @@ export default function checkCaptchaParams(params: Object, method: string) {
176180
}
177181
})
178182

183+
if(method === "altcha") {
184+
const hasChallengeUrl = params.hasOwnProperty('challenge_url')
185+
const hasChallengeJson = params.hasOwnProperty('challenge_json')
186+
187+
if(!hasChallengeUrl && !hasChallengeJson) {
188+
isCorrectCaptchaParams = false
189+
throw new Error(`Error when check params captcha.\nNot found "challenge_url" or "challenge_json" field in the Object. One of this field is required for "${method}" method. Please add field "challenge_url" or "challenge_json" to captcha parameters.`)
190+
}
191+
192+
if(hasChallengeUrl && hasChallengeJson) {
193+
isCorrectCaptchaParams = false
194+
throw new Error(`Error when check params captcha.\nYou must provide exactly one of "challenge_url" or "challenge_json" for "${method}" method.`)
195+
}
196+
197+
isCorrectCaptchaParams = true
198+
}
199+
179200
//The parameters `textinstructions` and `imginstructions` are mandatory for the methods `bounding_box`, `grid`, and `canvas`.
180201
if(method === "bounding_box" || method === "grid" || method === "canvas") {
181202
if(params.hasOwnProperty('textinstructions') || params.hasOwnProperty('imginstructions')) {

src/utils/renameParams.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export default function renameParams(params: any) {
3939

4040
// atbCAPTCHA
4141
"apiServer": "api_server",
42+
43+
// Altcha
44+
"challengeUrl": "challenge_url",
45+
"challengeJson": "challenge_json",
4246
}
4347

4448
for(let key in params) {

0 commit comments

Comments
 (0)