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

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

var vip = 'cursor';

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

// 2. 提取解码函数 _0xc90d
const decStart = code.indexOf('function _0xc90d(');
const decEnd = code.indexOf('},_0xc90d(_0x25a632,_0x29df0b);}', decStart) + '},_0xc90d(_0x25a632,_0x29df0b);}'.length;
const decCode = code.substring(decStart, decEnd);
eval(decCode);
console.log('2. _0xc90d 已定义');

// 3. 执行 IIFE 来打乱数组
const iifeStart = code.indexOf('const _0x50c5e9=_0xc90d;');
const iifeEnd = code.indexOf('var __createBinding');
let iifeCode = code.substring(iifeStart, iifeEnd).trim();
if (iifeCode.endsWith(';')) iifeCode = iifeCode.slice(0, -1);
eval(iifeCode);
console.log('3. IIFE 已执行, vip =', vip);

// 4. 测试解码
const decode = _0xc90d;

console.log('\n=== 测试解码 ===');
// require 路径
console.log('require(0x222, "8j^A"):', decode(0x222, '8j^A'));
console.log('require(0x203, "3pMS"):', decode(0x203, '3pMS'));

// 收集所有解码调用
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;
while ((match = regex.exec(code)) !== null) {
    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);
        } catch (e) {
            // 跳过
        }
    }
}

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

// 找出关键字符串
const important = [...decodedMap.entries()].filter(([k, v]) =>
    v.includes('client') ||
    v.includes('api') ||
    v.includes('verify') ||
    v.includes('http') ||
    v.includes('cursor')
);

console.log('=== 关键字符串 ===');
important.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/extension_decoded_map.json', JSON.stringify(mapObj, null, 2));
console.log('\n完整映射已保存到 extension_decoded_map.json');
