GithubHelp home page GithubHelp logo

dore51 / puppeteer-recaptcha-solver Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 0.0 760 KB

Puppeteer Recaptcha solver

Home Page: https://www.npmjs.com/package/puppeteer-recaptcha-solver

License: MIT License

TypeScript 100.00%
captcha puppeteer-recaptcha recaptcha recaptcha-solver puppeteer-captcha puppeteer

puppeteer-recaptcha-solver's Introduction

puppeteer-recaptcha-solver

Google Recapctha v2 solver with puppeteer. You can simply use it in your project by passing to the constructor your Page object. The solver is using SpeechToText recognition, you can use one of our integrated solvers with your API key or to provide your own solving function. You can also integrate your own logger.

Disclaimer

This is an academic project, it is not intended to be used in production. It is not recommended to use this project for any other purpose than educational. The author is not responsible for any misuse of this project.

Demo

demo.mp4

Table of contents

Prerequisites

This project requires NodeJS (version 8 or later) and NPM. Node and NPM are really easy to install.

Getting Started

These instructions will help to you install the package in your project, set it and use it. See Contributing for notes on how to help and contribute this project.

Installation

BEFORE YOU INSTALL: please read the prerequisites

To install and set up the library, run:

$ npm install puppeteer-recaptcha-solver

Usage

To use, simply create the object and execute the solve command.

Example:

(async () => {
    const browser = await puppeteer.launch({
        headless: false,
    });
    const page = await browser.newPage();

    const solver = new ReCaptchaSolver({
        page,
        maxRetries: 3,
        transcriber: Transcribers.witAI,
        apiKey: 'YOUR_API_KEY'
    });

    await page.goto(
        'https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php'
    );

    const solved = await solver.solve();

    console.log('Captcha solved: ', solved);
    await page.screenshot({ path: 'example/example.png' });
    await browser.close();
})();

API

Constructor

 const solver = new ReCaptchaSolver({
      page,
      log,
      maxRetries: 3,
      transcriber: Transcribers.witAI,
      apiKey: 'YOUR_API_KEY'
});

A constructor to the object.

Fields

Supported options for the constructor field are listed below.

Field Type Default value Required Description
page Page Yes puppeteer page object
log Logger console.log No A logger that the solver will use. You can also use the default logger or noopLogger to disable the logs
transcriber Transcriber witAI No A transcriber that the solver will use to transcriber the audio to text. You can can choose between witAI or googleSpeechToText by passing Transcribers.witAI or Transcribers.googeSpeechToText or passing you own Transcriber function.
maxRetries number 3 No Total number of retries until the captcha is solved
apiKey string No API key to your transcribe service

Solve

const solved: boolean = await solver.solve();

A command that will start the solving process. Returns a Promise<boolean> to indicate if the captcha successfully solved.

General Types

Type Signature Description
Logger
interface Logger {
log(message: string): void | Promise<void>;
error(message: string): void | Promise<void>;
warn(message: string): void | Promise<void>;
info(message: string): void | Promise<void>;
debug(message: string): void | Promise<void>;
}
A logger object that the solver will use.
Transcriber
type Transcriber = (
audioBuffer: ArrayBuffer,
apiKey?: string
) => Promise<string | null>;
A transcribe function that gets an ArrayBuffer and should return the text

Examples

default Logger

const defaultLogger: Logger = {
    log: (message: string) => console.log('[LOG]', message),
    error: (message: string) => console.error('[ERROR]', message),
    warn: (message: string) => console.warn('[WARN]', message),
    info: (message: string) => console.info('[INFO]', message),
    debug: (message: string) => console.debug('[DEBUG]', message),
};

witAI Transcriber

