Skip to content

Commit

Permalink
feat: addProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Dec 23, 2021
1 parent b008ca8 commit 22f9f01
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/config/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ export interface CreateConfig {
* @default false
*/
logger?: Logger;
/**
* Add proxy server
* @default null
*/
addProxy?: string[];
/**
* Proxy username
* @default null
*/
userProxy?: string;
/**
* Proxy password
* @default null
*/
userPass?: string;
}
export const defaultOptions: CreateConfig = {
multidevice: true,
Expand Down
25 changes: 25 additions & 0 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ export async function initWhatsapp(
try {
await waPage.setUserAgent(useragentOverride);
await waPage.setBypassCSP(true);
if (
typeof options.userPass === 'string' &&
options.userPass.length &&
typeof options.userProxy === 'string' &&
options.userProxy.length &&
Array.isArray(options.addProxy) &&
options.addProxy.length
) {
await waPage.authenticate({
username: options.userProxy,
password: options.userPass
});
}

await waPage.goto(puppeteerConfig.whatsappUrl, {
waitUntil: 'domcontentloaded'
});
Expand Down Expand Up @@ -236,6 +250,17 @@ export async function initBrowser(
// Use stealth plugin to avoid being detected as a bot
puppeteer.use(StealthPlugin());

if (Array.isArray(options.addProxy) && options.addProxy.length) {
const proxy =
options.addProxy[Math.floor(Math.random() * options.addProxy.length)];
options.browserArgs
? Object.assign(options.browserArgs, [`--proxy-server=${proxy}`])
: Object.assign(puppeteerConfig.chromiumArgs, [
`--proxy-server=${proxy}`
]);
// console.log(puppeteerConfig.chromiumArgs);
}

let browser = null;
if (options.browserWS && options.browserWS != '') {
await puppeteer
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export async function create(
() => {
if (
document.querySelector('canvas') &&
document.querySelectorAll('#startup').length == 0
document.querySelectorAll('._2Nr6U').length == 0
) {
return true;
}
Expand All @@ -299,7 +299,7 @@ export async function create(
if (state === SocketState.PAIRING) {
await page.waitForFunction(
() => {
if (document.querySelectorAll('#startup').length) {
if (document.querySelectorAll('._2Nr6U').length) {
return true;
}
},
Expand Down

0 comments on commit 22f9f01

Please sign in to comment.