全栈应用服务器

  • 全栈应用服务器 > 使用指南 > 沙箱服务概述 > 沙箱网络访问

    沙箱网络访问

    最近更新时间: 2026-02-12 11:22:01

    概述

    沙箱默认启用互联网访问,可以进行 HTTP 请求、下载文件、访问 API 等操作。同时提供灵活的网络配置选项。

    基本网络访问控制

    启用/禁用互联网访问

    通过 allowInternetAccess 参数可以快速启用或禁用所有互联网访问。

    import { Sandbox } from '@e2b/code-interpreter'
    
    async function main() {
      // Enable internet access (default)
      const sandbox = await Sandbox.create({ allowInternetAccess: true })
      await sandbox.kill()
    
      // Disable internet access
      const isolatedSandbox = await Sandbox.create({ allowInternetAccess: false })
      await isolatedSandbox.kill()
    }
    
    main()
    

    细粒度网络规则

    配置说明

    当需要更精确的网络控制时,可以使用 network 参数配置细粒度规则。注意: 使用 network 参数时,allowInternetAccess 参数会被忽略。

    IP 和 CIDR 过滤

    import { Sandbox, ALL_TRAFFIC } from '@e2b/code-interpreter'
    
    async function main() {
      // Allow only specific IPs
      const sandbox = await Sandbox.create({
        network: {
          denyOut: [ALL_TRAFFIC],
          allowOut: ['1.1.1.1', '8.8.8.0/24']
        }
      })
      await sandbox.kill()
    }
    
    main()
    

    域名过滤

    import { Sandbox, ALL_TRAFFIC } from '@e2b/code-interpreter'
    
    async function main() {
      const sandbox = await Sandbox.create({
        network: {
          allowOut: ['api.example.com', '*.github.com'],
          denyOut: [ALL_TRAFFIC]
        }
      })
      await sandbox.kill()
    }
    
    main()
    
    以上内容是否对您有帮助?