Flutter iOS proxy magically stopped working

Viewed 169

When I build apps I use Charles proxy to see/debug network traffic. When I came to flutter I found out that that's not possible. I found a solution to allow proxy, however, it only works on simulator. On device it just refuses to work.

Everything was fine up until now where Flutter no longer wants to deal with a proxy. I don't know if it's to do with the recent update to Flutter or something else.

Everytime I try to make a network call Charles gives me the following error:

SSL Proxying not enabled for this host: enable in Proxy Settings, SSL locations

The problem here, is that the Certificate is accepted so there's no reason for this to be happening. If I fire up any other non-Flutter app or even Safari then everything works as normal.

This is my current proxy enabling code:

class ProxiedHttpOverrides extends HttpOverrides {
  String _port;
  String _host;
  ProxiedHttpOverrides(this._host, this._port);

  @override
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
    // set proxy
      ..findProxy = (uri) {
        return _host != null ? "PROXY $_host:$_port;" : 'DIRECT';
      };
  }
}

/// Allows you to set and enable a proxy for your app
class Proxy {

  static enableProxyIfNeeded() async {
    const env = String.fromEnvironment('ENVIRONMENT');
    dLog('Checking environment for proxy: ${env}');
    if (env == 'DEVELOPMENT' || env == 'STAGING') {
      final proxy = await SystemProxy.getProxySettings();
      if (proxy != null) {
        HttpOverrides.global = ProxiedHttpOverrides(proxy['host']!, proxy['port']!);
        dLog('ENABLED PROXY $proxy');
      }
    } else {
      dLog('DID NOT ENABLE PROXY');
    }
  }
}

Is there a new way to enable proxy for Flutter apps?

0 Answers
Related