From 9c4c80f77ec4907ea65ee267990eb9ac5bbce08f Mon Sep 17 00:00:00 2001 From: ramin-deriv <55975218+ramin-deriv@users.noreply.github.com> Date: Tue, 12 Mar 2024 15:36:49 +0800 Subject: [PATCH] ramin/make_proxy_aware_connection_configurable (#310) * test HttpOverrides to pass the connection through the proxy server * get system proxy and add it to HttpOverride * add onProxy found callback * create custom HttpClient for the websocket connection * use flutter-system proxy dep * make onProxyFound optional * code cleanup * revert unnessary changes * remove debugging code * remove debugging code * use proxyAwareConnection flag in BinaryAPI --- .../connection/api_manager/binary_api.dart | 15 ++++++++++++--- lib/state/connection/connection_cubit.dart | 6 ++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/services/connection/api_manager/binary_api.dart b/lib/services/connection/api_manager/binary_api.dart index 784e1d4f54..43b58c060c 100644 --- a/lib/services/connection/api_manager/binary_api.dart +++ b/lib/services/connection/api_manager/binary_api.dart @@ -27,11 +27,15 @@ class BinaryAPI extends BaseAPI { BinaryAPI({ String? key, bool enableDebug = false, + this.proxyAwareConnection = false, }) : super(key: key ?? '${UniqueKey()}', enableDebug: enableDebug); static const Duration _disconnectTimeOut = Duration(seconds: 5); static const Duration _websocketConnectTimeOut = Duration(seconds: 10); + /// A flag to indicate if the connection is proxy aware. + final bool proxyAwareConnection; + /// Represents the active websocket connection. /// /// This is used to send and receive data from the websocket server. @@ -77,10 +81,15 @@ class BinaryAPI extends BaseAPI { _logDebugInfo('connecting to $uri.'); await _setUserAgent(); - final String proxy = await FlutterSystemProxy.findProxyFromEnvironment( - uri.toString().replaceAll('wss', 'https')); - final HttpClient client = HttpClient()..findProxy = (Uri uri) => proxy; + HttpClient? client; + + if (proxyAwareConnection) { + final String proxy = await FlutterSystemProxy.findProxyFromEnvironment( + uri.toString().replaceAll('wss', 'https')); + + client = HttpClient()..findProxy = (Uri uri) => proxy; + } // Initialize connection to websocket server. _webSocketChannel = IOWebSocketChannel.connect('$uri', diff --git a/lib/state/connection/connection_cubit.dart b/lib/state/connection/connection_cubit.dart index f8e43800da..4659014740 100644 --- a/lib/state/connection/connection_cubit.dart +++ b/lib/state/connection/connection_cubit.dart @@ -22,12 +22,15 @@ class ConnectionCubit extends Cubit { ConnectionInformation connectionInformation, { BaseAPI? api, this.enableDebug = false, + // TODO(NA): Refactor to only get BinaryAPI instance. and printResponse and proxyAwareConnection can be part of BinaryAPI only. this.printResponse = false, + this.proxyAwareConnection = false, }) : super(const ConnectionInitialState()) { APIInitializer().initialize( api: api ?? BinaryAPI( key: _key, + proxyAwareConnection: proxyAwareConnection, enableDebug: enableDebug, ), ); @@ -55,6 +58,9 @@ class ConnectionCubit extends Cubit { /// Default value is `false`. final bool printResponse; + /// A flag to indicate if the connection is proxy aware. + final bool proxyAwareConnection; + // In some devices like Samsung J6 or Huawei Y7, the call manager doesn't response to the ping call less than 5 sec. final Duration _pingTimeout = const Duration(seconds: 5);