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-05 16:19:23 +08:00
|
|
|
import 'package:fl_clash/models/ip.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;
|
2024-06-06 16:31:08 +08:00
|
|
|
bool _isStart = false;
|
2024-06-03 18:02:05 +08:00
|
|
|
|
|
|
|
|
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); // 继续请求
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 16:19:23 +08:00
|
|
|
_syncProxy() {
|
2024-06-03 18:02:05 +08:00
|
|
|
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) {
|
2024-06-03 18:02:05 +08:00
|
|
|
_port = port;
|
2024-06-06 16:31:08 +08:00
|
|
|
_isStart = isStart;
|
2024-06-03 18:02:05 +08:00
|
|
|
_dio.httpClientAdapter = IOHttpClientAdapter(
|
|
|
|
|
createHttpClient: () {
|
|
|
|
|
final client = HttpClient();
|
2024-06-06 16:31:08 +08:00
|
|
|
if(!_isStart) return client;
|
2024-06-03 18:02:05 +08:00
|
|
|
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(
|
2024-06-05 16:19:23 +08:00
|
|
|
url,
|
|
|
|
|
options: Options(
|
|
|
|
|
responseType: ResponseType.bytes,
|
|
|
|
|
),
|
|
|
|
|
)
|
2024-06-03 18:02:05 +08:00
|
|
|
.timeout(
|
2024-06-05 16:19:23 +08:00
|
|
|
httpTimeoutDuration,
|
|
|
|
|
);
|
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-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,
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-06 17:13:32 +08:00
|
|
|
Future<IpInfo?> checkIp() 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,
|
|
|
|
|
);
|
|
|
|
|
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
|
|
|
}
|
2024-06-03 18:02:05 +08:00
|
|
|
|
2024-06-06 16:31:08 +08:00
|
|
|
final request = Request();
|