const witAI: Transcriber = async (
    audioBuffer: ArrayBuffer,
    apiKey?: string
) => {
    if (!apiKey) {
        throw new Error('witAI transcriber requires API key');
    }

    const { data } = await axios.post<string>(
        'https://api.wit.ai/speech?v=20220622',
        audioBuffer,
        {
            headers: {
                Authorization: `Bearer ${apiKey}`,
                'Content-Type': 'audio/mpeg3',
            },
        }
    );

    const parsed =
        typeof data === 'string'
            ? JSON.parse(data.split('\r\n').slice(-1)[0] || '{}')
            : data;

    return parsed?.text;
}

Google SpeechToText Transcriber

const googleSpeechToText: Transcriber = async (
    audioBuffer: ArrayBuffer,
    apiKey?: string
) => {
    if (!apiKey) {
        throw new Error('googleSpeechToText transcriber requires API key');
    }

    const { data } = await axios.post<string>(
        `https://speech.googleapis.com/v1p1beta1/speech:recognize?key=${apiKey}`,
        {
            config: {
                encoding: 'MP3',
                sampleRateHertz: 16000,
                languageCode: 'en-US',
            },
            audio: {
                content: Buffer.from(audioBuffer).toString('base64'),
            },
        }
    );

    const parsed =
        typeof data === 'string'
            ? JSON.parse(data.split('\r\n').slice(-1)[0] || '{}')
            : data;

    return parsed?.results?.[0]?.alternatives?.[0]?.transcript;
};

Contributing

Start with cloning this repo on your local machine:

$ git clone https://github.com/dore51/puppeteer-captcha-solver.git
$ cd puppeteer-captcha-solver

To install and set up the library, run:

$ npm install

To check that everything works

$ npm run example

Running the tests

$ npm test

Building a distribution version

$ npm run build

This task will create a distribution version of the project inside your local lib/ folder

publishing the distribution version

$ npm publish

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Add your changes: git add .
  4. Commit your changes: git commit -am 'Add some feature'
  5. Push to the branch: git push origin my-new-feature
  6. Submit a pull request ๐Ÿ˜Ž

Built With

This package has the following dependencies:

  • Axios: A promise-based HTTP client for the browser and Node.js. Axios is used to make HTTP requests in the package.

The following dependencies are only required for development and testing purposes:

  • Node.js: A JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js is required to run the package.
  • Puppeteer: A Node library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer is used to automate and control the browser in order to solve the reCAPTCHA challenge.
  • Prettier: A code formatter that enforces a consistent style across the codebase.
  • Jest: A testing framework for JavaScript.
  • puppeteer-screen-recorder: A utility for recording screencasts of a Puppeteer page.
  • TSLint: A static analysis tool that checks TypeScript code for readability, maintainability, and functionality errors.

Authors

See also the list of contributors who participated in this project.

License

MIT License ยฉ Dor Eitan

puppeteer-recaptcha-solver's People

Contributors

dependabot[bot] avatar dore51 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

puppeteer-recaptcha-solver's Issues

Customise iframe selectors

The library has no option for passing/customizing the selectors of the iframe selectors.
For example, customizing the api2/anchor selector to anchor.

One should add the option to pass custom selectors for both iframe selectors.
The selectors can be found at src/puppeteer/selectors.ts
They are currently static read-only but that mechanism can be changed to another implementation.

Failed to open iframe

When try with a different site used on the example we found problems in order to go into the iframe:

[INFO] Starting to solve captcha
[DEBUG] switching to captcha anchor iframe
[DEBUG] waiting for iframe "captcha iframe"
[DEBUG] found iframe "captcha iframe"
[DEBUG] waiting for sub element "captcha checkbox"
[ERROR] Failed to switch to iframe "captcha iframe": TimeoutError: Waiting for selector `#recaptcha-anchor` failed: Waiting failed: 5000ms exceeded
[ERROR] Failed to solve captcha: TimeoutError: Waiting for selector `#recaptcha-anchor` failed: Waiting failed: 5000ms exceeded
Captcha solved:  false

Looking the code both captchas look identical

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.