11import 'dart:async' ;
2- import 'dart:convert' ;
32import 'dart:io' ;
43import 'package:flutter/services.dart' ;
54
65class FlutterSystemProxy {
76 static const MethodChannel _channel =
87 const MethodChannel ('flutter_system_proxy' );
9-
10- static Future <Map <String , String >?> _getSystemProxy (String url) async {
11- bool isHttps = Uri .parse (url).scheme == 'https' ;
12- dynamic proxySettings = await _channel.invokeMethod ("getDeviceProxy" );
13- if (Platform .isAndroid) {
14- if (isHttps) {
15- if (! isNullOrEmpty (proxySettings['https.proxyHost' ]) &&
16- isPort (proxySettings['https.proxyPort' ])) {
17- return {
18- "enabled" : "true" ,
19- "host" : proxySettings['https.proxyHost' ],
20- "port" : proxySettings['https.proxyPort' ]
21- };
22- }
23- } else {
24- if (! isNullOrEmpty (proxySettings['http.proxyHost' ]) &&
25- isPort (proxySettings['http.proxyPort' ])) {
26- return {
27- "enabled" : "true" ,
28- "host" : proxySettings['http.proxyHost' ],
29- "port" : proxySettings['http.proxyPort' ]
30- };
31- }
32- }
33- return null ;
34- } else if (Platform .isIOS) {
35- if (proxySettings["ProxyAutoConfigEnable" ] == 1 ) {
36- return {
37- "pacEnabled" : "true" ,
38- "pacUrl" : proxySettings['ProxyAutoConfigURLString' ]
39- };
40- } else {
41- if (isHttps) {
42- if (proxySettings['HTTPSEnable' ] == 1 ) {
43- return {
44- "enabled" : "true" ,
45- "host" : proxySettings['HTTPSProxy' ].toString (),
46- "port" : proxySettings['HTTPSPort' ].toString ()
47- };
48- }
49- } else {
50- if (proxySettings['HTTPEnable' ] == 1 ) {
51- return {
52- "enabled" : "true" ,
53- "host" : proxySettings['HTTPProxy' ].toString (),
54- "port" : proxySettings['HTTPPort' ].toString ()
55- };
56- }
57- }
58- return null ;
59- }
60- }
8+
9+ /// returns host and port from environment
10+ static Future <dynamic > getEnvironmentProxy (String url) async {
11+ return _channel.invokeMethod ("getDeviceProxy" , < String , dynamic > {'url' : url});
6112 }
6213
14+ /// returns Proxy String
6315 static Future <String > findProxyFromEnvironment (String url) async {
64- var parsedProxy = await _getSystemProxy (url);
65- var host = Uri . parse (url). host;
66- if (parsedProxy != null && parsedProxy[ "enabled" ] == "true" ) {
16+ dynamic proxySettings = await getEnvironmentProxy (url);
17+ if ( ! isNullOrEmpty (proxySettings[ ' host' ]) &&
18+ isPort (proxySettings[ 'port' ]) ) {
6719 return "PROXY " +
68- (parsedProxy[ " host" ] as String ) +
20+ (proxySettings[ ' host' ]. toString () ) +
6921 ":" +
70- (parsedProxy["port" ] as String );
71- } else if (parsedProxy != null &&
72- parsedProxy["pacEnabled" ] == "true" &&
73- parsedProxy["pacUrl" ] != null ) {
74- String pacLocation = parsedProxy["pacUrl" ] as String ;
75- String jsContents = await contents (pacLocation);
76- String proxy = await _channel.invokeMethod (
77- "executePAC" , {"url" : url, "host" : host, "js" : jsContents});
78- return proxy;
22+ (proxySettings['port' ].toString ());
7923 } else {
8024 return HttpClient .findProxyFromEnvironment (Uri .parse (url));
8125 }
@@ -86,24 +30,12 @@ bool isNullOrEmpty(String? str) {
8630 return str == null || str == "" ;
8731}
8832
89- bool isPort (String ? port) {
33+ bool isPort (dynamic port) {
9034 if (port == null ) return false ;
91- final number = num .tryParse (port);
92- if (number != null && number > 0 ) {
35+ final number = num .tryParse (port. toString () );
36+ if (number != null && number > 0 && number <= 65536 ) {
9337 return true ;
9438 } else {
9539 return false ;
9640 }
9741}
98-
99- Future <String > contents (String url) async {
100- HttpClient client = new HttpClient ();
101- var completor = new Completer <String >();
102- client.findProxy = null ;
103- var request = await client.getUrl (Uri .parse (url));
104- var response = await request.close ();
105- response.transform (utf8.decoder).listen ((contents) {
106- completor.complete (contents);
107- });
108- return completor.future;
109- }
0 commit comments