0

i need to set proxy globally in my project, i did that with flutter_socks_proxy package.

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SocksProxy.initProxy(
      proxy: 'SOCKS5 192.111.139.165:4145',
      onCreate: (client) {
        client.badCertificateCallback =
            (X509Certificate cert, String host, int port) => true;
      });

  runApp(MyApp());
}

it works but i have a problem, i can't access http (non ssl) api after set the proxy.

final url = Uri.parse('http://worldtimeapi.org/api/timezone/Europe/London');
final response = await http.get(url);
print(response.body);

this is the error

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Connection reset by peer

it works fine with https apis.

1 Answers1

0

I think the problem is related to the allowance of Clear Text Traffic, which in this case is the HTTP, you can allow it by the following steps:

For Android:

  1. Navigate to the this file: ./android/app/src/main/AndroidManifest.xml
  2. In the proprites of the application tag, add this property: android:usesCleartextTraffic="true"

The final file should be something like:

<?xml version="1.0" encoding="utf-8"?>
 <manifest ...>
 <uses-permission android:name="android.permission.INTERNET" />
 <application
    ...
    android:usesCleartextTraffic="true"
    ...>
    ...
</application>
</manifest>

For IOS:

  1. Navigate to the this file: ./ios/Runner/info.plist
  2. Add the following key-value pair:
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Note:

You may need to run flutter clean after applying the above changes to ensure that the native files compiles again with a fresh run.

Moaz El-sawaf
  • 2,306
  • 1
  • 16
  • 33