應用場景:
確認接口是能用的,但自己使用時就是不行,參數有沒有傳正確?格式對不對?傻傻分不清。
抓包工具:
這里演示 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);
});
在 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,
}
})
抓瀏覽器的包
導出相應的證書, 以供不同地方使用.
安裝證書到系統
配置系統代理。
抓手機的包
讓手機和電腦使用同一網絡
安裝證書到手機系統
把手機的代理設置為電腦的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