fb / scripts /obfuscate-extension.cjs
sare26's picture
Upload 6 files
31a8c31 verified
Raw
History Blame Contribute Delete
7.31 kB
#!/usr/bin/env node
/**
* ุณูƒุฑูŠุจุช ุชุดููŠุฑ ู…ู„ูุงุช ุงู„ุฅุถุงูุฉ ุงู„ู…ุชูˆุงูู‚ ู…ุน Manifest V3
* bridge.source.js โ†’ ุชุดููŠุฑ ู…ุชูˆุณุท-ู‚ูˆูŠ (ุฃู‚ู„ ู…ู† ุงู„ุดุฏูŠุฏ ุจู‚ู„ูŠู„)
* background.source.js โ†’ ุฅุนุฏุงุฏุงุช ุฎููŠูุฉ ุฌุฏุงู‹ (ุฏูˆู† ุชุบูŠูŠุฑ ูƒุจูŠุฑ)
*/
const JavaScriptObfuscator = require('javascript-obfuscator');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const EXT_DIR = __dirname;
// ========== ุฅุนุฏุงุฏุงุช ุฎููŠูุฉ ุฌุฏุงู‹ (ู„ู€ background) ==========
const LIGHT_OPTIONS = {
compact: true,
selfDefending: false,
debugProtection: false,
debugProtectionInterval: 0,
renameGlobals: false,
renameProperties: false,
transformObjectKeys: false,
controlFlowFlattening: false,
deadCodeInjection: false,
stringArray: false,
unicodeEscapeSequence: false,
numbersToExpressions: false,
identifierNamesGenerator: 'mangled',
target: 'browser',
seed: 42,
sourceMap: false,
};
// ========== ุฅุนุฏุงุฏุงุช ุชุดููŠุฑ ู…ุชูˆุณุท-ู‚ูˆูŠ (ู„ู€ bridge - ุฃู‚ู„ ู…ู† ุงู„ุดุฏูŠุฏ ุจู‚ู„ูŠู„) ==========
const MEDIUM_STRONG_OPTIONS = {
compact: true,
selfDefending: true, // ุญู…ุงูŠุฉ ุฐุงุชูŠุฉ (ุฎููŠูุฉุŒ ู„ุง ุชุณุจุจ ู…ุดุงูƒู„ ููŠ MV3)
debugProtection: false, // ุชู… ุชุนุทูŠู„ู‡ุง ู„ุชุฌู†ุจ ุฃูŠ ุชุจุงุทุค ู…ู„ุญูˆุธ
debugProtectionInterval: 0,
renameGlobals: false,
renameProperties: false,
transformObjectKeys: true, // ุชุนู‚ูŠุฏ ู…ูุงุชูŠุญ ุงู„ูƒุงุฆู†ุงุช
controlFlowFlattening: true, // ุชุดูˆูŠุด ุชุฏูู‚ ุงู„ุชุญูƒู… (ุจู‚ูŠู…ุฉ ู…ู†ุฎูุถุฉ)
controlFlowFlatteningThreshold: 0.5, // 50% ูู‚ุท ู…ู† ุงู„ูƒูˆุฏ ูŠุชุฃุซุฑ
deadCodeInjection: false, // ุชุนุทูŠู„ ุญู‚ู† ุงู„ูƒูˆุฏ ุงู„ู…ูŠุช (ูŠู‚ู„ู„ ุงู„ุญุฌู…)
stringArray: true, // ุชุดููŠุฑ ุงู„ู†ุตูˆุต
stringArrayThreshold: 0.5, // 50% ูู‚ุท ู…ู† ุงู„ู†ุตูˆุต
stringArrayEncoding: [], // ุจุฏูˆู† ุชุดููŠุฑ ุฅุถุงููŠ (ุฃุณุฑุน)
stringArrayIndexShift: false, // ุชุนุทูŠู„ ุฅุฒุงุญุฉ ุงู„ู…ุคุดุฑ
splitStrings: false, // ุชุนุทูŠู„ ุชู‚ุณูŠู… ุงู„ู†ุตูˆุต
unicodeEscapeSequence: false, // ุชุนุทูŠู„ ูŠูˆู†ูŠูƒูˆุฏ (ูŠู‚ู„ู„ ุงู„ุญุฌู…)
numbersToExpressions: false, // ุชุนุทูŠู„ ุชุญูˆูŠู„ ุงู„ุฃุฑู‚ุงู…
identifierNamesGenerator: 'mangled-shuffled',
target: 'browser',
seed: 999,
sourceMap: false,
};
// ========== ุชุดููŠุฑ ู…ู„ู ู…ุน ุฎูŠุงุฑุงุช ==========
function obfuscateFile(sourceFile, outputFile, label, options) {
if (!fs.existsSync(sourceFile)) {
console.error(`โŒ ุฎุทุฃ: ู„ู… ูŠุชู… ุงู„ุนุซูˆุฑ ุนู„ู‰ ุงู„ู…ู„ู ุงู„ู…ุตุฏุฑูŠ: ${sourceFile}`);
process.exit(1);
}
const src = fs.readFileSync(sourceFile, 'utf8');
console.log(`๐Ÿ“„ ${label} โ€” ุงู„ุฃุตู„ูŠ: ${(src.length / 1024).toFixed(1)} KB`);
const result = JavaScriptObfuscator.obfuscate(src, options).getObfuscatedCode();
fs.writeFileSync(outputFile, result, 'utf8');
console.log(`๐Ÿ” ${label} โ€” ุงู„ู…ุดูุฑ: ${(result.length / 1024).toFixed(1)} KB`);
return result;
}
// ========== background ุจุชุดููŠุฑ ุฎููŠู ุฌุฏุงู‹ ==========
obfuscateFile(
path.join(EXT_DIR, 'src', 'background.source.js'),
path.join(EXT_DIR, 'background.js'),
'background.js',
LIGHT_OPTIONS
);
// ========== bridge ุจุชุดููŠุฑ ู…ุชูˆุณุท-ู‚ูˆูŠ (ุฃู‚ู„ ู…ู† ุงู„ุดุฏูŠุฏ) ==========
obfuscateFile(
path.join(EXT_DIR, 'src', 'bridge.source.js'),
path.join(EXT_DIR, 'bridge.js'),
'bridge.js',
MEDIUM_STRONG_OPTIONS
);
console.log('โœ… ุงูƒุชู…ู„ ุชุดููŠุฑ ุงู„ู…ู„ููŠู† (bridge ุจุชุดููŠุฑ ู…ุชูˆุณุท-ู‚ูˆูŠุŒ background ุฎููŠู)');
// ========== ุจู†ุงุก ZIP (ุจุฏูˆู† ุชุนุฏูŠู„) ==========
function crc32(buf) {
const table = (() => {
const t = new Uint32Array(256);
for (let i = 0; i < 256; i++) {
let c = i;
for (let j = 0; j < 8; j++) c = (c & 1) ? 0xEDB88320 ^ (c >>> 1) : c >>> 1;
t[i] = c;
}
return t;
})();
let crc = 0xFFFFFFFF;
for (const b of buf) crc = table[(crc ^ b) & 0xFF] ^ (crc >>> 8);
return (crc ^ 0xFFFFFFFF) >>> 0;
}
const files = ['manifest.json', 'background.js', 'bridge.js', 'icon-128.png'];
const parts = [];
const centralDir = [];
let offset = 0;
for (const file of files) {
const filePath = path.join(EXT_DIR, file);
if (!fs.existsSync(filePath)) {
console.error(`โŒ ุฎุทุฃ: ุงู„ู…ู„ู ุงู„ู…ุทู„ูˆุจ ู„ู„ู€ ZIP ู…ูู‚ูˆุฏ: ${filePath}`);
process.exit(1);
}
const data = fs.readFileSync(filePath);
const compressed = zlib.deflateRawSync(data, { level: 6 });
const crc = crc32(data);
const nameBytes = Buffer.from(file, 'utf8');
const now = new Date();
const dosDate = ((now.getFullYear() - 1980) << 9) | ((now.getMonth() + 1) << 5) | now.getDate();
const dosTime = (now.getHours() << 11) | (now.getMinutes() << 5) | (now.getSeconds() >> 1);
const localHeader = Buffer.alloc(30 + nameBytes.length);
localHeader.writeUInt32LE(0x04034b50, 0);
localHeader.writeUInt16LE(20, 4);
localHeader.writeUInt16LE(0, 6);
localHeader.writeUInt16LE(8, 8);
localHeader.writeUInt16LE(dosTime, 10);
localHeader.writeUInt16LE(dosDate, 12);
localHeader.writeUInt32LE(crc, 14);
localHeader.writeUInt32LE(compressed.length, 18);
localHeader.writeUInt32LE(data.length, 22);
localHeader.writeUInt16LE(nameBytes.length, 26);
localHeader.writeUInt16LE(0, 28);
nameBytes.copy(localHeader, 30);
const centralEntry = Buffer.alloc(46 + nameBytes.length);
centralEntry.writeUInt32LE(0x02014b50, 0);
centralEntry.writeUInt16LE(20, 4);
centralEntry.writeUInt16LE(20, 6);
centralEntry.writeUInt16LE(0, 8);
centralEntry.writeUInt16LE(8, 10);
centralEntry.writeUInt16LE(dosTime, 12);
centralEntry.writeUInt16LE(dosDate, 14);
centralEntry.writeUInt32LE(crc, 16);
centralEntry.writeUInt32LE(compressed.length, 20);
centralEntry.writeUInt32LE(data.length, 24);
centralEntry.writeUInt16LE(nameBytes.length, 28);
centralEntry.writeUInt16LE(0, 30);
centralEntry.writeUInt16LE(0, 32);
centralEntry.writeUInt16LE(0, 34);
centralEntry.writeUInt16LE(0, 36);
centralEntry.writeUInt32LE(0, 38);
centralEntry.writeUInt32LE(offset, 42);
nameBytes.copy(centralEntry, 46);
parts.push(localHeader, compressed);
centralDir.push(centralEntry);
offset += localHeader.length + compressed.length;
}
const centralDirBuf = Buffer.concat(centralDir);
const eocd = Buffer.alloc(22);
eocd.writeUInt32LE(0x06054b50, 0);
eocd.writeUInt16LE(0, 4);
eocd.writeUInt16LE(0, 6);
eocd.writeUInt16LE(files.length, 8);
eocd.writeUInt16LE(files.length, 10);
eocd.writeUInt32LE(centralDirBuf.length, 12);
eocd.writeUInt32LE(offset, 16);
eocd.writeUInt16LE(0, 20);
const zip = Buffer.concat([...parts, centralDirBuf, eocd]);
const zipPath = path.join(EXT_DIR, 'fb-publisher-bridge.zip');
fs.writeFileSync(zipPath, zip);
console.log(`๐Ÿ“ฆ ZIP ุงู„ู…ุญุฏุซ: ${(zip.length / 1024).toFixed(1)} KB โ†’ ${zipPath}`);
console.log('๐ŸŽ‰ ุงูƒุชู…ู„ ุงู„ุชุดููŠุฑ ุงู„ู…ุชูˆุงูู‚ ูˆุจู†ุงุก ZIP ุจู†ุฌุงุญ!');