|
|
|
|
|
|
|
|
|
import { get_access_token, getEmails, activateMailbox, syncMailbox } from './mail.js'; |
|
|
|
export interface ActivationResult { |
|
success: boolean; |
|
activated: boolean; |
|
emailCount: number; |
|
latestEmailTime?: string; |
|
error?: string; |
|
activationMethods: string[]; |
|
} |
|
|
|
export class MailboxActivator { |
|
private accessToken: string; |
|
|
|
constructor(accessToken: string) { |
|
this.accessToken = accessToken; |
|
} |
|
|
|
|
|
|
|
|
|
async detectDormantState(): Promise<{ isDormant: boolean; reason: string }> { |
|
try { |
|
|
|
const emails = await getEmails(this.accessToken, 5, false); |
|
|
|
if (emails.length === 0) { |
|
return { isDormant: true, reason: "邮箱中没有邮件" }; |
|
} |
|
|
|
|
|
const latestEmail = emails[0]; |
|
const latestTime = new Date(latestEmail.date.received || latestEmail.date.created); |
|
const now = new Date(); |
|
const hoursDiff = (now.getTime() - latestTime.getTime()) / (1000 * 60 * 60); |
|
|
|
|
|
if (hoursDiff > 24) { |
|
return { isDormant: true, reason: `最新邮件时间过旧: ${hoursDiff.toFixed(1)}小时前` }; |
|
} |
|
|
|
|
|
if (emails.length < 3) { |
|
return { isDormant: true, reason: "邮件数量异常少" }; |
|
} |
|
|
|
return { isDormant: false, reason: "邮箱状态正常" }; |
|
} catch (error) { |
|
return { isDormant: true, reason: `检测失败: ${error}` }; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
async performFullActivation(): Promise<ActivationResult> { |
|
const activationMethods: string[] = []; |
|
let activated = false; |
|
|
|
try { |
|
console.log('开始执行邮箱激活...'); |
|
|
|
|
|
try { |
|
await activateMailbox(this.accessToken); |
|
activationMethods.push('基础激活'); |
|
} catch (error) { |
|
console.warn('基础激活失败:', error); |
|
} |
|
|
|
|
|
try { |
|
await syncMailbox(this.accessToken); |
|
activationMethods.push('强制同步'); |
|
} catch (error) { |
|
console.warn('强制同步失败:', error); |
|
} |
|
|
|
|
|
try { |
|
await this.deepActivation(); |
|
activationMethods.push('深度激活'); |
|
} catch (error) { |
|
console.warn('深度激活失败:', error); |
|
} |
|
|
|
|
|
try { |
|
await this.simulateUserActivity(); |
|
activationMethods.push('模拟用户活动'); |
|
} catch (error) { |
|
console.warn('模拟用户活动失败:', error); |
|
} |
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 5000)); |
|
|
|
|
|
const emails = await getEmails(this.accessToken, 10, false); |
|
const emailCount = emails.length; |
|
const latestEmailTime = emails.length > 0 ? emails[0].date.received : undefined; |
|
|
|
activated = activationMethods.length > 0; |
|
|
|
return { |
|
success: true, |
|
activated, |
|
emailCount, |
|
latestEmailTime, |
|
activationMethods |
|
}; |
|
|
|
} catch (error: any) { |
|
return { |
|
success: false, |
|
activated: false, |
|
emailCount: 0, |
|
error: error.message, |
|
activationMethods |
|
}; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
private async deepActivation(): Promise<void> { |
|
const endpoints = [ |
|
'https://graph.microsoft.com/v1.0/me/calendar', |
|
'https://graph.microsoft.com/v1.0/me/contacts', |
|
'https://graph.microsoft.com/v1.0/me/drive', |
|
'https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messages?$top=1', |
|
'https://graph.microsoft.com/v1.0/me/mailFolders/sentitems', |
|
'https://graph.microsoft.com/v1.0/me/mailFolders/drafts' |
|
]; |
|
|
|
const promises = endpoints.map(endpoint => |
|
fetch(endpoint, { |
|
method: 'GET', |
|
headers: { |
|
'Authorization': `Bearer ${this.accessToken}`, |
|
'Content-Type': 'application/json' |
|
} |
|
}).catch(error => { |
|
console.warn(`访问端点失败 ${endpoint}:`, error); |
|
}) |
|
); |
|
|
|
await Promise.allSettled(promises); |
|
} |
|
|
|
|
|
|
|
|
|
private async simulateUserActivity(): Promise<void> { |
|
try { |
|
|
|
await fetch('https://graph.microsoft.com/v1.0/me/mailboxSettings', { |
|
method: 'GET', |
|
headers: { |
|
'Authorization': `Bearer ${this.accessToken}`, |
|
'Content-Type': 'application/json' |
|
} |
|
}); |
|
|
|
|
|
await fetch('https://graph.microsoft.com/v1.0/me/photo', { |
|
method: 'GET', |
|
headers: { |
|
'Authorization': `Bearer ${this.accessToken}` |
|
} |
|
}).catch(() => {}); |
|
|
|
|
|
await fetch('https://graph.microsoft.com/v1.0/me/activities/recent', { |
|
method: 'GET', |
|
headers: { |
|
'Authorization': `Bearer ${this.accessToken}`, |
|
'Content-Type': 'application/json' |
|
} |
|
}).catch(() => {}); |
|
|
|
|
|
await fetch('https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messageRules', { |
|
method: 'GET', |
|
headers: { |
|
'Authorization': `Bearer ${this.accessToken}`, |
|
'Content-Type': 'application/json' |
|
} |
|
}).catch(() => {}); |
|
|
|
} catch (error) { |
|
console.warn('模拟用户活动时出错:', error); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
async smartActivation(): Promise<ActivationResult> { |
|
|
|
const dormantCheck = await this.detectDormantState(); |
|
|
|
if (!dormantCheck.isDormant) { |
|
|
|
try { |
|
await activateMailbox(this.accessToken); |
|
const emails = await getEmails(this.accessToken, 5, false); |
|
|
|
return { |
|
success: true, |
|
activated: true, |
|
emailCount: emails.length, |
|
latestEmailTime: emails.length > 0 ? emails[0].date.received : undefined, |
|
activationMethods: ['轻量级激活'] |
|
}; |
|
} catch (error: any) { |
|
return { |
|
success: false, |
|
activated: false, |
|
emailCount: 0, |
|
error: error.message, |
|
activationMethods: [] |
|
}; |
|
} |
|
} else { |
|
|
|
console.log(`检测到邮箱可能休眠: ${dormantCheck.reason}`); |
|
return await this.performFullActivation(); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
export async function activateMailboxSmart(accessToken: string): Promise<ActivationResult> { |
|
const activator = new MailboxActivator(accessToken); |
|
return await activator.smartActivation(); |
|
} |
|
|