Skip to content

过滤器

此页面由 docs/rules/filters.md 自动同步生成。

本章介绍控制规则生效条件的过滤器。


包含过滤器,只有满足条件的请求才会应用规则。

pattern rules... includeFilter://condition
条件类型语法说明
方法m:METHOD匹配请求方法
状态码s:CODE匹配响应状态码
请求头h:name=value匹配请求头
响应头H:name=value匹配响应头
请求体包含b:text请求体包含文本
响应体包含B:text响应体包含文本
IPi:ip匹配客户端 IP
路径p:/path匹配路径
Terminal window
# 只对 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

⚠️ 注意:小括号内不能有空格,含空格内容必须使用块变量

Terminal window
# 只对 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-found
Not Found
```
Terminal window
# 匹配带有特定头的请求
www.example.com host://debug.local includeFilter://h:X-Debug
# 匹配头部值
www.example.com host://admin.local includeFilter://h:X-Role=admin
# 匹配 Content-Type
www.example.com resHeaders://{X-Json: true} includeFilter://h:content-type=application/json
Terminal window
# 匹配响应头
www.example.com resBody://(cached) includeFilter://H:X-Cache=HIT

注:(cached) 无空格,可使用行内值

Terminal window
# 匹配特定路径
www.example.com resHeaders://{X-Api: true} includeFilter://p:/api/
# 匹配路径模式
www.example.com resDelay://1000 includeFilter://p:/slow/
Terminal window
# 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:POSTPOST应用规则
POST 方法test.com resHeaders://{X:1} includeFilter://m:POSTGET不应用
状态码 500test.com replaceStatus://200 includeFilter://s:500返回 500状态码变 200
状态码 500test.com replaceStatus://200 includeFilter://s:500返回 200不变
头部匹配test.com host://debug includeFilter://h:X-Debug=true有头部应用规则

排除过滤器,满足条件的请求不会应用规则。

pattern rules... excludeFilter://condition

includeFilter 相同的条件语法。

Terminal window
# 排除 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
测试场景规则请求预期
排除 GETtest.com resHeaders://{X:1} excludeFilter://m:GETGET不应用
排除 GETtest.com resHeaders://{X:1} excludeFilter://m:GETPOST应用规则

passthrough:// 用于忽略后续规则并直接透传请求。旧的 ignore:// 写法会在导入、同步或保存时自动转换为 passthrough://

pattern passthrough://
Terminal window
# 透传特定域名
internal.example.com passthrough://
# 透传健康检查路径
*.local/health passthrough://
*.local/metrics passthrough://
测试场景规则请求预期
透传域名ignore-this.local passthrough://ignore-this.local请求直接透传
透传路径*.local/health passthrough:///health请求直接透传

启用特定功能或特性。

pattern enable://feature
特性说明
intercept启用 HTTPS 拦截
hide隐藏请求(不在 UI 显示)
abort中断请求
proxy启用代理
Terminal window
# 启用 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://interceptHTTPS 流量可被解密

禁用特定功能或特性。

pattern disable://feature
特性说明
intercept禁用 HTTPS 拦截(隧道透传)
proxy禁用代理
cache禁用缓存
Terminal window
# 禁用 HTTPS 拦截
https://www.example.com disable://intercept
# 禁用缓存
www.example.com disable://cache
测试场景规则预期
禁用拦截https://test.com disable://interceptHTTPS 隧道透传

删除请求/响应中的头部、Cookie 或 URL 参数。

pattern delete://target.name
目标语法说明
请求头reqHeaders.name删除请求头
响应头resHeaders.name删除响应头
请求 CookiereqCookies.name删除请求 Cookie
响应 CookieresCookies.name删除响应 Set-Cookie
URL 参数urlParams.name删除 URL 参数
Terminal window
# 删除请求头
www.example.com delete://reqHeaders.X-Custom
# 删除响应头
www.example.com delete://resHeaders.X-Powered-By
# 删除请求 Cookie
www.example.com delete://reqCookies.session
# 删除响应 Cookie
www.example.com delete://resCookies.tracking
# 删除 URL 参数
www.example.com delete://urlParams.debug
Terminal window
# 删除多个头部
www.example.com delete://reqHeaders.X-A delete://reqHeaders.X-B
# 删除多个 Cookie
www.example.com delete://reqCookies.a delete://reqCookies.b
测试场景规则预期
删除请求头test.com delete://reqHeaders.X-Custom请求不含 X-Custom
删除响应头test.com delete://resHeaders.Server响应不含 Server
删除 Cookietest.com delete://reqCookies.sessionCookie 不含 session
删除参数test.com delete://urlParams.debugURL 不含 debug 参数

跳过指定的已命中规则,并继续尝试匹配剩余规则。

pattern skip://pattern=patternString
pattern skip://operation=protocol://value
Terminal window
# 跳过更具体的 pattern,回落到父级规则
www.example.com/api/blocked skip://pattern=www.example.com/api/blocked
# 跳过某条已经命中的操作
www.example.com/api skip://operation=resHeaders://X-Debug:first
  • pattern=...:按规则左侧的 pattern 跳过
  • operation=...:按 protocol://value 跳过
  • 跳过后不会终止匹配;后续规则仍会继续尝试
测试场景规则预期
跳过 operationtest.com skip://operation=resHeaders://X-A:first后续同类规则仍可继续生效
跳过 patterntest.com/api/blocked skip://pattern=test.com/api/blocked请求回落到更通用的规则

过滤器可以与其他规则组合使用:

Terminal window
# 多个过滤器
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/

  1. 条件顺序:多个 includeFilter 之间是 AND 关系
  2. 优先级excludeFilter 优先于 includeFilter
  3. 状态码过滤s: 过滤器用于响应阶段的规则
  4. 头部大小写:头部名称匹配不区分大小写
  5. 性能考虑:Body 过滤(b:/B:)需要读取整个 Body,可能影响性能