mirror of
https://github.com/LightZirconite/Microsoft-Rewards-Bot.git
synced 2026-01-23 06:41:03 +00:00
feat: Refactor compromised mode handling and improve error logging across flows
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
import type { MicrosoftRewardsBot } from '../index'
|
||||
import type { Account } from '../interface/Account'
|
||||
import { createBrowserInstance } from '../util/BrowserFactory'
|
||||
import { saveSessionData } from '../util/Load'
|
||||
import { handleCompromisedMode } from './FlowUtils'
|
||||
|
||||
export interface DesktopFlowResult {
|
||||
initialPoints: number
|
||||
@@ -66,32 +66,9 @@ export class DesktopFlow {
|
||||
await this.bot.login.login(this.bot.homePage, account.email, account.password, account.totp)
|
||||
|
||||
if (this.bot.compromisedModeActive) {
|
||||
// User wants the page to remain open for manual recovery. Do not proceed to tasks.
|
||||
keepBrowserOpen = true
|
||||
const reason = this.bot.compromisedReason || 'security-issue'
|
||||
this.bot.log(false, 'DESKTOP-FLOW', `Account security check failed (${reason}). Browser kept open for manual review: ${account.email}`, 'warn', 'yellow')
|
||||
|
||||
try {
|
||||
const { ConclusionWebhook } = await import('../util/ConclusionWebhook')
|
||||
await ConclusionWebhook(
|
||||
this.bot.config,
|
||||
'🔐 Security Check',
|
||||
`**Account:** ${account.email}\n**Status:** ${reason}\n**Action:** Browser kept open, activities paused`,
|
||||
undefined,
|
||||
0xFFAA00
|
||||
)
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error)
|
||||
this.bot.log(false, 'DESKTOP-FLOW', `Failed to send security webhook: ${errorMsg}`, 'warn')
|
||||
}
|
||||
|
||||
// Save session for convenience, but do not close the browser
|
||||
try {
|
||||
await saveSessionData(this.bot.config.sessionPath, this.bot.homePage.context(), account.email, false)
|
||||
} catch (e) {
|
||||
this.bot.log(false, 'DESKTOP-FLOW', `Failed to save session: ${e instanceof Error ? e.message : String(e)}`, 'warn')
|
||||
}
|
||||
|
||||
const result = await handleCompromisedMode(this.bot, account.email, reason, false)
|
||||
keepBrowserOpen = result.keepBrowserOpen
|
||||
return { initialPoints: 0, collectedPoints: 0 }
|
||||
}
|
||||
|
||||
|
||||
63
src/flows/FlowUtils.ts
Normal file
63
src/flows/FlowUtils.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Shared utilities for Desktop and Mobile flows
|
||||
* Extracts common patterns to reduce code duplication
|
||||
*/
|
||||
|
||||
import type { MicrosoftRewardsBot } from '../index'
|
||||
import { saveSessionData } from '../util/Load'
|
||||
|
||||
/**
|
||||
* Handle compromised/security check mode for an account
|
||||
* Sends security alert webhook, saves session, and keeps browser open for manual review
|
||||
*
|
||||
* @param bot Bot instance
|
||||
* @param account Email of affected account
|
||||
* @param reason Reason for security check (e.g., 'recovery-email-mismatch', '2fa-required')
|
||||
* @param isMobile Whether this is mobile flow (affects logging context)
|
||||
* @returns Object with keepBrowserOpen flag (always true for compromised mode)
|
||||
*
|
||||
* @example
|
||||
* const result = await handleCompromisedMode(bot, 'user@example.com', 'recovery-mismatch', false)
|
||||
* if (result.keepBrowserOpen) return { initialPoints: 0, collectedPoints: 0 }
|
||||
*/
|
||||
export async function handleCompromisedMode(
|
||||
bot: MicrosoftRewardsBot,
|
||||
account: string,
|
||||
reason: string,
|
||||
isMobile: boolean
|
||||
): Promise<{ keepBrowserOpen: boolean }> {
|
||||
const flowContext = isMobile ? 'MOBILE-FLOW' : 'DESKTOP-FLOW'
|
||||
|
||||
bot.log(
|
||||
isMobile,
|
||||
flowContext,
|
||||
`Account security check failed (${reason}). Browser kept open for manual review: ${account}`,
|
||||
'warn',
|
||||
'yellow'
|
||||
)
|
||||
|
||||
// Send security alert webhook
|
||||
try {
|
||||
const { ConclusionWebhook } = await import('../util/ConclusionWebhook')
|
||||
await ConclusionWebhook(
|
||||
bot.config,
|
||||
isMobile ? '🔐 Security Check (Mobile)' : '🔐 Security Check',
|
||||
`**Account:** ${account}\n**Status:** ${reason}\n**Action:** Browser kept open, ${isMobile ? 'mobile ' : ''}activities paused`,
|
||||
undefined,
|
||||
0xFFAA00
|
||||
)
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error)
|
||||
bot.log(isMobile, flowContext, `Failed to send security webhook: ${errorMsg}`, 'warn')
|
||||
}
|
||||
|
||||
// Save session for convenience (non-critical)
|
||||
try {
|
||||
await saveSessionData(bot.config.sessionPath, bot.homePage.context(), account, isMobile)
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error)
|
||||
bot.log(isMobile, flowContext, `Failed to save session: ${errorMsg}`, 'warn')
|
||||
}
|
||||
|
||||
return { keepBrowserOpen: true }
|
||||
}
|
||||
@@ -14,8 +14,8 @@
|
||||
import type { MicrosoftRewardsBot } from '../index'
|
||||
import type { Account } from '../interface/Account'
|
||||
import { createBrowserInstance } from '../util/BrowserFactory'
|
||||
import { saveSessionData } from '../util/Load'
|
||||
import { MobileRetryTracker } from '../util/MobileRetryTracker'
|
||||
import { handleCompromisedMode } from './FlowUtils'
|
||||
|
||||
export interface MobileFlowResult {
|
||||
initialPoints: number
|
||||
@@ -73,30 +73,9 @@ export class MobileFlow {
|
||||
await this.bot.login.login(this.bot.homePage, account.email, account.password, account.totp)
|
||||
|
||||
if (this.bot.compromisedModeActive) {
|
||||
keepBrowserOpen = true
|
||||
const reason = this.bot.compromisedReason || 'security-issue'
|
||||
this.bot.log(true, 'MOBILE-FLOW', `Mobile security check failed (${reason}). Browser kept open for manual review: ${account.email}`, 'warn', 'yellow')
|
||||
|
||||
try {
|
||||
const { ConclusionWebhook } = await import('../util/ConclusionWebhook')
|
||||
await ConclusionWebhook(
|
||||
this.bot.config,
|
||||
'🔐 Security Check (Mobile)',
|
||||
`**Account:** ${account.email}\n**Status:** ${reason}\n**Action:** Browser kept open, mobile activities paused`,
|
||||
undefined,
|
||||
0xFFAA00
|
||||
)
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error)
|
||||
this.bot.log(true, 'MOBILE-FLOW', `Failed to send security webhook: ${errorMsg}`, 'warn')
|
||||
}
|
||||
|
||||
try {
|
||||
await saveSessionData(this.bot.config.sessionPath, this.bot.homePage.context(), account.email, true)
|
||||
} catch (e) {
|
||||
this.bot.log(true, 'MOBILE-FLOW', `Failed to save session: ${e instanceof Error ? e.message : String(e)}`, 'warn')
|
||||
}
|
||||
|
||||
const result = await handleCompromisedMode(this.bot, account.email, reason, true)
|
||||
keepBrowserOpen = result.keepBrowserOpen
|
||||
return { initialPoints: 0, collectedPoints: 0 }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user