Debugging apps using mitmproxy

One of the tools I couldn't do without is an open source project called mitmproxy.

It's easy to setup, and lets you use your laptop as a proxy between a mobile application and the internet. You can then analyse all of the network requests being made, including the contents, payload size, and latency. All of this knowledge is golden when working on app performance.

Here are some examples of improvements I've made to apps as a result of using mitmproxy:

  • Identifying redundant network calls (i.e. the same network call is made twice).
  • Changing the timing of network calls, so the app isn't too data hungry when it starts.
  • Identifying important network calls to cache (high latency or payload size).
  • Optimising API endpoints which are particularly slow.

It's particularly useful when inheritting a messy, legacy app, where the networking behaviour might be difficult to follow. Mitmproxy lets you quickly get a handle on what the app is actually up to.

Mitmproxy is written in Python, and uses modern features like asyncio. This makes it particularly appealing to me.

Getting started on a Mac

# Install
brew install mitmproxy

# Then run the proxy with the web interface.
# The proxy usually run on port 8080, and the web interface on 8081.
mitmweb

On an iOS device:

  • Setup your device's WiFi settings to use the proxy.
  • Open a web browser, and go to mitm.it.
  • Click on the Apple icon, and it'll ask if you want to install a certificate - say yes.
  • Then enable trust for this certificate in the settings by going to General > About > Certificate Trust Settings.
  • Voila!

Gotchas

Don't forget to keep your laptop awake! If it goes to sleep your iOS app won't have internet access. It sounds obvious, but don't let this happen during a demo 😉

Sidekick

The perfect companion to mitmproxy on iOS is the Network Link Conditioner. This lets you simulate poor network performance on an iOS device, and lets you test that the improvements you're making are resulting in a superior user experience.

To set this up, just go to your settings on your iOS device, and Developer > Network Link Conditioner. I usually keep it set to 3G.