Skip to content

Commit

Permalink
Add bot detection prevention
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcardeenas committed Apr 4, 2020
1 parent 073e8a5 commit 08c73ee
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 19 deletions.
196 changes: 183 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
"file-type": "^14.1.4",
"ora": "^4.0.3",
"puppeteer": "^2.1.1",
"puppeteer-extra": "^3.1.9",
"puppeteer-extra-plugin-stealth": "^2.4.9",
"qrcode-terminal": "^0.12.0",
"rxjs": "^6.5.5",
"sharp": "^0.25.2"
Expand Down
20 changes: 14 additions & 6 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as ChromeLauncher from 'chrome-launcher';
import * as path from 'path';
import * as puppeteer from 'puppeteer';
import { Browser, Page } from 'puppeteer';
import puppeteer from 'puppeteer-extra';
import { CreateConfig } from '../config/create-config';
import { puppeteerConfig } from '../config/puppeteer.config';
import StealthPlugin = require('puppeteer-extra-plugin-stealth');

export async function initWhatsapp(session: string, options: CreateConfig) {
const browser = await initBrowser(session, options);
Expand All @@ -15,7 +17,7 @@ export async function initWhatsapp(session: string, options: CreateConfig) {
return waPage;
}

export async function injectApi(page: puppeteer.Page) {
export async function injectApi(page: Page) {
page.waitForFunction(() => {
// @ts-ignore
return webpackJsonp !== undefined;
Expand All @@ -37,28 +39,34 @@ export async function injectApi(page: puppeteer.Page) {
* Initializes browser, will try to use chrome as default
* @param session
*/
async function initBrowser(session: string, options: CreateConfig) {
let extras = {};
async function initBrowser(
session: string,
options: CreateConfig,
extras = {}
) {
const chromeInstalations = ChromeLauncher.Launcher.getInstallations();
if (chromeInstalations.length) {
extras = { ...extras, executablePath: chromeInstalations[0] };
}

// Use stealth plugin to avoid being detected as a bot
puppeteer.use(StealthPlugin());

const browser = await puppeteer.launch({
// headless: true,
headless: options.headless,
devtools: options.devtools,
userDataDir: path.join(
process.cwd(),
`session${session ? '-' + session : ''}`
`session${session && session.trim() !== '' ? '-' + session.trim() : ''}`
),
args: [...puppeteerConfig.chroniumArgs],
...extras,
});
return browser;
}

async function getWhatsappPage(browser: puppeteer.Browser) {
async function getWhatsappPage(browser: Browser) {
const pages = await browser.pages();
console.assert(pages.length > 0);
return pages[0];
Expand Down

0 comments on commit 08c73ee

Please sign in to comment.