Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Examples of API requests for different captcha types are available on the [JavaS
- [VkImage](#vkimage)
- [VkCaptcha](#vkcaptcha)
- [Temu](#temu)
- [Altcha](#altcha)
- [Audio Captcha](#audio-captcha)
- [Other methods](#other-methods)
- [goodReport](#goodreport)
Expand Down Expand Up @@ -737,6 +738,25 @@ console.log(err);
})
```

### Altcha

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

This method can be used to solve Altcha captcha. Returns a token.

```js
solver.altcha({
pageurl: "https://mysite.com/page/with/altcha",
challenge_url: "https://example/altcha",
})
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
```

### Audio Captcha

<sup>[API method description.](https://2captcha.com/2captcha-api#audio-recognition)</sup>
Expand Down
16 changes: 16 additions & 0 deletions examples/altcha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const TwoCaptcha = require("../dist/index.js");
require('dotenv').config();
const APIKEY = process.env.APIKEY
const solver = new TwoCaptcha.Solver(APIKEY);

solver.altcha({
pageurl: "https://mysite.com/page/with/altcha",
challenge_url: "https://example/altcha",
// challenge_json: '{"algorithm":"SHA-256","challenge":"a4c9d8e7f1b23a6c...",..."signature":"7b3e2a9d5c8f1046e2d91c3a..."}'
})
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
66 changes: 66 additions & 0 deletions src/structs/2captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ export interface paramsTemu {
part3: string,
}

export interface paramsAltcha {
pageurl: string,
challengeUrl?: string,
challengeJson?: string,
proxy?: string,
proxytype?: string,
}

export interface paramsAudioCaptcha {
body: string,
lang: string,
Expand Down Expand Up @@ -2144,6 +2152,64 @@ public async temu(params: paramsTemu): Promise<CaptchaAnswer> {
}
}

/**
* ### Solves Altcha Captcha
*
* This method can be used to solve Altcha captcha. Returns a token.
* [Read more about Altcha Method](https://2captcha.com/2captcha-api#altchacaptcha).
*
* @param {{ pageurl, challenge_url, challenge_json, proxy, proxytype}} params Parameters Altcha as an object.
* @param {string} params.pageurl Full URL of the page where you solve the captcha.
* @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.
* @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.
* @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.altcha({
* pageurl: "https://mysite.com/page/with/altcha",
* challenge_url: "https://example/altcha",
* })
* .then((res) => {
* console.log(res);
* })
* .catch((err) => {
* console.log(err);
* })
*/

public async altcha(params: paramsAltcha): Promise<CaptchaAnswer> {
params = renameParams(params)

checkCaptchaParams(params, "altcha")

const payload = {
...this.defaultPayload,
...params,
method: "altcha",
};

const response = await fetch(this.in, {
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)
}
}

/**
* ### Method for solving Audio captcha.
*
Expand Down
23 changes: 22 additions & 1 deletion src/utils/checkCaptchaParams.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Captcha methods for which parameter checking is available
const supportedMethods = ["userrecaptcha", "hcaptcha", "geetest", "geetest_v4","yandex","funcaptcha","lemin","amazon_waf",
"turnstile", "base64", "capy","datadome", "cybersiara", "mt_captcha", "bounding_box", 'friendly_captcha', 'grid',
'textcaptcha', 'canvas', 'rotatecaptcha', 'keycaptcha', 'cutcaptcha', 'tencent', 'atb_captcha', 'prosopo', 'captchafox', 'vkimage', 'vkcaptcha', 'temu', 'audio']
'textcaptcha', 'canvas', 'rotatecaptcha', 'keycaptcha', 'cutcaptcha', 'tencent', 'atb_captcha', 'prosopo', 'captchafox', 'vkimage', 'vkcaptcha', 'temu', 'altcha', 'audio']

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

/**
Expand Down Expand Up @@ -133,6 +134,9 @@ const getRequiredFildsArr = (method: string):Array<string> => {
case "temu":
requiredFieldsArr = temuRequiredFields
break;
case "altcha":
requiredFieldsArr = altchaRequiredFields
break;
case "audio":
requiredFieldsArr = audioRequiredFields
break;
Expand Down Expand Up @@ -176,6 +180,23 @@ export default function checkCaptchaParams(params: Object, method: string) {
}
})

if(method === "altcha") {
const hasChallengeUrl = params.hasOwnProperty('challenge_url')
const hasChallengeJson = params.hasOwnProperty('challenge_json')

if(!hasChallengeUrl && !hasChallengeJson) {
isCorrectCaptchaParams = false
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.`)
}

if(hasChallengeUrl && hasChallengeJson) {
isCorrectCaptchaParams = false
throw new Error(`Error when check params captcha.\nYou must provide exactly one of "challenge_url" or "challenge_json" for "${method}" method.`)
}

isCorrectCaptchaParams = true
}

//The parameters `textinstructions` and `imginstructions` are mandatory for the methods `bounding_box`, `grid`, and `canvas`.
if(method === "bounding_box" || method === "grid" || method === "canvas") {
if(params.hasOwnProperty('textinstructions') || params.hasOwnProperty('imginstructions')) {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/renameParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export default function renameParams(params: any) {

// atbCAPTCHA
"apiServer": "api_server",

// Altcha
"challengeUrl": "challenge_url",
"challengeJson": "challenge_json",
}

for(let key in params) {
Expand Down