Flutter and Proxy

Sergey Yamshchikov
4 min readFeb 17, 2021

I’ve been heavily using proxy to debug my apps networking for last years. And was surprised when I didn’t see network requests from Flutter app in my favourite proxy app. First I was thinking that the problem is in proxy but then it appears to be a known feature 😉

Flutter support only limited Proxy configuration out of the box. Proxy can be configured by providing system environment variables http_proxy, https_proxy, e.t.c (more details in documentation). If you won’t define the variables, your http requests will bypass system proxy configuration.

That’s quite surprising, especially because I didn’t notice any BIG RED warning about this. Just imagine that you configure proxy on your device to surf the Internet safely and some naughty app simply bypass it and send direct requests, not fancy. Also corporate networks often use proxy for access, means you won’t be able to use you awesome Flutter app there.

Proxy config matrix

Let’s dive a bit more deep. And see what option do we have for proxy configuration on different platforms.

Linux is a bit fractured, I will continue investigation. Flutter uses GTK, hence it’s be close to Mac OS, but further investigation needed

As you can see, it’s not that simple. We have different options, proxy config may have credentials which stored securely and we have to access them, there is also option to bypass specified URLs or even use of Proxy Auto Configuration file, which will requires us to fetch the file, evaluate (because it’s a JavaScript) and pass result back to the app. All that means that we have to request the configuration with each request from the app, to get the config enabled for requested URL.

Existing solutions

Well, for the debug purpose you can specify proxy manually with environment variable mentioned before or providing findProxy function to HttpClient or similar options for Dio or http .

Or you can use nice http_proxy plugin , it provides quite basic proxy configuration. But be cautious about using it on production because the plugin change badCertificateCallback to true (more details there), which might make your app vulnerable.

And last but not least option is Dart Dev Tools Networking View

What about production?

Perfect solution would be to have it within Dart VM as it done in JVM and Xamarin (I don’t know about React-Native).

But while we don’t have it I decided to start another plugin, as an experiment, which suppose to support all the features from *Proxy Config Matrix* on different platforms including desktops. It’s called Platform Proxy , it’s not under pub.dev yet, because it’s under development, but you can find it there to try it out.

Before we go to details there is one important thing to mention: findProxy used by Dart to configure proxy is synchronous, means we can’t communicate with the plugin from it. I have opened an issue on Dart-SDL.

Ok, what the plugin supports:

As you can see, it has lot of the features done. You can try it by adding as dependency:

platform_proxy:
git:
url: git@gitlab.com:yamsergey/platform_proxy.git
ref: e66156578858d552aa46a07594dd59c406938ee6

And then use ProxyAwareHttpClient from the package, which wrap HttpClient from dart.io and enrich it with proxy config:

final client = ProxyAwareHttpClient(client: HttpClient(), platformProxy: PlatformProxy());
client.getUrl(Uri.parse('https://flutter.dev')).then((value) => value.close()).then((value) {});

How it looks like:

I have updated basic Flutter app to send HTTP Request on counter click and configured proxy with PAC file (You can read more about it with another story)

Without plugin
With plugin

Seems to work so far 😆 Most of the feature has been done already but I highly appreciate any feedback especially for testing.

Enjoy and happy fluttering 😉

--

--