Skip to content

Commit

Permalink
feat: Added proxy authentication (#2354)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullahercan authored Oct 24, 2024
1 parent 49fd124 commit f0f0406
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/api/layers/host.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export class HostLayer {
null,
false,
this.options.whatsappVersion,
this.options.proxy,
this.log.bind(this)
);

Expand Down
20 changes: 20 additions & 0 deletions src/config/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -199,4 +218,5 @@ export const defaultOptions: CreateConfig = {
disableGoogleAnalytics: true,
googleAnalyticsId: null,
poweredBy: 'WPPConnect',
proxy: null,
};
28 changes: 25 additions & 3 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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`);
Expand Down Expand Up @@ -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,
});

Expand Down

0 comments on commit f0f0406

Please sign in to comment.