const express = require('express'); const fileUpload = require('express-fileupload'); const path = require('path'); const { exec } = require('child_process'); const fs = require('fs').promises; const rimraf = require('rimraf'); const app = express(); const PORT = process.env.PORT || 8000; // Use env var or default to 8000 const UPLOAD_DIR = 'uploads'; // Middleware to handle file uploads with a 500MB limit app.use(fileUpload({ limits: { fileSize: 500 * 1024 * 1024 }, // 500MB limit abortOnLimit: true, })); app.use(express.static('public')); (async () => { if (!await fs.access(UPLOAD_DIR).then(() => true).catch(() => false)) { await fs.mkdir(UPLOAD_DIR); } })(); app.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'views', 'index.html')); }); app.post('/process/debug', async (req, res) => { if (!req.files || !req.files.file || !req.files.file.name.endsWith('.apk')) { return res.status(400).json({ error: 'File must be an APK' }); } const file = req.files.file; const sanitizedFileName = file.name.replace(/\s/g, '_'); const filePath = path.join(UPLOAD_DIR, sanitizedFileName); try { await file.mv(filePath); const outputPath = await debugApk(filePath, UPLOAD_DIR); if (outputPath && await fs.access(outputPath).then(() => true).catch(() => false)) { res.download(outputPath, path.basename(outputPath), async (err) => { if (err) console.error(err); await cleanupFiles(); }); } else { await cleanupFiles(); res.status(500).json({ error: 'Failed to debug APK' }); } } catch (err) { await cleanupFiles(); res.status(500).json({ error: 'Failed to process request' }); } }); function debugApk(inputPath, outputDir) { return new Promise((resolve) => { const command = `apk-mitm ${inputPath} -o ${outputDir}`; exec(command, (error, stdout) => { if (error) { console.error(`Error: ${error.message}`); return resolve(null); } const outputFileMatch = stdout.match(/Patched file: (.+)/); const outputFileName = outputFileMatch ? outputFileMatch[1].trim() : null; resolve(outputFileName ? path.join(outputDir, outputFileName) : null); }); }); } async function cleanupFiles() { return new Promise((resolve) => { rimraf(UPLOAD_DIR, () => resolve()); }); } app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); });