Skip to content

Commit

Permalink
Setup Proxy if proxy found in devices
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangdat committed Jun 7, 2023
1 parent e9a989d commit beeb717
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:linshare_flutter_app/presentation/di/get_it_service.dart';
import 'package:linshare_flutter_app/presentation/localizations/app_localizations.dart';
import 'package:linshare_flutter_app/presentation/network/proxy/set_up_proxy.dart';
import 'package:linshare_flutter_app/presentation/util/router/app_navigation.dart';
import 'package:linshare_flutter_app/presentation/util/router/router.dart' as router;
import 'package:linshare_flutter_app/presentation/widget/initialize_get_it//initialize_get_it_widget.dart';
Expand All @@ -47,11 +48,11 @@ import 'package:redux/redux.dart';
import 'presentation/redux/states/app_state.dart';
import 'presentation/util/extensions/color_extension.dart';
import 'presentation/widget/initialize/initialize_widget.dart';
import 'dart:developer' as developer;

void main() async {
WidgetsFlutterBinding.ensureInitialized();
setUpGetIt();
await setUpProxy();
await Firebase.initializeApp();
if (kProfileMode) {
enableFlutterDriverExtension();
Expand Down
7 changes: 0 additions & 7 deletions lib/presentation/di/module/network_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import 'dart:io';

import 'package:data/data.dart';
import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:linshare_flutter_app/presentation/di/get_it_service.dart';
Expand Down Expand Up @@ -63,12 +62,6 @@ class NetworkModule {
void _provideDio() {
getIt.registerSingleton(Dio(getIt<BaseOptions>()));
_provideInterceptors();
if (Platform.isAndroid) {
(getIt<Dio>().httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
return client;
};
}
getIt<Dio>().interceptors.add(getIt<DynamicUrlInterceptors>());
getIt<Dio>().interceptors.add(getIt<DynamicAPIVersionSupportInterceptor>());
getIt<Dio>().interceptors.add(getIt<CookieInterceptors>());
Expand Down
67 changes: 67 additions & 0 deletions lib/presentation/network/proxy/set_up_proxy.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* LinShare is an open source filesharing software, part of the LinPKI software
* suite, developed by Linagora.
*
* Copyright (C) 2023 LINAGORA
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version,
* provided you comply with the Additional Terms applicable for LinShare software by
* Linagora pursuant to Section 7 of the GNU Affero General Public License,
* subsections (b), (c), and (e), pursuant to which you must notably (i) retain the
* display in the interface of the “LinShare™” trademark/logo, the "Libre & Free" mention,
* the words “You are using the Free and Open Source version of LinShare™, powered by
* Linagora © 2009–2021. Contribute to Linshare R&D by subscribing to an Enterprise
* offer!”. You must also retain the latter notice in all asynchronous messages such as
* e-mails sent with the Program, (ii) retain all hypertext links between LinShare and
* http://www.linshare.org, between linagora.com and Linagora, and (iii) refrain from
* infringing Linagora intellectual property rights over its trademarks and commercial
* brands. Other Additional Terms apply, see
* <http://www.linshare.org/licenses/LinShare-License_AfferoGPL-v3.pdf>
* for more details.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
* You should have received a copy of the GNU Affero General Public License and its
* applicable Additional Terms for LinShare along with this program. If not, see
* <http://www.gnu.org/licenses/> for the GNU Affero General Public License version
* 3 and <http://www.linshare.org/licenses/LinShare-License_AfferoGPL-v3.pdf> for
* the Additional Terms applicable to LinShare software.
*/

import 'dart:developer';
import 'dart:io';

import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';
import 'package:linshare_flutter_app/presentation/di/get_it_service.dart';
import 'package:native_flutter_proxy/native_proxy_reader.dart';

Future<void> setUpProxy() async {
try {
final settings = await NativeProxyReader.proxySetting;
log('::setUpProxy(): enable = ${settings.enabled}, host = ${settings.host}, port = ${settings.port}');
if (settings.enabled && settings.host != null) {
if (Platform.isAndroid) {
(getIt<Dio>().httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
client.badCertificateCallback = (X509Certificate cert, String host, int port) {
log('Bad certificate callback called. $host:$port. cert: ${cert.subject}');
return true;
};
client.findProxy = (url) {
log('::setUpProxy(): $url');
return HttpClient.findProxyFromEnvironment(
url,
environment: {'https_proxy': '${settings.host}:${settings.port}'}
);
};
return client;
};
}
}
} catch (e) {
log('::setUpProxy() exception: $e');
}
}
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.17"
native_flutter_proxy:
dependency: "direct main"
description:
name: native_flutter_proxy
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.15"
package_config:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies:

# Http client
dio: 4.0.0
native_flutter_proxy: 0.1.15
json_annotation: 4.4.0

#get_it
Expand Down

0 comments on commit beeb717

Please sign in to comment.