A fast way to connect to the web proxy
Configuring Android device to connect to the web proxy running on development build machine is tedious.
You have to make the same steps over and over again on every device you want to debug HTTP traffic.<br/ >
It goes like this:
- Check development machine IP address.
- Install proxy CA certificate on a mobile device in order to spy HTTPS traffic.
- Configure wifi connection on the mobile device to connect through the proxy (by typing it by hand).
Sometimes I have to enable the proxy for a moment and disable it afterward, then… repeat it a couple of times.
Seems to be pretty good case to automate it :)
I wanted to have a simple switcher which allows quickly enabling or disabling the proxy connection.
The solution I made is based on a static global variable.
If the flag is set to
true the app connects to the web proxy automatically.
You earned a few things here:
- It’s transparent for a developer. You don’t care about typing IP address and port.
- Installs CA certificate at runtime. You don’t have to install it by hand on the device. You don’t even have to have password for credential storage ;)
- Because the device isn’t directly connected, the web proxy can see traffic from your app only (other apps’ traffic isn’t visible).
- You can dynamically enable/disable the proxy for HTTP client in runtime so you can pass some requests over the proxy.
- It’s safer. You don’t install proxy or any third party certificate on the system directly.
Let’s code it.
Add this task on the top of
It gets local IP address of your desktop build machine and store it in
Remember to replace
ifconfig command with proper
ipconfig if your build machine is running Windows.
You have to also ensure that
en1 is the interface used to connect to the same wifi network as Android device.
Now, make above task depended on
preBuild Android’s task to run it on the beginning of the build process:
I made one more thing here.
Copied value of
BuildConfig.BUILD_MACHINE_LOCAL_IP to have access to IP address in code.
The last part is to set up our proxy for HTTP client.
if statement which enables proxy when flag is active.
Probably, you don’t want to have the proxy connection for release builds so I also check build type here.
The last thing is to set up CA certificate of the web proxy to make HTTP connections visible on it.
SslUtils is simple helper class which I created to trust given certificate despite system settings.
You can read about
SslUtils in this post and download it from GitHub.
If you have any tips or thoughts about this method, let me know about it :)
See this post on my personal blog.