stealify / request-cloudflare

request module that supports cloudflare anti ddos pages
Apache License 2.0
12 stars 3 forks source link

request-cloudflare

Node.js library to bypass cloudflare's anti-ddos page.

If the page you want to access is protected by CloudFlare, it will return special page, which expects client to support Javascript to solve challenge.

This small library encapsulates logic which extracts challenge, solves it, submits and returns the request page body.

You can use request-cloudflare even if you are not sure if CloudFlare protection is turned on.

In general, CloudFlare has 4 types of common anti-bot pages:

Unfortunatelly there is no solution, if website is protected by captcha.

If you notice that for some reason request-cloudflare stopped to work, do not hesitate and get in touch with me ( by creating an issue here, for example), so i can update it.

Install

npm install request-cloudflare

Usage

const { requestCloudflare } = require('request-cloudflare');
//API Promises
requestCloudflare.promises.get('http://website.com/').then(console.log)

// API Callback
requestCloudflare.get('http://website.com/', function(error, response, body) {
  if (error) {
    console.log('Error occurred');
  } else {
    console.log(body, response);
  }
});

or for POST action:

requestCloudflare.post('http://website.com/', {field1: 'value', field2: 2}, function(error, response, body) {
  //...
});

A generic request can be made with requestCloudflare.request(options, callback). The options object should follow request's options. Not everything is supported however, for example http methods other than GET and POST. If you wanted to request an image in binary data you could use the encoding option:

requestCloudflare.request({
    method: 'GET',
    url:'http://website.com/image',
    encoding: null, //=>utf8
    challengesToSolve: 3, // optional, if CF returns challenge after challenge, how many to solve before failing
    followAllRedirects: true, // mandatory for successful challenge solution
  }, function(err, response, body) {
    //body is now a buffer object instead of a string
});

Error object

Error object has following structure:

var error = {errorType: 0, error:...};

Where errorType can be following:

Running tests

Unknown error? Library stopped working?

Let me know, by opening issue in this repo and i will update library asap. Please, provide url and body of page where request-cloudflare failed.

request-cloudflare uses Request to perform requests.

WAT

Current cloudflare implementation requires browser to respect the timeout of 5 seconds and request-cloudflare mimics this behaviour. So everytime you call requestCloudflare.get you should expect it to return result after 5+ seconds.

TODO

Kudos to contributors

Dependencies

This library is inspired by python module cloudflare-scrape;