#cloudflare #bypass

cloudflare-bypasser

A crate to bypass Cloudflare’s anti-bot page, inspired by python module cloudflare-scrape

6 releases

✓ Uses Rust 2018 edition

0.1.5 May 14, 2019
0.1.4 Feb 11, 2019

37 downloads per month

MIT/Apache

12KB
196 lines

Intro

cloudflare-bypasser

Inspired by python module cloudflare-scrape

Require

  • Node.js

Example

extern crate cloudflare_bypasser;
extern crate reqwest;

fn main() {
    const WEBSITE: &'static str = "https://example.com";

    // quick start
    let mut bypasser = {
        cloudflare_nypasser::Bypasser::new()
    };

    // customize
    let mut bypasser = {
        cloudflare_bypasser::Bypasser::new()
            .retry(30)                     // retry times, it might be 10000, depends on your network environment, default 0 (infinity)
            .proxy("http://127.0.0.1:1087") // use proxy, default None
            .random_user_agent(true)        // use random user agent, default false
            .user_agent("Mozilla/5.0")      // specify user agent manually, default ""
            .wait(5);                       // cloudflare's waiting time, but in my test it can be 0, default 0
    };                           

    // to pass the verify both of the cookie and user agent are needed
    let (cookie, user_agent);
        loop {
            if let Ok((c, ua)) =  bypasser.bypass(WEBSITE) {
                cookie = c;
                user_agent = ua;
                break;
            }
        }
    
    // use reqwest without proxy
    {
        // 1
        {
            let client = {
                let headers = {
                    let mut h = reqwest::header::HeaderMap::new();
                    h.insert(reqwest::header::COOKIE, cookie);
                    h.insert(reqwest::header::USER_AGENT, user_agent);
                    h
                };
                
                reqwest::ClientBuilder::new()
                    .default_headers(headers)
                    .build()
                    .unwrap()
            };
                
            let text = client.get(WEBSITE)
                .send()
                .unwrap()
                .text()
                .unwrap();
            println!("{}", text);
        }
        
        // 2
        {
            let text = reqwest::Client::new()
                .get(WEBSITE)
                .header(reqwest::header::COOKIE, cookie)
                .header(reqwest::header::USER_AGENT, user_agent)
                .send()
                .unwrap()
                .text()
                .unwrap();
            println!("{}", text);
        }
    }
    
    // use reqwest with proxy
    {
        let client = {
            let headers = {
                let mut h = reqwest::header::HeaderMap::new();
                h.insert(reqwest::header::COOKIE, cookie);
                h.insert(reqwest::header::USER_AGENT, user_agent);
                h
            };
            
            reqwest::ClientBuilder::new()
                .default_headers(headers)
                .proxy(reqwest::Proxy::all("http://127.0.0.1:1087").unwrap())
                .build()
                .unwrap()
        };
            
        let text = client.get(WEBSITE)
            .send()
            .unwrap()
            .text()
            .unwrap();
        println!("{}", text);
    }
}

Dependencies

~17MB
~406K SLoC