Skip to content

Forking this to be able to automate publishing to Cloudsmith.. A Flutter Plugin to detect System proxy. When using HTTP client that are not proxy aware this plugin can help with finding the proxy from system settings which then can be used with HTTP Client to make a successful request.

License

Notifications You must be signed in to change notification settings

Alkami/flutter_system_proxy

 
 

Repository files navigation

Flutter System Proxy

A Flutter Plugin to detect System proxy. When using HTTP client that are not proxy aware this plugin can help with finding the proxy from system settings which then can be used with HTTP Client to make a successful request.

Getting Started

Installation

dependencies:
  ...
  flutter_system_proxy:
    git: 
      url: https://github.com/BrowserStackCE/flutter_system_proxy.git
      ref: main

Basic Usage (Example With Dio)

import 'package:flutter_system_proxy/flutter_system_proxy.dart';


...


var dio = new Dio();
var url = "https://....";
var proxy = await FlutterSystemProxy.findProxyFromEnvironment(url);
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
   (HttpClient client) {
      client.findProxy = (uri) {
        return proxy;
   };
};
var response = await dio.get(url);

Advanced Usage (Custom Dio Adapter)

// Create a custom adapter that can help resolve proxy based on urls 
// (This is important as in some senerio there are PAC files which might have different proxy based on different urls)
class MyAdapter extends HttpClientAdapter {
  final DefaultHttpClientAdapter _adapter = DefaultHttpClientAdapter();

  @override
  Future<ResponseBody> fetch(RequestOptions options,
      Stream<Uint8List>? requestStream, Future? cancelFuture) async {
    var uri = options.uri;
    var proxy =
        await FlutterSystemProxy.findProxyFromEnvironment(uri.toString()); // This line does the magic
    _adapter.onHttpClientCreate = (HttpClient clinet) {
      clinet.findProxy = (uri) {
        return proxy;
      };
    };
    return _adapter.fetch(options, requestStream, cancelFuture);
  }

  @override
  void close({bool force = false}) {
    _adapter.close(force: force);
  }
}

// Use a wrapper around getting dio
void getDio(){
  var dio = Dio();
  dio.httpClientAdapter = MyAdapter();
  return dio;
}

About

Forking this to be able to automate publishing to Cloudsmith.. A Flutter Plugin to detect System proxy. When using HTTP client that are not proxy aware this plugin can help with finding the proxy from system settings which then can be used with HTTP Client to make a successful request.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 36.3%
  • Dart 30.2%
  • Java 14.6%
  • Ruby 13.4%
  • Objective-C 4.6%
  • JavaScript 0.9%