本文主要是让你们对 whistle 功能有个总体认识,详细用法参考帮助文档:wproxy.org/whistle/javascript
若是你还不知道 whistle 是什么,能够访问 GitHub 了解:github.com/avwo/whistl…html
whistle 的基本上全部操做均可以经过形以下面的配置方式实现(标准模式):java
pattern operation1 operation2 ... [filter1 filter2 ...]
复制代码
其中:node
如:git
ke.qq.com 127.0.0.1:6001 reqCookies://(x-env=test) includeFilter://reqH:user-agent=chrome excludeFilter://m:post
复制代码
上述表示全部 ke.qq.com
的请求,若是请求头 user-agent
包含 chrome
(不区分大小写)字符串且不是 post
请求,则设置host(127.0.0.1:6001
),并添加请求Cookie(x-env=test
)。es6
因为本地没有开启
6001
端口的服务,因此请求被拒绝了github
若是规则的前两个不一样时为 URL,还能够用以下配置方式:web
operation pattern1 pattern2 … [filter1 filter2 …]
复制代码
operation 和 pattern1 不一样时为 URL正则表达式
也能够支持兼容模式:chrome
operation1 operation2 … pattern1 pattern2 … [filter1 filter2 …]
复制代码
operation1 ... operationN 不能为 URL
如:
127.0.0.1:6001 ke.qq.com fudao.qq.com includeFilter://reqH:user-agent=chrome excludeFilter://m:post
resCors://* *.url.cn *.alicdn.com
127.0.0.1:6001 reqCookies://(x-env=test) ke.qq.com fudao.qq.com includeFilter://reqH:user-agent=chrome excludeFilter://m:post
复制代码
pattern 能够为域名,路径,正则,通配符等:
ke.qq.com 127.0.0.1:6001
10.1.1.1:8080 *.url.cn *.alicdn.com
复制代码
# 只能匹配域名 ke.qq.com 且端口 5566 的请求
ke.qq.com:5566 127.0.0.1:6001
# 匹配 http 的 80 端口或 https 的 443 端口
ke.qq.com/ 127.0.0.1:6001
复制代码
https://ke.qq.com 127.0.0.1:6001
http://ke.qq.com 127.0.0.1:7001
复制代码
路径匹配跟域名匹配差很少,只是多了须要匹配路径片断:
ke.qq.com/statics file:///User/xxx/test
ke.qq.com/cgi-bin 10.1.1.1:8080
复制代码
file:///User/xxx/test ke.qq.com:8080/page fudao.qq.com/page/
ke.qq.com:8080/cgi-bin 10.1.1.1
复制代码
file:///User/xxx/test https://ke.qq.com:8080/page fudao.qq.com/page/
https://ke.qq.com:8080/cgi-bin 10.1.1.1
复制代码
跟普通 js 的正则表达式一致,支持 /regexp/
和 /regexp/i
两种模式,且支持子匹配,能够在 opValue
获取子匹配的值:
ke.qq.com/cgi-bin/
及其子路径 ke.qq.com/cgi-bin/path/to?xxx
的请求忽略全部规则(至关于直接请求现网)/^\w+://ke\.qq\.com/cgi-bin\// ignore://*
复制代码
imweb-test
(且忽略大小写)的 URL 请求忽略全部规则/imweb-test/i ignore://*
复制代码
/^\w+://\d\.url\.cn/path/to/(\w+)\.\w+\.\w+/ file:///User/test/xxx/dev/$1.$2
复制代码
正则表达式涉及转义符及精确匹配,有时写起比较繁琐,为解决这类问题,whistle 提供了通配符匹配解决经常使用匹配问题:
精确匹配
# 只匹配 protocol://ke.qq.com 请求,能够带端口及请求参数,但不包含子路径
$ke.qq.com file:///User/test/index.html
# 只匹配 http://ke.qq.com/test.html 请求,能够包含请求参数,但不包含子路径
$http://ke.qq.com file:///User/test/index.html
复制代码
通配路径匹配
# 匹配全部域名下的 /cgi-bin 及其子路径 /cgi-bin/path/to 请求(本地替换会自动补齐后续路径)
*/cgi-bin file:///User/test/
复制代码
普统统配符匹配(1个或连续几个 * 表示通配符,且能够做为一个子匹配)
^*.url.cn/path/to/*.*.* file:///User/test/xxx/dev/$2.$3
复制代码
域名匹配、路径匹配、通配路径匹配有一个好处是会自动补齐路径,即:
*/cgi-bin file:///User/test/
复制代码
请求 https://ke.qq.com/cgi-bin/test.json
会自动匹配本地文件 /User/test/test.json
,而其它匹配方式须要经过子匹配把路径本身补齐,详细匹配规则参见文档:wproxy.org/whistle/pat…
whistle 将每类操做抽象成一个协议,每一个具体操做对应一个值,即:operation=protocol://opValue
,protocol 表示操做类型,opValue 表示具体操做。
whistle 把每类操做抽象成一个 protocol
,若是修改请求方法(method://post
),修改请求头(reqHeaders://x-client=test&x-id=123
),修改响应 cors(resCors://*
) 等等,大体可分红这几类:
@
功能详细文档参见:wproxy.org/whistle/rul…
每一个具体操做对应一个 opValue
,即为一个字符串或对像,opValue
有一下几种写法:
内联值:
# 一些支持字符串等规则,能够不用小括号
ke.qq.com proxy://127.0.0.1:8888
# 通常须要设置小括号
fudao.qq.com file://(hello) reqCookies://(x-env=test&x-id=abc)
复制代码
``` keyName1
Hello world!
-- 2020-1-1
```
``` keyName2
x-env: test
x-id: abc
```
fudao.qq.com file://{keyName1} reqCookies://{keyName2}
复制代码
用三个及以上等反引号 ` 对 + 键名做为内嵌值的边界
Values
对一些比较大对值,不适宜直接内嵌到规则里面,能够放在跟 Rules
同级到 Values
里面,用法跟内嵌方式一致。
文件或目录
有些内容太大不适合放在 WebUI 里面,或者要本地替换,能够采用文件或目录到方式:
ke.qq.com/test file:///User/statics resCookies:///User/json/cookies.json
复制代码
ke.qq.com/test 及其子路径请求会自动到 /Users/statics 经过补齐路径找到对应文件,并会设置
/User/json/cookies.json
里面的 cookies
模板字符串 方便你们在规则里面获取请求 URL、方法、请求参数、请求头、请求Cookie 以及响应等相关内容,whistle 提供了相似 es6 等模板字符串功能,该功能只支持 内联值 或 Values 里面设置的值,不支持文件及目录:
ke.qq.com file://`(${query.test})`
复制代码
访问
https://ke.qq.com?test=hello
返回hello
``` test.html
url: ${url}
```
ke.qq.com file://`{test.html}`
复制代码
访问
https://ke.qq.com?test=hello
返回url: https://ke.qq.com?test=hello
,也能够放 Values 里面
详细内容参见:wproxy.org/whistle/dat…
pattern
只能匹配请求的 URL,若是根据请求方法、客户端IP、请求头、请求内容,响应状态码,响应头等进行跟灵活精确的匹配,能够采用 filter
,filter
包含 includeFilter
和 excludeFilter
分别用于实现包含及排除匹配规则,一行规则能够用任意多个 filter
:
ke.qq.com/cgi-bin resCors://enable resMerge://retcode=10000 includeFilter://resH:content-type=javascript includeFilter://resH:content-type=json excludeFilter://resH:content-type=gbk
复制代码
上述配置表示因此 ke.qq.com/cgi-bin
及其子路径 ke.qq.com/cgi-bin/path/to
的请求,若是响应的 content-type
包含(不区分大小写) javascript
或 json
,且不能包含 gbk,则会执行 resCors://enable
和 resMerge://retcode=10000
操做。
若是你觉这一行太长了,也能够写成:
line`
ke.qq.com/cgi-bin resCors://enable resMerge://retcode=10000
includeFilter://resH:content-type=javascript
includeFilter://resH:content-type=json
excludeFilter://resH:content-type=gbk
`
复制代码
多个 includeFilter
或 excludeFilter
混合时,includeFilter
表示 或
的关系,excludeFilter
表示 且
的关系,即:
pattern operation1 ... excludeFilter://p1 includeFilter://p2 excludeFilter://p3 includeFilter://p4 includeFilter://p5
复制代码
上述表示 URL 匹配 pattern 的请求,还要匹配 p2 或 p4 或 p5 中的一个,且不能匹配 p1 及 p3。
filter 还支持正则表达式,详细文档参见:wproxy.org/whistle/rul…
上面提到的 filter
只能针对单行进行过滤,有时须要在其它行或配置文件里面过滤可能存在的规则,这时能够用 pattern ignore://protocol1|protocol2|...
的方式来过滤指定协议:
^**/cgi-* ignore://*
^**/cgi-proxy ignore://!host
ke.qq.com/index.html ignore://file|host
www.baidu.com ignore://proxy|jsAppend
复制代码
上述表示全部形如 protocol://xxx/cgi-yyy
及其子路径的请求都忽略全部配置规则,至关于不配任何规则(但 protocol://xxx/cgi-proxy
及其子路径保留 host 规则);ke.qq.com/index.html
的请求忽略本地替换及host规则;www.baidu.com
的全部请求忽略代理及追加jsAppend的规则。
有关 ignore
的内容参见:wproxy.org/whistle/rul…
除了 filter
和 ignore
,还能够结合脚本或插件实现更复杂的匹配方式,具体参见:
w2 add
:执行当前目录的 .whistle.js
文件设置项目规则,详细文档参见:wproxy.org/whistle/cli…w2 i whistle.xxx
:安装 whislte 插件(插件也能够用 npm 全局安装)
也能够指定 registy 安装插件:
w2 i whistle.xxx --registry=http://xxx
,或直接用内部命令安装如tnpm
:w2 ti whistle.xxx
w2 uninstall whistle.xxx
:这种卸载方式只支持经过 w2 [x]i whistle.xxx
安装的插件w2 run xxx
:执行经过 w2 i
安装的插件自带命令(不是全局安装,因此不能直接执行)w2 help
whistle 提供了强大且较为完善的操做协议,并支持经过插件定制本身的操做协议,每一个插件就是一个 npm 包,不只拥有 whistle 提供的扩展能力,且具有 node 的全部能力,插件至少能够作如下事情:
如何开发、调试、发布、使用插件,这里不赘述,参见文档便可:wproxy.org/whistle/plu…
whistle 不只支持插件扩展,也能够做为普通 npm 包和插件一块儿打包到第三方应用,具体实现参考: github.com/nohosts/noh…
本文简单说明了 whistle 的操做方式(pattern operation filter
,其中 operation=protocol://opValue
)、命令行操做、插件扩展、以及如何在应用里面集成 whistle,除此以外 whistle 还有不少功能,很难在一篇文章里面详细罗列清楚,你们结合自身业务不断挖掘其功能,期间有什么问题或建议能够经过 GitHub 的 issue 反馈:github.com/avwo/whistl…