乐闻世界logo
搜索文章和话题

whistle 支持哪些协议,如何处理不同协议的代理?

2月21日 16:26

答案

Whistle 支持多种协议的代理,包括 HTTP、HTTPS、WebSocket、HTTP/2 等,可以满足不同场景的调试需求。

支持的协议

1. HTTP 协议

基本代理:

shell
www.example.com host 127.0.0.1:8080

转发代理:

shell
www.example.com forward http://127.0.0.1:8080

修改请求:

shell
www.example.com reqHeaders://{custom-headers.json}

2. HTTPS 协议

基本代理:

shell
www.example.com host 127.0.0.1:8443

启用 HTTPS 拦截:

shell
pattern whistle.https://

配置 HTTPS 证书:

  1. 下载根证书
  2. 安装到系统
  3. 启用 HTTPS 拦截

3. WebSocket 协议

基本代理:

shell
ws://example.com host 127.0.0.1:8080 wss://example.com host 127.0.0.1:8080

转发代理:

shell
ws://example.com forward ws://127.0.0.1:8080 wss://example.com forward wss://127.0.0.1:8080

WebSocket 消息捕获:

  • 自动捕获连接建立
  • 记录发送和接收的消息
  • 显示连接状态

4. HTTP/2 协议

Whistle 支持 HTTP/2 协议的代理:

  • 自动降级到 HTTP/1.1
  • 支持 HTTP/2 的多路复用
  • 支持服务器推送

5. SOCKS 代理

配置 SOCKS 代理:

shell
www.example.com socks://127.0.0.1:1080

SOCKS5 代理:

shell
www.example.com socks5://127.0.0.1:1080

协议转换

1. HTTP 转 HTTPS

配置规则:

shell
http://www.example.com forward https://www.example.com

2. HTTPS 转 HTTP

配置规则:

shell
https://www.example.com forward http://www.example.com

3. WebSocket 转 HTTP

配置规则:

shell
ws://example.com forward http://127.0.0.1:8080

协议特定配置

1. HTTP 协议配置

设置请求方法:

shell
www.example.com method://POST

设置请求头:

shell
www.example.com reqHeaders://{http-headers.json}

设置响应头:

shell
www.example.com resHeaders://{http-headers.json}

2. HTTPS 协议配置

禁用 SSL 验证:

shell
www.example.com disable://ssl

设置 SSL 版本:

shell
www.example.com ssl://TLSv1.2

自定义 CA 证书:

shell
www.example.com ca://{custom-ca.crt}

3. WebSocket 协议配置

设置 WebSocket 子协议:

shell
ws://example.com protocol://chat

设置 WebSocket 超时:

shell
ws://example.com timeout://30000

设置 WebSocket 心跳:

shell
ws://example.com ping://30000

多协议场景

1. 混合协议代理

同时代理 HTTP 和 HTTPS:

shell
www.example.com host 127.0.0.1:8080 api.example.com host 127.0.0.1:8080

同时代理 WebSocket 和 HTTP:

shell
ws://example.com host 127.0.0.1:8080 www.example.com host 127.0.0.1:8080

2. 协议路由

根据协议路由:

shell
# HTTP 路由到本地 http://www.example.com host 127.0.0.1:8080 # HTTPS 路由到测试服务器 https://www.example.com host test.example.com # WebSocket 路由到本地 ws://www.example.com host 127.0.0.1:8080

3. 协议转换场景

开发环境使用 HTTP,生产环境使用 HTTPS:

shell
# 开发环境 www.example.com forward http://127.0.0.1:3000 # 生产环境 www.example.com forward https://api.example.com

协议调试技巧

1. 查看协议信息

在 whistle 管理界面中:

  1. 点击 "Network" 标签
  2. 点击请求查看详情
  3. 查看 "General" 标签中的协议信息

2. 协议降级测试

测试 HTTP/2 降级:

shell
www.example.com disable://h2

测试 WebSocket 降级:

shell
ws://example.com disable://websocket

3. 协议性能测试

测试不同协议的性能:

shell
# HTTP/1.1 www.example.com disable://h2 # HTTP/2 www.example.com enable://h2

协议安全

1. HTTPS 安全

启用 HTTPS 拦截:

shell
pattern whistle.https://

验证证书:

shell
www.example.com verify://ssl

禁用不安全的协议:

shell
www.example.com disable://ssl3 www.example.com disable://tls1

2. WebSocket 安全

使用 WSS:

shell
wss://example.com host 127.0.0.1:8080

验证 WebSocket 握手:

shell
ws://example.com verify://websocket

协议兼容性

1. 浏览器兼容性

  • Chrome/Edge:完全支持所有协议
  • Firefox:支持 HTTP、HTTPS、WebSocket
  • Safari:支持 HTTP、HTTPS、WebSocket

2. 服务器兼容性

  • Nginx:支持 HTTP、HTTPS、WebSocket
  • Apache:支持 HTTP、HTTPS
  • Node.js:支持所有协议

3. 移动端兼容性

  • iOS:支持 HTTP、HTTPS、WebSocket
  • Android:支持 HTTP、HTTPS、WebSocket

协议最佳实践

1. 开发环境

  • 使用 HTTP 协议便于调试
  • 使用 WebSocket 实时通信
  • 禁用 SSL 验证加快速度

2. 测试环境

  • 使用 HTTPS 模拟生产环境
  • 使用 WSS 测试安全连接
  • 启用 SSL 验证确保安全

3. 生产环境

  • 使用 HTTPS 确保安全
  • 使用 WSS 保护 WebSocket
  • 启用所有安全验证

协议故障排查

1. HTTPS 连接失败

检查项:

  • 证书是否正确安装
  • SSL 版本是否匹配
  • 防火墙是否阻止

解决方法:

shell
www.example.com disable://ssl

2. WebSocket 连接失败

检查项:

  • 代理规则是否正确
  • 服务器是否支持 WebSocket
  • 网络是否稳定

解决方法:

shell
ws://example.com forward ws://127.0.0.1:8080

3. 协议不兼容

检查项:

  • 客户端和服务器协议版本
  • 浏览器支持情况
  • 服务器配置

解决方法:

shell
www.example.com disable://h2
标签:Whistle