Skip to content

Commit

Permalink
ramin/make_proxy_aware_connection_configurable (#310)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ramin-deriv authored Mar 12, 2024
1 parent f337d60 commit 9c4c80f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/services/connection/api_manager/binary_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions lib/state/connection/connection_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ class ConnectionCubit extends Cubit<ConnectionState> {
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,
),
);
Expand Down Expand Up @@ -55,6 +58,9 @@ class ConnectionCubit extends Cubit<ConnectionState> {
/// 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);

Expand Down

0 comments on commit 9c4c80f

Please sign in to comment.