Files
MWClash/lib/common/request.dart

108 lines
3.0 KiB
Dart
Raw Normal View History

import 'dart:io';
2024-04-30 23:38:49 +08:00
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:fl_clash/common/common.dart';
2024-06-05 16:19:23 +08:00
import 'package:fl_clash/models/ip.dart';
import 'package:fl_clash/state.dart';
2024-04-30 23:38:49 +08:00
class Request {
late final Dio _dio;
int? _port;
2024-06-06 16:31:08 +08:00
bool _isStart = false;
Request() {
_dio = Dio();
_dio.options = BaseOptions(
headers: {"User-Agent": globalState.appController.clashConfig.globalUa},
);
_dio.interceptors.add(
InterceptorsWrapper(
onRequest: (options, handler) {
_updateAdapter();
return handler.next(options); // 继续请求
},
),
);
}
_updateAdapter() {
final port = globalState.appController.clashConfig.mixedPort;
2024-06-06 16:31:08 +08:00
final isStart = globalState.appController.appState.isStart;
if (_port != port || isStart != _isStart) {
_port = port;
2024-06-06 16:31:08 +08:00
_isStart = isStart;
_dio.httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () {
final client = HttpClient();
if (!_isStart) return client;
client.userAgent = globalState.appController.clashConfig.globalUa;
client.findProxy = (url) {
return "PROXY localhost:$_port;DIRECT";
};
return client;
},
validateCertificate: (_, __, ___) => true,
2024-04-30 23:38:49 +08:00
);
}
}
Future<Response> getFileResponseForUrl(String url) async {
final response = await _dio
.get(
2024-06-05 16:19:23 +08:00
url,
options: Options(
responseType: ResponseType.bytes,
),
)
.timeout(
httpTimeoutDuration * 2,
2024-06-05 16:19:23 +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'];
final version = globalState.packageInfo.version;
2024-04-30 23:38:49 +08:00
final hasUpdate =
other.compareVersions(remoteVersion.replaceAll('v', ''), version) > 0;
if (!hasUpdate) return null;
return data;
2024-04-30 23:38:49 +08:00
}
2024-06-05 16:19:23 +08:00
final Map<String, IpInfo Function(Map<String, dynamic>)> _ipInfoSources = {
"https://ipwho.is/": IpInfo.fromIpwhoIsJson,
"https://api.ip.sb/geoip/": IpInfo.fromIpSbJson,
"https://ipapi.co/json/": IpInfo.fromIpApiCoJson,
"https://ipinfo.io/json/": IpInfo.fromIpInfoIoJson,
};
Future<IpInfo?> checkIp(CancelToken? cancelToken) async {
2024-06-05 16:19:23 +08:00
for (final source in _ipInfoSources.entries) {
try {
final response = await _dio
.get<Map<String, dynamic>>(source.key, cancelToken: cancelToken)
.timeout(
httpTimeoutDuration,
);
2024-06-05 16:19:23 +08:00
if (response.statusCode == 200 && response.data != null) {
return source.value(response.data!);
}
} catch (e) {
continue;
}
}
2024-06-06 16:31:08 +08:00
return null;
2024-06-05 16:19:23 +08:00
}
2024-04-30 23:38:49 +08:00
}
final request = Request();