initial
This commit is contained in:
116
lib/data/repository/clash_facade_impl.dart
Normal file
116
lib/data/repository/clash_facade_impl.dart
Normal file
@@ -0,0 +1,116 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:hiddify/data/repository/exception_handlers.dart';
|
||||
import 'package:hiddify/domain/clash/clash.dart';
|
||||
import 'package:hiddify/domain/constants.dart';
|
||||
import 'package:hiddify/services/clash/clash.dart';
|
||||
import 'package:hiddify/services/files_editor_service.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
|
||||
class ClashFacadeImpl
|
||||
with ExceptionHandler, InfraLogger
|
||||
implements ClashFacade {
|
||||
ClashFacadeImpl({
|
||||
required ClashService clashService,
|
||||
required FilesEditorService filesEditor,
|
||||
}) : _clash = clashService,
|
||||
_filesEditor = filesEditor;
|
||||
|
||||
final ClashService _clash;
|
||||
final FilesEditorService _filesEditor;
|
||||
|
||||
@override
|
||||
TaskEither<ClashFailure, ClashConfig> getConfigs() {
|
||||
return exceptionHandler(
|
||||
() async => _clash.getConfigs().mapLeft(ClashFailure.core).run(),
|
||||
ClashFailure.unexpected,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ClashFailure, bool> validateConfig(String configFileName) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
final path = _filesEditor.configPath(configFileName);
|
||||
return _clash.validateConfig(path).mapLeft(ClashFailure.core).run();
|
||||
},
|
||||
ClashFailure.unexpected,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ClashFailure, Unit> changeConfigs(String configFileName) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
loggy.debug("changing config, file name: [$configFileName]");
|
||||
final path = _filesEditor.configPath(configFileName);
|
||||
return _clash.updateConfigs(path).mapLeft(ClashFailure.core).run();
|
||||
},
|
||||
ClashFailure.unexpected,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ClashFailure, Unit> patchOverrides(ClashConfig overrides) {
|
||||
return exceptionHandler(
|
||||
() async =>
|
||||
_clash.patchConfigs(overrides).mapLeft(ClashFailure.core).run(),
|
||||
ClashFailure.unexpected,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ClashFailure, List<ClashProxy>> getProxies() {
|
||||
return exceptionHandler(
|
||||
() async => _clash.getProxies().mapLeft(ClashFailure.core).run(),
|
||||
ClashFailure.unexpected,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ClashFailure, Unit> changeProxy(
|
||||
String selectorName,
|
||||
String proxyName,
|
||||
) {
|
||||
return exceptionHandler(
|
||||
() async => _clash
|
||||
.changeProxy(selectorName, proxyName)
|
||||
.mapLeft(ClashFailure.core)
|
||||
.run(),
|
||||
ClashFailure.unexpected,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ClashFailure, ClashTraffic> getTraffic() {
|
||||
return exceptionHandler(
|
||||
() async => _clash.getTraffic().mapLeft(ClashFailure.core).run(),
|
||||
ClashFailure.unexpected,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ClashFailure, int> testDelay(
|
||||
String proxyName, {
|
||||
String testUrl = Constants.delayTestUrl,
|
||||
}) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
final result = _clash
|
||||
.getProxyDelay(proxyName, testUrl)
|
||||
.mapLeft(ClashFailure.core)
|
||||
.run();
|
||||
return result;
|
||||
},
|
||||
ClashFailure.unexpected,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<Either<ClashFailure, ClashLog>> watchLogs() {
|
||||
return _clash
|
||||
.watchLogs(LogLevel.info)
|
||||
.handleExceptions(ClashFailure.unexpected);
|
||||
}
|
||||
}
|
||||
32
lib/data/repository/exception_handlers.dart
Normal file
32
lib/data/repository/exception_handlers.dart
Normal file
@@ -0,0 +1,32 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
mixin ExceptionHandler implements LoggerMixin {
|
||||
TaskEither<F, R> exceptionHandler<F, R>(
|
||||
Future<Either<F, R>> Function() run,
|
||||
F Function(Object error, StackTrace stackTrace) onError,
|
||||
) {
|
||||
return TaskEither(
|
||||
() async {
|
||||
try {
|
||||
return await run();
|
||||
} catch (error, stackTrace) {
|
||||
return Left(onError(error, stackTrace));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension StreamExceptionHandler<R extends Object?> on Stream<R> {
|
||||
Stream<Either<F, R>> handleExceptions<F>(
|
||||
F Function(Object error, StackTrace stackTrace) onError,
|
||||
) {
|
||||
return map(right<F, R>).onErrorReturnWith(
|
||||
(error, stackTrace) {
|
||||
return Left(onError(error, stackTrace));
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
155
lib/data/repository/profiles_repository_impl.dart
Normal file
155
lib/data/repository/profiles_repository_impl.dart
Normal file
@@ -0,0 +1,155 @@
|
||||
import 'dart:io';
|
||||
|
||||
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';
|
||||
import 'package:hiddify/domain/clash/clash.dart';
|
||||
import 'package:hiddify/domain/profiles/profiles.dart';
|
||||
import 'package:hiddify/services/files_editor_service.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
class ProfilesRepositoryImpl
|
||||
with ExceptionHandler, InfraLogger
|
||||
implements ProfilesRepository {
|
||||
ProfilesRepositoryImpl({
|
||||
required this.profilesDao,
|
||||
required this.filesEditor,
|
||||
required this.clashFacade,
|
||||
required this.dio,
|
||||
});
|
||||
|
||||
final ProfilesDao profilesDao;
|
||||
final FilesEditorService filesEditor;
|
||||
final ClashFacade clashFacade;
|
||||
final Dio dio;
|
||||
|
||||
@override
|
||||
TaskEither<ProfileFailure, Profile?> get(String id) {
|
||||
return TaskEither.tryCatch(
|
||||
() => profilesDao.getById(id),
|
||||
ProfileUnexpectedFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<Either<ProfileFailure, Profile?>> watchActiveProfile() {
|
||||
return profilesDao
|
||||
.watchActiveProfile()
|
||||
.handleExceptions(ProfileUnexpectedFailure.new);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<Either<ProfileFailure, bool>> watchHasAnyProfile() {
|
||||
return profilesDao
|
||||
.watchProfileCount()
|
||||
.map((event) => event != 0)
|
||||
.handleExceptions(ProfileUnexpectedFailure.new);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<Either<ProfileFailure, List<Profile>>> watchAll() {
|
||||
return profilesDao
|
||||
.watchAll()
|
||||
.handleExceptions(ProfileUnexpectedFailure.new);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ProfileFailure, Unit> add(Profile baseProfile) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
return fetch(baseProfile.url, baseProfile.id)
|
||||
.flatMap(
|
||||
(subInfo) => TaskEither(() async {
|
||||
await profilesDao.create(
|
||||
baseProfile.copyWith(
|
||||
subInfo: subInfo,
|
||||
lastUpdate: DateTime.now(),
|
||||
),
|
||||
);
|
||||
return right(unit);
|
||||
}),
|
||||
)
|
||||
.run();
|
||||
},
|
||||
ProfileUnexpectedFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ProfileFailure, Unit> update(Profile baseProfile) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
return fetch(baseProfile.url, baseProfile.id)
|
||||
.flatMap(
|
||||
(subInfo) => TaskEither(() async {
|
||||
await profilesDao.edit(
|
||||
baseProfile.copyWith(
|
||||
subInfo: subInfo,
|
||||
lastUpdate: DateTime.now(),
|
||||
),
|
||||
);
|
||||
return right(unit);
|
||||
}),
|
||||
)
|
||||
.run();
|
||||
},
|
||||
ProfileUnexpectedFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@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,
|
||||
);
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
TaskEither<ProfileFailure, SubscriptionInfo?> fetch(
|
||||
String url,
|
||||
String fileName,
|
||||
) {
|
||||
return TaskEither(
|
||||
() async {
|
||||
final path = filesEditor.configPath(fileName);
|
||||
final response = await dio.download(url, path);
|
||||
if (response.statusCode != 200) {
|
||||
await File(path).delete();
|
||||
return left(const ProfileUnexpectedFailure());
|
||||
}
|
||||
final isValid = await clashFacade
|
||||
.validateConfig(fileName)
|
||||
.getOrElse((_) => false)
|
||||
.run();
|
||||
if (!isValid) {
|
||||
await File(path).delete();
|
||||
return left(const ProfileFailure.invalidConfig());
|
||||
}
|
||||
final subInfoString =
|
||||
response.headers.map['subscription-userinfo']?.single;
|
||||
final subInfo = subInfoString != null
|
||||
? SubscriptionInfo.fromResponseHeader(subInfoString)
|
||||
: null;
|
||||
return right(subInfo);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
2
lib/data/repository/repository.dart
Normal file
2
lib/data/repository/repository.dart
Normal file
@@ -0,0 +1,2 @@
|
||||
export 'clash_facade_impl.dart';
|
||||
export 'profiles_repository_impl.dart';
|
||||
Reference in New Issue
Block a user