docs/docs/cli.md
Whistle 命令行版本支持以下命令行操作,查看完整命令行功能执行命令:w2 -h:
Usage: w2 <command> [options]
Commands:
status Display running status
add Add rules from local JS file (.whistle.js by default)
proxy Configure system proxy settings
ca Manage Root CA certificates
install Install Whistle plugin
uninstall Uninstall Whistle plugin
exec Execute 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 storage root path
-z, --certDir [directory] set custom certificate directory
-l, --localUIHost [hostname] set web UI domain (local.whistlejs.com by default)
-L, --pluginHost [hostname] set plugin UI domains (as: "script=a.b.com&vase=x.y.com")
-n, --username [username] set web UI username
-w, --password [password] set web UI password
-N, --guestName [username] set web UI guest username (read-only)
-W, --guestPassword [password] set web UI guest password (read-only)
-s, --sockets [number] set max cached connections per domain (256 by default)
-S, --storage [newStorageDir] set configuration storage directory
-C, --copy [storageDir] copy configuration from specified directory
-c, --dnsCache [time] set DNS cache time (default: 60000ms)
-H, --host [boundHost] set bound host (default: INADDR_ANY)
-p, --port [proxyPort] set proxy port (default: 8899 by default)
-P, --uiport [uiport] set web UI port
-m, --middlewares [script path or module name] set startup middlewares (format: xx,yy/zz.js)
-M, --mode [mode] set startup mode (options: pureProxy|debug|multiEnv|capture|disableH2|network|rules|plugins|prod)
-t, --timeout [ms] set request timeout (default: 360000
-e, --extra [extraData] set plugin extra parameters
-f, --secureFilter [secureFilter] set secure filter path
-r, --shadowRules [shadowRules] set default shadow rules
-R, --reqCacheSize [reqCacheSize] set request data cache size (default: 600)
-F, --frameCacheSize [frameCacheSize] set WebSocket frame cache size (default: 512)
-A, --addon [pluginPaths] add custom plugin paths
--init [bypass] auto configure proxy and install Root CA
--cluster [workers] start cluster with worker count (default: CPU cores)
--config [config] load startup config from file
--rcPath [rcPath] load configuration from file (default: ~/.whistlerc)
--dnsServer [dnsServer] set custom DNS servers
--socksPort [socksPort] set SOCKSv5 server port
--httpPort [httpPort] set HTTP server port
--httpsPort [httpsPort] set HTTPS server port
--allowOrigin [originList] set allowed CORS origins (format: a.b.c,x.y.z or *)
--no-global-plugins disable global plugins
--no-prev-options ignore previous options on restart
--inspect [[host:]port] enable inspector (default: 127.0.0.1:9229)
--inspectBrk [[host:]port] enable inspector with breakpoint (default: 127.0.0.1:9229)
-V, --version output the version number
w2 start:启动 Whistle,并使用默认存储目录w2 start -p 8100:启动指定端口的 Whistle(默认为 8899)w2 start --httpsPort 8001:启动 Whistle,并开启 HTTPS 代理端口w2 start --socksPort 1080:启动 Whistle,并开启 SOCKSv5 代理端口w2 start -S storageName:启动指定存储目录的 Whistle(大写 S)
storageName应为纯目录名(非完整路径),多实例运行时需满足:
- 每个实例使用独立目录
- 配置不同监听端口
shw2 start w2 start -p 8100 -S storageName2
w2 restart:重启 Whistle
w2 restart --no-prev-options:等价于 w2 stop && w2 startw2 restart -p 8100:重启并修改端口w2 starrestartt --httpsPort 8001:重启并开启 HTTPS 代理端口w2 restart --socksPort 1080:重启并开启 SOCKSv5 代理端口w2 restart -S storageName:重启指定存储目录的 Whistle 实例w2 stop:停止默认存储目录下的 Whistlew2 stop -S storageName:停止指定存储目录的 Whistle可通过下面
w2 status --all查看当前命令行后台运行的 Whistle 实例
w2 status --all
[i] All running Whistle instances:
1. PID: 51512, Port: 8899
2. PID: 53951, Port: 8080, Storage: 8080
w2 status
[!] whistle@version is running
[i] 1. use your device to visit the following URL list, gets the IP of the URL you can access:
http://127.0.0.1:8899/
http://192.168.10.153:8899/
http://10.211.55.2:8899/
http://10.37.129.2:8899/
Note: If all URLs are inaccessible, check firewall settings
For help see https://github.com/avwo/whistle
[i] 2. set the HTTP proxy on your device with the above IP & PORT(8899)
[i] 3. use Chrome to visit http://local.whistlejs.com/ to get started
w2 add:执行当前目录下的 .whistle.js(或 .whistle.mjs)并将 export 出来的 name、rules、或 groupName(可选) 设置到界面的 Rules 里面w2 add filepath:自定义执行的文件.whistle.js 文件内容:
const pkg = require('./package.json');
exports.groupName = '项目开发环境'; // 可选,设置分组, 要求 Whistle 版本 >= v2.9.21
exports.name = `[${pkg.name}]本地环境配置`;
exports.rules = `
test.example.com http://127.0.0.1:5566
# cgi走现网
test.example.com/cgi-bin ignore://http
或异步获取规则:
const assert = require('assert);
const path = require('path');
const pkg = require('./package.json');
module.exports = (cb, util) => {
// 如果依赖插件,可以检查插件
assert(util.existsPlugin('@scope/whistle.combo')
|| util.existsPlugin('whistle.combo'), '请先安装插件: w2 i whisltle.combo');
// 也可以远程获取规则
// do sth
cb({
name: `[${pkg.name}]本地环境配置`,
rules: `
test.example.com/combo whisle.combo://${path.join(__dirname, 'dev')}
test.example.com http://127.0.0.1:5566
# 接口走现网
test.example.com/cgi-bin ignore://http
`
});
};
Whistle 会检查是否存在同名规则:
--force 参数:
w2 add --force
w2 proxy:设置系统代理
127.0.0.18899w2 proxy 0:关闭系统代理w2 proxy 8100:设置系统代理
127.0.0.18100w2 proxy www.test.com:8100:设置系统代理
www.test.com8100w2 ca:安装本地 Whistle 的根证书(安装本地 Whistle 根证书一般用这个命令即可)w2 ca 8080:从指定端口(IP:127.0.0.1)下载 Whistle 根证书并安装w2 ca www.test.com:8080:安装指定端口及 IP(或域名)的 Whistle 证书(可用于安装远程的 Whistle 根证书)w2 ca certUrl:下载指定 URL 的证书并安装w2 ca localCertPath:安装本地指定路径的证书w2 ca 在 macOS 和 Windows 上可自动安装根证书,但在 Linux 上需要手动安装。请先通过 Whistle 界面(HTTPS 菜单 → Download RootCA)下载根证书文件 rootCA.crt 到当前目录,然后根据发行版执行以下命令:
sudo cp rootCA.crt /usr/local/share/ca-certificates/
sudo update-ca-certificates
sudo cp rootCA.crt /etc/pki/ca-trust/source/anchors/
sudo update-ca-trust
sudo apk add ca-certificates
sudo cp rootCA.crt /usr/local/share/ca-certificates/
sudo update-ca-certificates
安装完成后,可通过以下命令验证证书是否生效:
sh# 设置代理并测试 HTTPS 请求 export https_proxy=http://127.0.0.1:8899 curl https://www.baidu.com
w2 install whistle.script:安装插件w2 install whistle.script --registry=https://npm-registry:安装插件,并指定 npm registry推荐使用界面安装:使用插件
w2 uninstall whistle.script:卸载指定插件
推荐使用界面卸载:使用插件
w2 exec xxx:执行插件在 package.json 中配置的 bin 命令(即插件提供的可执行脚本)
适用于插件开发者或需要调用插件 CLI 功能的场景
w2 run:以开发调试模式启动 Whistle,实时输出插件和系统的日志信息到控制台,修改界面代码刷新自动生效等,并支持所有 w2 start 的参数配置
以下是优化后的帮助文档:
当需要同时运行多个实例时,必须为每个实例配置不同的端口和独立的存储目录,避免资源冲突。
# 实例1 - 端口8010,存储目录8010
w2 start --port 8010 --storage 8010
# 实例2 - 端口8011,存储目录8011
w2 start --port 8011 --storage 8011
# 实例3 - 端口8012,存储目录8012
w2 start --port 8012 --storage 8012
| 参数 | 作用 | 要求 |
|---|---|---|
--port | 服务监听端口 | 必须唯一,不能重复 |
--storage | 数据存储目录 | 必须唯一,避免数据混淆 |
推荐配置:
配置示例:
# ✅ 推荐:端口与存储目录一致
w2 start -p 8010 -S 8010
w2 start -p 8011 -S 8011
# ✅ 允许:端口与存储目录不同(需确保唯一性)
w2 start -p 8020 -S instance_1
w2 start -p 8021 -S instance_2
# ❌ 错误:端口或存储目录重复
w2 start -p 8010 -S 8010
w2 start -p 8010 -S 8011 # 端口冲突!
w2 start -p 8011 -S 8010 # 存储目录冲突!
通过为每个实例分配唯一的端口和存储目录,可以确保多个实例稳定并行运行。
Whistle 启动时会自动加载 ~/.whistlerc 配置文件。您可以通过以下方式管理配置加载:
~/.whistlerc--rcPath [rcPath] 参数指定其他配置文件-rcPath none 参数完全忽略配置文件.whistlerc 文件采用键值对格式,支持多种配置作用域:
# 全局默认配置(没有指定 storage)
username=test
password=123
# 特定存储配置(通过命令行指定 storage)
xxx.username=test-storage
xxx.password=123-storage
# 通配符配置(对所有实例生效,优先级最低)
*.username=test-default
*.password=123-default
xxx 为启动参数 --storage 对应的值(没有 storage 则使用缺省的配置)* 表示对所实例生效(优先级最低)-D, --baseDir [baseDir]:自定义 Whistle 存储根目录(默认为 $WWHISTLE_PATH/.whistle)
示例:
w2 start -D ~/my_whistle_data
-n, --username [username]:设置管理界面登录用户名-w, --password [password]:设置管理界面登录密码
示例:
w2 start -n abc -w 123
-N, --guestName [username]:设置只读权限的游客账号(游客仅��查看配置和抓包,不可修改)-W, --guestPassword [password]:设置游客账号密码(游客仅可查看配置和抓包,不可修改)
示例:
w2 start -N test -W 123
-P, --uiport [uiport]:单独设置管理界面端口(默认与代理端口相同)
示例:
w2 start -P 8889
-e, --extra [extraData]:启动时向指定插件传递数据(如:{inspect: data})
示例:
w2 start -e '{"debug":true}'
--allowOrigin [originList]:允许跨域请求管理界面接口的域名
示例:
w2 start --allowOrigin *
--no-global-plugins:启动时不加载 npm i -g whistle.xxx 安装的插件
示例:
w2 start --no-global-plugins
--inspect [[host:]port]:启用Node.js调试(默认9229端口),配合Chrome DevTools使用
示例:
w2 start --inspect
--inspectBrk [[host:]port]:启用调试并在首行断点
示例:
w2 start --inspectBrk
--config [config]:通过配置文件加载参数
示例:
w2 start --config /data/xxx.json
给请求设置用户密码需要用到插件(或自己开发插件):whistle.proxyauth