一、环境搭建
1、需要node.js(node.js可以使用npm命令安装或直接去官网下载即可)
2、必须使用 install -g命令来安装,-g 代表global全局安装 , 无-g的时候代码本地安装,无法直接使用anyproxy命令
npm install -g anyproxy
3、启动命令:anyproxy -i,这时会启动两个端口,分别为8001和8002.8001端口负责监测http proxy,8002端口负责监测web interface。若部分机型使用anyproxy -i无法启动,那可能是你需要找到npm安装anyproxy的具体路径,从该路径下启动anyproxy。例如我自己的机器启动命令:(打码处为用户名)
注:当然可以使用anyproxy -h可以查看帮助命令
二、工具使用
1、anyproxy默认可以抓取http请求,当然也可以抓取https请求。若希望抓取https请求,则需要在android设备和本机都安装RootCA。在command line启动anyproxy后,浏览器访问http://127.0.0.1:8002/,如下:
下载下来后需要在本机信任该证书,关于导入到android设备则可以用adb push命令导入后在android设备对该证书进行信任。
2、启动anyproxy -i命令后,则可以抓取所有的http与https请求,但如果我想抓取特定的接口请求信息呢?别急,anyproxy支持自己定义配置规则。配置规则支持JS格式,通过配置规则,anyproxy会抓取符合配置规则下的接口请求,其余的会过滤掉。启动命令为anyproxy -i -r <配置规则文件名>
3、在第二步结束后,这是其实在http://127.0.0.1:8002/网页或者command line中看到我想抓取的一些接口请求。那么如何获取接口的一些流量信息呢?其实接口的流量信息无非为Request和Response中的Body大小。目前浏览器都支持gizp技术,会把实际body大小进行压缩,保证传输的速率,因此对于Response来说,其实要获取的就是压缩过后的大小,这部分对应的值其实就为Content-length,那这部分值其实就可以在header中获取,下面贴上我的配置规则文件:
module.exports = {
// 获取接口的请求流量
*beforeSendRequest(requestDetail) {
if ((requestDetail.url.indexOf(".PNG") >= 0)||(requestDetail.url.indexOf(".mp4") >= 0)) {//获取接口请求为PNG或者mp4格式
var url = requestDetail.url;
requestLength = requestDetail.requestData.length;//获取接口请求大小
// requestLength = requestDetail.length;
return null;
}
},
// 获取接口的响应流量
// 存储到文件
*beforeSendResponse(requestDetail, responseDetail) {
var fs = require("fs");
const resStr1 = JSON.stringify(responseDetail.response.header.Date);//转换为json格式
const resStr2 = JSON.stringify(responseDetail.response.header.date);
let date1 = new Date(resStr1);
let Str1=date1.getFullYear() + '-' +(date1.getMonth() + 1) + '-' + date1.getDate() + ' ' + date1.getHours() + ':' + date1.getMinutes() + ':' + date1.getSeconds();//将接口的格林威治时间转换为北京时间
let date2 = new Date(resStr2);
let Str2=date2.getFullYear() + '-' +(date2.getMonth() + 1) + '-' + date2.getDate() + ' ' + date2.getHours() + ':' + date2.getMinutes() + ':' + date2.getSeconds();
if ((requestDetail.url.indexOf(".PNG") >= 0)){ //获取接口数据为png的接口请求
var url = requestDetail.url;
var requestLength = requestDetail.requestData.length;
responseLength = responseDetail.response.header['x-anyproxy-origin-content-length']; //获取response header中的content-length
fs.appendFile('/Users/用户名/Desktop/anyproxy/interfacelog.txt',url + ',' +Str1+','+ requestLength + ',' + responseLength+'\n','utf-8',function(err)
{
if(err)
{
console.log(err);
}
});
}
else if(requestDetail.url.indexOf("attention") >= 0)
{
var url = requestDetail.url;
var requestLength = requestDetail.requestData.length;
responseLength = responseDetail.response.header['x-anyproxy-origin-content-length'];
fs.appendFile('/Users/用户名/Desktop/anyproxy/interfacelog.txt',url + ',' +Str2+','+ requestLength + ',' + responseLength+'\n','utf-8',function(err)
{
if(err)
{
console.log(err);
}
});
}
return null;
}
}
4、在txt文件中会展示为url+北京时间+request body大小+response body大小(gzip过后)