File size: 2,435 Bytes
ce6c0f3
 
 
 
 
 
2c66a64
c5dd0cf
91d1194
ce6c0f3
c5dd0cf
ce6c0f3
 
 
 
 
c5dd0cf
ce6c0f3
c5dd0cf
ce6c0f3
 
 
 
 
c5dd0cf
ce6c0f3
 
 
c5dd0cf
ce6c0f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5dd0cf
ce6c0f3
 
 
 
c5dd0cf
 
ce6c0f3
 
 
 
 
 
 
 
 
 
 
 
 
 
c5dd0cf
ce6c0f3
 
 
 
 
c5dd0cf
ce6c0f3
 
0c0fa0b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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}`);
});