/**
 * 解码 provider.js 的混淆字符串
 */
const fs = require('fs');

const code = fs.readFileSync('D:/temp/破解/cursorpro-0.4.5/原版本/extension/out/webview/provider.js', 'utf8');

var vip = 'cursor';

// 找函数名
const funcMatch = code.match(/function (_0x[a-f0-9]+)\(/g);
const funcNames = [...new Set(funcMatch)].map(f => f.match(/_0x[a-f0-9]+/)[0]);
console.log('函数名:', funcNames.slice(0, 5));

// 1. 提取字符串数组函数
const arrFuncName = funcNames.find(n => {
    const regex = new RegExp(`function ${n}\\(\\)\\{`);
    return regex.test(code);
});
console.log('字符串数组函数:', arrFuncName);

const arrMatch = code.match(new RegExp(`function ${arrFuncName}\\(\\)\\{[\\s\\S]*?return ${arrFuncName}\\(\\);\\}`));
if (!arrMatch) {
    console.log('找不到字符串数组函数');
    process.exit(1);
}
eval(arrMatch[0]);
console.log('1. 字符串数组函数已定义');

// 2. 找解码函数 (两个参数的函数)
const decFuncName = funcNames.find(n => {
    const regex = new RegExp(`function ${n}\\(_0x[a-f0-9]+,_0x[a-f0-9]+\\)\\{`);
    return regex.test(code) && n !== arrFuncName;
});
console.log('解码函数:', decFuncName);

// 提取解码函数
const decStart = code.indexOf(`function ${decFuncName}(`);
const decEndMarker = `},${decFuncName}(`;
let searchPos = decStart + 100;
while (true) {
    const pos = code.indexOf(decEndMarker, searchPos);
    if (pos === -1) break;
    const endPos = code.indexOf(';}', pos);
    if (endPos !== -1 && endPos - pos < 100) {
        const decCode = code.substring(decStart, endPos + 2);
        try {
            eval(decCode);
            console.log('2. 解码函数已定义');
            break;
        } catch (e) {
            searchPos = pos + 10;
        }
    } else {
        searchPos = pos + 10;
    }
}

// 3. 执行 IIFE
const aliasMatch = code.match(/const (_0x[a-f0-9]+)=_0x[a-f0-9]+;/);
if (aliasMatch) {
    const aliasName = aliasMatch[1];
    console.log('别名:', aliasName);

    const iifeStart = code.indexOf(aliasMatch[0]);
    const iifeEnd = code.indexOf('var __createBinding');
    if (iifeStart >= 0 && iifeEnd > iifeStart) {
        let iifeCode = code.substring(iifeStart, iifeEnd).trim();
        if (iifeCode.endsWith(';')) iifeCode = iifeCode.slice(0, -1);
        try {
            eval(iifeCode);
            console.log('3. IIFE 已执行');
        } catch (e) {
            console.log('IIFE 执行失败:', e.message);
        }
    }
}

// 4. 测试解码并收集
const decode = eval(decFuncName);
console.log('\n=== 收集所有字符串 ===');

const regex = /_0x[a-zA-Z0-9]+\s*\(\s*(0x[0-9a-fA-F]+)\s*,\s*'([^']+)'\s*\)/g;
const decodedMap = new Map();

let match;
let count = 0;
while ((match = regex.exec(code)) !== null && count < 5000) {
    const fullMatch = match[0];
    const index = parseInt(match[1], 16);
    const key = match[2];

    if (!decodedMap.has(fullMatch)) {
        try {
            const decoded = decode(index, key);
            decodedMap.set(fullMatch, decoded);
            count++;
        } catch (e) {
            // 跳过
        }
    }
}

console.log(`共解码 ${decodedMap.size} 个字符串\n`);

// 找关键字符串
console.log('=== 关键字符串 (client/api/verify) ===');
[...decodedMap.entries()]
    .filter(([k, v]) =>
        v.includes('client') ||
        v.includes('/api') ||
        v.includes('verify') ||
        v.includes('http')
    )
    .slice(0, 30)
    .forEach(([call, decoded]) => {
        console.log(decoded);
    });

// 保存
const mapObj = {};
for (const [call, decoded] of decodedMap) {
    mapObj[call] = decoded;
}
fs.writeFileSync('D:/temp/破解/cursorpro-0.4.5/provider_decoded_map.json', JSON.stringify(mapObj, null, 2));
console.log('\n完整映射已保存到 provider_decoded_map.json');
