diff --git a/CHANGELOG.md b/CHANGELOG.md index 47514ab..ea637f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.3.1 +* Update changelog + ## 0.3.0 * Support of latest Android and iOS * Improvements on example app diff --git a/README.md b/README.md index 4898812..23e5384 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,13 @@ # native_flutter_proxy -A flutter plugin to read network proxy info from native. It can be used to set up the network proxy for flutter. -The plugin provides classes to provide the HttpOverrides.global property with a proxy setting. -This ensures that the gap of flutter in supporting proxy communication is filled by a convenient solution. +A Flutter plugin to read system proxy settings from native code and apply them in Dart. +Use it to configure `HttpOverrides.global` with either the system proxy or a custom proxy. + +Key features: +- Read system proxy settings on Android and iOS. +- Auto-update when the system proxy changes via `NativeProxyReader.setProxyChangedCallback`. +- Apply a custom proxy using `CustomProxy` / `CustomProxyHttpOverride`. ## Installing @@ -12,49 +16,57 @@ You should add the following to your `pubspec.yaml` file: ```yaml dependencies: - native_flutter_proxy: latest + native_flutter_proxy: ^0.3.0 ``` +## Quick Start -## Example +```dart +import 'dart:io'; -- Step 1: make your main()-method async -- Step 2: add WidgetsFlutterBinding.ensureInitialized(); to your async-main()-method -- Step 3: read the proxy settings from the wifi profile natively -- Step 4: if enabled, override the proxy settings with the CustomProxy. +import 'package:flutter/widgets.dart'; +import 'package:native_flutter_proxy/native_flutter_proxy.dart'; + +Future applyProxy(ProxySetting settings) async { + if (!settings.enabled || settings.host == null) { + HttpOverrides.global = null; + return; + } + + CustomProxy(ipAddress: settings.host!, port: settings.port).enable(); +} -```dart void main() async { WidgetsFlutterBinding.ensureInitialized(); - bool enabled = false; - String? host; - int? port; try { - ProxySetting settings = await NativeProxyReader.proxySetting; - enabled = settings.enabled; - host = settings.host; - port = settings.port; + final settings = await NativeProxyReader.proxySetting; + await applyProxy(settings); } catch (e) { print(e); } - if (enabled && host != null) { - final proxy = CustomProxy(ipAddress: host, port: port); - proxy.enable(); - print("proxy enabled"); - } + + NativeProxyReader.setProxyChangedCallback((settings) async { + await applyProxy(settings); + }); runApp(MyApp()); } ``` -## Getting Started +## Auto-update on proxy changes + +`NativeProxyReader.setProxyChangedCallback` is invoked whenever the system proxy +changes, so your app can react immediately (including PAC-based proxies on +Android). Register it once at startup and update your overrides there. + +## Manual proxy (optional) -This project is a starting point for a Flutter -[plug-in package](https://flutter.dev/developing-packages/), -a specialized package that includes platform-specific implementation code for -Android and/or iOS. +If you want to force a proxy (e.g. for debugging), you can set it directly: + +```dart +final proxy = CustomProxy(ipAddress: '127.0.0.1', port: 8888); +proxy.enable(); +``` -For help getting started with Flutter, view our -[online documentation](https://flutter.dev/docs), which offers tutorials, -samples, guidance on mobile development, and a full API reference. \ No newline at end of file +For a full example, see `example/lib/main.dart`. diff --git a/pubspec.yaml b/pubspec.yaml index b4299dc..01e307d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: native_flutter_proxy description: A flutter plugin to read and set network proxy info from native. -version: 0.3.0 +version: 0.3.1 homepage: https://github.com/victorblaess/native_flutter_proxy environment: