From 92538d137b132ca928ad46e6e25ac78eabed739d Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Fri, 22 Sep 2023 23:52:20 +0330 Subject: [PATCH] Fix profile update bug --- .../com/hiddify/hiddify/MethodHandler.kt | 6 ++-- .../com/hiddify/hiddify/bg/BoxService.kt | 6 ++-- lib/data/data_providers.dart | 4 ++- lib/data/repository/core_facade_impl.dart | 8 +++-- .../repository/profiles_repository_impl.dart | 34 +++++++++++-------- lib/domain/singbox/singbox_facade.dart | 6 +++- lib/gen/singbox_generated_bindings.dart | 13 +++++-- lib/services/singbox/ffi_singbox_service.dart | 12 +++++-- .../singbox/mobile_singbox_service.dart | 8 +++-- lib/services/singbox/singbox_service.dart | 6 +++- libcore | 2 +- 11 files changed, 73 insertions(+), 32 deletions(-) diff --git a/android/app/src/main/kotlin/com/hiddify/hiddify/MethodHandler.kt b/android/app/src/main/kotlin/com/hiddify/hiddify/MethodHandler.kt index 6a3b8cbe..e1a6c5a0 100644 --- a/android/app/src/main/kotlin/com/hiddify/hiddify/MethodHandler.kt +++ b/android/app/src/main/kotlin/com/hiddify/hiddify/MethodHandler.kt @@ -50,8 +50,10 @@ class MethodHandler : FlutterPlugin, MethodChannel.MethodCallHandler { GlobalScope.launch { result.runCatching { val args = call.arguments as Map<*, *> - val path = args["path"] as String? ?: "" - val msg = BoxService.parseConfig(path) + val path = args["path"] as String + val tempPath = args["tempPath"] as String + val debug = args["debug"] as Boolean + val msg = BoxService.parseConfig(path, tempPath, debug) success(msg) } } diff --git a/android/app/src/main/kotlin/com/hiddify/hiddify/bg/BoxService.kt b/android/app/src/main/kotlin/com/hiddify/hiddify/bg/BoxService.kt index 78032dad..f443c111 100644 --- a/android/app/src/main/kotlin/com/hiddify/hiddify/bg/BoxService.kt +++ b/android/app/src/main/kotlin/com/hiddify/hiddify/bg/BoxService.kt @@ -61,9 +61,9 @@ class BoxService( return } - fun parseConfig(path: String): String { + fun parseConfig(path: String, tempPath: String, debug: Boolean): String { return try { - Mobile.parse(path) + Mobile.parse(path, tempPath, debug) "" } catch (e: Exception) { Log.w(TAG, e) @@ -157,7 +157,7 @@ class BoxService( return } - if(Settings.debugMode) { + if (Settings.debugMode) { File(workingDir, "current-config.json").writeText(content) } diff --git a/lib/data/data_providers.dart b/lib/data/data_providers.dart index 6d81974b..6790d425 100644 --- a/lib/data/data_providers.dart +++ b/lib/data/data_providers.dart @@ -24,10 +24,12 @@ AppDatabase appDatabase(AppDatabaseRef ref) => AppDatabase.connect(); SharedPreferences sharedPreferences(SharedPreferencesRef ref) => throw UnimplementedError('sharedPreferences must be overridden'); -// TODO: set options for dio @Riverpod(keepAlive: true) Dio dio(DioRef ref) => Dio( BaseOptions( + connectTimeout: const Duration(seconds: 15), + sendTimeout: const Duration(seconds: 15), + receiveTimeout: const Duration(seconds: 15), headers: { "User-Agent": ref.watch(appInfoProvider).userAgent, }, diff --git a/lib/data/repository/core_facade_impl.dart b/lib/data/repository/core_facade_impl.dart index 238d0a6d..7600957b 100644 --- a/lib/data/repository/core_facade_impl.dart +++ b/lib/data/repository/core_facade_impl.dart @@ -57,11 +57,15 @@ class CoreFacadeImpl with ExceptionHandler, InfraLogger implements CoreFacade { } @override - TaskEither parseConfig(String path) { + TaskEither parseConfig( + String path, + String tempPath, + bool debug, + ) { return exceptionHandler( () { return singbox - .parseConfig(path) + .parseConfig(path, tempPath, debug) .mapLeft(CoreServiceFailure.invalidConfig) .run(); }, diff --git a/lib/data/repository/profiles_repository_impl.dart b/lib/data/repository/profiles_repository_impl.dart index e5d07e43..ac9169cc 100644 --- a/lib/data/repository/profiles_repository_impl.dart +++ b/lib/data/repository/profiles_repository_impl.dart @@ -198,21 +198,27 @@ class ProfilesRepositoryImpl ) { return TaskEither( () async { + final tempPath = filesEditor.configPath("temp_$fileName"); final path = filesEditor.configPath(fileName); - final response = await dio.download(url.trim(), path); - final headers = await _populateHeaders(response.headers.map, path); - final parseResult = await singbox.parseConfig(path).run(); - return parseResult.fold( - (l) async { - await File(path).delete(); - loggy.warning("error parsing config: $l"); - return left(ProfileFailure.invalidConfig(l.msg)); - }, - (_) async { - final profile = Profile.fromResponse(url, headers); - return right(profile); - }, - ); + try { + final response = await dio.download(url.trim(), tempPath); + final headers = + await _populateHeaders(response.headers.map, tempPath); + final parseResult = + await singbox.parseConfig(path, tempPath, false).run(); + return parseResult.fold( + (l) async { + loggy.warning("error parsing config: $l"); + return left(ProfileFailure.invalidConfig(l.msg)); + }, + (_) async { + final profile = Profile.fromResponse(url, headers); + return right(profile); + }, + ); + } finally { + if (await File(tempPath).exists()) await File(tempPath).delete(); + } }, ); } diff --git a/lib/domain/singbox/singbox_facade.dart b/lib/domain/singbox/singbox_facade.dart index 496da38d..bcd40f80 100644 --- a/lib/domain/singbox/singbox_facade.dart +++ b/lib/domain/singbox/singbox_facade.dart @@ -8,7 +8,11 @@ import 'package:hiddify/domain/singbox/outbounds.dart'; abstract interface class SingboxFacade { TaskEither setup(); - TaskEither parseConfig(String path); + TaskEither parseConfig( + String path, + String tempPath, + bool debug, + ); TaskEither changeConfigOptions( ConfigOptions options, diff --git a/lib/gen/singbox_generated_bindings.dart b/lib/gen/singbox_generated_bindings.dart index a0f8be3f..8fd548a4 100644 --- a/lib/gen/singbox_generated_bindings.dart +++ b/lib/gen/singbox_generated_bindings.dart @@ -895,17 +895,23 @@ class SingboxNativeLibrary { ffi.Pointer parse( ffi.Pointer path, + ffi.Pointer tempPath, + int debug, ) { return _parse( path, + tempPath, + debug, ); } late final _parsePtr = _lookup< ffi.NativeFunction< - ffi.Pointer Function(ffi.Pointer)>>('parse'); - late final _parse = _parsePtr - .asFunction Function(ffi.Pointer)>(); + ffi.Pointer Function( + ffi.Pointer, ffi.Pointer, GoUint8)>>('parse'); + late final _parse = _parsePtr.asFunction< + ffi.Pointer Function( + ffi.Pointer, ffi.Pointer, int)>(); ffi.Pointer changeConfigOptions( ffi.Pointer configOptionsJson, @@ -1099,6 +1105,7 @@ final class GoSlice extends ffi.Struct { typedef GoInt = GoInt64; typedef GoInt64 = ffi.LongLong; +typedef GoUint8 = ffi.UnsignedChar; const int _VCRT_COMPILER_PREPROCESSOR = 1; diff --git a/lib/services/singbox/ffi_singbox_service.dart b/lib/services/singbox/ffi_singbox_service.dart index 69582c6b..21488eee 100644 --- a/lib/services/singbox/ffi_singbox_service.dart +++ b/lib/services/singbox/ffi_singbox_service.dart @@ -84,12 +84,20 @@ class FFISingboxService } @override - TaskEither parseConfig(String path) { + TaskEither parseConfig( + String path, + String tempPath, + bool debug, + ) { return TaskEither( () => CombineWorker().execute( () { final err = _box - .parse(path.toNativeUtf8().cast()) + .parse( + path.toNativeUtf8().cast(), + tempPath.toNativeUtf8().cast(), + debug ? 1 : 0, + ) .cast() .toDartString(); if (err.isNotEmpty) { diff --git a/lib/services/singbox/mobile_singbox_service.dart b/lib/services/singbox/mobile_singbox_service.dart index 23708a8d..a4bc5b91 100644 --- a/lib/services/singbox/mobile_singbox_service.dart +++ b/lib/services/singbox/mobile_singbox_service.dart @@ -42,12 +42,16 @@ class MobileSingboxService TaskEither.of(unit); @override - TaskEither parseConfig(String path) { + TaskEither parseConfig( + String path, + String tempPath, + bool debug, + ) { return TaskEither( () async { final message = await _methodChannel.invokeMethod( "parse_config", - {"path": path}, + {"path": path, "tempPath": tempPath, "debug": debug}, ); if (message == null || message.isEmpty) return right(unit); return left(message); diff --git a/lib/services/singbox/singbox_service.dart b/lib/services/singbox/singbox_service.dart index 6b8451fb..5f1c31f4 100644 --- a/lib/services/singbox/singbox_service.dart +++ b/lib/services/singbox/singbox_service.dart @@ -24,7 +24,11 @@ abstract interface class SingboxService { String tempDir, ); - TaskEither parseConfig(String path); + TaskEither parseConfig( + String path, + String tempPath, + bool debug, + ); TaskEither changeConfigOptions(ConfigOptions options); diff --git a/libcore b/libcore index 00bc20b8..75e342b6 160000 --- a/libcore +++ b/libcore @@ -1 +1 @@ -Subproject commit 00bc20b86a51911988c8ffb76f97d5649f356936 +Subproject commit 75e342b6bacca604b4cc19b397aa29124e5c2338