Add error handling for geo assets
This commit is contained in:
@@ -28,6 +28,12 @@ class GeoAssetsDao extends DatabaseAccessor<AppDatabase>
|
||||
Future<void> edit(GeoAsset patch) async {
|
||||
await transaction(
|
||||
() async {
|
||||
if (patch.active) {
|
||||
await (update(geoAssetEntries)
|
||||
..where((tbl) => tbl.active.equals(true))
|
||||
..where((tbl) => tbl.type.equalsValue(patch.type)))
|
||||
.write(const GeoAssetEntriesCompanion(active: Value(false)));
|
||||
}
|
||||
await (update(geoAssetEntries)..where((tbl) => tbl.id.equals(patch.id)))
|
||||
.write(patch.toCompanion());
|
||||
},
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:hiddify/data/api/clash_api.dart';
|
||||
@@ -33,6 +34,21 @@ class CoreFacadeImpl with ExceptionHandler, InfraLogger implements CoreFacade {
|
||||
|
||||
bool _initialized = false;
|
||||
|
||||
TaskEither<CoreServiceFailure, ConfigOptions> _getConfigOptions() {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
final options = await configOptions();
|
||||
final geoip = filesEditor.resolveGeoAssetPath(options.geoipPath);
|
||||
final geosite = filesEditor.resolveGeoAssetPath(options.geositePath);
|
||||
if (!await File(geoip).exists() || !await File(geosite).exists()) {
|
||||
return left(const CoreMissingGeoAssets());
|
||||
}
|
||||
return right(options);
|
||||
},
|
||||
CoreServiceFailure.unexpected,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<CoreServiceFailure, Unit> setup() {
|
||||
if (_initialized) return TaskEither.of(unit);
|
||||
@@ -94,21 +110,17 @@ class CoreFacadeImpl with ExceptionHandler, InfraLogger implements CoreFacade {
|
||||
TaskEither<CoreServiceFailure, String> generateConfig(
|
||||
String fileName,
|
||||
) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
return TaskEither<CoreServiceFailure, String>.Do(
|
||||
($) async {
|
||||
final configPath = filesEditor.configPath(fileName);
|
||||
final options = await configOptions();
|
||||
return setup()
|
||||
.andThen(() => changeConfigOptions(options))
|
||||
.andThen(
|
||||
() => singbox
|
||||
.generateConfig(configPath)
|
||||
.mapLeft(CoreServiceFailure.other),
|
||||
)
|
||||
.run();
|
||||
final options = await $(_getConfigOptions());
|
||||
await $(setup());
|
||||
await $(changeConfigOptions(options));
|
||||
return await $(
|
||||
singbox.generateConfig(configPath).mapLeft(CoreServiceFailure.other),
|
||||
);
|
||||
},
|
||||
CoreServiceFailure.unexpected,
|
||||
);
|
||||
).handleExceptions(CoreServiceFailure.unexpected);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -116,33 +128,35 @@ class CoreFacadeImpl with ExceptionHandler, InfraLogger implements CoreFacade {
|
||||
String fileName,
|
||||
bool disableMemoryLimit,
|
||||
) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
return TaskEither<CoreServiceFailure, Unit>.Do(
|
||||
($) async {
|
||||
final configPath = filesEditor.configPath(fileName);
|
||||
final options = await configOptions();
|
||||
final options = await $(_getConfigOptions());
|
||||
loggy.info(
|
||||
"config options: ${options.format()}\nMemory Limit: ${!disableMemoryLimit}",
|
||||
);
|
||||
|
||||
if (options.enableTun) {
|
||||
final hasPrivilege = await platformServices.hasPrivilege();
|
||||
if (!hasPrivilege) {
|
||||
loggy.warning("missing privileges for tun mode");
|
||||
return left(const CoreMissingPrivilege());
|
||||
}
|
||||
}
|
||||
|
||||
return setup()
|
||||
.andThen(() => changeConfigOptions(options))
|
||||
.andThen(
|
||||
() => singbox
|
||||
.start(configPath, disableMemoryLimit)
|
||||
.mapLeft(CoreServiceFailure.start),
|
||||
)
|
||||
.run();
|
||||
await $(
|
||||
TaskEither(() async {
|
||||
if (options.enableTun) {
|
||||
final hasPrivilege = await platformServices.hasPrivilege();
|
||||
if (!hasPrivilege) {
|
||||
loggy.warning("missing privileges for tun mode");
|
||||
return left(const CoreMissingPrivilege());
|
||||
}
|
||||
}
|
||||
return right(unit);
|
||||
}),
|
||||
);
|
||||
await $(setup());
|
||||
await $(changeConfigOptions(options));
|
||||
return await $(
|
||||
singbox
|
||||
.start(configPath, disableMemoryLimit)
|
||||
.mapLeft(CoreServiceFailure.start),
|
||||
);
|
||||
},
|
||||
CoreServiceFailure.unexpected,
|
||||
);
|
||||
).handleExceptions(CoreServiceFailure.unexpected);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -161,7 +175,8 @@ class CoreFacadeImpl with ExceptionHandler, InfraLogger implements CoreFacade {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
final configPath = filesEditor.configPath(fileName);
|
||||
return changeConfigOptions(await configOptions())
|
||||
return _getConfigOptions()
|
||||
.flatMap((options) => changeConfigOptions(options))
|
||||
.andThen(
|
||||
() => singbox
|
||||
.restart(configPath, disableMemoryLimit)
|
||||
|
||||
@@ -30,3 +30,19 @@ extension StreamExceptionHandler<R extends Object?> on Stream<R> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension TaskEitherExceptionHandler<F, R> on TaskEither<F, R> {
|
||||
TaskEither<F, R> handleExceptions(
|
||||
F Function(Object error, StackTrace stackTrace) onError,
|
||||
) {
|
||||
return TaskEither(
|
||||
() async {
|
||||
try {
|
||||
return await run();
|
||||
} catch (error, stackTrace) {
|
||||
return Left(onError(error, stackTrace));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,9 +68,7 @@ class GeoAssetsRepositoryImpl
|
||||
filesEditor.geoAssetsDir.path,
|
||||
pollingDelay: const Duration(seconds: 1),
|
||||
).events.asyncMap((event) async {
|
||||
if (event.type == ChangeType.MODIFY) {
|
||||
await _readGeoFiles();
|
||||
}
|
||||
await _readGeoFiles();
|
||||
return _geoFiles;
|
||||
});
|
||||
}
|
||||
@@ -137,4 +135,15 @@ class GeoAssetsRepositoryImpl
|
||||
GeoAssetFailure.unexpected,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<GeoAssetFailure, Unit> markAsActive(GeoAsset geoAsset) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
await geoAssetsDao.edit(geoAsset.copyWith(active: true));
|
||||
return right(unit);
|
||||
},
|
||||
GeoAssetFailure.unexpected,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,9 @@ sealed class CoreServiceFailure with _$CoreServiceFailure, Failure {
|
||||
@With<ExpectedFailure>()
|
||||
const factory CoreServiceFailure.missingPrivilege() = CoreMissingPrivilege;
|
||||
|
||||
@With<ExpectedFailure>()
|
||||
const factory CoreServiceFailure.missingGeoAssets() = CoreMissingGeoAssets;
|
||||
|
||||
const factory CoreServiceFailure.invalidConfigOptions([
|
||||
String? message,
|
||||
]) = InvalidConfigOptions;
|
||||
@@ -46,6 +49,7 @@ sealed class CoreServiceFailure with _$CoreServiceFailure, Failure {
|
||||
UnexpectedCoreServiceFailure() => null,
|
||||
CoreServiceNotRunning(:final message) => message,
|
||||
CoreMissingPrivilege() => null,
|
||||
CoreMissingGeoAssets() => null,
|
||||
InvalidConfigOptions(:final message) => message,
|
||||
InvalidConfig(:final message) => message,
|
||||
CoreServiceCreateFailure(:final message) => message,
|
||||
@@ -68,6 +72,10 @@ sealed class CoreServiceFailure with _$CoreServiceFailure, Failure {
|
||||
type: t.failure.singbox.missingPrivilege,
|
||||
message: t.failure.singbox.missingPrivilegeMsg,
|
||||
),
|
||||
CoreMissingGeoAssets() => (
|
||||
type: t.failure.singbox.missingGeoAssets,
|
||||
message: t.failure.singbox.missingGeoAssetsMsg,
|
||||
),
|
||||
InvalidConfigOptions(:final message) => (
|
||||
type: t.failure.singbox.invalidConfigOptions,
|
||||
message: message
|
||||
|
||||
@@ -9,4 +9,6 @@ abstract interface class GeoAssetsRepository {
|
||||
Stream<Either<GeoAssetFailure, List<GeoAssetWithFileSize>>> watchAll();
|
||||
|
||||
TaskEither<GeoAssetFailure, Unit> update(GeoAsset geoAsset);
|
||||
|
||||
TaskEither<GeoAssetFailure, Unit> markAsActive(GeoAsset geoAsset);
|
||||
}
|
||||
|
||||
@@ -99,6 +99,10 @@ class FilesEditorService with InfraLogger {
|
||||
return p.relative(fullPath, from: workingDir.path);
|
||||
}
|
||||
|
||||
String resolveGeoAssetPath(String path) {
|
||||
return p.absolute(workingDir.path, path);
|
||||
}
|
||||
|
||||
String tempConfigPath(String fileName) => configPath("temp_$fileName");
|
||||
|
||||
Future<void> deleteConfig(String fileName) {
|
||||
|
||||
Reference in New Issue
Block a user