2024-08-04 08:21:14 +08:00
|
|
|
// ignore_for_file: invalid_annotation_target
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
|
|
import 'package:fl_clash/clash/core.dart';
|
|
|
|
|
import 'package:fl_clash/common/common.dart';
|
2024-11-09 20:17:57 +08:00
|
|
|
import 'package:fl_clash/enum/enum.dart';
|
2024-06-19 13:13:31 +08:00
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2024-06-19 13:13:31 +08:00
|
|
|
part 'generated/profile.freezed.dart';
|
2024-11-09 20:17:57 +08:00
|
|
|
part 'generated/profile.g.dart';
|
2024-06-19 13:13:31 +08:00
|
|
|
|
2024-05-10 10:11:27 +08:00
|
|
|
typedef SelectedMap = Map<String, String>;
|
|
|
|
|
|
2024-06-19 13:13:31 +08:00
|
|
|
@freezed
|
2024-11-09 20:17:57 +08:00
|
|
|
class SubscriptionInfo with _$SubscriptionInfo {
|
|
|
|
|
const factory SubscriptionInfo({
|
2024-06-19 13:13:31 +08:00
|
|
|
@Default(0) int upload,
|
|
|
|
|
@Default(0) int download,
|
|
|
|
|
@Default(0) int total,
|
|
|
|
|
@Default(0) int expire,
|
2024-11-09 20:17:57 +08:00
|
|
|
}) = _SubscriptionInfo;
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2024-11-09 20:17:57 +08:00
|
|
|
factory SubscriptionInfo.fromJson(Map<String, Object?> json) =>
|
|
|
|
|
_$SubscriptionInfoFromJson(json);
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2024-11-09 20:17:57 +08:00
|
|
|
factory SubscriptionInfo.formHString(String? info) {
|
|
|
|
|
if (info == null) return const SubscriptionInfo();
|
2024-06-07 17:22:55 +08:00
|
|
|
final list = info.split(";");
|
2024-04-30 23:38:49 +08:00
|
|
|
Map<String, int?> map = {};
|
2024-06-07 17:22:55 +08:00
|
|
|
for (final i in list) {
|
|
|
|
|
final keyValue = i.trim().split("=");
|
2024-04-30 23:38:49 +08:00
|
|
|
map[keyValue[0]] = int.tryParse(keyValue[1]);
|
|
|
|
|
}
|
2024-11-09 20:17:57 +08:00
|
|
|
return SubscriptionInfo(
|
2024-06-19 13:13:31 +08:00
|
|
|
upload: map["upload"] ?? 0,
|
|
|
|
|
download: map["download"] ?? 0,
|
|
|
|
|
total: map["total"] ?? 0,
|
|
|
|
|
expire: map["expire"] ?? 0,
|
2024-04-30 23:38:49 +08:00
|
|
|
);
|
|
|
|
|
}
|
2024-06-19 13:13:31 +08:00
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2024-06-19 13:13:31 +08:00
|
|
|
@freezed
|
|
|
|
|
class Profile with _$Profile {
|
|
|
|
|
const factory Profile({
|
|
|
|
|
required String id,
|
|
|
|
|
String? label,
|
|
|
|
|
String? currentGroupName,
|
|
|
|
|
@Default("") String url,
|
|
|
|
|
DateTime? lastUpdateDate,
|
|
|
|
|
required Duration autoUpdateDuration,
|
2024-11-09 20:17:57 +08:00
|
|
|
SubscriptionInfo? subscriptionInfo,
|
2024-06-19 13:13:31 +08:00
|
|
|
@Default(true) bool autoUpdate,
|
|
|
|
|
@Default({}) SelectedMap selectedMap,
|
2024-06-23 00:26:24 +08:00
|
|
|
@Default({}) Set<String> unfoldSet,
|
2024-08-04 08:21:14 +08:00
|
|
|
@JsonKey(includeToJson: false, includeFromJson: false)
|
|
|
|
|
@Default(false)
|
|
|
|
|
bool isUpdating,
|
2024-06-19 13:13:31 +08:00
|
|
|
}) = _Profile;
|
|
|
|
|
|
|
|
|
|
factory Profile.fromJson(Map<String, Object?> json) =>
|
|
|
|
|
_$ProfileFromJson(json);
|
|
|
|
|
|
|
|
|
|
factory Profile.normal({
|
|
|
|
|
String? label,
|
|
|
|
|
String url = '',
|
2024-08-04 08:21:14 +08:00
|
|
|
}) {
|
2024-06-19 13:13:31 +08:00
|
|
|
return Profile(
|
|
|
|
|
label: label,
|
|
|
|
|
url: url,
|
|
|
|
|
id: DateTime.now().millisecondsSinceEpoch.toString(),
|
|
|
|
|
autoUpdateDuration: defaultUpdateDuration,
|
|
|
|
|
);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-19 13:13:31 +08:00
|
|
|
extension ProfileExtension on Profile {
|
2024-06-07 17:22:55 +08:00
|
|
|
ProfileType get type =>
|
2024-06-19 13:13:31 +08:00
|
|
|
url.isEmpty == true ? ProfileType.file : ProfileType.url;
|
|
|
|
|
|
2024-08-04 08:21:14 +08:00
|
|
|
bool get realAutoUpdate => url.isEmpty == true ? false : autoUpdate;
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
Future<void> checkAndUpdate() async {
|
2024-05-20 15:15:09 +08:00
|
|
|
final isExists = await check();
|
2024-06-03 18:02:05 +08:00
|
|
|
if (!isExists) {
|
2024-06-19 13:13:31 +08:00
|
|
|
if (url.isNotEmpty) {
|
|
|
|
|
await update();
|
2024-05-20 15:15:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
Future<bool> check() async {
|
|
|
|
|
final profilePath = await appPath.getProfilePath(id);
|
|
|
|
|
return await File(profilePath!).exists();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-19 13:13:31 +08:00
|
|
|
Future<Profile> update() async {
|
|
|
|
|
final response = await request.getFileResponseForUrl(url);
|
|
|
|
|
final disposition = response.headers.value("content-disposition");
|
|
|
|
|
final userinfo = response.headers.value('subscription-userinfo');
|
|
|
|
|
return await copyWith(
|
|
|
|
|
label: label ?? other.getFileNameForDisposition(disposition) ?? id,
|
2024-11-09 20:17:57 +08:00
|
|
|
subscriptionInfo: SubscriptionInfo.formHString(userinfo),
|
2024-06-19 13:13:31 +08:00
|
|
|
).saveFile(response.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<Profile> saveFile(Uint8List bytes) async {
|
2024-06-03 18:02:05 +08:00
|
|
|
final message = await clashCore.validateConfig(utf8.decode(bytes));
|
|
|
|
|
if (message.isNotEmpty) {
|
|
|
|
|
throw message;
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
final path = await appPath.getProfilePath(id);
|
|
|
|
|
final file = File(path!);
|
|
|
|
|
final isExists = await file.exists();
|
|
|
|
|
if (!isExists) {
|
|
|
|
|
await file.create(recursive: true);
|
|
|
|
|
}
|
|
|
|
|
await file.writeAsBytes(bytes);
|
2024-06-19 13:13:31 +08:00
|
|
|
return copyWith(lastUpdateDate: DateTime.now());
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-19 13:13:31 +08:00
|
|
|
Future<Profile> saveFileWithString(String value) async {
|
|
|
|
|
final message = await clashCore.validateConfig(value);
|
|
|
|
|
if (message.isNotEmpty) {
|
|
|
|
|
throw message;
|
|
|
|
|
}
|
|
|
|
|
final path = await appPath.getProfilePath(id);
|
|
|
|
|
final file = File(path!);
|
|
|
|
|
final isExists = await file.exists();
|
|
|
|
|
if (!isExists) {
|
|
|
|
|
await file.create(recursive: true);
|
|
|
|
|
}
|
|
|
|
|
await file.writeAsString(value);
|
|
|
|
|
return copyWith(lastUpdateDate: DateTime.now());
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
}
|