Skip to content

Commit 9c4c80f

Browse files
authored
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
1 parent f337d60 commit 9c4c80f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/services/connection/api_manager/binary_api.dart

+12-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ class BinaryAPI extends BaseAPI {
2727
BinaryAPI({
2828
String? key,
2929
bool enableDebug = false,
30+
this.proxyAwareConnection = false,
3031
}) : super(key: key ?? '${UniqueKey()}', enableDebug: enableDebug);
3132

3233
static const Duration _disconnectTimeOut = Duration(seconds: 5);
3334
static const Duration _websocketConnectTimeOut = Duration(seconds: 10);
3435

36+
/// A flag to indicate if the connection is proxy aware.
37+
final bool proxyAwareConnection;
38+
3539
/// Represents the active websocket connection.
3640
///
3741
/// This is used to send and receive data from the websocket server.
@@ -77,10 +81,15 @@ class BinaryAPI extends BaseAPI {
7781
_logDebugInfo('connecting to $uri.');
7882

7983
await _setUserAgent();
80-
final String proxy = await FlutterSystemProxy.findProxyFromEnvironment(
81-
uri.toString().replaceAll('wss', 'https'));
8284

83-
final HttpClient client = HttpClient()..findProxy = (Uri uri) => proxy;
85+
HttpClient? client;
86+
87+
if (proxyAwareConnection) {
88+
final String proxy = await FlutterSystemProxy.findProxyFromEnvironment(
89+
uri.toString().replaceAll('wss', 'https'));
90+
91+
client = HttpClient()..findProxy = (Uri uri) => proxy;
92+
}
8493

8594
// Initialize connection to websocket server.
8695
_webSocketChannel = IOWebSocketChannel.connect('$uri',

lib/state/connection/connection_cubit.dart

+6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ class ConnectionCubit extends Cubit<ConnectionState> {
2222
ConnectionInformation connectionInformation, {
2323
BaseAPI? api,
2424
this.enableDebug = false,
25+
// TODO(NA): Refactor to only get BinaryAPI instance. and printResponse and proxyAwareConnection can be part of BinaryAPI only.
2526
this.printResponse = false,
27+
this.proxyAwareConnection = false,
2628
}) : super(const ConnectionInitialState()) {
2729
APIInitializer().initialize(
2830
api: api ??
2931
BinaryAPI(
3032
key: _key,
33+
proxyAwareConnection: proxyAwareConnection,
3134
enableDebug: enableDebug,
3235
),
3336
);
@@ -55,6 +58,9 @@ class ConnectionCubit extends Cubit<ConnectionState> {
5558
/// Default value is `false`.
5659
final bool printResponse;
5760

61+
/// A flag to indicate if the connection is proxy aware.
62+
final bool proxyAwareConnection;
63+
5864
// In some devices like Samsung J6 or Huawei Y7, the call manager doesn't response to the ping call less than 5 sec.
5965
final Duration _pingTimeout = const Duration(seconds: 5);
6066

0 commit comments

Comments
 (0)