可下载示例
这些示例使用保留测试域名,没有真实订阅、账户或凭据。除第一项外,均为配置片段,需要合并进已有 sing-box JSON。
配置下载
| 示例 | 类型 | 用途 |
|---|---|---|
| http-lab.json | 完整演示配置 | 本机 2080 代理,HTTP Map Local;无需 CA、DNS 或上游服务器 |
| mitm.json | experimental.mitm 片段 | 开启一个 HTTPS 域名的解密 |
| rewrite.json | experimental.http_rewrite 片段 | Header、URL、Map Remote、jq 和 Map Local |
| empty-responses.json | experimental.http_rewrite 片段 | reject-200 空正文与 reject-dict 空 JSON |
| body-limits.json | 正文限制片段 | 为已有改写和脚本选择字节上限;不启用规则 |
| http-scripts.json | experimental.scripts 片段 | 动态 Mock 与响应 JSON 改写;内嵌源码 |
| grpc-scripts.json | MITM + 脚本片段 | 已知一元 gRPC 方法的单消息帧改写;内嵌源码 |
| remote-script.json | experimental.scripts 片段 | URL 资源引用与刷新间隔 |
| cron-cache.json | experimental.scripts 片段 | Cron 获取响应,HTTP 脚本读取同一缓存 |
浏览器可能直接显示 JSON,可使用浏览器保存或复制内容。
JavaScript 文件
| 文件 | 使用条件 |
|---|---|
| response.js | 响应阶段,requires_body: true |
| dynamic-mock.js | 请求阶段,直接返回 Mock |
| cron-cache.js | Cron,配置 argument 和存储命名空间 |
| grpc-unary.js | 响应阶段,启用 grpc_mode: unary、requires_body 与 binary_body;仅用于教程中的测试消息 |
这些文件是对应 JSON 内嵌源码的可读版本。使用 http-scripts.json 不需要额外下载 JavaScript 文件;要改成 script_path,遵守资源路径规则。
合并时检查四项
- 顶层只保留一个
experimental,MITM、HTTP 改写、脚本保持平级。 - 已有同名规则数组时合并内容,保留顺序;不要直接覆盖订阅的入站、出站和路由。
- 替换域名、映射地址、
argument与allowed_origins;HTTPS 单独准备 CA 和 MITM 范围。 - 成功保存并应用到正在运行的本地服务,再发出新请求验证。
包含规则的下载示例已按演示目的开启相应开关;body-limits.json 只设置限额,不添加或启用规则。应用内通过“新增”创建的样例默认禁用,需要编辑 disabled。两种来源不要混淆。
查看规则组合
下面是 rewrite.json 的实际下载内容:
json
{
"experimental": {
"http_rewrite": {
"enabled": true,
"header_rewrite": [
{
"name": "Add request header",
"pattern": "^https?://api\\.example\\.test/",
"phase": "request",
"operation": "add",
"header": "X-Debug",
"value": "B-box"
}
],
"url_rewrite": [
{
"name": "Temporary redirect",
"pattern": "^https?://api\\.example\\.test/old$",
"action": "307",
"replacement": "https://api.example.test/new"
}
],
"map_remote": [
{
"name": "Use staging",
"pattern": "^https?://api\\.example\\.test/staging/(.*)$",
"replacement": "https://backend.example.test/$1",
"preserve_host": false
}
],
"body_rewrite": [
{
"name": "Set JSON field",
"pattern": "^https?://api\\.example\\.test/profile(?:\\?.*)?$",
"phase": "response",
"jq": ".debug = true"
}
],
"map_local": [
{
"name": "Local mock",
"pattern": "^https?://api\\.example\\.test/mock$",
"status": 200,
"headers": [
{
"name": "Content-Type",
"value": "application/json"
}
],
"data_type": "text",
"data": "{\"ok\":true,\"source\":\"Map Local\"}"
}
]
}
}
}查看 Cron 共享缓存
Cron 每小时第 0、30 分钟尝试刷新。未获取缓存前,HTTP 脚本会继续请求路径,不伪造“缓存成功”。首次运行不会立即触发 Cron。
json
{
"experimental": {
"scripts": {
"enabled": true,
"http": [
{
"name": "Read cached JSON",
"pattern": "^https?://api\\.example\\.test/cached$",
"phase": "request",
"storage_namespace": "docs-demo",
"http_client": {
"allowed_origins": []
},
"source": "const cached = $persistentStore.read(\"latest\");\nif (cached === null) {\n $done();\n}\n$done({\n response: {\n status: 200,\n headers: { \"Content-Type\": \"application/json\" },\n body: cached\n }\n});"
}
],
"cron": [
{
"name": "Refresh cached JSON",
"cron": "*/30 * * * *",
"timezone": "Asia/Shanghai",
"timeout_ms": 5000,
"storage_namespace": "docs-demo",
"argument": "https://api.example.test/status",
"http_client": {
"allowed_origins": [
"https://api.example.test"
],
"max_requests": 1,
"max_body_bytes": 8192
},
"source": "$httpClient.get({ url: $argument, timeout: 4 }, function (error, response, data) {\n if (error || response.status !== 200) {\n console.warn(\"Update failed:\", error || response.status);\n $done();\n }\n if (!$persistentStore.write(data, \"latest\")) {\n console.warn(\"Could not save latest response\");\n $done();\n }\n console.log(\"Updated latest response\");\n $done();\n});"
}
]
}
}
}示例的 JSON 语法与站内引用由文档站检查。具体 App 运行依赖 B-box 版本、核心路由、证书和目标服务,建议从第一条请求逐步验证。
