抓包 抓nodejs的包 抓浏览器的包 抓手机的包

应用场景:

确认接口是能用的,但自己使用时就是不行,参数有没有传正确?格式对不对?傻傻分不清。

抓包工具:
这里演示 charles , 常用的还有 Fiddler, HttpWatch, WireShark.

安装
下载 charles 并安装, 如何激活? 方法较多也比较简单. 你懂的佛曰不可多说.
此处省略500字…

查看代理地址:
点击 Proxy,选择 proxy settings, 输入端口 8888 .
所以我们本机的代理地址可以直接是 127.0.0.1:8888 .
如果其他设备要使用本机的代理, 那就是 本机IP:8888 .

使用之前请确认 http://127.0.0.1:8888 代理程序打开.

抓nodejs的包
我们可以使用 https-proxy-agent 这个库来抓 node.js 的包.

node 原生 https 抓包示例:

var url = require(‘url‘);
var https = require(‘https‘);
var HttpsProxyAgent = require(‘https-proxy-agent‘);

// 要连接的HTTP / HTTPS代理
var proxy = process.env.http_proxy || ‘http://127.0.0.1:8888‘;
console.log(‘using proxy server %j‘, proxy);

// 代理连接的HTTPS端点
var endpoint = process.argv[2] || ‘https://www.httpbin.org/get‘;
console.log(‘attempting to GET %j‘, endpoint);
var options = url.parse(endpoint);

// 使用代理服务器信息创建`HttpsProxyAgent`类的实例
var agent = new HttpsProxyAgent(proxy);
options.agent = agent;

https.get(options, function (res) {
  console.log(‘"response" event!‘, res.headers);
  res.pipe(process.stdout);
});

抓包 抓nodejs的包 抓浏览器的包 抓手机的包

 

 

在 fetch 中使用:

fetch(api, {
  agent: new HttpsProxyAgent("http://127.0.0.1:8888")
})

在 axios 中使用: 很高兴告诉你, axios 中不需要其他依赖库

axios.get(api, {
  proxy: {
    host: ‘127.0.0.1‘,
    port: 8888,
  }
})

抓浏览器的包
导出相应的证书, 以供不同地方使用.

抓包 抓nodejs的包 抓浏览器的包 抓手机的包

安装证书到系统

配置系统代理。

抓手机的包
让手机和电脑使用同一网络

安装证书到手机系统

把手机的代理设置为电脑的IP和端口

防坑锦囊
一些浏览器需要把证书安装到浏览器上, 比如火狐.

不允许自签证书抓包
Error: SSL Error: SELF_SIGNED_CERT_IN_CHAIN
Error: self signed certificate in certificate chain

使用 NODE_TLS_REJECT_UNAUTHORIZED=’0’ 变量启动 node 程序即可.

解决抓到的报文乱码
在 Proxy → SSL Proxying 菜单下, 下载根证书, 并且在钥匙串里设置信任此证书.

安装证书是为了解析 https 请求.

扩展阅读
https-proxy-agent 官网:
https://www.npmjs.com/package/https-proxy-agent

Charles抓包工具 for MAC配置与使用
https://juejin.im/post/5b690cbaf265da0f6436ec67

解决Charles抓取https报文乱码问题
https://www.jianshu.com/p/60b2b76b9066

?

抓包 抓nodejs的包 抓浏览器的包 抓手机的包

上一篇:sign in with apple后端校验(java)


下一篇:【iOS翻译】App启动时的响应过程