Flutter Proxy Detector

A Flutter plugin that detects system proxy settings and can enforce them on the native network configuration.

Motivation

Many Flutter plugins and SDKs, such as the Amplify SDK (AWS), use native networking stacks for their communication. In some cases, these native modules might ignore the user's system-level proxy settings on Android or iOS. This can cause applications to fail or become unreachable when operating under a network that requires a proxy.

This plugin allows you to detect the active proxy configuration and explicitly apply it to the JVM/system properties, ensuring that native network modules honor the proxy settings.

Features

  • Detect Proxy Settings: Retrieve the current HTTP/HTTPS proxy host and port configured on the device.
  • Apply System Proxy: Enforce the native network stack to use the detected system proxy. This is critical for ensuring that all network traffic, including that from native libraries, honors the user's proxy configuration.
  • Platform Support:
    • ✅ Android
    • ✅ iOS
    • ✅ macOS
    • ✅ Linux
    • ✅ Windows

Getting Started

Installation

Add the plugin to your pubspec.yaml.

dependencies:
  flutter_proxy_detector: ^0.0.1

Usage

Import the package in your Dart code:

import 'package:flutter_proxy_detector/flutter_proxy_detector.dart';

1. Get Current Proxy Settings

You can check if a proxy is currently configured and get its details (host and port).

final _flutterProxyDetectorPlugin = FlutterProxyDetector();

try {
  // Returns "host:port" string or null if no proxy is detected
  final String? proxyInfo = await _flutterProxyDetectorPlugin.getProxySetting();
  
  if (proxyInfo != null) {
    print('Proxy detected: $proxyInfo');
  } else {
    print('No system proxy detected');
  }
} on PlatformException catch (e) {
  print('Failed to get proxy setting: ${e.message}');
}

2. Apply System Proxy

Force the application's native network layer to apply the system proxy settings. This is useful on Android to ensure System.getProperty("http.proxyHost") and related properties are synced with the active network's proxy configuration.

try {
  await _flutterProxyDetectorPlugin.applySystemProxyIfAvailable();
  print('System proxy applied successfully');
} on PlatformException catch (e) {
  print('Failed to apply proxy: ${e.message}');
}

Platform Specifics

Android

The plugin uses ConnectivityManager to detect the default proxy for the active network.

  • getProxySetting: Returns the host and port of the active proxy.
  • applySystemProxyIfAvailable: Sets the JVM system properties (http.proxyHost, http.proxyPort, https.proxyHost, https.proxyPort) to match the active network proxy.

iOS

The plugin uses CFNetworkCopySystemProxySettings to retrieve proxy configuration.

  • getProxySetting: Returns the HTTP proxy host and port from the system settings.
  • applySystemProxyIfAvailable: Sets the connectionProxyDictionary of URLSessionConfiguration.default. Note that this affects URLSession based connections that use the default configuration.

macOS

The plugin uses CFNetworkCopySystemProxySettings similar to iOS but includes full HTTPS proxy support.

  • getProxySetting: Returns the HTTP proxy host and port from the system settings.
  • applySystemProxyIfAvailable: Sets the connectionProxyDictionary of URLSessionConfiguration.default with both HTTP and HTTPS proxy configurations.

Linux

The plugin checks environment variables for proxy configuration.

  • getProxySetting: Checks http_proxy and HTTP_PROXY environment variables.
  • applySystemProxyIfAvailable: No-op on Linux as typically environment variables are automatically respected by most libraries.

Windows

The plugin uses WinHttpGetIEProxyConfigForCurrentUser to retrieve the current user's IE proxy configuration.

  • getProxySetting: Returns the proxy string from Windows settings.
  • applySystemProxyIfAvailable: No-op on Windows.

License

See the LICENSE file for details.