/**
 * CursorPro 反编译脚本 v7
 * 正确的执行顺序
 */
const fs = require('fs');

// 读取原版代码
const code = fs.readFileSync('D:/temp/破解/cursorpro-0.4.5/原版本/extension/out/api/client.js', 'utf8');

// 设置 vip
var vip = 'cursor';

// 1. 首先执行 _0x81f8 函数定义
const func81f8Start = code.indexOf('function _0x81f8()');
const func81f8End = code.indexOf('return _0x81f8();}', func81f8Start) + 'return _0x81f8();}'.length;
const func81f8Code = code.substring(func81f8Start, func81f8End);
eval(func81f8Code);
console.log('1. _0x81f8 已定义');

// 2. 然后执行 _0x3fbb 函数定义
const func3fbbStart = code.indexOf('function _0x3fbb(');
const func3fbbEnd = code.indexOf(',_0x3fbb(_0x91e0a,_0x2e3a7e);}', func3fbbStart) + ',_0x3fbb(_0x91e0a,_0x2e3a7e);}'.length;
const func3fbbCode = code.substring(func3fbbStart, func3fbbEnd);
eval(func3fbbCode);
console.log('2. _0x3fbb 已定义');

// 3. 提取并执行混淆IIFE
// 从 const _0x1ede40=_0x3fbb; 开始到 &&(vip=`\xf42`); 结束
const iifeStart = code.indexOf("const _0x1ede40=_0x3fbb;");
const iifeEnd = code.indexOf("var __createBinding");
let iifeCode = code.substring(iifeStart, iifeEnd).trim();
// 移除最后的分号
if (iifeCode.endsWith(';')) {
    iifeCode = iifeCode.slice(0, -1);
}

console.log('3. IIFE 代码长度:', iifeCode.length);
console.log('IIFE 开头:', iifeCode.substring(0, 100));

// 执行IIFE
try {
    eval(iifeCode);
    console.log('4. IIFE 执行成功');
    console.log('   vip =', vip);
} catch (e) {
    console.log('4. IIFE 执行失败:', e.message);
}

// 查看数组变化
const arr = _0x81f8();
console.log('\n字符串数组前5项:', arr.slice(0, 5));

// 测试解码
console.log('\n=== 测试解码 ===');
const decode = _0x3fbb;

const testCases = [
    [0x18a, 'yC20', 'DEFAULT_API_URL'],
    [0x151, 'pIfk', 'vscode'],
    [0x177, ']8Ci', 'workspace'],
    [0x15b, 'j#7G', 'apiUrl'],
    [0x197, 'Kfdi', '/api/verify'],
    [0x186, 'dpeu', 'POST'],
    [0x141, 'z&uV', '/api/switch'],
    [0x144, '*1hl', '__esModule'],
];

for (const [index, key, desc] of testCases) {
    try {
        const decoded = decode(index, key);
        console.log(`${desc}: "${decoded}"`);
    } catch (e) {
        console.log(`${desc}: ERROR - ${e.message}`);
    }
}
