Files
umbrix/lib/data/repository/profiles_repository_impl.dart

256 lines
7.1 KiB
Dart
Raw Normal View History

import 'dart:convert';
2023-09-15 16:41:20 +02:00
import 'dart:io';
2023-07-06 17:18:41 +03:30
import 'package:dio/dio.dart';
import 'package:fpdart/fpdart.dart';
import 'package:hiddify/data/local/dao/dao.dart';
import 'package:hiddify/data/repository/exception_handlers.dart';
2023-07-26 16:42:31 +03:30
import 'package:hiddify/domain/enums.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/domain/profiles/profiles.dart';
2023-08-19 22:27:23 +03:30
import 'package:hiddify/domain/singbox/singbox.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/services/files_editor_service.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:meta/meta.dart';
2023-07-26 14:17:11 +03:30
import 'package:uuid/uuid.dart';
2023-07-06 17:18:41 +03:30
class ProfilesRepositoryImpl
with ExceptionHandler, InfraLogger
implements ProfilesRepository {
ProfilesRepositoryImpl({
required this.profilesDao,
required this.filesEditor,
2023-08-19 22:27:23 +03:30
required this.singbox,
2023-07-06 17:18:41 +03:30
required this.dio,
});
final ProfilesDao profilesDao;
final FilesEditorService filesEditor;
2023-08-19 22:27:23 +03:30
final SingboxFacade singbox;
2023-07-06 17:18:41 +03:30
final Dio dio;
@override
TaskEither<ProfileFailure, Profile?> get(String id) {
return TaskEither.tryCatch(
() => profilesDao.getById(id),
ProfileUnexpectedFailure.new,
);
}
@override
Stream<Either<ProfileFailure, Profile?>> watchActiveProfile() {
2023-08-23 00:06:51 +03:30
return profilesDao.watchActiveProfile().handleExceptions(
(error, stackTrace) {
loggy.warning("error watching active profile", error, stackTrace);
return ProfileUnexpectedFailure(error, stackTrace);
},
);
2023-07-06 17:18:41 +03:30
}
@override
Stream<Either<ProfileFailure, bool>> watchHasAnyProfile() {
return profilesDao
.watchProfileCount()
.map((event) => event != 0)
.handleExceptions(ProfileUnexpectedFailure.new);
}
@override
2023-07-26 16:42:31 +03:30
Stream<Either<ProfileFailure, List<Profile>>> watchAll({
ProfilesSort sort = ProfilesSort.lastUpdate,
SortMode mode = SortMode.ascending,
}) {
2023-07-06 17:18:41 +03:30
return profilesDao
2023-07-26 16:42:31 +03:30
.watchAll(sort: sort, mode: mode)
2023-07-06 17:18:41 +03:30
.handleExceptions(ProfileUnexpectedFailure.new);
}
2023-07-26 14:17:11 +03:30
@override
TaskEither<ProfileFailure, Unit> addByUrl(
String url, {
bool markAsActive = false,
}) {
return exceptionHandler(
() async {
final profileId = const Uuid().v4();
return fetch(url, profileId)
.flatMap(
(profile) => TaskEither(
() async {
await profilesDao.create(
profile.copyWith(
id: profileId,
active: markAsActive,
),
);
return right(unit);
},
),
)
.run();
},
2023-08-22 01:02:33 +03:30
(error, stackTrace) {
loggy.warning("error adding profile by url", error, stackTrace);
return ProfileUnexpectedFailure(error, stackTrace);
},
2023-07-26 14:17:11 +03:30
);
}
2023-07-06 17:18:41 +03:30
@override
TaskEither<ProfileFailure, Unit> add(Profile baseProfile) {
return exceptionHandler(
() async {
return fetch(baseProfile.url, baseProfile.id)
.flatMap(
2023-07-26 14:17:11 +03:30
(remoteProfile) => TaskEither(() async {
2023-07-06 17:18:41 +03:30
await profilesDao.create(
baseProfile.copyWith(
2023-07-26 14:17:11 +03:30
subInfo: remoteProfile.subInfo,
extra: remoteProfile.extra,
2023-07-06 17:18:41 +03:30
lastUpdate: DateTime.now(),
),
);
return right(unit);
}),
)
.run();
},
2023-08-22 01:02:33 +03:30
(error, stackTrace) {
loggy.warning("error adding profile", error, stackTrace);
return ProfileUnexpectedFailure(error, stackTrace);
},
2023-07-06 17:18:41 +03:30
);
}
@override
TaskEither<ProfileFailure, Unit> update(Profile baseProfile) {
return exceptionHandler(
() async {
2023-08-22 01:02:33 +03:30
loggy.debug(
"updating profile [${baseProfile.name} (${baseProfile.id})]",
);
2023-07-06 17:18:41 +03:30
return fetch(baseProfile.url, baseProfile.id)
.flatMap(
2023-07-26 14:17:11 +03:30
(remoteProfile) => TaskEither(() async {
2023-07-06 17:18:41 +03:30
await profilesDao.edit(
baseProfile.copyWith(
2023-07-26 14:17:11 +03:30
subInfo: remoteProfile.subInfo,
extra: remoteProfile.extra,
2023-07-06 17:18:41 +03:30
lastUpdate: DateTime.now(),
),
);
return right(unit);
}),
)
.run();
},
2023-08-22 01:02:33 +03:30
(error, stackTrace) {
loggy.warning("error updating profile", error, stackTrace);
return ProfileUnexpectedFailure(error, stackTrace);
},
2023-07-06 17:18:41 +03:30
);
}
@override
TaskEither<ProfileFailure, Unit> setAsActive(String id) {
return TaskEither.tryCatch(
() async {
await profilesDao.setAsActive(id);
return unit;
},
ProfileUnexpectedFailure.new,
);
}
@override
TaskEither<ProfileFailure, Unit> delete(String id) {
return TaskEither.tryCatch(
() async {
await profilesDao.removeById(id);
await filesEditor.deleteConfig(id);
return unit;
},
ProfileUnexpectedFailure.new,
);
}
2023-09-16 00:30:21 +03:30
final _subInfoHeaders = [
'profile-title',
'content-disposition',
'subscription-userinfo',
'profile-update-interval',
'support-url',
'profile-web-page-url',
];
2023-07-06 17:18:41 +03:30
@visibleForTesting
2023-07-26 14:17:11 +03:30
TaskEither<ProfileFailure, Profile> fetch(
2023-07-06 17:18:41 +03:30
String url,
String fileName,
) {
return TaskEither(
() async {
final path = filesEditor.configPath(fileName);
2023-08-26 16:26:32 +03:30
final response = await dio.download(url.trim(), path);
2023-09-16 00:30:21 +03:30
final headers = await _populateHeaders(response.headers.map, path);
2023-08-19 22:27:23 +03:30
final parseResult = await singbox.parseConfig(path).run();
return parseResult.fold(
(l) async {
await File(path).delete();
2023-08-22 01:02:33 +03:30
loggy.warning("error parsing config: $l");
2023-08-19 22:27:23 +03:30
return left(ProfileFailure.invalidConfig(l.msg));
},
(_) async {
final profile = Profile.fromResponse(url, headers);
2023-08-19 22:27:23 +03:30
return right(profile);
},
);
2023-07-06 17:18:41 +03:30
},
);
}
2023-09-16 00:30:21 +03:30
Future<Map<String, List<String>>> _populateHeaders(
Map<String, List<String>> headers,
2023-09-16 00:30:21 +03:30
String path,
) async {
var headersFound = 0;
for (final key in _subInfoHeaders) {
if (headers.containsKey(key)) headersFound++;
}
if (headersFound >= 4) return headers;
loggy.debug(
"only [$headersFound] headers found, checking file content for possible information",
);
var content = await File(path).readAsString();
content = safeDecodeBase64(content);
final lines = content.split("\n");
for (int i = 0; i < 10; i++) {
final line = lines[i];
if (line.startsWith("#") || line.startsWith("//")) {
final index = line.indexOf(':');
if (index == -1) continue;
2023-09-16 00:30:21 +03:30
final key = line
.substring(0, index)
.replaceFirst(RegExp("^#|//"), "")
.trim()
.toLowerCase();
2023-09-16 00:30:21 +03:30
final value = line.substring(index + 1).trim();
if (!headers.keys.contains(key) &&
_subInfoHeaders.contains(key) &&
value.isNotEmpty) {
headers[key] = [value];
}
}
}
return headers;
}
String safeDecodeBase64(String str) {
try {
return utf8.decode(base64.decode(str));
} catch (e) {
return str;
}
}
2023-07-06 17:18:41 +03:30
}