2024-07-30 08:07:46 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:dartx/dartx.dart';
|
|
|
|
|
import 'package:fpdart/fpdart.dart';
|
2023-11-26 21:20:58 +03:30
|
|
|
import 'package:hiddify/features/profile/data/profile_data_providers.dart';
|
|
|
|
|
import 'package:hiddify/features/profile/data/profile_repository.dart';
|
|
|
|
|
import 'package:hiddify/features/profile/details/profile_details_state.dart';
|
|
|
|
|
import 'package:hiddify/features/profile/model/profile_entity.dart';
|
|
|
|
|
import 'package:hiddify/features/profile/model/profile_failure.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/utils/utils.dart';
|
|
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
2023-11-26 21:20:58 +03:30
|
|
|
part 'profile_details_notifier.g.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
|
|
|
|
|
@riverpod
|
2023-11-26 21:20:58 +03:30
|
|
|
class ProfileDetailsNotifier extends _$ProfileDetailsNotifier with AppLogger {
|
2023-07-06 17:18:41 +03:30
|
|
|
@override
|
2023-11-26 21:20:58 +03:30
|
|
|
Future<ProfileDetailsState> build(
|
2023-07-06 17:18:41 +03:30
|
|
|
String id, {
|
|
|
|
|
String? url,
|
2023-09-02 22:33:29 +03:30
|
|
|
String? profileName,
|
2023-07-06 17:18:41 +03:30
|
|
|
}) async {
|
|
|
|
|
if (id == 'new') {
|
2023-11-26 21:20:58 +03:30
|
|
|
return ProfileDetailsState(
|
|
|
|
|
profile: RemoteProfileEntity(
|
2023-07-06 17:18:41 +03:30
|
|
|
id: const Uuid().v4(),
|
|
|
|
|
active: true,
|
2023-09-02 22:33:29 +03:30
|
|
|
name: profileName ?? "",
|
2023-07-06 17:18:41 +03:30
|
|
|
url: url ?? "",
|
|
|
|
|
lastUpdate: DateTime.now(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-11-26 21:20:58 +03:30
|
|
|
final failureOrProfile = await _profilesRepo.getById(id).run();
|
2023-07-06 17:18:41 +03:30
|
|
|
return failureOrProfile.match(
|
2023-10-03 21:12:14 +03:30
|
|
|
(err) {
|
|
|
|
|
loggy.warning('failed to load profile', err);
|
|
|
|
|
throw err;
|
2023-07-06 17:18:41 +03:30
|
|
|
},
|
2024-07-30 08:07:46 +02:00
|
|
|
(profile) async {
|
2023-07-06 17:18:41 +03:30
|
|
|
if (profile == null) {
|
|
|
|
|
loggy.warning('profile with id: [$id] does not exist');
|
|
|
|
|
throw const ProfileNotFoundFailure();
|
|
|
|
|
}
|
2024-07-30 08:07:46 +02:00
|
|
|
|
2023-09-28 14:03:45 +03:30
|
|
|
_originalProfile = profile;
|
2024-07-30 08:07:46 +02:00
|
|
|
final result = await _profilesRepo.generateConfig(id).run();
|
|
|
|
|
|
|
|
|
|
var configContent = result.fold(
|
|
|
|
|
(failure) => throw Exception('Failed to generate config: $failure'),
|
|
|
|
|
(config) => config,
|
|
|
|
|
);
|
|
|
|
|
if (configContent.isNotEmpty) {
|
|
|
|
|
try {
|
|
|
|
|
final jsonObject = jsonDecode(configContent);
|
|
|
|
|
List<Map<String, dynamic>> res = [];
|
|
|
|
|
if (jsonObject is Map<String, dynamic> && jsonObject['outbounds'] is List) {
|
|
|
|
|
for (var outbound in jsonObject['outbounds'] as List<dynamic>) {
|
|
|
|
|
if (outbound is Map<String, dynamic> && outbound['type'] != null && !['selector', 'urltest', 'dns', 'block'].contains(outbound['type']) && !['direct', 'bypass', 'direct-fragment'].contains(outbound['tag'])) {
|
|
|
|
|
res.add(outbound);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// print('No outbounds found in the config');
|
|
|
|
|
}
|
|
|
|
|
configContent = '{"outbounds": ${json.encode(res)}}';
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// print('Error parsing JSON: $e');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// print('Config content is null or empty');
|
|
|
|
|
}
|
|
|
|
|
return ProfileDetailsState(profile: profile, isEditing: true, configContent: configContent);
|
2023-07-06 17:18:41 +03:30
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-30 08:07:46 +02:00
|
|
|
ProfileRepository get _profilesRepo => ref.read(profileRepositoryProvider).requireValue;
|
2023-11-26 21:20:58 +03:30
|
|
|
ProfileEntity? _originalProfile;
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2024-07-30 08:07:46 +02:00
|
|
|
void setField({
|
|
|
|
|
String? name,
|
|
|
|
|
String? url,
|
|
|
|
|
Option<int>? updateInterval,
|
|
|
|
|
String? configContent,
|
|
|
|
|
}) {
|
2023-07-06 17:18:41 +03:30
|
|
|
if (state case AsyncData(:final value)) {
|
|
|
|
|
state = AsyncData(
|
|
|
|
|
value.copyWith(
|
2023-10-02 18:51:14 +03:30
|
|
|
profile: value.profile.map(
|
|
|
|
|
remote: (rp) => rp.copyWith(
|
|
|
|
|
name: name ?? rp.name,
|
|
|
|
|
url: url ?? rp.url,
|
|
|
|
|
options: updateInterval == null
|
|
|
|
|
? rp.options
|
|
|
|
|
: updateInterval.fold(
|
|
|
|
|
() => null,
|
|
|
|
|
(t) => ProfileOptions(
|
|
|
|
|
updateInterval: Duration(hours: t),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
local: (lp) => lp.copyWith(name: name ?? lp.name),
|
2023-07-06 17:18:41 +03:30
|
|
|
),
|
2024-07-30 08:07:46 +02:00
|
|
|
configContentChanged: value.configContentChanged || value.configContent != configContent,
|
|
|
|
|
configContent: configContent ?? value.configContent,
|
2023-07-06 17:18:41 +03:30
|
|
|
),
|
2023-09-28 14:03:45 +03:30
|
|
|
);
|
2023-07-06 17:18:41 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> save() async {
|
|
|
|
|
if (state case AsyncData(:final value)) {
|
2023-11-26 21:20:58 +03:30
|
|
|
if (value.save case AsyncLoading()) return;
|
|
|
|
|
|
2023-07-06 17:18:41 +03:30
|
|
|
final profile = value.profile;
|
|
|
|
|
Either<ProfileFailure, Unit>? failureOrSuccess;
|
2023-11-26 21:20:58 +03:30
|
|
|
state = AsyncData(value.copyWith(save: const AsyncLoading()));
|
|
|
|
|
|
2023-10-02 18:51:14 +03:30
|
|
|
switch (profile) {
|
2023-11-26 21:20:58 +03:30
|
|
|
case RemoteProfileEntity():
|
2023-10-02 18:51:14 +03:30
|
|
|
loggy.debug(
|
|
|
|
|
'saving profile, url: [${profile.url}], name: [${profile.name}]',
|
|
|
|
|
);
|
|
|
|
|
if (profile.name.isBlank || profile.url.isBlank) {
|
2023-11-26 21:20:58 +03:30
|
|
|
loggy.debug('save: invalid arguments');
|
2023-10-02 18:51:14 +03:30
|
|
|
} else if (value.isEditing) {
|
2024-07-30 08:07:46 +02:00
|
|
|
if (_originalProfile case RemoteProfileEntity(:final url) when url == profile.url) {
|
2023-10-02 18:51:14 +03:30
|
|
|
loggy.debug('editing profile');
|
2023-11-26 21:20:58 +03:30
|
|
|
failureOrSuccess = await _profilesRepo.patch(profile).run();
|
2024-07-30 08:07:46 +02:00
|
|
|
if (failureOrSuccess.isRight()) {
|
|
|
|
|
failureOrSuccess = await _profilesRepo
|
|
|
|
|
.updateContent(
|
|
|
|
|
profile.id,
|
|
|
|
|
value.configContent,
|
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
}
|
2023-10-02 18:51:14 +03:30
|
|
|
} else {
|
|
|
|
|
loggy.debug('updating profile');
|
2024-07-30 08:07:46 +02:00
|
|
|
failureOrSuccess = await _profilesRepo.updateSubscription(profile, patchBaseProfile: true).run();
|
|
|
|
|
if (failureOrSuccess.isRight()) {
|
|
|
|
|
failureOrSuccess = await _profilesRepo
|
|
|
|
|
.updateContent(
|
|
|
|
|
profile.id,
|
|
|
|
|
value.configContent,
|
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
}
|
2023-10-02 18:51:14 +03:30
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
loggy.debug('adding profile, url: [${profile.url}]');
|
|
|
|
|
failureOrSuccess = await _profilesRepo.add(profile).run();
|
|
|
|
|
}
|
2023-11-26 21:20:58 +03:30
|
|
|
|
|
|
|
|
case LocalProfileEntity() when value.isEditing:
|
2023-09-28 14:03:45 +03:30
|
|
|
loggy.debug('editing profile');
|
2023-11-26 21:20:58 +03:30
|
|
|
failureOrSuccess = await _profilesRepo.patch(profile).run();
|
2024-07-30 15:54:44 +02:00
|
|
|
if (failureOrSuccess.isRight()) {
|
|
|
|
|
failureOrSuccess = await _profilesRepo
|
|
|
|
|
.updateContent(
|
|
|
|
|
profile.id,
|
|
|
|
|
value.configContent,
|
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
}
|
2023-10-02 18:51:14 +03:30
|
|
|
default:
|
|
|
|
|
loggy.warning("local profile can't be added manually");
|
2023-07-06 17:18:41 +03:30
|
|
|
}
|
2023-11-26 21:20:58 +03:30
|
|
|
|
2023-07-06 17:18:41 +03:30
|
|
|
state = AsyncData(
|
|
|
|
|
value.copyWith(
|
|
|
|
|
save: failureOrSuccess?.fold(
|
2023-11-26 21:20:58 +03:30
|
|
|
(l) => AsyncError(l, StackTrace.current),
|
|
|
|
|
(_) => const AsyncData(null),
|
2023-07-06 17:18:41 +03:30
|
|
|
) ??
|
|
|
|
|
value.save,
|
|
|
|
|
showErrorMessages: true,
|
|
|
|
|
),
|
2023-09-28 14:03:45 +03:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> updateProfile() async {
|
|
|
|
|
if (state case AsyncData(:final value)) {
|
2023-11-26 21:20:58 +03:30
|
|
|
if (value.update?.isLoading ?? false || !value.isEditing) return;
|
|
|
|
|
if (value.profile case LocalProfileEntity()) {
|
2023-10-02 18:51:14 +03:30
|
|
|
loggy.warning("local profile can't be updated");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-26 21:20:58 +03:30
|
|
|
|
2023-09-28 14:03:45 +03:30
|
|
|
final profile = value.profile;
|
2023-11-26 21:20:58 +03:30
|
|
|
state = AsyncData(value.copyWith(update: const AsyncLoading()));
|
|
|
|
|
|
2024-07-30 08:07:46 +02:00
|
|
|
final failureOrUpdatedProfile = await _profilesRepo.updateSubscription(profile as RemoteProfileEntity).flatMap((_) => _profilesRepo.getById(id)).run();
|
2023-11-26 21:20:58 +03:30
|
|
|
|
2023-09-28 14:03:45 +03:30
|
|
|
state = AsyncData(
|
|
|
|
|
value.copyWith(
|
|
|
|
|
update: failureOrUpdatedProfile.match(
|
2023-11-26 21:20:58 +03:30
|
|
|
(l) => AsyncError(l, StackTrace.current),
|
|
|
|
|
(_) => const AsyncData(null),
|
2023-09-28 14:03:45 +03:30
|
|
|
),
|
|
|
|
|
profile: failureOrUpdatedProfile.match(
|
|
|
|
|
(_) => profile,
|
|
|
|
|
(updatedProfile) => updatedProfile ?? profile,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
2023-07-06 17:18:41 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> delete() async {
|
|
|
|
|
if (state case AsyncData(:final value)) {
|
2023-11-26 21:20:58 +03:30
|
|
|
if (value.delete case AsyncLoading()) return;
|
2023-07-06 17:18:41 +03:30
|
|
|
final profile = value.profile;
|
2023-11-26 21:20:58 +03:30
|
|
|
state = AsyncData(value.copyWith(delete: const AsyncLoading()));
|
|
|
|
|
|
2023-07-06 17:18:41 +03:30
|
|
|
state = AsyncData(
|
|
|
|
|
value.copyWith(
|
2023-11-26 21:20:58 +03:30
|
|
|
delete: await AsyncValue.guard(() async {
|
2024-07-30 08:07:46 +02:00
|
|
|
await _profilesRepo.deleteById(profile.id).getOrElse((l) => throw l).run();
|
2023-11-26 21:20:58 +03:30
|
|
|
}),
|
2023-07-06 17:18:41 +03:30
|
|
|
),
|
2023-09-28 14:03:45 +03:30
|
|
|
);
|
2023-07-06 17:18:41 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|