/**
 * CursorPro 反编译脚本 v5
 * 直接执行原版解码器
 */
const fs = require('fs');

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

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

// 从代码中提取 _0x81f8 函数 - 找到从 function _0x81f8 到最后的 return _0x81f8();}
const func81f8Start = code.indexOf('function _0x81f8()');
const func81f8End = code.indexOf('return _0x81f8();}', func81f8Start) + 'return _0x81f8();}'.length;
const func81f8Code = code.substring(func81f8Start, func81f8End);

// 从代码中提取 _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);

console.log('找到 _0x81f8 函数，长度:', func81f8Code.length);
console.log('找到 _0x3fbb 函数，长度:', func3fbbCode.length);

// 执行解码器
eval(func81f8Code);
eval(func3fbbCode);

const decode = _0x3fbb;

console.log('\n=== CursorPro 原版代码解码 ===\n');

// 测试解码
const testCases = [
    [0x18a, 'yC20', 'DEFAULT_API_URL'],
    [0x151, 'pIfk', 'vscode require'],
    [0x177, ']8Ci', 'workspace'],
    [0x15b, 'j#7G', 'apiUrl config'],
    [0x197, 'Kfdi', '/api/verify'],
    [0x186, 'dpeu', 'POST'],
    [0x141, 'z&uV', '/api/switch'],
    [0x144, '*1hl', '__esModule'],
    [0x1b6, 'mBo%', 'getApiUrl'],
    [0x146, 'pIfk', 'getOnlineStatus'],
    [0x145, 'j#7G', 'switchAccount'],
    [0x133, '3nFX', 'body'],
    [0x19c, ']$%b', 'stringify'],
    [0x172, ')0X3', 'json'],
    [0x185, 'yC20', 'application/json'],
    [0x181, '^UhV', 'offline'],
    [0x1a6, 'H%&^', 'abort'],
    [0x179, ')0X3', 'push'],
    [0x14b, 'dpeu', 'filter'],
    [0x147, 'j#7G', 'forEach'],
];

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

console.log('\n=== 关键解码信息 ===');
console.log('API 地址 DEFAULT_API_URL:', results['DEFAULT_API_URL']);

// 扫描所有调用
console.log('\n=== 扫描代码中的所有调用 ===');
const regex = /_0x[a-zA-Z0-9]+\s*\(\s*(0x[0-9a-fA-F]+)\s*,\s*'([^']+)'\s*\)/g;
const allDecoded = new Map();

let match;
while ((match = regex.exec(code)) !== null) {
    const index = parseInt(match[1], 16);
    const key = match[2];
    const callStr = `${match[1]},'${key}'`;

    if (!allDecoded.has(callStr)) {
        try {
            const decoded = decode(index, key);
            allDecoded.set(callStr, decoded);
        } catch (e) {
            allDecoded.set(callStr, `ERROR: ${e.message}`);
        }
    }
}

console.log('\n所有唯一解码结果:');
for (const [call, decoded] of allDecoded) {
    console.log(`  (${call}) => "${decoded}"`);
}

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