UNPKG

chrome-cookie-extractor

Version:

Extract and decrypt Chrome cookies with curl integration - includes auth-curl command for authenticated requests

152 lines 6.29 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = require("commander"); const extractor_1 = require("./extractor"); const child_process_1 = require("child_process"); const chalk_1 = __importDefault(require("chalk")); const program = new commander_1.Command(); program .name('auth-curl') .description('curl with automatic Chrome cookie authentication') .version('1.0.0') .argument('<url>', 'URL to request') .option('-v, --verbose', 'Show detailed output') .option('-o, --output <file>', 'Write output to file instead of stdout') .option('-H, --header <header>', 'Add custom header (can be used multiple times)', []) .option('-X, --request <method>', 'HTTP method (GET, POST, etc.)', 'GET') .option('-d, --data <data>', 'HTTP POST data') .option('--json', 'Send data as JSON and set content-type') .option('--follow-redirects', 'Follow HTTP redirects') .option('--insecure', 'Allow insecure SSL connections') .action(async (url, options) => { try { // Extract domain from URL const urlObj = new URL(url); const domain = urlObj.hostname; if (options.verbose) { console.log(chalk_1.default.blue(`🔍 Extracting cookies for domain: ${domain}`)); } // Extract cookies for the domain const extractor = new extractor_1.ChromeCookieExtractor(); const cookies = await extractor.extractCookies({ domain }); if (cookies.length === 0) { console.log(chalk_1.default.yellow(`⚠️ No cookies found for ${domain}`)); console.log(chalk_1.default.gray('Proceeding without authentication...')); } else { const usableCookies = cookies.filter(c => c.value !== '[ENCRYPTED]'); if (options.verbose) { console.log(chalk_1.default.green(`✅ Found ${cookies.length} cookies (${usableCookies.length} decrypted)`)); } } // Build curl command const curlArgs = ['curl']; // Add default headers for better compatibility curlArgs.push('-H', '"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"'); curlArgs.push('-H', '"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"'); curlArgs.push('-H', '"Accept-Language: en-US,en;q=0.5"'); curlArgs.push('-H', '"Connection: keep-alive"'); curlArgs.push('-H', '"Upgrade-Insecure-Requests: 1"'); // Add compressed support - curl will handle decompression automatically curlArgs.push('--compressed'); // Add cookies using temporary file (safer than command line) let tempCookieFile = null; if (cookies.length > 0) { const usableCookies = cookies.filter(c => c.value !== '[ENCRYPTED]'); if (usableCookies.length > 0) { const os = require('os'); const fs = require('fs'); const path = require('path'); tempCookieFile = path.join(os.tmpdir(), `auth-curl-cookies-${Date.now()}.txt`); const netscapeFormat = extractor.formatAsNetscape(usableCookies); fs.writeFileSync(tempCookieFile, netscapeFormat); curlArgs.push('-b', tempCookieFile); } } // Add custom headers if (options.header && options.header.length > 0) { options.header.forEach((header) => { curlArgs.push('-H', `"${header}"`); }); } // Add JSON content-type if --json flag is used if (options.json) { curlArgs.push('-H', '"Content-Type: application/json"'); } // Add HTTP method if (options.request !== 'GET') { curlArgs.push('-X', options.request); } // Add data if (options.data) { curlArgs.push('-d', `"${options.data}"`); } // Add other options if (options.followRedirects) { curlArgs.push('-L'); } if (options.insecure) { curlArgs.push('-k'); } if (options.output) { curlArgs.push('-o', options.output); } // Add URL curlArgs.push(`"${url}"`); const curlCommand = curlArgs.join(' '); if (options.verbose) { console.log(chalk_1.default.blue(`🚀 Executing: ${curlCommand}`)); console.log(chalk_1.default.gray('─'.repeat(50))); } // Execute curl command and cleanup try { (0, child_process_1.execSync)(curlCommand, { encoding: 'utf8', stdio: options.output ? 'pipe' : 'inherit' }); if (options.output) { console.log(chalk_1.default.green(`✅ Output saved to ${options.output}`)); } } catch (error) { if (options.verbose) { console.error(chalk_1.default.red('❌ curl command failed:'), error); } process.exit(1); } finally { // Cleanup temporary cookie file if (tempCookieFile) { try { const fs = require('fs'); fs.unlinkSync(tempCookieFile); } catch (e) { // Ignore cleanup errors } } } } catch (error) { console.error(chalk_1.default.red('❌ Error:'), error.message); process.exit(1); } }); // Add help examples program.addHelpText('after', ` Examples: $ auth-curl https://github.com/user/repo $ auth-curl https://api.github.com/user -v $ auth-curl https://example.com/api -X POST -d '{"key":"value"}' --json $ auth-curl https://myaccount.google.com/profile -o profile.html $ auth-curl https://private-site.com -H "Accept: application/json" -v `); if (require.main === module) { program.parse(); } exports.default = program; //# sourceMappingURL=auth-curl.js.map