better get requests

This commit is contained in:
hiddify-com
2024-08-02 08:39:49 +02:00
parent 0e5d45fda5
commit 442486d9ff

View File

@@ -8,49 +8,82 @@ import 'package:flutter_loggy_dio/flutter_loggy_dio.dart';
import 'package:hiddify/utils/custom_loggers.dart'; import 'package:hiddify/utils/custom_loggers.dart';
class DioHttpClient with InfraLogger { class DioHttpClient with InfraLogger {
final Map<String, Dio> _dio = {};
DioHttpClient({ DioHttpClient({
required Duration timeout, required Duration timeout,
required String userAgent, required String userAgent,
required bool debug, required bool debug,
}) { }) {
_dio = Dio( for (var mode in ["proxy", "direct", "both"]) {
BaseOptions( _dio[mode] = Dio(
connectTimeout: timeout, BaseOptions(
sendTimeout: timeout, connectTimeout: timeout,
receiveTimeout: timeout, sendTimeout: timeout,
headers: {"User-Agent": userAgent}, receiveTimeout: timeout,
), headers: {"User-Agent": userAgent},
); ),
);
_dio[mode]!.interceptors.add(
RetryInterceptor(
dio: _dio[mode]!,
retryDelays: [
const Duration(seconds: 1),
const Duration(seconds: 2),
if (mode != "proxy") const Duration(seconds: 3),
],
),
);
_dio.interceptors.add( _dio[mode]!.httpClientAdapter = IOHttpClientAdapter(
RetryInterceptor( createHttpClient: () {
dio: _dio, final client = HttpClient();
retryDelays: const [ client.findProxy = (url) {
Duration(seconds: 1), if (mode == "proxy") {
Duration(seconds: 2), return "PROXY localhost:$port";
Duration(seconds: 3), } else if (mode == "direct") {
], return "DIRECT";
), } else {
); return "PROXY localhost:$port; DIRECT";
}
};
return client;
},
);
}
if (debug) { if (debug) {
_dio.interceptors.add(LoggyDioInterceptor(requestHeader: true)); // _dio.interceptors.add(LoggyDioInterceptor(requestHeader: true));
} }
} }
late final Dio _dio; int port = 0;
// bool isPortOpen(String host, int port, {Duration timeout = const Duration(milliseconds: 200)}) async{
// try {
// Socket.connect(host, port, timeout: timeout).then((socket) {
// socket.destroy();
// });
// return true;
// } on SocketException catch (_) {
// return false;
// } catch (_) {
// return false;
// }
// }
Future<bool> isPortOpen(String host, int port, {Duration timeout = const Duration(seconds: 5)}) async {
try {
final socket = await Socket.connect(host, port, timeout: timeout);
await socket.close();
return true;
} on SocketException catch (_) {
return false;
} catch (_) {
return false;
}
}
void setProxyPort(int port) { void setProxyPort(int port) {
this.port = port;
loggy.debug("setting proxy port: [$port]"); loggy.debug("setting proxy port: [$port]");
_dio.httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () {
final client = HttpClient();
client.findProxy = (url) {
return "PROXY localhost:$port; DIRECT";
};
return client;
},
);
} }
Future<Response<T>> get<T>( Future<Response<T>> get<T>(
@@ -58,8 +91,16 @@ class DioHttpClient with InfraLogger {
CancelToken? cancelToken, CancelToken? cancelToken,
String? userAgent, String? userAgent,
({String username, String password})? credentials, ({String username, String password})? credentials,
bool proxyOnly = false,
}) async { }) async {
return _dio.get<T>( final mode = proxyOnly
? "proxy"
: await isPortOpen("127.0.0.1", port)
? "both"
: "direct";
final dio = _dio[mode]!;
return dio.get<T>(
url, url,
cancelToken: cancelToken, cancelToken: cancelToken,
options: _options(url, userAgent: userAgent, credentials: credentials), options: _options(url, userAgent: userAgent, credentials: credentials),
@@ -72,8 +113,15 @@ class DioHttpClient with InfraLogger {
CancelToken? cancelToken, CancelToken? cancelToken,
String? userAgent, String? userAgent,
({String username, String password})? credentials, ({String username, String password})? credentials,
bool proxyOnly = false,
}) async { }) async {
return _dio.download( final mode = proxyOnly
? "proxy"
: await isPortOpen("127.0.0.1", port)
? "both"
: "direct";
final dio = _dio[mode]!;
return dio.download(
url, url,
path, path,
cancelToken: cancelToken, cancelToken: cancelToken,