Skip to content

A Flutter Plugin to get system plugin. Use to set dart HttpClient proxy for grabing requests

License

Notifications You must be signed in to change notification settings

Betterment/system_proxy

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

system_proxy

A Flutter Plugin to get system proxy setting. It is used to proxy for Flutter and Dart HttpClient request。Because Dart HttpClient does not adapt the system proxy setting automatically in default, it's difficult to grab requests for testing .

获取系统代理配置。 可用于为flutter的Dart HttpClient的请求设置代理,因为默认Dart请求不会理睬系统代理配置,这对于抓取请求来进行测试等场景造成不小困难。

Getting Started

Install

Please add the plugin to pubspec.yaml.

Caution

It's useful for Android >= 23 .

Usage

Import

import 'package:system_proxy/system_proxy.dart';

Code

// the proxy value likes:  {port: 8899, host: 127.0.0.1}
Map<String, String> proxy = await SystemProxy.getProxySettings();
if (proxy != null) {
    print('proxy $proxy');
}

Setting Proxy for HttpClient

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';
      };
  }
}

void main() async {
  Map<String, String> proxy = await SystemProxy.getProxySettings();
  if (proxy == null) {
    proxy = {
    'host': null,
    'port': null
    };
  }
  HttpOverrides.global = new ProxiedHttpOverrides(proxy['host'], proxy['port']);
  runApp(MyApp());
}

About

A Flutter Plugin to get system plugin. Use to set dart HttpClient proxy for grabing requests

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 46.5%
  • Java 19.9%
  • Ruby 19.4%
  • Swift 10.6%
  • Objective-C 3.6%