Skip to main content

为沙箱提供 SSH 访问权限

Deno Sandbox provides a sandboxed Linux microVM. This is useful for evaluating code that is not trusted or for testing code that is not safe to run in your main runtime.

SSH 访问允许您通过 SSH 协议安全地连接到沙箱环境。sandbox.create({ ssh: true }) 方法可以用来为沙箱提供 SSH 访问权限。

import { Sandbox } from "@deno/sandbox";

await using sandbox = await Sandbox.create({ ssh: true });

// 等待 Deploy 配置 SSH 访问信息。
const creds = sandbox.ssh ?? await sandbox.exposeSsh();
if (!creds) {
  throw new Error("未为此沙箱配置 SSH 凭据");
}

const { hostname, username } = creds;
console.log(`ssh ${username}@${hostname}`);

// 通过睡眠保持进程存活,否则脚本退出时沙箱将被销毁。
await new Promise((resolve) => setTimeout(resolve, 10 * 60 * 1000)); // 10 分钟

For more information, see the Deno Sandbox documentation.

你找到了你需要的东西吗?

编辑此页面
隐私政策