|
2 | 2 |
|
3 | 3 | # native_flutter_proxy |
4 | 4 |
|
5 | | -A flutter plugin to read network proxy info from native. It can be used to set up the network proxy for flutter. |
6 | | -The plugin provides classes to provide the HttpOverrides.global property with a proxy setting. |
7 | | -This ensures that the gap of flutter in supporting proxy communication is filled by a convenient solution. |
| 5 | +A Flutter plugin to read system proxy settings from native code and apply them in Dart. |
| 6 | +Use it to configure `HttpOverrides.global` with either the system proxy or a custom proxy. |
| 7 | + |
| 8 | +Key features: |
| 9 | +- Read system proxy settings on Android and iOS. |
| 10 | +- Auto-update when the system proxy changes via `NativeProxyReader.setProxyChangedCallback`. |
| 11 | +- Apply a custom proxy using `CustomProxy` / `CustomProxyHttpOverride`. |
8 | 12 |
|
9 | 13 | ## Installing |
10 | 14 |
|
11 | 15 | You should add the following to your `pubspec.yaml` file: |
12 | 16 |
|
13 | 17 | ```yaml |
14 | 18 | dependencies: |
15 | | - native_flutter_proxy: latest |
| 19 | + native_flutter_proxy: ^0.3.0 |
16 | 20 | ``` |
17 | 21 |
|
| 22 | +## Quick Start |
18 | 23 |
|
19 | | -## Example |
| 24 | +```dart |
| 25 | +import 'dart:io'; |
20 | 26 |
|
21 | | -- Step 1: make your main()-method async |
22 | | -- Step 2: add WidgetsFlutterBinding.ensureInitialized(); to your async-main()-method |
23 | | -- Step 3: read the proxy settings from the wifi profile natively |
24 | | -- Step 4: if enabled, override the proxy settings with the CustomProxy. |
| 27 | +import 'package:flutter/widgets.dart'; |
| 28 | +import 'package:native_flutter_proxy/native_flutter_proxy.dart'; |
| 29 | + |
| 30 | +Future<void> applyProxy(ProxySetting settings) async { |
| 31 | + if (!settings.enabled || settings.host == null) { |
| 32 | + HttpOverrides.global = null; |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + CustomProxy(ipAddress: settings.host!, port: settings.port).enable(); |
| 37 | +} |
25 | 38 |
|
26 | | -```dart |
27 | 39 | void main() async { |
28 | 40 | WidgetsFlutterBinding.ensureInitialized(); |
29 | 41 |
|
30 | | - bool enabled = false; |
31 | | - String? host; |
32 | | - int? port; |
33 | 42 | try { |
34 | | - ProxySetting settings = await NativeProxyReader.proxySetting; |
35 | | - enabled = settings.enabled; |
36 | | - host = settings.host; |
37 | | - port = settings.port; |
| 43 | + final settings = await NativeProxyReader.proxySetting; |
| 44 | + await applyProxy(settings); |
38 | 45 | } catch (e) { |
39 | 46 | print(e); |
40 | 47 | } |
41 | | - if (enabled && host != null) { |
42 | | - final proxy = CustomProxy(ipAddress: host, port: port); |
43 | | - proxy.enable(); |
44 | | - print("proxy enabled"); |
45 | | - } |
| 48 | + |
| 49 | + NativeProxyReader.setProxyChangedCallback((settings) async { |
| 50 | + await applyProxy(settings); |
| 51 | + }); |
46 | 52 |
|
47 | 53 | runApp(MyApp()); |
48 | 54 | } |
49 | 55 | ``` |
50 | 56 |
|
51 | | -## Getting Started |
| 57 | +## Auto-update on proxy changes |
| 58 | + |
| 59 | +`NativeProxyReader.setProxyChangedCallback` is invoked whenever the system proxy |
| 60 | +changes, so your app can react immediately (including PAC-based proxies on |
| 61 | +Android). Register it once at startup and update your overrides there. |
| 62 | + |
| 63 | +## Manual proxy (optional) |
52 | 64 |
|
53 | | -This project is a starting point for a Flutter |
54 | | -[plug-in package](https://flutter.dev/developing-packages/), |
55 | | -a specialized package that includes platform-specific implementation code for |
56 | | -Android and/or iOS. |
| 65 | +If you want to force a proxy (e.g. for debugging), you can set it directly: |
| 66 | + |
| 67 | +```dart |
| 68 | +final proxy = CustomProxy(ipAddress: '127.0.0.1', port: 8888); |
| 69 | +proxy.enable(); |
| 70 | +``` |
57 | 71 |
|
58 | | -For help getting started with Flutter, view our |
59 | | -[online documentation](https://flutter.dev/docs), which offers tutorials, |
60 | | -samples, guidance on mobile development, and a full API reference. |
| 72 | +For a full example, see `example/lib/main.dart`. |
0 commit comments