Skip to content

Commit

Permalink
feat: Estabilidade para evitar problemas na atualização do WhatsApp
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Jul 12, 2021
1 parent 1df42c1 commit 97d26ab
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"webpack-cli": "^4.5.0"
},
"dependencies": {
"@wppconnect/wa-version": "^1.1.1",
"atob": "^2.1.2",
"axios": "^0.21.1",
"boxen": "^5.0.0",
Expand Down
5 changes: 4 additions & 1 deletion src/api/layers/host.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class HostLayer {
this.log('verbose', 'Injecting session token', { type: 'token' });
}

await initWhatsapp(this.page, sessionToken);
await initWhatsapp(this.page, sessionToken, this.options.whatsappVersion);

this.page.on('load', () => {
this.log('verbose', 'Page loaded', { type: 'page' });
Expand All @@ -130,6 +130,9 @@ export class HostLayer {
await injectApi(this.page)
.then(() => {
this.log('verbose', 'wapi.js injected');
this.getWAVersion().then((version) => {
this.log('info', `WhatsApp WEB version: ${version}`);
});
})
.catch((e) => {
this.log('verbose', 'wapi.js failed');
Expand Down
10 changes: 10 additions & 0 deletions src/config/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ export interface CreateConfig {
* @default true
*/
createPathFileToken?: boolean;

/**
* Quando definido, essa opção força o carregamento de uma versão específica do WhatsApp WEB
* Normalmente, versões mais antigas continuam funcionando por mais um tempo,
* assim, com essa opção, é possível ter uma janela de tempo maior para se preparar para atualizações.
* Caso seja definido vazio ou null, será usado a versão atual, ou seja, sem forçar versão específica.
* @default 2.2126.x
*/
whatsappVersion?: string;
}
export const defaultOptions: CreateConfig = {
folderNameToken: './tokens',
Expand All @@ -139,4 +148,5 @@ export const defaultOptions: CreateConfig = {
waitForLogin: true,
logger: defaultLogger,
tokenStore: 'file',
whatsappVersion: '2.2126.10',
};
43 changes: 40 additions & 3 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import * as ChromeLauncher from 'chrome-launcher';
import * as os from 'os';
import * as path from 'path';
import * as rimraf from 'rimraf';
import * as waVersion from '@wppconnect/wa-version';
import axios from 'axios';
import { addExitCallback } from 'catch-exit';
import { Browser, BrowserContext, Page } from 'puppeteer';
import { Browser, BrowserContext, Page, Request } from 'puppeteer';
import puppeteer from 'puppeteer-extra';
import { CreateConfig } from '../config/create-config';
import { puppeteerConfig } from '../config/puppeteer.config';
Expand All @@ -32,11 +33,47 @@ import { WebSocketTransport } from './websocket';
import { Logger } from 'winston';
import { SessionToken } from '../token-store';

export async function initWhatsapp(page: Page, token?: SessionToken) {
/**
* Força o carregamento de uma versão específica do WhatsApp WEB
* @param page Página a ser injetada
* @param version Versão ou expressão semver
*/
export async function setWhatsappVersion(page: Page, version: string) {
const body = waVersion.getPageContent(version);

await page.setRequestInterception(true);

page.on('request', (req) => {
if (req.url().startsWith('https://web.whatsapp.com/check-update')) {
req.abort();
return;
}
if (req.url() !== 'https://web.whatsapp.com/') {
req.continue();
return;
}

req.respond({
status: 200,
contentType: 'text/html',
body: body,
});
});
}

export async function initWhatsapp(
page: Page,
token?: SessionToken,
version?: string
) {
await page.setUserAgent(useragentOverride);

// Auth with token
await injectSessionToken(page, token);

await page.setUserAgent(useragentOverride);
if (version) {
await setWhatsappVersion(page, version);
}

const timeout = 10 * 1000;
await Promise.race([
Expand Down

0 comments on commit 97d26ab

Please sign in to comment.