AnyProxy做App网络流量测试

前言:

AnyProxy是一个开放式的HTTP代理服务器。
Github主页:[https://github.com/alibaba/anyproxy]
主要特性包括:

  • 基于Node.js,开放二次开发能力,允许自定义请求处理逻辑
  • 支持Https的解析
  • 提供GUI界面,用以观察请求

本次教程以Mac系统为主。


一、安装

作为全局模块,安装anyproxy:

sudo npm install -g anyproxy
命令行启动AnyProxy,默认端口号8001
sudo anyproxy
  • 启动后将终端http代理服务器配置为127.0.0.1:8001即可
  • 访问http://127.0.0.1:8002 ,web界面上能看到所有的请求信息

其他命令:配置启动端口,如8888端口启动

sudo anyproxy --port 8888

二、安装证书

1、Mac系统生成RootCA

命令行执行:sudo anyproxy –root
执行完成之后,会打开证书的安装路径,即可看到 rootCA.crt 文件,双击打开,找到刚刚导入的AnyProxy证书,配置为信任(Always Trust)。
 
2、iOS系统信任CA证书
1、配置手机Wi-Fi代理:本地IP+8001
2、点击web界面http://localhost:8002/中的 Root CA,按提示扫描二维码即可安装 

3、按提示扫描二维码即可安装

4、信任CA证书:
设置->通用->关于本机->证书信任设置 中把AnyProxy证书的开关打开,否则safari将报错。
 
 

三、rule模块

AnyProxy提供了二次开发的能力,你可以用js编写自己的规则模块(rule),来自定义网络请求的处理逻辑。
下文编写anyproxy过滤规则my.js ,过滤规则是将请求资源地址、请求数据大小、接受数据大小记录到文件,代码如下:

var fs = require("fs");
var path = "/Users/wj/log1";
var log = path + "/" + "log.txt";
console.log(log);

module.exports = {
    *beforeSendRequest(requestDetail) {
            //获取请求资源地址
            url = requestDetail.url;
            //获取Request数据大小
            Request = requestDetail.requestData.length;
    },
    
    *beforeSendResponse(requestDetail, responseDetail){
        //获取Response数据大小
        Response = responseDetail.response.body.length;
        fs.appendFileSync(log, url + "," + Request + "," + Response + "
 ", 'utf-8', function (err) {
              if (err) throw err;
              console.log('err');
        });
  },        
};
引用规则命令:
sudo anyproxy --intercept --rule my.js

打开简书app,点击各个页面,查看log.txt:

 https://upload-images.jianshu.io/upload_images/7116457-7df74ab5437ee06a.png?imageMogr2/auto-orient/strip|imageView2/1/w/160/h/160,0,25332
 https://upload-images.jianshu.io/upload_images/7116457-7df74ab5437ee06a.png?imageMogr2/auto-orient/strip|imageView2/1/w/160/h/160,0,5850
 https://upload-images.jianshu.io/upload_images/7116457-7df74ab5437ee06a.png?imageMogr2/auto-orient/strip|imageView2/1/w/160/h/160,0,11190
 https://upload-images.jianshu.io/upload_images/7116457-7df74ab5437ee06a.png?imageMogr2/auto-orient/strip|imageView2/1/w/160/h/160,0,1609
 https://www.jianshu.com/mobile/notes/23257228/reward_section,0,0
 https://www.jianshu.com/mobile/notes/23257228/reward_section,0,33254

逗号做分隔,资源地址、请求数据大小、返回数据大小,单位是字节数。


四、读取log文件

下一步使用python读取log文件内容,统计数据信息并做分析。

import xlwt

read = open("/Users/wangjuan/log1/log.txt", 'r')
file = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet = file.add_sheet('data')

line = 1
column = 0
title = ['Resource', '流量(KB)']
for i in title:
    sheet.write(0, column, i)
    column += 1

res = read.readlines()
for i in range(0, len(res)):
    res_list = res[i].split(",")
    resource = res_list[0].split(",")[0]
    resource_url = resource.split("?")[0]
    request_length = res_list[1]
    response_length = res_list[2].split("
")[0]
    length = int(response_length) - int(request_length)

    sheet.write(line, 0, resource_url)
    sheet.write(line, 1, length)
    line += 1

file.save("/Users/wangjuan/log1/log.xls")
结果如下图:

以上~欢迎沟通~~

 
原文地址:https://www.cnblogs.com/xiaoxi-3-/p/8693347.html