我曾尝试使用
isolated-vm
和 vm2
库在隔离环境中运行代码,但我遇到了在隔离环境中未安装所需包的问题。
isolated-vm
不支持模块和 vm2
不在隔离环境中安装它。
这里我尝试用
vm2
运行脚本-
import { NodeVM, VMScript } from "vm2";
import { resolve } from "path";
import fs from "fs";
async function runScript(scriptPath, dependencies = []) {
const scriptSource = fs.readFileSync(scriptPath, "utf8");
const vm = new NodeVM({
console: "inherit",
sandbox: {},
require: {
external: true,
import: dependencies,
},
});
const script = new VMScript(scriptSource);
const result = await vm.run(script);
console.log(result);
}
runScript(resolve("./src/discordService.js"), ["discord.js-selfbot-v13"]);