Skip to content

Commit

Permalink
feat: removing multi device option
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Apr 20, 2023
1 parent 1df477e commit 4564c24
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 319 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Installing the current repository "you can download the beta version from the cu
> npm i github:orkestral/venom
```

## Getting started Multidevice and Normal
## Getting started

```javascript
// Supports ES6
Expand All @@ -115,7 +115,6 @@ const venom = require('venom-bot');
venom
.create({
session: 'session-name', //name of session
multidevice: true // for version not multidevice use false.(default: true)
})
.then((client) => start(client))
.catch((erro) => {
Expand Down Expand Up @@ -183,7 +182,6 @@ venom
},
// options
{
multidevice: false, // for version not multidevice use false.(default: true)
folderNameToken: 'tokens', //folder name when saving tokens
mkdirFolderToken: '', //folder directory tokens, just inside the venom folder, example: { mkdirFolderToken: '/node_modules', } //will save the tokens folder in the node_modules directory
headless: true, // Headless chrome
Expand Down Expand Up @@ -268,9 +266,6 @@ venom
//Create session wss return "serverClose" case server for close
console.log('Session name: ', session);
},
{
multidevice: false // for version not multidevice use false.(default: true)
}
)
.then((client) => {
start(client);
Expand Down
15 changes: 0 additions & 15 deletions src/api/layers/retriever.layer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Page, Browser } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { tokenSession } from '../../config/tokenSession.config';
import { WhatsappProfile } from '../model';
import { SenderLayer } from './sender.layer';
import { Scope, checkValuesSender } from '../helpers/layers-interface';
Expand Down Expand Up @@ -93,20 +92,6 @@ export class RetrieverLayer extends SenderLayer {
public async getStateConnection() {
return await this.page.evaluate(() => WAPI.getStateConnection());
}
/**
* Returns browser session token
* @returns obj [token]
*/
public async getSessionTokenBrowser(
removePath?: boolean
): Promise<tokenSession> {
if (removePath === true) {
await this.page.evaluate(() => {
window['pathSession'] = true;
});
}
return await this.page.evaluate(() => WAPI.getSessionTokenBrowser());
}

/**
* Receive the current theme
Expand Down
13 changes: 0 additions & 13 deletions src/config/tokenSession.config.ts

This file was deleted.

130 changes: 0 additions & 130 deletions src/controllers/auth.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import * as puppeteer from 'puppeteer';
import * as qrcode from 'qrcode-terminal';
import { existsSync, readFileSync } from 'fs';
import { CreateConfig } from '../config/create-config';
import { ScrapQrcode } from '../api/model/qrcode';
import { tokenSession } from '../config/tokenSession.config';
//import { InterfaceMode } from '../api/model/enum/interface-mode';
//import { injectApi } from './browser';
import { sleep } from '../utils/sleep';
import { Whatsapp } from '../api/whatsapp';
const QRCode = require('qrcode');
Expand Down Expand Up @@ -161,130 +155,6 @@ export async function retrieveQR(
.catch(() => undefined);
}

export function SessionTokenCkeck(token: object) {
if (
token &&
token['WABrowserId'] &&
token['WASecretBundle'] &&
token['WAToken1'] &&
token['WAToken2']
) {
return true;
} else {
return false;
}
}

export async function auth_InjectToken(
page: puppeteer.Page,
session: string,
options: CreateConfig,
token?: tokenSession
) {
if (!token) {
const pathToken: string = path.join(
path.resolve(
process.cwd() + options.mkdirFolderToken,
options.folderNameToken
),
`${session}.data.json`
);
if (existsSync(pathToken)) {
token = JSON.parse(readFileSync(pathToken).toString());
}
}

if (!token || !SessionTokenCkeck(token)) {
return false;
}

await page
.evaluate((session) => {
localStorage.clear();
Object.keys(session).forEach((key) => {
localStorage.setItem(key, session[key]);
});
}, token as any)
.catch();
}

export async function isClassic(page: puppeteer.Page): Promise<Boolean> {
return await page
.evaluate(() => {
if (
window.localStorage.getItem('WASecretBundle') &&
window.localStorage.getItem('WAToken1') &&
window.localStorage.getItem('WAToken2')
) {
return true;
}
return false;
})
.catch(() => undefined);
}

export async function saveToken(
page: puppeteer.Page,
session: string,
options: CreateConfig
) {
const token = (await page
.evaluate(() => {
if (window.localStorage) {
return {
WABrowserId: window.localStorage.getItem('WABrowserId'),
WASecretBundle: window.localStorage.getItem('WASecretBundle'),
WAToken1: window.localStorage.getItem('WAToken1'),
WAToken2: window.localStorage.getItem('WAToken2')
};
}
return undefined;
})
.catch(() => undefined)) as tokenSession;

if (!token || !SessionTokenCkeck(token)) {
return false;
}

const folder = path.join(
path.resolve(
process.cwd(),
options.mkdirFolderToken,
options.folderNameToken
)
);

try {
fs.mkdirSync(folder, { recursive: true });
} catch (error) {
throw 'Failed to create folder tokens...';
}

try {
fs.writeFileSync(
path.join(folder, `${session}.data.json`),
JSON.stringify(token)
);
//fs.chmodSync(folder, '777');
//fs.chmodSync(folder + '/' + session + '.data.json', '777');
} catch (error) {
throw 'Failed to save token...';
}

return token;
}

// export async function resetStore(page: puppeteer.Page) {
// await page
// .evaluate(() => {
// let last = window['webpackChunkwhatsapp_web_client'].length - 1;
// window['webpackChunkwhatsapp_web_client'][last][0] = [];
// window.Store = undefined;
// window.WAPI = undefined;
// })
// .catch(() => {});
// }

export async function checkDisconnect(page: puppeteer.Page, wpp: Whatsapp) {
while (true) {
const erroBrowser = await page
Expand Down
14 changes: 2 additions & 12 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@ import puppeteer from 'puppeteer-extra';
import { CreateConfig } from '../config/create-config';
import { puppeteerConfig } from '../config/puppeteer.config';
import StealthPlugin = require('puppeteer-extra-plugin-stealth');
import { auth_InjectToken } from './auth';
import { useragentOverride } from '../config/WAuserAgente';
import { tokenSession } from '../config/tokenSession.config';
import { sleep } from '../utils/sleep';
import * as Spinnies from 'spinnies';
import * as os from 'os';
import * as rimraf from 'rimraf';
export async function initWhatsapp(
session: string,
options: CreateConfig,
browser: Browser,
token?: tokenSession
browser: Browser
): Promise<false | Page> {
const waPage: Page = await getWhatsappPage(browser);
if (waPage != null) {
try {
await waPage.setUserAgent(useragentOverride);
//await waPage.setCacheEnabled(true);
if (
typeof options.userPass === 'string' &&
options.userPass.length &&
Expand Down Expand Up @@ -52,8 +47,6 @@ export async function initWhatsapp(
});

await browser.userAgent();
// Auth with token
await auth_InjectToken(waPage, session, options, token);

return waPage;
} catch (e) {
Expand Down Expand Up @@ -115,7 +108,6 @@ export function folderSession(
process.cwd(),
options.mkdirFolderToken,
options.folderNameToken,
options.mkdirFolderTokenMultidevice,
session
)
);
Expand Down Expand Up @@ -232,9 +224,7 @@ export async function initBrowser(
}
}

if (options.multidevice === true) {
folderSession(options, session);
}
folderSession(options, session);

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

0 comments on commit 4564c24

Please sign in to comment.