开启或关闭互联网访问
创建 Sandbox 时,可以使用allowInternetAccess / allow_internet_access 参数配置互联网连接。互联网访问默认开启,但对于安全要求更严格的工作负载,可以将其禁用。
from ppio_sandbox import PPIO
ppio = PPIO()
# 创建启用互联网访问的 Sandbox(默认)。
sandbox = ppio.sandbox.create(allow_internet_access=True)
# 创建不允许互联网访问的 Sandbox。
isolated_sandbox = ppio.sandbox.create(allow_internet_access=False)
import { PPIO } from 'ppio-sandbox'
const ppio = new PPIO()
// 创建启用互联网访问的 Sandbox(默认)。
const sandbox = await ppio.sandbox.create({ allowInternetAccess: true })
// 创建不允许互联网访问的 Sandbox。
const isolatedSandbox = await ppio.sandbox.create({ allowInternetAccess: false })
# 创建启用互联网访问的 Sandbox(默认行为,无需额外参数)
ppio sandbox create base -d
# 创建不允许互联网访问的 Sandbox
ppio sandbox create base --no-internet -d
细粒度网络控制
网络配置支持定义允许列表和拒绝列表,从而对出站流量进行更细粒度的控制。具体可以查阅配置网络规则。注意: 如果未指定
allowOut,则允许所有出站流量。省略 --allow-out 和 --deny-out 会清除所有出站规则。
allow-out 和 deny-out 仅支持 CIDR 的配置方式,不支持域名。from ppio_sandbox import PPIO
ppio = PPIO()
# 拒绝除指定 IP 外的所有流量。
sandbox = ppio.sandbox.create(
network={
'deny_out': ['0.0.0.0/0'],
'allow_out': ['1.1.1.1', '8.8.8.0/24'],
}
)
# 仅允许访问指定域名的流量。
domain_sandbox = ppio.sandbox.create(
network={
'allow_out': ['api.example.com', '*.github.com'],
'deny_out': ['0.0.0.0/0'],
}
)
import { PPIO } from 'ppio-sandbox'
const ppio = new PPIO()
// 拒绝除指定 IP 外的所有流量。
const sandbox = await ppio.sandbox.create({
network: {
denyOut: ['0.0.0.0/0'],
allowOut: ['1.1.1.1', '8.8.8.0/24'],
},
})
// 仅允许访问指定域名的流量。
const domainSandbox = await ppio.sandbox.create({
network: {
allowOut: ['api.example.com', '*.github.com'],
denyOut: ['0.0.0.0/0'],
},
})
# 拒绝除指定 IP 外的所有流量
# (--allow-out / --deny-out 接受逗号分隔的 CIDR 列表,可重复传入)
ppio sandbox create base -d \
--deny-out 0.0.0.0/0 \
--allow-out 1.1.1.1,8.8.8.0/24
沙箱公网 URL
可以通过 Sandbox 的公网 URL 访问 Sandbox 中的服务。from ppio_sandbox import PPIO
ppio = PPIO()
sandbox = ppio.sandbox.create()
host = sandbox.get_host(3000)
print(f'https://{host}')
import { PPIO } from 'ppio-sandbox'
const ppio = new PPIO()
const sandbox = await ppio.sandbox.create()
const host = sandbox.getHost(3000)
console.log(`https://${host}`)
# 创建 Sandbox 并允许公网流量直接访问其 URL
# (--public-traffic 使 Sandbox URL 无需访问令牌即可访问)
ppio sandbox create base --public-traffic -d
连接到沙箱内运行的服务器
import requests
from ppio_sandbox import PPIO
ppio = PPIO()
sandbox = ppio.sandbox.create()
process = sandbox.commands.run('python -m http.server 3000', background=True)
url = f'https://{sandbox.get_host(3000)}'
print('Server started at:', url)
response = requests.get(url)
data = response.text
print('Response from server inside sandbox:', data)
process.kill()
sandbox.kill()
import { PPIO } from 'ppio-sandbox'
const ppio = new PPIO()
const sandbox = await ppio.sandbox.create()
const process = await sandbox.commands.run('python -m http.server 3000', { background: true })
const url = `https://${sandbox.getHost(3000)}`
console.log('Server started at:', url)
const response = await fetch(url)
const data = await response.text()
console.log('Response from server inside sandbox:', data)
await process.kill()
await sandbox.kill()
伪装请求 Host 头
你可以使用maskRequestHost / mask_request_host 选项,自定义发送给 Sandbox 内服务的 Host 头。
from ppio_sandbox import PPIO
ppio = PPIO()
sandbox = ppio.sandbox.create(
network={
'mask_request_host': 'localhost:${PORT}',
}
)
import { PPIO } from 'ppio-sandbox'
const ppio = new PPIO()
const sandbox = await ppio.sandbox.create({
network: {
maskRequestHost: 'localhost:${PORT}',
},
})