better get requests
This commit is contained in:
@@ -8,12 +8,14 @@ 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"]) {
|
||||||
|
_dio[mode] = Dio(
|
||||||
BaseOptions(
|
BaseOptions(
|
||||||
connectTimeout: timeout,
|
connectTimeout: timeout,
|
||||||
sendTimeout: timeout,
|
sendTimeout: timeout,
|
||||||
@@ -21,45 +23,84 @@ class DioHttpClient with InfraLogger {
|
|||||||
headers: {"User-Agent": userAgent},
|
headers: {"User-Agent": userAgent},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
_dio[mode]!.interceptors.add(
|
||||||
_dio.interceptors.add(
|
|
||||||
RetryInterceptor(
|
RetryInterceptor(
|
||||||
dio: _dio,
|
dio: _dio[mode]!,
|
||||||
retryDelays: const [
|
retryDelays: [
|
||||||
Duration(seconds: 1),
|
const Duration(seconds: 1),
|
||||||
Duration(seconds: 2),
|
const Duration(seconds: 2),
|
||||||
Duration(seconds: 3),
|
if (mode != "proxy") const Duration(seconds: 3),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (debug) {
|
_dio[mode]!.httpClientAdapter = IOHttpClientAdapter(
|
||||||
_dio.interceptors.add(LoggyDioInterceptor(requestHeader: true));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
late final Dio _dio;
|
|
||||||
|
|
||||||
void setProxyPort(int port) {
|
|
||||||
loggy.debug("setting proxy port: [$port]");
|
|
||||||
_dio.httpClientAdapter = IOHttpClientAdapter(
|
|
||||||
createHttpClient: () {
|
createHttpClient: () {
|
||||||
final client = HttpClient();
|
final client = HttpClient();
|
||||||
client.findProxy = (url) {
|
client.findProxy = (url) {
|
||||||
|
if (mode == "proxy") {
|
||||||
|
return "PROXY localhost:$port";
|
||||||
|
} else if (mode == "direct") {
|
||||||
|
return "DIRECT";
|
||||||
|
} else {
|
||||||
return "PROXY localhost:$port; DIRECT";
|
return "PROXY localhost:$port; DIRECT";
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return client;
|
return client;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (debug) {
|
||||||
|
// _dio.interceptors.add(LoggyDioInterceptor(requestHeader: true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
this.port = port;
|
||||||
|
loggy.debug("setting proxy port: [$port]");
|
||||||
|
}
|
||||||
|
|
||||||
Future<Response<T>> get<T>(
|
Future<Response<T>> get<T>(
|
||||||
String url, {
|
String url, {
|
||||||
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,
|
||||||
|
|||||||
Reference in New Issue
Block a user