-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c8e632
commit 58f3e93
Showing
10 changed files
with
138 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:dio/dio.dart'; | ||
import 'package:dio_smart_retry/dio_smart_retry.dart'; | ||
import 'package:flutter_loggy_dio/flutter_loggy_dio.dart'; | ||
import 'package:hiddify/utils/custom_loggers.dart'; | ||
|
||
class DioHttpClient with InfraLogger { | ||
DioHttpClient({ | ||
required Duration timeout, | ||
required String userAgent, | ||
required bool debug, | ||
}) { | ||
_dio = Dio( | ||
BaseOptions( | ||
connectTimeout: timeout, | ||
sendTimeout: timeout, | ||
receiveTimeout: timeout, | ||
headers: {"User-Agent": userAgent}, | ||
), | ||
); | ||
|
||
_dio.interceptors.add( | ||
RetryInterceptor( | ||
dio: _dio, | ||
retryDelays: const [ | ||
Duration(seconds: 1), | ||
Duration(seconds: 2), | ||
Duration(seconds: 3), | ||
], | ||
), | ||
); | ||
|
||
if (debug) { | ||
_dio.interceptors.add(LoggyDioInterceptor(requestHeader: true)); | ||
} | ||
} | ||
|
||
late final Dio _dio; | ||
|
||
Future<Response<T>> get<T>( | ||
String url, { | ||
CancelToken? cancelToken, | ||
String? userAgent, | ||
({String username, String password})? credentials, | ||
}) async { | ||
return _dio.get<T>( | ||
url, | ||
cancelToken: cancelToken, | ||
options: _options(url, userAgent: userAgent, credentials: credentials), | ||
); | ||
} | ||
|
||
Future<Response> download( | ||
String url, | ||
String path, { | ||
CancelToken? cancelToken, | ||
String? userAgent, | ||
({String username, String password})? credentials, | ||
}) async { | ||
return _dio.download( | ||
url, | ||
path, | ||
cancelToken: cancelToken, | ||
options: _options(url, userAgent: userAgent, credentials: credentials), | ||
); | ||
} | ||
|
||
Options _options( | ||
String url, { | ||
String? userAgent, | ||
({String username, String password})? credentials, | ||
}) { | ||
final uri = Uri.parse(url); | ||
|
||
String? userInfo; | ||
if (credentials != null) { | ||
userInfo = "${credentials.username}:${credentials.password}"; | ||
} else if (uri.userInfo.isNotEmpty) { | ||
userInfo = uri.userInfo; | ||
} | ||
|
||
String? basicAuth; | ||
if (userInfo != null) { | ||
basicAuth = "Basic ${base64.encode(utf8.encode(userInfo))}"; | ||
} | ||
|
||
return Options( | ||
headers: { | ||
if (userAgent != null) "User-Agent": userAgent, | ||
if (basicAuth != null) "authorization": basicAuth, | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,15 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:hiddify/core/app_info/app_info_provider.dart'; | ||
import 'package:hiddify/core/http_client/dio_http_client.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
part 'http_client_provider.g.dart'; | ||
|
||
@Riverpod(keepAlive: true) | ||
Dio httpClient(HttpClientRef ref) { | ||
final dio = Dio( | ||
BaseOptions( | ||
connectTimeout: const Duration(seconds: 15), | ||
sendTimeout: const Duration(seconds: 15), | ||
receiveTimeout: const Duration(seconds: 15), | ||
headers: { | ||
"User-Agent": ref.watch(appInfoProvider).requireValue.userAgent, | ||
}, | ||
), | ||
DioHttpClient httpClient(HttpClientRef ref) { | ||
return DioHttpClient( | ||
timeout: const Duration(seconds: 15), | ||
userAgent: ref.watch(appInfoProvider).requireValue.userAgent, | ||
debug: kDebugMode, | ||
); | ||
// https://github.com/dart-lang/http/issues/1047 | ||
// https://github.com/cfug/dio/issues/2042 | ||
// final debug = ref.read(debugModeNotifierProvider); | ||
// if (debug && (Platform.isAndroid || Platform.isIOS || Platform.isMacOS)) { | ||
// dio.httpClientAdapter = NativeAdapter(); | ||
// } | ||
return dio; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters