From f0f0406188ae8fdd94b5534bc62733e0cdfdff90 Mon Sep 17 00:00:00 2001 From: "A. Ercan" Date: Fri, 25 Oct 2024 00:38:52 +0300 Subject: [PATCH] feat: Added proxy authentication (#2354) --- src/api/layers/host.layer.ts | 1 + src/config/create-config.ts | 20 ++++++++++++++++++++ src/controllers/browser.ts | 28 +++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/api/layers/host.layer.ts b/src/api/layers/host.layer.ts index 44d947bb0..ab8f6133a 100644 --- a/src/api/layers/host.layer.ts +++ b/src/api/layers/host.layer.ts @@ -162,6 +162,7 @@ export class HostLayer { null, false, this.options.whatsappVersion, + this.options.proxy, this.log.bind(this) ); diff --git a/src/config/create-config.ts b/src/config/create-config.ts index a2a918b49..681287437 100644 --- a/src/config/create-config.ts +++ b/src/config/create-config.ts @@ -173,6 +173,25 @@ export interface CreateConfig { * Insert the phone number for connect by link phone, qr code not wil generate */ phoneNumber?: string; + + /** + * Define the proxy settings for the connection + * @default null + */ + proxy?: { + /** + * The URL of the proxy server + */ + url: string; + /** + * The username for the proxy server + */ + username: string; + /** + * The password for the proxy server + */ + password: string; + }; } export const defaultOptions: CreateConfig = { @@ -199,4 +218,5 @@ export const defaultOptions: CreateConfig = { disableGoogleAnalytics: true, googleAnalyticsId: null, poweredBy: 'WPPConnect', + proxy: null, }; diff --git a/src/controllers/browser.ts b/src/controllers/browser.ts index f87e871c1..f60dc6e7e 100644 --- a/src/controllers/browser.ts +++ b/src/controllers/browser.ts @@ -105,8 +105,20 @@ export async function initWhatsapp( token?: SessionToken, clear = true, version?: string, + proxy?: { + url: string; + username: string; + password: string; + }, log?: (level: LogLevel, message: string, meta?: object) => any ) { + if (proxy) { + await page.authenticate({ + username: proxy.username, + password: proxy.password, + }); + } + await page.setUserAgent(useragentOverride); await unregisterServiceWorker(page); @@ -122,6 +134,7 @@ export async function initWhatsapp( timeout: 0, referer: 'https://whatsapp.com/', }); + log?.('verbose', 'WhatsApp WEB loaded'); /*setTimeout(() => { log?.('verbose', `Loading WhatsApp WEB`); @@ -278,13 +291,22 @@ export async function initBrowser( /** * Setting the headless mode to the old Puppeteer mode, when using the 'new' mode, results in an error on CentOS7 and Debian11. * Temporary fix. + * + * If proxy settings are provided, they are applied to the browser launch arguments. + * This allows the browser to use the specified proxy server for all network requests. */ + + const args = options.browserArgs + ? options.browserArgs + : [...puppeteerConfig.chromiumArgs]; + if (options.proxy && options.proxy.url) { + args.push(`--proxy-server=${options.proxy.url}`); + } + browser = await puppeteer.launch({ headless: options.headless, devtools: options.devtools, - args: options.browserArgs - ? options.browserArgs - : [...puppeteerConfig.chromiumArgs], + args, ...options.puppeteerOptions, });