wudysoft commited on
Commit
6a522dc
·
verified ·
1 Parent(s): dca944c

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +36 -5
app.js CHANGED
@@ -8,6 +8,7 @@ import fetch from 'node-fetch';
8
  import sharp from 'sharp';
9
  import { spawn } from 'child_process';
10
  import path from 'path';
 
11
  import { createRequire } from "module";
12
  const require = createRequire(import.meta.url);
13
 
@@ -2147,17 +2148,47 @@ app.get('/ask', async (req, res) => {
2147
  app.post("/playwright", async (req, res) => {
2148
  const { code } = req.body;
2149
 
2150
- if (!code) return res.status(400).json({ error: "Kode tidak boleh kosong" });
 
 
 
 
 
 
 
2151
 
2152
  try {
2153
- const executeCode = new Function(`
2154
- return (async (require, importModule, chromium) => { ${code} })();
2155
- `).bind(null);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2156
 
2157
- const result = await executeCode()(require, (mod) => import(mod), chromium);
 
 
2158
 
2159
  res.json({ result });
2160
  } catch (error) {
 
2161
  res.status(500).json({ error: error.message });
2162
  }
2163
  });
 
8
  import sharp from 'sharp';
9
  import { spawn } from 'child_process';
10
  import path from 'path';
11
+ import { NodeVM } from "vm2";
12
  import { createRequire } from "module";
13
  const require = createRequire(import.meta.url);
14
 
 
2148
  app.post("/playwright", async (req, res) => {
2149
  const { code } = req.body;
2150
 
2151
+ if (!code) {
2152
+ console.error("[ERROR] Kode tidak boleh kosong");
2153
+ return res.status(400).json({ error: "Kode tidak boleh kosong" });
2154
+ }
2155
+
2156
+ console.log("\n====================================");
2157
+ console.log("[INFO] Eksekusi kode dimulai...");
2158
+ console.log("[CODE] ====>\n", code);
2159
 
2160
  try {
2161
+ const vm = new NodeVM({
2162
+ console: "inherit",
2163
+ sandbox: {},
2164
+ require: {
2165
+ external: true,
2166
+ builtin: ["fs", "path"],
2167
+ root: "./",
2168
+ mock: {
2169
+ playwright: { chromium },
2170
+ },
2171
+ },
2172
+ });
2173
+
2174
+ const script = `
2175
+ module.exports = async () => {
2176
+ console.log("[LOG] Eksekusi dimulai di dalam sandbox...");
2177
+ ${code}
2178
+ };
2179
+ `;
2180
+
2181
+ const runCode = vm.run(script, "sandbox.js");
2182
+
2183
+ const result = await runCode();
2184
 
2185
+ console.log("[INFO] Eksekusi berhasil!");
2186
+ console.log("[RESULT] ====>", result);
2187
+ console.log("====================================\n");
2188
 
2189
  res.json({ result });
2190
  } catch (error) {
2191
+ console.error("[ERROR] Eksekusi gagal!", error);
2192
  res.status(500).json({ error: error.message });
2193
  }
2194
  });