过滤器
此页面由
docs/rules/filters.md自动同步生成。
本章介绍控制规则生效条件的过滤器。
includeFilter
Section titled “includeFilter”包含过滤器,只有满足条件的请求才会应用规则。
pattern rules... includeFilter://condition| 条件类型 | 语法 | 说明 |
|---|---|---|
| 方法 | m:METHOD | 匹配请求方法 |
| 状态码 | s:CODE | 匹配响应状态码 |
| 请求头 | h:name=value | 匹配请求头 |
| 响应头 | H:name=value | 匹配响应头 |
| 请求体包含 | b:text | 请求体包含文本 |
| 响应体包含 | B:text | 响应体包含文本 |
| IP | i:ip | 匹配客户端 IP |
| 路径 | p:/path | 匹配路径 |
# 只对 POST 请求生效www.example.com resHeaders://(X-Method:POST) includeFilter://m:POST
# 只对 GET 请求生效www.example.com resHeaders://(X-Method:GET) includeFilter://m:GET
# 只对 PUT 请求生效www.example.com resHeaders://(X-Method:PUT) includeFilter://m:PUT⚠️ 注意:小括号内不能有空格,含空格内容必须使用块变量
# 只对 500 响应生效www.example.com replaceStatus://200 includeFilter://s:500
# 只对 404 响应生效(使用块变量)www.example.com resBody://{not-found} includeFilter://s:404
# 对 4xx 响应生效www.example.com resHeaders://(X-Error:true) includeFilter://s:4块变量定义:
``` not-foundNot Found```# 匹配带有特定头的请求www.example.com host://debug.local includeFilter://h:X-Debug
# 匹配头部值www.example.com host://admin.local includeFilter://h:X-Role=admin
# 匹配 Content-Typewww.example.com resHeaders://{X-Json: true} includeFilter://h:content-type=application/json# 匹配响应头www.example.com resBody://(cached) includeFilter://H:X-Cache=HIT注:
(cached)无空格,可使用行内值
# 匹配特定路径www.example.com resHeaders://{X-Api: true} includeFilter://p:/api/
# 匹配路径模式www.example.com resDelay://1000 includeFilter://p:/slow/# AND 条件(同时满足)www.example.com replaceStatus://200 includeFilter://m:POST includeFilter://s:500
# 方法 + 头部www.example.com host://special.local includeFilter://m:POST includeFilter://h:X-Special| 测试场景 | 规则 | 请求 | 预期 |
|---|---|---|---|
| POST 方法 | test.com resHeaders://{X:1} includeFilter://m:POST | POST | 应用规则 |
| POST 方法 | test.com resHeaders://{X:1} includeFilter://m:POST | GET | 不应用 |
| 状态码 500 | test.com replaceStatus://200 includeFilter://s:500 | 返回 500 | 状态码变 200 |
| 状态码 500 | test.com replaceStatus://200 includeFilter://s:500 | 返回 200 | 不变 |
| 头部匹配 | test.com host://debug includeFilter://h:X-Debug=true | 有头部 | 应用规则 |
excludeFilter
Section titled “excludeFilter”排除过滤器,满足条件的请求不会应用规则。
pattern rules... excludeFilter://condition与 includeFilter 相同的条件语法。
# 排除 GET 请求www.example.com resDelay://1000 excludeFilter://m:GET
# 排除静态资源www.example.com resHeaders://(X-Dynamic:true) excludeFilter://p:.js excludeFilter://p:.css
# 排除成功响应www.example.com resHeaders://(X-Error:true) excludeFilter://s:200
# 排除特定头部www.example.com host://default.local excludeFilter://h:X-Special| 测试场景 | 规则 | 请求 | 预期 |
|---|---|---|---|
| 排除 GET | test.com resHeaders://{X:1} excludeFilter://m:GET | GET | 不应用 |
| 排除 GET | test.com resHeaders://{X:1} excludeFilter://m:GET | POST | 应用规则 |
passthrough
Section titled “passthrough”passthrough:// 用于忽略后续规则并直接透传请求。旧的 ignore:// 写法会在导入、同步或保存时自动转换为 passthrough://。
pattern passthrough://# 透传特定域名internal.example.com passthrough://
# 透传健康检查路径*.local/health passthrough://*.local/metrics passthrough://| 测试场景 | 规则 | 请求 | 预期 |
|---|---|---|---|
| 透传域名 | ignore-this.local passthrough:// | ignore-this.local | 请求直接透传 |
| 透传路径 | *.local/health passthrough:// | /health | 请求直接透传 |
enable
Section titled “enable”启用特定功能或特性。
pattern enable://feature| 特性 | 说明 |
|---|---|
intercept | 启用 HTTPS 拦截 |
hide | 隐藏请求(不在 UI 显示) |
abort | 中断请求 |
proxy | 启用代理 |
# 启用 HTTPS 拦截https://www.example.com enable://intercept
# 隐藏请求www.example.com/internal enable://hide
# 中断请求www.example.com/blocked enable://abort| 测试场景 | 规则 | 预期 |
|---|---|---|
| 中断请求 | test.com enable://abort | 请求被中断 |
| HTTPS 拦截 | https://test.com enable://intercept | HTTPS 流量可被解密 |
disable
Section titled “disable”禁用特定功能或特性。
pattern disable://feature| 特性 | 说明 |
|---|---|
intercept | 禁用 HTTPS 拦截(隧道透传) |
proxy | 禁用代理 |
cache | 禁用缓存 |
# 禁用 HTTPS 拦截https://www.example.com disable://intercept
# 禁用缓存www.example.com disable://cache| 测试场景 | 规则 | 预期 |
|---|---|---|
| 禁用拦截 | https://test.com disable://intercept | HTTPS 隧道透传 |
delete
Section titled “delete”删除请求/响应中的头部、Cookie 或 URL 参数。
pattern delete://target.name| 目标 | 语法 | 说明 |
|---|---|---|
| 请求头 | reqHeaders.name | 删除请求头 |
| 响应头 | resHeaders.name | 删除响应头 |
| 请求 Cookie | reqCookies.name | 删除请求 Cookie |
| 响应 Cookie | resCookies.name | 删除响应 Set-Cookie |
| URL 参数 | urlParams.name | 删除 URL 参数 |
# 删除请求头www.example.com delete://reqHeaders.X-Custom
# 删除响应头www.example.com delete://resHeaders.X-Powered-By
# 删除请求 Cookiewww.example.com delete://reqCookies.session
# 删除响应 Cookiewww.example.com delete://resCookies.tracking
# 删除 URL 参数www.example.com delete://urlParams.debug# 删除多个头部www.example.com delete://reqHeaders.X-A delete://reqHeaders.X-B
# 删除多个 Cookiewww.example.com delete://reqCookies.a delete://reqCookies.b| 测试场景 | 规则 | 预期 |
|---|---|---|
| 删除请求头 | test.com delete://reqHeaders.X-Custom | 请求不含 X-Custom |
| 删除响应头 | test.com delete://resHeaders.Server | 响应不含 Server |
| 删除 Cookie | test.com delete://reqCookies.session | Cookie 不含 session |
| 删除参数 | test.com delete://urlParams.debug | URL 不含 debug 参数 |
跳过指定的已命中规则,并继续尝试匹配剩余规则。
pattern skip://pattern=patternStringpattern skip://operation=protocol://value# 跳过更具体的 pattern,回落到父级规则www.example.com/api/blocked skip://pattern=www.example.com/api/blocked
# 跳过某条已经命中的操作www.example.com/api skip://operation=resHeaders://X-Debug:firstpattern=...:按规则左侧的 pattern 跳过operation=...:按protocol://value跳过- 跳过后不会终止匹配;后续规则仍会继续尝试
| 测试场景 | 规则 | 预期 |
|---|---|---|
| 跳过 operation | test.com skip://operation=resHeaders://X-A:first | 后续同类规则仍可继续生效 |
| 跳过 pattern | test.com/api/blocked skip://pattern=test.com/api/blocked | 请求回落到更通用的规则 |
过滤器可以与其他规则组合使用:
# 多个过滤器www.example.com host://backend.local includeFilter://m:POST excludeFilter://p:/health
# 过滤器 + 修改规则www.example.com resHeaders://{X-Debug: true} includeFilter://h:X-Debug
# 删除 + 过滤器www.example.com delete://reqHeaders.X-Internal includeFilter://m:GET
# 条件透传www.example.com passthrough:// includeFilter://p:/static/- 条件顺序:多个
includeFilter之间是 AND 关系 - 优先级:
excludeFilter优先于includeFilter - 状态码过滤:
s:过滤器用于响应阶段的规则 - 头部大小写:头部名称匹配不区分大小写
- 性能考虑:Body 过滤(
b:/B:)需要读取整个 Body,可能影响性能