2024-06-03 18:02:05 +08:00
|
|
|
import 'dart:io';
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
import 'package:dio/io.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/common/common.dart';
|
2024-06-03 18:02:05 +08:00
|
|
|
import 'package:fl_clash/state.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
|
|
|
|
|
class Request {
|
2024-06-03 18:02:05 +08:00
|
|
|
late final Dio _dio;
|
|
|
|
|
int? _port;
|
|
|
|
|
|
|
|
|
|
Request() {
|
|
|
|
|
_dio = Dio(
|
|
|
|
|
BaseOptions(
|
|
|
|
|
connectTimeout: httpTimeoutDuration,
|
|
|
|
|
sendTimeout: httpTimeoutDuration,
|
|
|
|
|
receiveTimeout: httpTimeoutDuration,
|
|
|
|
|
headers: {"User-Agent": coreName},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
_dio.interceptors.add(InterceptorsWrapper(
|
|
|
|
|
onRequest: (options, handler) {
|
|
|
|
|
_syncProxy();
|
|
|
|
|
return handler.next(options); // 继续请求
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_syncProxy(){
|
|
|
|
|
final port = globalState.appController.clashConfig.mixedPort;
|
|
|
|
|
if (_port != port) {
|
|
|
|
|
_port = port;
|
|
|
|
|
_dio.httpClientAdapter = IOHttpClientAdapter(
|
|
|
|
|
createHttpClient: () {
|
|
|
|
|
final client = HttpClient();
|
|
|
|
|
client.findProxy = (url) {
|
|
|
|
|
return "PROXY localhost:$_port;DIRECT";
|
|
|
|
|
};
|
|
|
|
|
return client;
|
|
|
|
|
},
|
2024-04-30 23:38:49 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
Future<Response> getFileResponseForUrl(String url) async {
|
|
|
|
|
final response = await _dio
|
|
|
|
|
.get(
|
|
|
|
|
url,
|
|
|
|
|
options: Options(
|
|
|
|
|
responseType: ResponseType.bytes,
|
2024-04-30 23:38:49 +08:00
|
|
|
),
|
2024-06-03 18:02:05 +08:00
|
|
|
)
|
|
|
|
|
.timeout(
|
|
|
|
|
httpTimeoutDuration,
|
2024-04-30 23:38:49 +08:00
|
|
|
);
|
2024-06-03 18:02:05 +08:00
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>?> checkForUpdate() async {
|
|
|
|
|
final response = await _dio.get(
|
|
|
|
|
"https://api.github.com/repos/$repository/releases/latest",
|
|
|
|
|
options: Options(
|
|
|
|
|
responseType: ResponseType.json,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
if (response.statusCode != 200) return null;
|
|
|
|
|
final data = response.data as Map<String, dynamic>;
|
|
|
|
|
final remoteVersion = data['tag_name'];
|
2024-04-30 23:38:49 +08:00
|
|
|
final packageInfo = await appPackage.packageInfoCompleter.future;
|
|
|
|
|
final version = packageInfo.version;
|
|
|
|
|
final hasUpdate =
|
2024-05-11 17:02:34 +08:00
|
|
|
other.compareVersions(remoteVersion.replaceAll('v', ''), version) > 0;
|
2024-06-03 18:02:05 +08:00
|
|
|
if (!hasUpdate) return null;
|
|
|
|
|
return data;
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-03 18:02:05 +08:00
|
|
|
|
|
|
|
|
final request = Request();
|