whistle使用实践

关于 whistle

whistle 基于 Node 实现的跨平台 web 调试代理工具,主要用于查看、修改 HTTP、HTTPS、Websocket 请求、相应,也可以作为 HTTP 代理服务器使用。一切操作可以通过配置实现,支持域名、路径、正则表达式、通配符等多种匹配方式。

whistle 实践

安装启动

  1. 安装 Node

whistle 支持 v0.10.0 以上版本的 Node,为获取更好的性能,推荐安装最新版本的 Node。

mac 电脑建议使用 nvm 进行 node 的版本管理~

  1. 安装 whistle

Node 安装完成后,执行以下命令安装 whistle:

$ npm install -g whistle

安装完成后运行$ w2 help

如果出现以下结果说明安装成功:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
➜  ~ w2 help

Usage: w2 <command> [options]


Commands:

status Show the running status
add [filepath] Add rules from local js file (.whistle.js by default)
install Install a whistle plugin
uninstall Uninstall a whistle plugin
exec Exec whistle plugin command
run Start a front service
start Start a background service
stop Stop current background service
restart Restart current background service
help Display help information

Options:

-h, --help output usage information
-D, --baseDir [baseDir] set the configured storage root path
-z, --certDir [directory] set custom certificate store directory
-l, --localUIHost [hostname] set the domain for the web ui (local.whistlejs.com by default)
-L, --pluginHost [hostname] set the domain for the web ui of plugin (as: "script=a.b.com&vase=x.y.com")
-n, --username [username] set the username to access the web ui
-w, --password [password] set the password to access the web ui
-N, --guestName [username] set the the guest name to access the web ui (can only view the data)
-W, --guestPassword [password] set the guest password to access the web ui (can only view the data)
-s, --sockets [number] set the max number of cached connections on each domain (256 by default)
-S, --storage [newStorageDir] set the configured storage directory
-C, --copy [storageDir] copy the configuration of the specified directory to a new directory
-c, --dnsCache [time] set the cache time of DNS (60000ms by default)
-H, --host [boundHost] set the bound host (INADDR_ANY by default)
-p, --port [proxyPort] set the proxy port (8899 by default)
-P, --uiport [uiport] set the webui port
-m, --middlewares [script path or module name] set the express middlewares loaded at startup (as: xx,yy/zz.js)
-M, --mode [mode] set the starting mode (as: pureProxy|debug|multiEnv|capture|disableH2|network|rules|plugins|prod)
-t, --timeout [ms] set the request timeout (360000ms by default)
-e, --extra [extraData] set the extra parameters for plugin
-f, --secureFilter [secureFilter] set the path of secure filter
-r, --shadowRules [shadowRules] set the shadow (default) rules
-R, --reqCacheSize [reqCacheSize] set the cache size of request data (600 by default)
-F, --frameCacheSize [frameCacheSize] set the cache size of webSocket and socket's frames (512 by default)
-A, --addon [pluginPaths] add custom plugin paths
--config [workers] start the cluster server and set worker number (os.cpus().length by default)
--cluster [config] load the startup config from a local file
--dnsServer [dnsServer] set custom dns servers
--socksPort [socksPort] set the socksv5 server port
--httpPort [httpPort] set the http server port
--httpsPort [httpsPort] set the https server port
--no-global-plugins do not load any globally installed plugins
--no-prev-options do not reuse the previous options when restarting
--inspect [[host:]port] activate inspector on host:port (127.0.0.1:9229 by default)
--inspectBrk [[host:]port] activate inspector on host:port and break at start of user script (127.0.0.1:9229 by default)
-V, --version output the version number
  1. 启动 whistle

最新版本支持 whistle 三种等价命令:whistlew2wproxy

执行以下命令行启动 whistle:

$ w2 start

执行以下命令行停止 whistle:

$ w2 stop

  1. 配置代理

代理配置有全局代理和浏览器代理(推荐)两种方式。

推荐使用浏览器代理,安装 Chrome 代理插件:SwitchyOmega

可以通过以下两种方式来访问配置页面:

  1. 安装根证书

安装证书请参考文档: http://wproxy.org/whistle/webui/https.html

更多请移步:whistle 官方文档 - 安装启动

whistle 配置

whistle 所有的操作都可以通过类似如下配置方式实现:

pattern operatorURI

    1. whistle 的匹配模式(pattern)大体可以分成 域名、路径、正则、精确匹配、通配符匹配
    1. operatorURI 为对应的操作,由操作协议+操作值组成(operatorURI = opProtocol://opValue):opProtocol(操作协议), 对应某类操作。
    1. pattern 和 operatorURI 在多数情况下位置可以调换,且支持组合模式,具体参见:配置方式
    1. 当 operatorURI 不为 url 的时候,pattern 和 operatorURI 的位置可以交换。

请求替换

1
2
## 访问百度时返回github页面
https://www.baidu.com https://www.github.com

改变请求状态

1
2
## 访问url时返回404
www.example.com statusCode://404

插入标签

1
2
3
www.example.com log://{test_log.js}

www.example.com js:///Users/myname/test/test.js

替换请求内容

1
www.example.com resBody:///Users/myname/Desktop/mock.json

host 修改

1
https://admin.local.ym.qq.com http://localhost:8000

跨域

1
2
# `*` 表示设置 access-control-allow-origin: *
www.example.com resCors://*