Skip to content

Commit

Permalink
Merge pull request #205 from Goodluckhf/auth-fix
Browse files Browse the repository at this point in the history
Fix Authroize regarding VK ID
  • Loading branch information
Goodluckhf authored Apr 29, 2022
2 parents 0d045c7 + 6c79c3a commit 7d5bc8b
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 33 deletions.
26 changes: 14 additions & 12 deletions services/taskConsumer/actions/create-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,20 @@ export async function createBrowserPage(proxy: ProxyInterface, userAgent: string
await page.authenticate({ username: proxy.login, password: proxy.password });
}

await page.setRequestInterception(true);
page.on('request', req => {
if (
req.resourceType() === 'stylesheet' ||
req.resourceType() === 'font' ||
req.resourceType() === 'image'
) {
req.abort();
} else {
req.continue();
}
});
if (process.env.NODE_ENV === 'production') {
await page.setRequestInterception(true);
page.on('request', req => {
if (
req.resourceType() === 'stylesheet' ||
req.resourceType() === 'font' ||
req.resourceType() === 'image'
) {
req.abort();
} else {
req.continue();
}
});
}

return { page, browser, userAgent };
}
17 changes: 15 additions & 2 deletions services/taskConsumer/actions/vk/action-applier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,34 @@ export class ActionApplier {
);

const waitForPhoneConfirmation = page.waitForFunction(
() => !!document.querySelector('.vkc__ConfirmPhone__input'),
{ timeout: 10000 },
);

const waitForOldPhoneConfirmation = page.waitForFunction(
() => !!document.querySelector('#validation_phone_row'),
{ timeout: 10000 },
);

const result = await callback();

await bluebird.any([waitForCaptchaPromise, goalAction(), waitForPhoneConfirmation]);
await bluebird.any([
waitForCaptchaPromise,
goalAction(),
waitForPhoneConfirmation,
waitForOldPhoneConfirmation,
]);
try {
await this.captchaSolver.solveIfHas(page);
} catch (error) {
error.login = login;
throw error;
}

const needPhoneConfirmation = await page.evaluate(
() => !!document.querySelector('#validation_phone_row'),
() =>
!!document.querySelector('.vkc__ConfirmPhone__input') ||
!!document.querySelector('#validation_phone_row'),
);

if (needPhoneConfirmation) {
Expand Down
115 changes: 98 additions & 17 deletions services/taskConsumer/actions/vk/vk-authorizer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { inject, injectable } from 'inversify';
import { Page } from 'puppeteer';
import { AggregateError } from 'bluebird';
import bluebird from 'bluebird';
import { LoggerInterface } from '../../../../lib/logger.interface';
import { ProxyInterface } from '../../proxy.interface';
import { AccountException } from '../../rpc-handlers/account.exception';
Expand All @@ -26,27 +27,18 @@ export class VkAuthorizer {
await page.reload({ waitUntil: 'networkidle2' });
await this.checkAccount(page, login);

const loginForm = await page.$('#login_form_wrap');
if (loginForm) {
const oldLoginForm = await page.$('#login_form_wrap');
const newVkIDLoginForm = await page.$('.VkIdForm__form');
if (oldLoginForm || newVkIDLoginForm) {
await client.send('Network.deleteCookies', {
name: 'remixsid',
domain: '.vk.com',
});
}
return !loginForm;
return !oldLoginForm && !newVkIDLoginForm;
}

private async checkAccount(page: Page, login: string) {
const loginFailedElement = await page.$('#login_message .error');
if (loginFailedElement) {
throw new AccountException(
'Account credentials is invalid',
'login_failed',
login,
false,
);
}

const blockedElement = await page.$('#login_blocked_wrap');
if (blockedElement) {
throw new AccountException('Account is blocked', 'blocked', login, false);
Expand All @@ -58,10 +50,11 @@ export class VkAuthorizer {
}
}

async signInWithCredentials(
page: Page,
{ login, password }: { login: string; password: string },
) {
async signInOldForm(page: Page, { login, password }: { login: string; password: string }) {
this.logger.info({
message: 'Логинимся через старую форму',
login,
});
await page.evaluate(
(_login, _password) => {
document.querySelector<HTMLInputElement>('#email').value = _login;
Expand All @@ -86,9 +79,97 @@ export class VkAuthorizer {
await page.reload({ waitUntil: 'networkidle2' });
}

const loginFailedElement = await page.$('#login_message .error');
if (loginFailedElement) {
throw new AccountException(
'Account credentials is invalid',
'login_failed',
login,
false,
);
}
await this.checkAccount(page, login);
}

async signInNewVkIDForm(page: Page, { login, password }: { login: string; password: string }) {
this.logger.info({
message: 'Логинимся через новую форму',
login,
});
await this.actionApplier.click({
page,
goalAction: () => page.waitForNavigation({ timeout: 10000 }),
selector: '.VkIdForm__signInButton',
login,
});

await page.type('input[name=login]', login);

await this.actionApplier.click({
page,
goalAction: () => page.waitForSelector('input[name=password]'),
selector: 'button[type=submit]',
login,
});

await page.type('input[name=password]', password);

try {
await this.actionApplier.click({
page,
goalAction: () =>
bluebird.any([
page.waitForSelector('.vkc__Password__Wrapper .vkc__TextField__errorIcon'),
page.waitForNavigation({ timeout: 10000 }) as Promise<any>,
]),
selector: 'button[type=submit]',
login,
});
} catch (error) {
if (!(error instanceof AggregateError)) {
throw error;
}

this.logger.warn({
message: 'Warning при авторизации (нажатие на кнопку авторизоваться)',
error,
});

await page.reload({ waitUntil: 'networkidle2' });
}

const loginFailedElement = await page.$(
'.vkc__Password__Wrapper .vkc__TextField__errorIcon',
);

if (loginFailedElement) {
const errorMessageElement = await page.$(
'.vkc__TextField__tooltip .vkc__TextField__text',
);
const errorMessage = await page.evaluate(el => el.textContent, errorMessageElement);
throw new AccountException(
`Account credentials is invalid: [${errorMessage}]`,
'login_failed',
login,
false,
);
}

await this.checkAccount(page, login);
}

async signInWithCredentials(
page: Page,
{ login, password }: { login: string; password: string },
) {
const isOldForm = await page.$('#login_form_wrap');
if (isOldForm) {
await this.signInOldForm(page, { login, password });
} else {
await this.signInNewVkIDForm(page, { login, password });
}
}

async authorize(
page: Page,
{
Expand Down
11 changes: 9 additions & 2 deletions services/taskConsumer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,18 @@ process.on('SIGINT', () => {
});

process.on('uncaughtException', error => {
logger.error({ error });
logger.error({ error, stack: error.stack, message: 'uncaughtException' });
gracefulStop.forceStop();
});

process.on('unhandledRejection', (reason, promise) => {
logger.error({ reason, promise });
logger.error({
reason,
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
stack: reason.stack || undefined,
promise,
message: 'unhandledRejection',
});
gracefulStop.forceStop(1);
});

0 comments on commit 7d5bc8b

Please sign in to comment.