简体   繁体   English

Android:如何以编程方式捕获App的网络流量?

[英]Android: How to capture App's Network Traffic Programmatically?

I'm currently working on a library that enables QA or Developers debug network traffic in their app, we currently use OKHttp and I know how to create an interceptor and dispatch all request data to the lib. 我目前正在开发一个库,使QA或开发人员能够在他们的应用程序中调试网络流量,我们目前使用OKHttp,我知道如何创建拦截器并将所有请求数据分发给lib。 such that Developers or QA can view such data (Payload/URL/Size/Response Codes/Duration...etc), however I want to create a more generic solution that listens to HTTP traffic or even TCP traffic then take it from there, however I couldn't find a starting point, I know that this is possible since Firebase Performance is doing it, but still I couldn't find an API or anyway to listen to such traffic. 这样开发人员或QA可以查看这样的数据(有效载荷/ URL /大小/响应代码/持续时间......等),但我想创建一个更通用的解决方案来监听HTTP流量甚至TCP流量,然后从那里获取,但是我找不到起点,我知道这可能是因为Firebase Performance正在这样做,但我仍然无法找到API或者无论如何都要听这样的流量。

I hope that someone from Google's Firebase Performance Team shares some info about how they do it if it's not a trade-secret :) 我希望来自Google Firebase Performance Team的人分享一些关于他们如何做的信息,如果它不是商业机密:)

I came across this solution: https://github.com/cyruliu/Sensitive_API_Monitor/blob/master/app/src/main/java/com/android/reverse/apimonitor/NetWorkHook.java However it looks kinda bad with reflection, I hope to find a better way. 我遇到了这个解决方案: https//github.com/cyruliu/Sensitive_API_Monitor/blob/master/app/src/main/java/com/android/reverse/apimonitor/NetWorkHook.java然而它看起来有点反思,我希望找到更好的方法。

The short answer: It's not possible without proxying the whole device. 简短的回答:没有代理整个设备是不可能的。

For OkHttp3 specifically I wrote a lib here: https://github.com/shehabic/sherlock that implements an interceptor that creates a new session every time your open your app and captures network requests, you can also have multiple session, it adds a floating icon that helps you access sessions and requests' history as well as some export capabilities, additionally it adds a secondary launcher icon that you can use to access the captures requests without interrupting the app's process. 对于OkHttp3,我在这里写了一个lib: https//github.com/shehabic/sherlock ,它实现了一个拦截器,每当你打开你的应用程序并捕获网络请求时创建一个新的会话,你也可以有多个会话,它增加了一个浮动图标,可帮助您访问会话和请求的历史记录以及一些导出功能,此外,它还添加了一个辅助启动器图标,您可以使用该图标访问捕获请求,而不会中断应用程序的进程。

Regarding Firebase Performance, all the magic is actually in the Firebase-Perf Gradle plugin; 关于Firebase性能,所有的魔力实际上都在Firebase-Perf Gradle插件中; to simplify it, it scans through the project's code and replaces direct calls to OkHttpClient to be be proxied through their own FirebaseOkHttpClient and from there they get all the details even for https request as they become the http client. 为了简化它,它扫描项目的代码并替换对OkHttpClient的直接调用,以通过他们自己的FirebaseOkHttpClient进行代理,并从那里获得所有细节,甚至是https请求,因为它们成为http客户端。

I hope that this saves someone some time in the future. 我希望将来能节省一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM